mirror of
https://github.com/pocket-id/pocket-id.git
synced 2026-02-15 21:00:06 +00:00
fix: ensure indexes on audit_logs table (#415)
Co-authored-by: Kyle Mendell <kmendell@ofkm.us>
This commit is contained in:
committed by
GitHub
parent
731113183e
commit
9e88926283
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
@@ -100,10 +101,13 @@ func (s *AuditLogService) DeviceStringFromUserAgent(userAgent string) string {
|
||||
return ua.Name + " on " + ua.OS + " " + ua.OSVersion
|
||||
}
|
||||
|
||||
func (s *AuditLogService) ListAllAuditLogs(sortedPaginationRequest utils.SortedPaginationRequest, filters dto.AuditLogFilterDto) ([]model.AuditLog, utils.PaginationResponse, error) {
|
||||
func (s *AuditLogService) ListAllAuditLogs(ctx context.Context, sortedPaginationRequest utils.SortedPaginationRequest, filters dto.AuditLogFilterDto) ([]model.AuditLog, utils.PaginationResponse, error) {
|
||||
var logs []model.AuditLog
|
||||
|
||||
query := s.db.Preload("User").Model(&model.AuditLog{})
|
||||
query := s.db.
|
||||
WithContext(ctx).
|
||||
Preload("User").
|
||||
Model(&model.AuditLog{})
|
||||
|
||||
if filters.UserID != "" {
|
||||
query = query.Where("user_id = ?", filters.UserID)
|
||||
@@ -131,8 +135,11 @@ func (s *AuditLogService) ListAllAuditLogs(sortedPaginationRequest utils.SortedP
|
||||
return logs, pagination, nil
|
||||
}
|
||||
|
||||
func (s *AuditLogService) ListUsernamesWithIds() (users map[string]string, err error) {
|
||||
query := s.db.Joins("User").Model(&model.AuditLog{}).
|
||||
func (s *AuditLogService) ListUsernamesWithIds(ctx context.Context) (users map[string]string, err error) {
|
||||
query := s.db.
|
||||
WithContext(ctx).
|
||||
Joins("User").
|
||||
Model(&model.AuditLog{}).
|
||||
Select("DISTINCT User.id, User.username").
|
||||
Where("User.username IS NOT NULL")
|
||||
|
||||
@@ -146,7 +153,7 @@ func (s *AuditLogService) ListUsernamesWithIds() (users map[string]string, err e
|
||||
return nil, fmt.Errorf("failed to query user IDs: %w", err)
|
||||
}
|
||||
|
||||
users = make(map[string]string)
|
||||
users = make(map[string]string, len(results))
|
||||
for _, result := range results {
|
||||
users[result.ID] = result.Username
|
||||
}
|
||||
@@ -154,25 +161,27 @@ func (s *AuditLogService) ListUsernamesWithIds() (users map[string]string, err e
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (s *AuditLogService) ListClientNames() (clientNames []string, err error) {
|
||||
dialect := s.db.Name()
|
||||
var query *gorm.DB
|
||||
func (s *AuditLogService) ListClientNames(ctx context.Context) (clientNames []string, err error) {
|
||||
query := s.db.
|
||||
WithContext(ctx).
|
||||
Model(&model.AuditLog{})
|
||||
|
||||
dialect := s.db.Name()
|
||||
switch dialect {
|
||||
case "sqlite":
|
||||
query = s.db.Model(&model.AuditLog{}).
|
||||
Select("DISTINCT json_extract(data, '$.clientName') as clientName").
|
||||
query = query.
|
||||
Select("DISTINCT json_extract(data, '$.clientName') as client_name").
|
||||
Where("json_extract(data, '$.clientName') IS NOT NULL")
|
||||
case "postgres":
|
||||
query = s.db.Model(&model.AuditLog{}).
|
||||
Select("DISTINCT data->>'clientName' as clientName").
|
||||
query = query.
|
||||
Select("DISTINCT data->>'clientName' as client_name").
|
||||
Where("data->>'clientName' IS NOT NULL")
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported database dialect: %s", dialect)
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
ClientName string `gorm:"column:clientName"`
|
||||
ClientName string `gorm:"column:client_name"`
|
||||
}
|
||||
|
||||
var results []Result
|
||||
@@ -180,9 +189,9 @@ func (s *AuditLogService) ListClientNames() (clientNames []string, err error) {
|
||||
return nil, fmt.Errorf("failed to query client IDs: %w", err)
|
||||
}
|
||||
|
||||
for _, result := range results {
|
||||
clientNames = append(clientNames, result.ClientName)
|
||||
|
||||
clientNames = make([]string, len(results))
|
||||
for i, result := range results {
|
||||
clientNames[i] = result.ClientName
|
||||
}
|
||||
|
||||
return clientNames, nil
|
||||
|
||||
Reference in New Issue
Block a user