Speed up activity log listing with batch actor lookups and search filter.
Batch-load users and team members per page instead of N+1 lookups, and add server-side search on message, action, and resource fields. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
parent
d69e6a1d70
commit
977e4e73e2
|
|
@ -32,6 +32,14 @@ WHERE
|
||||||
AND (sqlc.narg('filter_resource_id')::bigint IS NULL OR al.resource_id = sqlc.narg('filter_resource_id'))
|
AND (sqlc.narg('filter_resource_id')::bigint IS NULL OR al.resource_id = sqlc.narg('filter_resource_id'))
|
||||||
AND (sqlc.narg('filter_after')::timestamptz IS NULL OR al.created_at >= sqlc.narg('filter_after'))
|
AND (sqlc.narg('filter_after')::timestamptz IS NULL OR al.created_at >= sqlc.narg('filter_after'))
|
||||||
AND (sqlc.narg('filter_before')::timestamptz IS NULL OR al.created_at <= sqlc.narg('filter_before'))
|
AND (sqlc.narg('filter_before')::timestamptz IS NULL OR al.created_at <= sqlc.narg('filter_before'))
|
||||||
|
AND (
|
||||||
|
sqlc.narg('filter_search')::text IS NULL
|
||||||
|
OR btrim(sqlc.narg('filter_search')::text) = ''
|
||||||
|
OR al.message ILIKE '%' || btrim(sqlc.narg('filter_search')::text) || '%'
|
||||||
|
OR al.action ILIKE '%' || btrim(sqlc.narg('filter_search')::text) || '%'
|
||||||
|
OR al.resource_type ILIKE '%' || btrim(sqlc.narg('filter_search')::text) || '%'
|
||||||
|
OR al.resource_id::text LIKE '%' || btrim(sqlc.narg('filter_search')::text) || '%'
|
||||||
|
)
|
||||||
ORDER BY al.created_at DESC
|
ORDER BY al.created_at DESC
|
||||||
LIMIT @log_limit OFFSET @log_offset;
|
LIMIT @log_limit OFFSET @log_offset;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,10 @@ RETURNING *;
|
||||||
SELECT * FROM team_members
|
SELECT * FROM team_members
|
||||||
WHERE id = $1;
|
WHERE id = $1;
|
||||||
|
|
||||||
|
-- name: GetTeamMembersByIDs :many
|
||||||
|
SELECT * FROM team_members
|
||||||
|
WHERE id = ANY($1::BIGINT[]);
|
||||||
|
|
||||||
-- name: GetTeamMemberByEmail :one
|
-- name: GetTeamMemberByEmail :one
|
||||||
SELECT * FROM team_members
|
SELECT * FROM team_members
|
||||||
WHERE email = $1
|
WHERE email = $1
|
||||||
|
|
|
||||||
|
|
@ -176,6 +176,11 @@ SELECT *
|
||||||
FROM users
|
FROM users
|
||||||
WHERE id = $1;
|
WHERE id = $1;
|
||||||
|
|
||||||
|
-- name: GetUsersByIDs :many
|
||||||
|
SELECT *
|
||||||
|
FROM users
|
||||||
|
WHERE id = ANY($1::BIGINT[]);
|
||||||
|
|
||||||
-- name: GetUserByGoogleID :one
|
-- name: GetUserByGoogleID :one
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM users
|
FROM users
|
||||||
|
|
|
||||||
|
|
@ -110,8 +110,16 @@ WHERE
|
||||||
AND ($4::bigint IS NULL OR al.resource_id = $4)
|
AND ($4::bigint IS NULL OR al.resource_id = $4)
|
||||||
AND ($5::timestamptz IS NULL OR al.created_at >= $5)
|
AND ($5::timestamptz IS NULL OR al.created_at >= $5)
|
||||||
AND ($6::timestamptz IS NULL OR al.created_at <= $6)
|
AND ($6::timestamptz IS NULL OR al.created_at <= $6)
|
||||||
|
AND (
|
||||||
|
$7::text IS NULL
|
||||||
|
OR btrim($7::text) = ''
|
||||||
|
OR al.message ILIKE '%' || btrim($7::text) || '%'
|
||||||
|
OR al.action ILIKE '%' || btrim($7::text) || '%'
|
||||||
|
OR al.resource_type ILIKE '%' || btrim($7::text) || '%'
|
||||||
|
OR al.resource_id::text LIKE '%' || btrim($7::text) || '%'
|
||||||
|
)
|
||||||
ORDER BY al.created_at DESC
|
ORDER BY al.created_at DESC
|
||||||
LIMIT $8 OFFSET $7
|
LIMIT $9 OFFSET $8
|
||||||
`
|
`
|
||||||
|
|
||||||
type ListActivityLogsParams struct {
|
type ListActivityLogsParams struct {
|
||||||
|
|
@ -121,6 +129,7 @@ type ListActivityLogsParams struct {
|
||||||
FilterResourceID pgtype.Int8 `json:"filter_resource_id"`
|
FilterResourceID pgtype.Int8 `json:"filter_resource_id"`
|
||||||
FilterAfter pgtype.Timestamptz `json:"filter_after"`
|
FilterAfter pgtype.Timestamptz `json:"filter_after"`
|
||||||
FilterBefore pgtype.Timestamptz `json:"filter_before"`
|
FilterBefore pgtype.Timestamptz `json:"filter_before"`
|
||||||
|
FilterSearch pgtype.Text `json:"filter_search"`
|
||||||
LogOffset int32 `json:"log_offset"`
|
LogOffset int32 `json:"log_offset"`
|
||||||
LogLimit int32 `json:"log_limit"`
|
LogLimit int32 `json:"log_limit"`
|
||||||
}
|
}
|
||||||
|
|
@ -148,6 +157,7 @@ func (q *Queries) ListActivityLogs(ctx context.Context, arg ListActivityLogsPara
|
||||||
arg.FilterResourceID,
|
arg.FilterResourceID,
|
||||||
arg.FilterAfter,
|
arg.FilterAfter,
|
||||||
arg.FilterBefore,
|
arg.FilterBefore,
|
||||||
|
arg.FilterSearch,
|
||||||
arg.LogOffset,
|
arg.LogOffset,
|
||||||
arg.LogLimit,
|
arg.LogLimit,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -469,6 +469,55 @@ func (q *Queries) GetTeamMembersByDepartment(ctx context.Context, department pgt
|
||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const GetTeamMembersByIDs = `-- name: GetTeamMembersByIDs :many
|
||||||
|
SELECT id, first_name, last_name, email, phone_number, password, team_role, department, job_title, employment_type, hire_date, profile_picture_url, bio, work_phone, emergency_contact, status, email_verified, permissions, last_login, created_by, updated_by, created_at, updated_at FROM team_members
|
||||||
|
WHERE id = ANY($1::BIGINT[])
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetTeamMembersByIDs(ctx context.Context, dollar_1 []int64) ([]TeamMember, error) {
|
||||||
|
rows, err := q.db.Query(ctx, GetTeamMembersByIDs, dollar_1)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var items []TeamMember
|
||||||
|
for rows.Next() {
|
||||||
|
var i TeamMember
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.FirstName,
|
||||||
|
&i.LastName,
|
||||||
|
&i.Email,
|
||||||
|
&i.PhoneNumber,
|
||||||
|
&i.Password,
|
||||||
|
&i.TeamRole,
|
||||||
|
&i.Department,
|
||||||
|
&i.JobTitle,
|
||||||
|
&i.EmploymentType,
|
||||||
|
&i.HireDate,
|
||||||
|
&i.ProfilePictureUrl,
|
||||||
|
&i.Bio,
|
||||||
|
&i.WorkPhone,
|
||||||
|
&i.EmergencyContact,
|
||||||
|
&i.Status,
|
||||||
|
&i.EmailVerified,
|
||||||
|
&i.Permissions,
|
||||||
|
&i.LastLogin,
|
||||||
|
&i.CreatedBy,
|
||||||
|
&i.UpdatedBy,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
const GetTeamMembersByRole = `-- name: GetTeamMembersByRole :many
|
const GetTeamMembersByRole = `-- name: GetTeamMembersByRole :many
|
||||||
SELECT
|
SELECT
|
||||||
id,
|
id,
|
||||||
|
|
|
||||||
|
|
@ -1277,6 +1277,71 @@ func (q *Queries) GetUserSummary(ctx context.Context) (GetUserSummaryRow, error)
|
||||||
return i, err
|
return i, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const GetUsersByIDs = `-- name: GetUsersByIDs :many
|
||||||
|
SELECT id, first_name, last_name, gender, birth_day, email, phone_number, role, password, education_level, country, region, knowledge_level, nick_name, occupation, learning_goal, language_goal, language_challange, favourite_topic, initial_assessment_completed, email_verified, phone_verified, status, last_login, profile_completed, profile_picture_url, preferred_language, created_at, updated_at, age_group, google_id, google_email_verified, profile_completion_percentage, deletion_requested_at, deletion_scheduled_at, deletion_cancelled_at, apple_id, apple_email_verified
|
||||||
|
FROM users
|
||||||
|
WHERE id = ANY($1::BIGINT[])
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) GetUsersByIDs(ctx context.Context, dollar_1 []int64) ([]User, error) {
|
||||||
|
rows, err := q.db.Query(ctx, GetUsersByIDs, dollar_1)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var items []User
|
||||||
|
for rows.Next() {
|
||||||
|
var i User
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.ID,
|
||||||
|
&i.FirstName,
|
||||||
|
&i.LastName,
|
||||||
|
&i.Gender,
|
||||||
|
&i.BirthDay,
|
||||||
|
&i.Email,
|
||||||
|
&i.PhoneNumber,
|
||||||
|
&i.Role,
|
||||||
|
&i.Password,
|
||||||
|
&i.EducationLevel,
|
||||||
|
&i.Country,
|
||||||
|
&i.Region,
|
||||||
|
&i.KnowledgeLevel,
|
||||||
|
&i.NickName,
|
||||||
|
&i.Occupation,
|
||||||
|
&i.LearningGoal,
|
||||||
|
&i.LanguageGoal,
|
||||||
|
&i.LanguageChallange,
|
||||||
|
&i.FavouriteTopic,
|
||||||
|
&i.InitialAssessmentCompleted,
|
||||||
|
&i.EmailVerified,
|
||||||
|
&i.PhoneVerified,
|
||||||
|
&i.Status,
|
||||||
|
&i.LastLogin,
|
||||||
|
&i.ProfileCompleted,
|
||||||
|
&i.ProfilePictureUrl,
|
||||||
|
&i.PreferredLanguage,
|
||||||
|
&i.CreatedAt,
|
||||||
|
&i.UpdatedAt,
|
||||||
|
&i.AgeGroup,
|
||||||
|
&i.GoogleID,
|
||||||
|
&i.GoogleEmailVerified,
|
||||||
|
&i.ProfileCompletionPercentage,
|
||||||
|
&i.DeletionRequestedAt,
|
||||||
|
&i.DeletionScheduledAt,
|
||||||
|
&i.DeletionCancelledAt,
|
||||||
|
&i.AppleID,
|
||||||
|
&i.AppleEmailVerified,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
const IsUserNameUnique = `-- name: IsUserNameUnique :one
|
const IsUserNameUnique = `-- name: IsUserNameUnique :one
|
||||||
SELECT
|
SELECT
|
||||||
CASE WHEN COUNT(*) = 0 THEN true ELSE false END AS is_unique
|
CASE WHEN COUNT(*) = 0 THEN true ELSE false END AS is_unique
|
||||||
|
|
|
||||||
|
|
@ -197,6 +197,7 @@ type ActivityLogFilter struct {
|
||||||
Action *string
|
Action *string
|
||||||
ResourceType *string
|
ResourceType *string
|
||||||
ResourceID *int64
|
ResourceID *int64
|
||||||
|
Search *string
|
||||||
After *time.Time
|
After *time.Time
|
||||||
Before *time.Time
|
Before *time.Time
|
||||||
Limit int32
|
Limit int32
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import (
|
||||||
type TeamStore interface {
|
type TeamStore interface {
|
||||||
CreateTeamMember(ctx context.Context, member domain.TeamMember) (domain.TeamMember, error)
|
CreateTeamMember(ctx context.Context, member domain.TeamMember) (domain.TeamMember, error)
|
||||||
GetTeamMemberByID(ctx context.Context, id int64) (domain.TeamMember, error)
|
GetTeamMemberByID(ctx context.Context, id int64) (domain.TeamMember, error)
|
||||||
|
GetTeamMembersByIDs(ctx context.Context, ids []int64) (map[int64]domain.TeamMember, error)
|
||||||
GetTeamMemberByEmail(ctx context.Context, email string) (domain.TeamMember, error)
|
GetTeamMemberByEmail(ctx context.Context, email string) (domain.TeamMember, error)
|
||||||
GetAllTeamMembers(
|
GetAllTeamMembers(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
|
|
|
||||||
|
|
@ -56,6 +56,10 @@ type UserStore interface {
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
id int64,
|
id int64,
|
||||||
) (domain.User, error)
|
) (domain.User, error)
|
||||||
|
GetUsersByIDs(
|
||||||
|
ctx context.Context,
|
||||||
|
ids []int64,
|
||||||
|
) (map[int64]domain.User, error)
|
||||||
GetAllUsers(
|
GetAllUsers(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
filter domain.UserFilter,
|
filter domain.UserFilter,
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"Yimaru-Backend/internal/domain"
|
"Yimaru-Backend/internal/domain"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
)
|
)
|
||||||
|
|
@ -78,6 +79,13 @@ func (s *Store) ListActivityLogs(ctx context.Context, filter domain.ActivityLogF
|
||||||
if filter.Before != nil {
|
if filter.Before != nil {
|
||||||
filterBefore = pgtype.Timestamptz{Time: *filter.Before, Valid: true}
|
filterBefore = pgtype.Timestamptz{Time: *filter.Before, Valid: true}
|
||||||
}
|
}
|
||||||
|
var filterSearch pgtype.Text
|
||||||
|
if filter.Search != nil {
|
||||||
|
trimmed := strings.TrimSpace(*filter.Search)
|
||||||
|
if trimmed != "" {
|
||||||
|
filterSearch = pgtype.Text{String: trimmed, Valid: true}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
rows, err := s.queries.ListActivityLogs(ctx, dbgen.ListActivityLogsParams{
|
rows, err := s.queries.ListActivityLogs(ctx, dbgen.ListActivityLogsParams{
|
||||||
FilterActorID: filterActorID,
|
FilterActorID: filterActorID,
|
||||||
|
|
@ -86,6 +94,7 @@ func (s *Store) ListActivityLogs(ctx context.Context, filter domain.ActivityLogF
|
||||||
FilterResourceID: filterResourceID,
|
FilterResourceID: filterResourceID,
|
||||||
FilterAfter: filterAfter,
|
FilterAfter: filterAfter,
|
||||||
FilterBefore: filterBefore,
|
FilterBefore: filterBefore,
|
||||||
|
FilterSearch: filterSearch,
|
||||||
LogOffset: filter.Offset,
|
LogOffset: filter.Offset,
|
||||||
LogLimit: filter.Limit,
|
LogLimit: filter.Limit,
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -74,6 +74,38 @@ func (s *Store) GetTeamMemberByID(ctx context.Context, id int64) (domain.TeamMem
|
||||||
return mapDBTeamMember(res), nil
|
return mapDBTeamMember(res), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Store) GetTeamMembersByIDs(ctx context.Context, ids []int64) (map[int64]domain.TeamMember, error) {
|
||||||
|
out := make(map[int64]domain.TeamMember, len(ids))
|
||||||
|
if len(ids) == 0 {
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
unique := make([]int64, 0, len(ids))
|
||||||
|
seen := make(map[int64]struct{}, len(ids))
|
||||||
|
for _, id := range ids {
|
||||||
|
if id <= 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, ok := seen[id]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[id] = struct{}{}
|
||||||
|
unique = append(unique, id)
|
||||||
|
}
|
||||||
|
if len(unique) == 0 {
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := s.queries.GetTeamMembersByIDs(ctx, unique)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, row := range rows {
|
||||||
|
out[row.ID] = mapDBTeamMember(row)
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Store) GetTeamMemberByEmail(ctx context.Context, email string) (domain.TeamMember, error) {
|
func (s *Store) GetTeamMemberByEmail(ctx context.Context, email string) (domain.TeamMember, error) {
|
||||||
res, err := s.queries.GetTeamMemberByEmail(ctx, email)
|
res, err := s.queries.GetTeamMemberByEmail(ctx, email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -428,6 +428,76 @@ func (s *Store) GetUserByID(
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Store) GetUsersByIDs(ctx context.Context, ids []int64) (map[int64]domain.User, error) {
|
||||||
|
out := make(map[int64]domain.User, len(ids))
|
||||||
|
if len(ids) == 0 {
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
unique := make([]int64, 0, len(ids))
|
||||||
|
seen := make(map[int64]struct{}, len(ids))
|
||||||
|
for _, id := range ids {
|
||||||
|
if id <= 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if _, ok := seen[id]; ok {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
seen[id] = struct{}{}
|
||||||
|
unique = append(unique, id)
|
||||||
|
}
|
||||||
|
if len(unique) == 0 {
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := s.queries.GetUsersByIDs(ctx, unique)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, u := range rows {
|
||||||
|
var lastLogin *time.Time
|
||||||
|
if u.LastLogin.Valid {
|
||||||
|
lastLogin = &u.LastLogin.Time
|
||||||
|
}
|
||||||
|
var updatedAt *time.Time
|
||||||
|
if u.UpdatedAt.Valid {
|
||||||
|
updatedAt = &u.UpdatedAt.Time
|
||||||
|
}
|
||||||
|
out[u.ID] = domain.User{
|
||||||
|
ID: u.ID,
|
||||||
|
FirstName: u.FirstName.String,
|
||||||
|
LastName: u.LastName.String,
|
||||||
|
Gender: u.Gender.String,
|
||||||
|
BirthDay: u.BirthDay.Time,
|
||||||
|
Email: u.Email.String,
|
||||||
|
PhoneNumber: u.PhoneNumber.String,
|
||||||
|
Role: domain.Role(u.Role),
|
||||||
|
AgeGroup: u.AgeGroup.String,
|
||||||
|
EducationLevel: u.EducationLevel.String,
|
||||||
|
Country: u.Country.String,
|
||||||
|
Region: u.Region.String,
|
||||||
|
NickName: u.NickName.String,
|
||||||
|
Occupation: u.Occupation.String,
|
||||||
|
LearningGoal: u.LearningGoal.String,
|
||||||
|
LanguageGoal: u.LanguageGoal.String,
|
||||||
|
LanguageChallange: u.LanguageChallange.String,
|
||||||
|
FavouriteTopic: u.FavouriteTopic.String,
|
||||||
|
EmailVerified: u.EmailVerified,
|
||||||
|
PhoneVerified: u.PhoneVerified,
|
||||||
|
Status: domain.UserStatus(u.Status),
|
||||||
|
LastLogin: lastLogin,
|
||||||
|
ProfileCompleted: u.ProfileCompleted.Bool,
|
||||||
|
ProfileCompletionPercentage: int(u.ProfileCompletionPercentage),
|
||||||
|
ProfilePictureURL: u.ProfilePictureUrl.String,
|
||||||
|
PreferredLanguage: u.PreferredLanguage.String,
|
||||||
|
CreatedAt: u.CreatedAt.Time,
|
||||||
|
UpdatedAt: updatedAt,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
// GetUserCreatedAt returns account created_at (used for timeline "joined" events).
|
// GetUserCreatedAt returns account created_at (used for timeline "joined" events).
|
||||||
func (s *Store) GetUserCreatedAt(ctx context.Context, userID int64) (time.Time, error) {
|
func (s *Store) GetUserCreatedAt(ctx context.Context, userID int64) (time.Time, error) {
|
||||||
ts, err := s.queries.GetUserCreatedAt(ctx, userID)
|
ts, err := s.queries.GetUserCreatedAt(ctx, userID)
|
||||||
|
|
|
||||||
|
|
@ -70,6 +70,10 @@ func (s *Service) GetTeamMemberByID(ctx context.Context, id int64) (domain.TeamM
|
||||||
return s.teamStore.GetTeamMemberByID(ctx, id)
|
return s.teamStore.GetTeamMemberByID(ctx, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetTeamMembersByIDs(ctx context.Context, ids []int64) (map[int64]domain.TeamMember, error) {
|
||||||
|
return s.teamStore.GetTeamMembersByIDs(ctx, ids)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Service) GetTeamMemberByEmail(ctx context.Context, email string) (domain.TeamMember, error) {
|
func (s *Service) GetTeamMemberByEmail(ctx context.Context, email string) (domain.TeamMember, error) {
|
||||||
return s.teamStore.GetTeamMemberByEmail(ctx, email)
|
return s.teamStore.GetTeamMemberByEmail(ctx, email)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,10 @@ func (s *Service) GetUserByID(ctx context.Context, id int64) (domain.User, error
|
||||||
return s.userStore.GetUserByID(ctx, id)
|
return s.userStore.GetUserByID(ctx, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Service) GetUsersByIDs(ctx context.Context, ids []int64) (map[int64]domain.User, error) {
|
||||||
|
return s.userStore.GetUsersByIDs(ctx, ids)
|
||||||
|
}
|
||||||
|
|
||||||
// GetAllUsers retrieves users based on the provided filter
|
// GetAllUsers retrieves users based on the provided filter
|
||||||
// func (s *Service) GetAllUsers(ctx context.Context, filter domain.UserFilter) ([]domain.User, int64, error) {
|
// func (s *Service) GetAllUsers(ctx context.Context, filter domain.UserFilter) ([]domain.User, int64, error) {
|
||||||
// var role *string
|
// var role *string
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,11 @@ type activityLogActorDisplay struct {
|
||||||
Role *string
|
Role *string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type activityLogActorCache struct {
|
||||||
|
users map[int64]domain.User
|
||||||
|
teams map[int64]domain.TeamMember
|
||||||
|
}
|
||||||
|
|
||||||
func userDisplayName(u domain.User) string {
|
func userDisplayName(u domain.User) string {
|
||||||
name := strings.TrimSpace(u.FirstName + " " + u.LastName)
|
name := strings.TrimSpace(u.FirstName + " " + u.LastName)
|
||||||
if name != "" {
|
if name != "" {
|
||||||
|
|
@ -79,14 +84,62 @@ func inferActivityLogActorSubject(log domain.ActivityLog) (subjectID int64, pref
|
||||||
return 0, false, false
|
return 0, false, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func collectActivityLogActorIDs(logs []domain.ActivityLog) []int64 {
|
||||||
|
seen := make(map[int64]struct{})
|
||||||
|
var ids []int64
|
||||||
|
add := func(id int64) {
|
||||||
|
if id <= 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, ok := seen[id]; ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
seen[id] = struct{}{}
|
||||||
|
ids = append(ids, id)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, log := range logs {
|
||||||
|
if log.ActorID != nil {
|
||||||
|
add(*log.ActorID)
|
||||||
|
}
|
||||||
|
if subjectID, _, ok := inferActivityLogActorSubject(log); ok {
|
||||||
|
add(subjectID)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ids
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) loadActivityLogActorCache(ctx context.Context, logs []domain.ActivityLog) activityLogActorCache {
|
||||||
|
ids := collectActivityLogActorIDs(logs)
|
||||||
|
cache := activityLogActorCache{
|
||||||
|
users: map[int64]domain.User{},
|
||||||
|
teams: map[int64]domain.TeamMember{},
|
||||||
|
}
|
||||||
|
if len(ids) == 0 {
|
||||||
|
return cache
|
||||||
|
}
|
||||||
|
|
||||||
|
if users, err := h.userSvc.GetUsersByIDs(ctx, ids); err == nil {
|
||||||
|
cache.users = users
|
||||||
|
}
|
||||||
|
if teams, err := h.teamSvc.GetTeamMembersByIDs(ctx, ids); err == nil {
|
||||||
|
cache.teams = teams
|
||||||
|
}
|
||||||
|
return cache
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Handler) resolveActivityLogActor(ctx context.Context, log domain.ActivityLog) activityLogActorDisplay {
|
func (h *Handler) resolveActivityLogActor(ctx context.Context, log domain.ActivityLog) activityLogActorDisplay {
|
||||||
|
return h.resolveActivityLogActorWithCache(log, h.loadActivityLogActorCache(ctx, []domain.ActivityLog{log}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) resolveActivityLogActorWithCache(log domain.ActivityLog, cache activityLogActorCache) activityLogActorDisplay {
|
||||||
storedRole := ""
|
storedRole := ""
|
||||||
if log.ActorRole != nil {
|
if log.ActorRole != nil {
|
||||||
storedRole = *log.ActorRole
|
storedRole = *log.ActorRole
|
||||||
}
|
}
|
||||||
|
|
||||||
if log.ActorID != nil {
|
if log.ActorID != nil {
|
||||||
if display, ok := h.resolveActorByID(ctx, *log.ActorID, storedRole, log); ok {
|
if display, ok := h.resolveActorByIDWithCache(*log.ActorID, storedRole, log, cache); ok {
|
||||||
if display.Role == nil && log.ActorRole != nil {
|
if display.Role == nil && log.ActorRole != nil {
|
||||||
display.Role = log.ActorRole
|
display.Role = log.ActorRole
|
||||||
}
|
}
|
||||||
|
|
@ -98,7 +151,7 @@ func (h *Handler) resolveActivityLogActor(ctx context.Context, log domain.Activi
|
||||||
}
|
}
|
||||||
|
|
||||||
if subjectID, preferTeam, ok := inferActivityLogActorSubject(log); ok {
|
if subjectID, preferTeam, ok := inferActivityLogActorSubject(log); ok {
|
||||||
if display, found := h.lookupActorByID(ctx, subjectID, preferTeam); found {
|
if display, found := h.lookupActorByIDWithCache(subjectID, preferTeam, cache); found {
|
||||||
return display
|
return display
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -106,33 +159,42 @@ func (h *Handler) resolveActivityLogActor(ctx context.Context, log domain.Activi
|
||||||
return activityLogActorDisplay{}
|
return activityLogActorDisplay{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) resolveActorByID(ctx context.Context, id int64, role string, log domain.ActivityLog) (activityLogActorDisplay, bool) {
|
func (h *Handler) resolveActorByIDWithCache(
|
||||||
return h.lookupActorByID(ctx, id, preferTeamMemberActorLookup(log, role))
|
id int64,
|
||||||
|
role string,
|
||||||
|
log domain.ActivityLog,
|
||||||
|
cache activityLogActorCache,
|
||||||
|
) (activityLogActorDisplay, bool) {
|
||||||
|
return h.lookupActorByIDWithCache(id, preferTeamMemberActorLookup(log, role), cache)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) lookupActorByID(ctx context.Context, id int64, preferTeam bool) (activityLogActorDisplay, bool) {
|
func (h *Handler) lookupActorByIDWithCache(
|
||||||
|
id int64,
|
||||||
|
preferTeam bool,
|
||||||
|
cache activityLogActorCache,
|
||||||
|
) (activityLogActorDisplay, bool) {
|
||||||
if preferTeam {
|
if preferTeam {
|
||||||
if display, ok := h.lookupTeamMemberActor(ctx, id); ok {
|
if display, ok := lookupTeamMemberActorFromCache(id, cache); ok {
|
||||||
return display, true
|
return display, true
|
||||||
}
|
}
|
||||||
if display, ok := h.lookupUserActor(ctx, id); ok {
|
if display, ok := lookupUserActorFromCache(id, cache); ok {
|
||||||
return display, true
|
return display, true
|
||||||
}
|
}
|
||||||
return activityLogActorDisplay{}, false
|
return activityLogActorDisplay{}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
if display, ok := h.lookupUserActor(ctx, id); ok {
|
if display, ok := lookupUserActorFromCache(id, cache); ok {
|
||||||
return display, true
|
return display, true
|
||||||
}
|
}
|
||||||
if display, ok := h.lookupTeamMemberActor(ctx, id); ok {
|
if display, ok := lookupTeamMemberActorFromCache(id, cache); ok {
|
||||||
return display, true
|
return display, true
|
||||||
}
|
}
|
||||||
return activityLogActorDisplay{}, false
|
return activityLogActorDisplay{}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) lookupUserActor(ctx context.Context, id int64) (activityLogActorDisplay, bool) {
|
func lookupUserActorFromCache(id int64, cache activityLogActorCache) (activityLogActorDisplay, bool) {
|
||||||
user, err := h.userSvc.GetUserByID(ctx, id)
|
user, ok := cache.users[id]
|
||||||
if err != nil {
|
if !ok {
|
||||||
return activityLogActorDisplay{}, false
|
return activityLogActorDisplay{}, false
|
||||||
}
|
}
|
||||||
name := userDisplayName(user)
|
name := userDisplayName(user)
|
||||||
|
|
@ -149,9 +211,9 @@ func (h *Handler) lookupUserActor(ctx context.Context, id int64) (activityLogAct
|
||||||
}, name != "" || email != ""
|
}, name != "" || email != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) lookupTeamMemberActor(ctx context.Context, id int64) (activityLogActorDisplay, bool) {
|
func lookupTeamMemberActorFromCache(id int64, cache activityLogActorCache) (activityLogActorDisplay, bool) {
|
||||||
member, err := h.teamSvc.GetTeamMemberByID(ctx, id)
|
member, ok := cache.teams[id]
|
||||||
if err != nil {
|
if !ok {
|
||||||
return activityLogActorDisplay{}, false
|
return activityLogActorDisplay{}, false
|
||||||
}
|
}
|
||||||
name := teamMemberDisplayName(member)
|
name := teamMemberDisplayName(member)
|
||||||
|
|
@ -167,3 +229,43 @@ func (h *Handler) lookupTeamMemberActor(ctx context.Context, id int64) (activity
|
||||||
Role: &roleStr,
|
Role: &roleStr,
|
||||||
}, name != "" || email != ""
|
}, name != "" || email != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Handler) toActivityLogResWithCache(l domain.ActivityLog, cache activityLogActorCache) activityLogRes {
|
||||||
|
actor := h.resolveActivityLogActorWithCache(l, cache)
|
||||||
|
|
||||||
|
actorID := l.ActorID
|
||||||
|
if actor.ActorID != nil {
|
||||||
|
actorID = actor.ActorID
|
||||||
|
}
|
||||||
|
|
||||||
|
actorRole := l.ActorRole
|
||||||
|
if actor.Role != nil {
|
||||||
|
actorRole = actor.Role
|
||||||
|
}
|
||||||
|
|
||||||
|
return activityLogRes{
|
||||||
|
ID: l.ID,
|
||||||
|
ActorID: actorID,
|
||||||
|
ActorRole: actorRole,
|
||||||
|
ActorName: actor.Name,
|
||||||
|
ActorEmail: actor.Email,
|
||||||
|
ActorKind: actor.Kind,
|
||||||
|
Action: l.Action,
|
||||||
|
ResourceType: l.ResourceType,
|
||||||
|
ResourceID: l.ResourceID,
|
||||||
|
Message: l.Message,
|
||||||
|
Metadata: l.Metadata,
|
||||||
|
IPAddress: l.IPAddress,
|
||||||
|
UserAgent: l.UserAgent,
|
||||||
|
CreatedAt: l.CreatedAt,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) toActivityLogResponses(ctx context.Context, logs []domain.ActivityLog) []activityLogRes {
|
||||||
|
cache := h.loadActivityLogActorCache(ctx, logs)
|
||||||
|
out := make([]activityLogRes, 0, len(logs))
|
||||||
|
for _, log := range logs {
|
||||||
|
out = append(out, h.toActivityLogResWithCache(log, cache))
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
|
|
@ -106,6 +107,10 @@ func (h *Handler) GetActivityLogs(c *fiber.Ctx) error {
|
||||||
filter.Before = &t
|
filter.Before = &t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if v := strings.TrimSpace(c.Query("search")); v != "" {
|
||||||
|
filter.Search = &v
|
||||||
|
}
|
||||||
|
|
||||||
limitStr := c.Query("limit", "20")
|
limitStr := c.Query("limit", "20")
|
||||||
limit, err := strconv.Atoi(limitStr)
|
limit, err := strconv.Atoi(limitStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -137,10 +142,7 @@ func (h *Handler) GetActivityLogs(c *fiber.Ctx) error {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var logResponses []activityLogRes
|
logResponses := h.toActivityLogResponses(c.Context(), logs)
|
||||||
for _, l := range logs {
|
|
||||||
logResponses = append(logResponses, h.toActivityLogRes(c.Context(), l))
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.JSON(domain.Response{
|
return c.JSON(domain.Response{
|
||||||
Message: "Activity logs retrieved successfully",
|
Message: "Activity logs retrieved successfully",
|
||||||
|
|
@ -188,32 +190,5 @@ func (h *Handler) GetActivityLogByID(c *fiber.Ctx) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) toActivityLogRes(ctx context.Context, l domain.ActivityLog) activityLogRes {
|
func (h *Handler) toActivityLogRes(ctx context.Context, l domain.ActivityLog) activityLogRes {
|
||||||
actor := h.resolveActivityLogActor(ctx, l)
|
return h.toActivityLogResWithCache(l, h.loadActivityLogActorCache(ctx, []domain.ActivityLog{l}))
|
||||||
|
|
||||||
actorID := l.ActorID
|
|
||||||
if actor.ActorID != nil {
|
|
||||||
actorID = actor.ActorID
|
|
||||||
}
|
|
||||||
|
|
||||||
actorRole := l.ActorRole
|
|
||||||
if actor.Role != nil {
|
|
||||||
actorRole = actor.Role
|
|
||||||
}
|
|
||||||
|
|
||||||
return activityLogRes{
|
|
||||||
ID: l.ID,
|
|
||||||
ActorID: actorID,
|
|
||||||
ActorRole: actorRole,
|
|
||||||
ActorName: actor.Name,
|
|
||||||
ActorEmail: actor.Email,
|
|
||||||
ActorKind: actor.Kind,
|
|
||||||
Action: l.Action,
|
|
||||||
ResourceType: l.ResourceType,
|
|
||||||
ResourceID: l.ResourceID,
|
|
||||||
Message: l.Message,
|
|
||||||
Metadata: l.Metadata,
|
|
||||||
IPAddress: l.IPAddress,
|
|
||||||
UserAgent: l.UserAgent,
|
|
||||||
CreatedAt: l.CreatedAt,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -268,9 +268,9 @@ func (h *Handler) ExportActivityLogsCSV(c *fiber.Ctx) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
rows := make([][]string, len(logs))
|
responses := h.toActivityLogResponses(ctx, logs)
|
||||||
for i, l := range logs {
|
rows := make([][]string, len(responses))
|
||||||
res := h.toActivityLogRes(ctx, l)
|
for i, res := range responses {
|
||||||
rows[i] = []string{
|
rows[i] = []string{
|
||||||
strconv.FormatInt(res.ID, 10),
|
strconv.FormatInt(res.ID, 10),
|
||||||
csvOptionalInt64(res.ActorID),
|
csvOptionalInt64(res.ActorID),
|
||||||
|
|
|
||||||
|
|
@ -340,6 +340,9 @@ func parseActivityLogFilterFields(c *fiber.Ctx) (domain.ActivityLogFilter, error
|
||||||
}
|
}
|
||||||
filter.Before = &t
|
filter.Before = &t
|
||||||
}
|
}
|
||||||
|
if v := strings.TrimSpace(c.Query("search")); v != "" {
|
||||||
|
filter.Search = &v
|
||||||
|
}
|
||||||
|
|
||||||
return filter, nil
|
return filter, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user