...

Source file src/edge-infra.dev/pkg/edge/api/services/activity_service.go

Documentation: edge-infra.dev/pkg/edge/api/services

     1  package services
     2  
     3  import (
     4  	"context"
     5  	"database/sql"
     6  	"time"
     7  
     8  	sqlerr "edge-infra.dev/pkg/edge/api/apierror/sql"
     9  	"edge-infra.dev/pkg/edge/api/graph/model"
    10  	sqlquery "edge-infra.dev/pkg/edge/api/sql"
    11  	"edge-infra.dev/pkg/edge/api/types"
    12  )
    13  
    14  //go:generate mockgen -destination=../mocks/mock_activity_service.go -package=mocks edge-infra.dev/pkg/edge/api/services ActivityService
    15  type ActivityService interface {
    16  	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)
    17  	CreateAction(ctx context.Context, action model.Action) error
    18  }
    19  
    20  type activityService struct {
    21  	Config *types.Config
    22  	SQLDB  *sql.DB
    23  }
    24  
    25  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) {
    26  	var (
    27  		actions    []*model.Action
    28  		rows       *sql.Rows
    29  		err        error
    30  		totalCount int
    31  	)
    32  
    33  	row := s.SQLDB.QueryRowContext(ctx, sqlquery.GetActionsCount, username, bannerEdgeID, clusterEdgeID, batchID, action, status, tenantEdgeID)
    34  	err = row.Scan(&totalCount)
    35  	if err != nil {
    36  		return nil, totalCount, err
    37  	}
    38  
    39  	actionsQuery := sqlquery.GetActionsByCatchAllQuery
    40  	if previous {
    41  		actionsQuery = sqlquery.GetPreviousActionsQuery
    42  	}
    43  
    44  	rows, err = s.SQLDB.QueryContext(ctx, actionsQuery, username, bannerEdgeID, clusterEdgeID, batchID, action, status, tenantEdgeID, cursor, count)
    45  	if err != nil {
    46  		return nil, totalCount, err
    47  	}
    48  
    49  	for rows.Next() {
    50  		action := model.Action{}
    51  		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 {
    52  			return nil, totalCount, sqlerr.Wrap(err)
    53  		}
    54  		if previous {
    55  			actions = append([]*model.Action{&action}, actions...)
    56  		} else {
    57  			actions = append(actions, &action)
    58  		}
    59  	}
    60  	if err := rows.Err(); err != nil {
    61  		return nil, totalCount, sqlerr.Wrap(err)
    62  	}
    63  	return actions, totalCount, nil
    64  }
    65  
    66  func (s activityService) CreateAction(ctx context.Context, action model.Action) error {
    67  	_, 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())
    68  	return err
    69  }
    70  
    71  func NewActivityService(config *types.Config, sqlDB *sql.DB) ActivityService { //nolint
    72  	return &activityService{
    73  		Config: config,
    74  		SQLDB:  sqlDB,
    75  	}
    76  }
    77  

View as plain text