[MM-55726] Create batch report worker, add batch report job for exporting users to CSV (#25832)

* Split out migration logic and create generic BatchWorker

* WIP

* WIP

* POC batch reporting

* Oops

* Job hookup

* Working export to file

* PR feedback

* Merge'd

* Fix error handling

* Add API to start report, translations, couple fixes

* Add DMs to send reports to users

* Merge'd

* Update types

* A bit of cleanup

* Some fixes

* Add missing API doc

* PR feedback

* Fix generated

* Fix bug with post creation

* PR feedback

* Add some tests

* PR feedback

* Fix lint

* Some test changes

* Fix tests

* Add comment to explain why we forcibly stop

* Rework of some tests

* Batch report test

* Restrict batch exports to Pro and Enterprise licenses

* Fix erroneous comment

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Devin Binnie
2024-01-19 15:22:17 -05:00
коммит произвёл GitHub
родитель eac9a39677
Коммит f7446d7443
23 изменённых файлов: 1571 добавлений и 361 удалений

Просмотреть файл

@@ -5,6 +5,7 @@ package model
import (
"net/http"
"strconv"
"time"
pUtils "github.com/mattermost/mattermost/server/public/utils"
@@ -19,9 +20,15 @@ const (
)
var (
ReportExportFormats = []string{"csv"}
UserReportSortColumns = []string{"CreateAt", "Username", "FirstName", "LastName", "Nickname", "Email", "Roles"}
)
type ReportableObject interface {
ToReport() []string
}
type ReportingBaseOptions struct {
SortDesc bool
Direction string // Accepts only "prev" or "next"
@@ -34,20 +41,26 @@ type ReportingBaseOptions struct {
EndAt int64
}
func (options *ReportingBaseOptions) PopulateDateRange(now time.Time) {
func GetReportDateRange(dateRange string, now time.Time) (int64, int64) {
startAt := int64(0)
endAt := int64(0)
if options.DateRange == ReportDurationLast30Days {
if dateRange == ReportDurationLast30Days {
startAt = now.AddDate(0, 0, -30).UnixMilli()
} else if options.DateRange == ReportDurationPreviousMonth {
} else if dateRange == ReportDurationPreviousMonth {
startOfMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, time.Local)
startAt = startOfMonth.AddDate(0, -1, 0).UnixMilli()
endAt = startOfMonth.UnixMilli()
} else if options.DateRange == ReportDurationLast6Months {
} else if dateRange == ReportDurationLast6Months {
startAt = now.AddDate(0, -6, -0).UnixMilli()
}
return startAt, endAt
}
func (options *ReportingBaseOptions) PopulateDateRange(now time.Time) {
startAt, endAt := GetReportDateRange(options.DateRange, now)
options.StartAt = startAt
options.EndAt = endAt
}
@@ -70,6 +83,43 @@ type UserReport struct {
UserPostStats
}
func (u *UserReport) ToReport() []string {
lastStatusAt := ""
if u.LastStatusAt != nil {
lastStatusAt = time.UnixMilli(*u.LastStatusAt).String()
}
lastPostDate := ""
if u.LastPostDate != nil {
lastPostDate = time.UnixMilli(*u.LastPostDate).String()
}
daysActive := ""
if u.DaysActive != nil {
daysActive = strconv.Itoa(*u.DaysActive)
}
totalPosts := ""
if u.TotalPosts != nil {
totalPosts = strconv.Itoa(*u.TotalPosts)
}
lastLogin := ""
if u.LastLogin > 0 {
lastLogin = time.UnixMilli(u.LastLogin).String()
}
return []string{
u.Id,
u.Username,
u.Email,
time.UnixMilli(u.CreateAt).String(),
u.User.GetDisplayName(ShowNicknameFullName),
u.Roles,
lastLogin,
lastStatusAt,
lastPostDate,
daysActive,
totalPosts,
}
}
type UserReportOptions struct {
ReportingBaseOptions
Role string
@@ -99,3 +149,13 @@ func (u *UserReportQuery) ToReport() *UserReport {
UserPostStats: u.UserPostStats,
}
}
func IsValidReportExportFormat(format string) bool {
for _, fmt := range ReportExportFormats {
if format == fmt {
return true
}
}
return false
}