package services import ( "context" "database/sql" "time" sqlerr "edge-infra.dev/pkg/edge/api/apierror/sql" "edge-infra.dev/pkg/edge/api/graph/model" sqlquery "edge-infra.dev/pkg/edge/api/sql" "edge-infra.dev/pkg/edge/api/types" ) //go:generate mockgen -destination=../mocks/mock_activity_service.go -package=mocks edge-infra.dev/pkg/edge/api/services ActivityService type ActivityService interface { GetActions(ctx context.Context, username *string, bannerEdgeID *string, clusterEdgeID *string, batchID *string, action *string, status *string, tenantEdgeID, cursor string, count int, previous bool) ([]*model.Action, int, error) CreateAction(ctx context.Context, action model.Action) error } type activityService struct { Config *types.Config SQLDB *sql.DB } func (s activityService) GetActions(ctx context.Context, username *string, bannerEdgeID *string, clusterEdgeID *string, batchID *string, action *string, status *string, tenantEdgeID, cursor string, count int, previous bool) ([]*model.Action, int, error) { var ( actions []*model.Action rows *sql.Rows err error totalCount int ) row := s.SQLDB.QueryRowContext(ctx, sqlquery.GetActionsCount, username, bannerEdgeID, clusterEdgeID, batchID, action, status, tenantEdgeID) err = row.Scan(&totalCount) if err != nil { return nil, totalCount, err } actionsQuery := sqlquery.GetActionsByCatchAllQuery if previous { actionsQuery = sqlquery.GetPreviousActionsQuery } rows, err = s.SQLDB.QueryContext(ctx, actionsQuery, username, bannerEdgeID, clusterEdgeID, batchID, action, status, tenantEdgeID, cursor, count) if err != nil { return nil, totalCount, err } for rows.Next() { action := model.Action{} if err := rows.Scan(&action.ActionID, &action.Input, &action.Action, &action.Username, &action.TenantEdgeID, &action.BannerEdgeID, &action.ClusterEdgeID, &action.BatchID, &action.Status, &action.Error, &action.Time); err != nil { return nil, totalCount, sqlerr.Wrap(err) } if previous { actions = append([]*model.Action{&action}, actions...) } else { actions = append(actions, &action) } } if err := rows.Err(); err != nil { return nil, totalCount, sqlerr.Wrap(err) } return actions, totalCount, nil } func (s activityService) CreateAction(ctx context.Context, action model.Action) error { _, err := s.SQLDB.ExecContext(ctx, sqlquery.CreateActionEntry, action.Input, action.Action, action.Username, action.TenantEdgeID, action.BannerEdgeID, action.ClusterEdgeID, action.BatchID, action.Status, action.Error, time.Now()) return err } func NewActivityService(config *types.Config, sqlDB *sql.DB) ActivityService { //nolint return &activityService{ Config: config, SQLDB: sqlDB, } }