Add GraphQL endpoint for counting jobs

This commit is contained in:
Lou Knauer
2022-02-19 10:28:29 +01:00
parent 2ca27a83b9
commit dcb3fd6a6a
8 changed files with 382 additions and 17 deletions

View File

@@ -7,6 +7,7 @@ import (
"strconv"
"github.com/ClusterCockpit/cc-backend/auth"
"github.com/ClusterCockpit/cc-backend/graph/model"
"github.com/ClusterCockpit/cc-backend/schema"
sq "github.com/Masterminds/squirrel"
"github.com/jmoiron/sqlx"
@@ -16,6 +17,10 @@ type JobRepository struct {
DB *sqlx.DB
}
func (r *JobRepository) Init() error {
return nil
}
// Find executes a SQL query to find a specific batch job.
// The job is queried using the batch job id, the cluster name,
// and the start time of the job in UNIX epoch time seconds.
@@ -93,15 +98,18 @@ func (r *JobRepository) Stop(
return
}
// CountJobsPerCluster returns the number of jobs for the specified user (if a non-admin user is found in that context) and state.
// The counts are grouped by cluster.
func (r *JobRepository) CountJobsPerCluster(ctx context.Context, state *schema.JobState) (map[string]int, error) {
q := sq.Select("job.cluster, count(*)").From("job").GroupBy("job.cluster")
if state != nil {
q = q.Where("job.job_state = ?", string(*state))
func (r *JobRepository) CountGroupedJobs(ctx context.Context, aggreg model.Aggregate, filters []*model.JobFilter, limit *int) (map[string]int, error) {
if !aggreg.IsValid() {
return nil, errors.New("invalid aggregate")
}
if user := auth.GetUser(ctx); user != nil && !user.HasRole(auth.RoleAdmin) {
q = q.Where("job.user = ?", user.Username)
q := sq.Select("job."+string(aggreg), "count(*) as count").From("job").GroupBy("job." + string(aggreg)).OrderBy("count DESC")
q = SecurityCheck(ctx, q)
for _, f := range filters {
q = BuildWhereClause(f, q)
}
if limit != nil {
q = q.Limit(uint64(*limit))
}
counts := map[string]int{}
@@ -111,13 +119,13 @@ func (r *JobRepository) CountJobsPerCluster(ctx context.Context, state *schema.J
}
for rows.Next() {
var cluster string
var group string
var count int
if err := rows.Scan(&cluster, &count); err != nil {
if err := rows.Scan(&group, &count); err != nil {
return nil, err
}
counts[cluster] = count
counts[group] = count
}
return counts, nil

View File

@@ -16,9 +16,14 @@ func init() {
}
func setup(t *testing.T) *JobRepository {
return &JobRepository{
r := &JobRepository{
DB: db,
}
if err := r.Init(); err != nil {
t.Fatal(err)
}
return r
}
func TestFind(t *testing.T) {