Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
Server CI Nightly Race / Build Environment (push) Failing after 1s
186 lines
4.5 KiB
Go
186 lines
4.5 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package model
|
|
|
|
import (
|
|
"net/http"
|
|
"slices"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
ReportDurationAllTime = "all_time"
|
|
ReportDurationLast30Days = "last_30_days"
|
|
ReportDurationPreviousMonth = "previous_month"
|
|
ReportDurationLast6Months = "last_6_months"
|
|
|
|
ReportingMaxPageSize = 100
|
|
|
|
GuestFilterAll = "all"
|
|
GuestFilterSingleChannel = "single_channel"
|
|
GuestFilterMultipleChannel = "multi_channel"
|
|
)
|
|
|
|
var (
|
|
ReportExportFormats = []string{"csv"}
|
|
|
|
UserReportSortColumns = []string{"CreateAt", "Username", "FirstName", "LastName", "Nickname", "Email", "Roles"}
|
|
|
|
AllowedGuestFilters = []string{GuestFilterAll, GuestFilterSingleChannel, GuestFilterMultipleChannel}
|
|
)
|
|
|
|
type ReportableObject interface {
|
|
ToReport() []string
|
|
}
|
|
|
|
type ReportingBaseOptions struct {
|
|
SortDesc bool
|
|
Direction string // Accepts only "prev" or "next"
|
|
PageSize int
|
|
SortColumn string
|
|
FromColumnValue string
|
|
FromId string
|
|
DateRange string
|
|
StartAt int64
|
|
EndAt int64
|
|
}
|
|
|
|
func GetReportDateRange(dateRange string, now time.Time) (int64, int64) {
|
|
startAt := int64(0)
|
|
endAt := int64(0)
|
|
|
|
if dateRange == ReportDurationLast30Days {
|
|
startAt = now.AddDate(0, 0, -30).UnixMilli()
|
|
} 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 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
|
|
}
|
|
|
|
func (options *ReportingBaseOptions) IsValid() *AppError {
|
|
if options.EndAt > 0 && options.StartAt > options.EndAt {
|
|
return NewAppError("ReportingBaseOptions.IsValid", "model.reporting_base_options.is_valid.bad_date_range", nil, "", http.StatusBadRequest)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type UserReportQuery struct {
|
|
User
|
|
UserPostStats
|
|
ChannelCount *int
|
|
Teams string
|
|
}
|
|
|
|
type UserReport struct {
|
|
User
|
|
UserPostStats
|
|
ChannelCount *int `json:"channel_count,omitempty"`
|
|
Teams string `json:"teams,omitempty"`
|
|
}
|
|
|
|
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)
|
|
}
|
|
channelCount := ""
|
|
if u.ChannelCount != nil {
|
|
channelCount = strconv.Itoa(*u.ChannelCount)
|
|
}
|
|
lastLogin := ""
|
|
if u.LastLogin > 0 {
|
|
lastLogin = time.UnixMilli(u.LastLogin).String()
|
|
}
|
|
|
|
deleteAt := ""
|
|
if u.DeleteAt > 0 {
|
|
deleteAt = time.UnixMilli(u.DeleteAt).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,
|
|
channelCount,
|
|
u.Teams,
|
|
deleteAt,
|
|
}
|
|
}
|
|
|
|
type UserReportOptions struct {
|
|
ReportingBaseOptions
|
|
Role string
|
|
Team string
|
|
HasNoTeam bool
|
|
HideActive bool
|
|
HideInactive bool
|
|
SearchTerm string
|
|
GuestFilter string
|
|
}
|
|
|
|
func (u *UserReportOptions) IsValid() *AppError {
|
|
if appErr := u.ReportingBaseOptions.IsValid(); appErr != nil {
|
|
return appErr
|
|
}
|
|
|
|
// Validate against the columns we allow sorting for
|
|
if !slices.Contains(UserReportSortColumns, u.SortColumn) {
|
|
return NewAppError("UserReportOptions.IsValid", "model.user_report_options.is_valid.invalid_sort_column", nil, "", http.StatusBadRequest)
|
|
}
|
|
|
|
if u.GuestFilter != "" && !slices.Contains(AllowedGuestFilters, u.GuestFilter) {
|
|
return NewAppError("UserReportOptions.IsValid", "model.user_report_options.is_valid.invalid_guest_filter", nil, "", http.StatusBadRequest)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (u *UserReportQuery) ToReport() *UserReport {
|
|
u.ClearNonProfileFields(true)
|
|
return &UserReport{
|
|
User: u.User,
|
|
UserPostStats: u.UserPostStats,
|
|
ChannelCount: u.ChannelCount,
|
|
Teams: u.Teams,
|
|
}
|
|
}
|
|
|
|
func IsValidReportExportFormat(format string) bool {
|
|
return slices.Contains(ReportExportFormats, format)
|
|
}
|