package graph import ( "context" "database/sql" "errors" "fmt" "math" "github.com/99designs/gqlgen/graphql" "github.com/ClusterCockpit/cc-backend/config" "github.com/ClusterCockpit/cc-backend/graph/model" "github.com/ClusterCockpit/cc-backend/metricdata" "github.com/ClusterCockpit/cc-backend/schema" sq "github.com/Masterminds/squirrel" ) // GraphQL validation should make sure that no unkown values can be specified. var groupBy2column = map[model.Aggregate]string{ model.AggregateUser: "job.user", model.AggregateProject: "job.project", model.AggregateCluster: "job.cluster", } // Helper function for the jobsStatistics GraphQL query placed here so that schema.resolvers.go is not too full. func (r *queryResolver) jobsStatistics(ctx context.Context, filter []*model.JobFilter, groupBy *model.Aggregate) ([]*model.JobsStatistics, error) { // In case `groupBy` is nil (not used), the model.JobsStatistics used is at the key '' (empty string) stats := map[string]*model.JobsStatistics{} // `socketsPerNode` and `coresPerSocket` can differ from cluster to cluster, so we need to explicitly loop over those. for _, cluster := range config.Clusters { for _, partition := range cluster.Partitions { corehoursCol := fmt.Sprintf("ROUND(SUM(job.duration * job.num_nodes * %d * %d) / 3600)", partition.SocketsPerNode, partition.CoresPerSocket) var query sq.SelectBuilder if groupBy == nil { query = sq.Select( "''", "COUNT(job.id)", "ROUND(SUM(job.duration) / 3600)", corehoursCol, ).From("job") } else { col := groupBy2column[*groupBy] query = sq.Select( col, "COUNT(job.id)", "ROUND(SUM(job.duration) / 3600)", corehoursCol, ).From("job").GroupBy(col) } query = query. Where("job.cluster = ?", cluster.Name). Where("job.partition = ?", partition.Name) query = securityCheck(ctx, query) for _, f := range filter { query = buildWhereClause(f, query) } rows, err := query.RunWith(r.DB).Query() if err != nil { return nil, err } for rows.Next() { var id sql.NullString var jobs, walltime, corehours sql.NullInt64 if err := rows.Scan(&id, &jobs, &walltime, &corehours); err != nil { return nil, err } if id.Valid { if s, ok := stats[id.String]; ok { s.TotalJobs += int(jobs.Int64) s.TotalWalltime += int(walltime.Int64) s.TotalCoreHours += int(corehours.Int64) } else { stats[id.String] = &model.JobsStatistics{ ID: id.String, TotalJobs: int(jobs.Int64), TotalWalltime: int(walltime.Int64), TotalCoreHours: int(corehours.Int64), } } } } } } if groupBy == nil { query := sq.Select("COUNT(job.id)").From("job").Where("job.duration < 120") query = securityCheck(ctx, query) for _, f := range filter { query = buildWhereClause(f, query) } if err := query.RunWith(r.DB).QueryRow().Scan(&(stats[""].ShortJobs)); err != nil { return nil, err } } else { col := groupBy2column[*groupBy] query := sq.Select(col, "COUNT(job.id)").From("job").Where("job.duration < 120") query = securityCheck(ctx, query) for _, f := range filter { query = buildWhereClause(f, query) } rows, err := query.RunWith(r.DB).Query() if err != nil { return nil, err } for rows.Next() { var id sql.NullString var shortJobs sql.NullInt64 if err := rows.Scan(&id, &shortJobs); err != nil { return nil, err } if id.Valid { stats[id.String].ShortJobs = int(shortJobs.Int64) } } } // Calculating the histogram data is expensive, so only do it if needed. // An explicit resolver can not be used because we need to know the filters. histogramsNeeded := false fields := graphql.CollectFieldsCtx(ctx, nil) for _, col := range fields { if col.Name == "histWalltime" || col.Name == "histNumNodes" { histogramsNeeded = true } } res := make([]*model.JobsStatistics, 0, len(stats)) for _, stat := range stats { res = append(res, stat) id, col := "", "" if groupBy != nil { id = stat.ID col = groupBy2column[*groupBy] } if histogramsNeeded { var err error stat.HistWalltime, err = r.jobsStatisticsHistogram(ctx, "ROUND(job.duration / 3600) as value", filter, id, col) if err != nil { return nil, err } stat.HistNumNodes, err = r.jobsStatisticsHistogram(ctx, "job.num_nodes as value", filter, id, col) if err != nil { return nil, err } } } return res, nil } // `value` must be the column grouped by, but renamed to "value". `id` and `col` can optionally be used // to add a condition to the query of the kind "