chore: update find activities

This commit is contained in:
Steven 2024-08-13 23:04:13 +08:00
parent 9876fb27a4
commit f057cd0078
4 changed files with 15 additions and 2 deletions

View File

@ -19,6 +19,7 @@ import (
v1pb "github.com/yourselfhosted/slash/proto/gen/api/v1" v1pb "github.com/yourselfhosted/slash/proto/gen/api/v1"
storepb "github.com/yourselfhosted/slash/proto/gen/store" storepb "github.com/yourselfhosted/slash/proto/gen/store"
"github.com/yourselfhosted/slash/server/metric" "github.com/yourselfhosted/slash/server/metric"
"github.com/yourselfhosted/slash/server/service/license"
"github.com/yourselfhosted/slash/store" "github.com/yourselfhosted/slash/store"
) )
@ -288,10 +289,15 @@ func (s *APIV1Service) GetShortcutAnalytics(ctx context.Context, request *v1pb.G
return nil, status.Errorf(codes.NotFound, "shortcut not found") return nil, status.Errorf(codes.NotFound, "shortcut not found")
} }
activities, err := s.Store.ListActivities(ctx, &store.FindActivity{ activityFind := &store.FindActivity{
Type: store.ActivityShortcutView, Type: store.ActivityShortcutView,
PayloadShortcutID: &shortcut.Id, PayloadShortcutID: &shortcut.Id,
}) }
if !s.LicenseService.IsFeatureEnabled(license.FeatureTypeAdvancedAnalytics) {
createdTsAfter := time.Now().AddDate(0, 0, -14).Unix()
activityFind.CreatedTsAfter = &createdTsAfter
}
activities, err := s.Store.ListActivities(ctx, activityFind)
if err != nil { if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get activities, err: %v", err) return nil, status.Errorf(codes.Internal, "failed to get activities, err: %v", err)
} }

View File

@ -59,6 +59,7 @@ type FindActivity struct {
Type ActivityType Type ActivityType
Level ActivityLevel Level ActivityLevel
PayloadShortcutID *int32 PayloadShortcutID *int32
CreatedTsAfter *int64
} }
func (s *Store) CreateActivity(ctx context.Context, create *Activity) (*Activity, error) { func (s *Store) CreateActivity(ctx context.Context, create *Activity) (*Activity, error) {

View File

@ -46,6 +46,9 @@ func (d *DB) ListActivities(ctx context.Context, find *store.FindActivity) ([]*s
if find.PayloadShortcutID != nil { if find.PayloadShortcutID != nil {
where, args = append(where, fmt.Sprintf("CAST(payload::JSON->>'shortcutId' AS INTEGER) = %s", placeholder(len(args)+1))), append(args, *find.PayloadShortcutID) where, args = append(where, fmt.Sprintf("CAST(payload::JSON->>'shortcutId' AS INTEGER) = %s", placeholder(len(args)+1))), append(args, *find.PayloadShortcutID)
} }
if find.CreatedTsAfter != nil {
where, args = append(where, "created_ts > "+placeholder(len(args)+1)), append(args, *find.CreatedTsAfter)
}
query := ` query := `
SELECT SELECT

View File

@ -45,6 +45,9 @@ func (d *DB) ListActivities(ctx context.Context, find *store.FindActivity) ([]*s
if find.PayloadShortcutID != nil { if find.PayloadShortcutID != nil {
where, args = append(where, "json_extract(payload, '$.shortcutId') = ?"), append(args, *find.PayloadShortcutID) where, args = append(where, "json_extract(payload, '$.shortcutId') = ?"), append(args, *find.PayloadShortcutID)
} }
if find.CreatedTsAfter != nil {
where, args = append(where, "created_ts > ?"), append(args, *find.CreatedTsAfter)
}
query := ` query := `
SELECT SELECT