     1→// Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
     2→// All rights reserved. This file is part of cc-backend.
     3→// Use of this source code is governed by a MIT-style
     4→// license that can be found in the LICENSE file.
     5→
     6→// This file contains job statistics and histogram generation functionality for the JobRepository.
     7→//
     8→// # Job Statistics
     9→//
    10→// The statistics methods provide aggregated metrics about jobs including total jobs, users,
    11→// walltime, and resource usage (nodes, cores, accelerators). Statistics can be computed:
    12→//   - Overall (JobsStats): Single aggregate across all matching jobs
    13→//   - Grouped (JobsStatsGrouped): Aggregated by user, project, cluster, or subcluster
    14→//   - Counts (JobCountGrouped, AddJobCount): Simple job counts with optional filtering
    15→//
    16→// All statistics methods support filtering via JobFilter and respect security contexts.
    17→//
    18→// # Histograms
    19→//
    20→// Histogram methods generate distribution data for visualization:
    21→//   - Duration, nodes, cores, accelerators (AddHistograms)
    22→//   - Job metrics like CPU load, memory usage (AddMetricHistograms)
    23→//
    24→// Histograms use intelligent binning:
    25→//   - Duration: Variable bin sizes (1m, 10m, 1h, 6h, 12h, 24h) with zero-padding
    26→//   - Resources: Natural value-based bins
    27→//   - Metrics: Normalized to peak values with configurable bin counts
    28→//
    29→// # Running vs. Completed Jobs
    30→//
    31→// Statistics handle running jobs specially:
    32→//   - Duration calculated as (now - start_time) for running jobs
    33→//   - Metric histograms for running jobs load data from metric backend instead of footprint
    34→//   - Job state filtering distinguishes running/completed jobs
    35→//
    36→// # Performance Considerations
    37→//
    38→// - All queries use prepared statements via stmtCache
    39→// - Complex aggregations use SQL for efficiency
    40→// - Histogram pre-initialization ensures consistent bin ranges
    41→// - Metric histogram queries limited to 5000 jobs for running job analysis
    42→
    43→package repository
    44→
    45→import (
    46→	"context"
    47→	"database/sql"
    48→	"fmt"
    49→	"time"
    50→
    51→	"github.com/ClusterCockpit/cc-backend/internal/config"
    52→	"github.com/ClusterCockpit/cc-backend/internal/graph/model"
    53→	"github.com/ClusterCockpit/cc-backend/internal/metricdispatch"
    54→	"github.com/ClusterCockpit/cc-backend/pkg/archive"
    55→	cclog "github.com/ClusterCockpit/cc-lib/v2/ccLogger"
    56→	"github.com/ClusterCockpit/cc-lib/v2/schema"
    57→	sq "github.com/Masterminds/squirrel"
    58→	"golang.org/x/sync/errgroup"
    59→)
    60→
    61→// groupBy2column maps GraphQL Aggregate enum values to their corresponding database column names.
    62→// Used by JobsStatsGrouped and JobCountGrouped to translate user-facing grouping dimensions
    63→// into SQL GROUP BY clauses. GraphQL validation ensures only valid enum values are accepted.
    64→var groupBy2column = map[model.Aggregate]string{
    65→	model.AggregateUser:       "job.hpc_user",
    66→	model.AggregateProject:    "job.project",
    67→	model.AggregateCluster:    "job.cluster",
    68→	model.AggregateSubcluster: "job.subcluster",
    69→}
    70→
    71→// sortBy2column maps GraphQL SortByAggregate enum values to their corresponding computed column names.
    72→// Used by JobsStatsGrouped to translate sort preferences into SQL ORDER BY clauses.
    73→// Column names match the AS aliases used in buildStatsQuery.
    74→var sortBy2column = map[model.SortByAggregate]string{
    75→	model.SortByAggregateTotaljobs:      "totalJobs",
    76→	model.SortByAggregateTotalusers:     "totalUsers",
    77→	model.SortByAggregateTotalwalltime:  "totalWalltime",
    78→	model.SortByAggregateTotalnodes:     "totalNodes",
    79→	model.SortByAggregateTotalnodehours: "totalNodeHours",
    80→	model.SortByAggregateTotalcores:     "totalCores",
    81→	model.SortByAggregateTotalcorehours: "totalCoreHours",
    82→	model.SortByAggregateTotalaccs:      "totalAccs",
    83→	model.SortByAggregateTotalacchours:  "totalAccHours",
    84→}
    85→
    86→// buildCountQuery constructs a SQL query to count jobs with optional grouping and filtering.
    87→//
    88→// Parameters:
    89→//   - filter: Job filters to apply (cluster, user, time range, etc.)
    90→//   - kind: Special filter - "running" for running jobs only, "short" for jobs under threshold
    91→//   - col: Column name to GROUP BY; empty string for total count without grouping
    92→//
    93→// Returns a SelectBuilder that produces either:
    94→//   - Single count: COUNT(job.id) when col is empty
    95→//   - Grouped counts: col, COUNT(job.id) when col is specified
    96→//
    97→// The kind parameter enables counting specific job categories:
    98→//   - "running": Only jobs with job_state = 'running'
    99→//   - "short": Only jobs with duration < ShortRunningJobsDuration config value
   100→//   - empty: All jobs matching filters
   101→func (r *JobRepository) buildCountQuery(
   102→	filter []*model.JobFilter,
   103→	kind string,
   104→	col string,
   105→) sq.SelectBuilder {
   106→	var query sq.SelectBuilder
   107→
   108→	if col != "" {
   109→		query = sq.Select(col, "COUNT(*)").From("job").GroupBy(col)
   110→	} else {
   111→		query = sq.Select("COUNT(*)").From("job")
   112→	}
   113→
   114→	switch kind {
   115→	case "running":
   116→		query = query.Where("job.job_state = ?", "running")
   117→	case "short":
   118→		query = query.Where("job.duration < ?", config.Keys.ShortRunningJobsDuration)
   119→	}
   120→
   121→	for _, f := range filter {
   122→		query = BuildWhereClause(f, query)
   123→	}
   124→
   125→	return query
   126→}
   127→
   128→// buildStatsQuery constructs a SQL query to compute job statistics with optional grouping.
   129→// Only requested columns are computed; unrequested columns select 0 as placeholder.
   130→//
   131→// Parameters:
   132→//   - filter: Job filters to apply (cluster, user, time range, etc.)
   133→//   - col: Column name to GROUP BY; empty string for overall statistics without grouping
   134→//   - shortThreshold: Duration threshold in seconds for counting short-running jobs
   135→//   - reqFields: Set of requested field names; nil means compute all fields
   136→func (r *JobRepository) buildStatsQuery(
   137→	filter []*model.JobFilter,
   138→	col string,
   139→	shortThreshold int,
   140→	reqFields map[string]bool,
   141→) sq.SelectBuilder {
   142→	now := time.Now().Unix()
   143→
   144→	// Helper: return real expression if field is requested (or reqFields is nil), else "0 as alias"
   145→	need := func(field string) bool {
   146→		return reqFields == nil || reqFields[field]
   147→	}
   148→	durationExpr := fmt.Sprintf(`(CASE WHEN job.job_state = 'running' THEN %d - job.start_time ELSE job.duration END)`, now)
   149→
   150→	// Build column list
   151→	columns := make([]string, 0, 14)
   152→
   153→	if col != "" {
   154→		columns = append(columns, col)
   155→	}
   156→
   157→	columns = append(columns, "COUNT(*) as totalJobs")
   158→
   159→	if need("totalUsers") && col != "job.hpc_user" {
   160→		columns = append(columns, "COUNT(DISTINCT job.hpc_user) AS totalUsers")
   161→	} else {
   162→		columns = append(columns, "0 AS totalUsers")
   163→	}
   164→
   165→	if need("totalWalltime") {
   166→		columns = append(columns, fmt.Sprintf(`CAST(ROUND(SUM(%s) / 3600) as int) as totalWalltime`, durationExpr))
   167→	} else {
   168→		columns = append(columns, "0 as totalWalltime")
   169→	}
   170→
   171→	if need("totalNodes") {
   172→		columns = append(columns, `CAST(SUM(job.num_nodes) as int) as totalNodes`)
   173→	} else {
   174→		columns = append(columns, "0 as totalNodes")
   175→	}
   176→
   177→	if need("totalNodeHours") {
   178→		columns = append(columns, fmt.Sprintf(`CAST(ROUND(SUM(%s * job.num_nodes) / 3600) as int) as totalNodeHours`, durationExpr))
   179→	} else {
   180→		columns = append(columns, "0 as totalNodeHours")
   181→	}
   182→
   183→	if need("totalCores") {
   184→		columns = append(columns, `CAST(SUM(job.num_hwthreads) as int) as totalCores`)
   185→	} else {
   186→		columns = append(columns, "0 as totalCores")
   187→	}
   188→
   189→	if need("totalCoreHours") {
   190→		columns = append(columns, fmt.Sprintf(`CAST(ROUND(SUM(%s * job.num_hwthreads) / 3600) as int) as totalCoreHours`, durationExpr))
   191→	} else {
   192→		columns = append(columns, "0 as totalCoreHours")
   193→	}
   194→
   195→	if need("totalAccs") {
   196→		columns = append(columns, `CAST(SUM(job.num_acc) as int) as totalAccs`)
   197→	} else {
   198→		columns = append(columns, "0 as totalAccs")
   199→	}
   200→
   201→	if need("totalAccHours") {
   202→		columns = append(columns, fmt.Sprintf(`CAST(ROUND(SUM(%s * job.num_acc) / 3600) as int) as totalAccHours`, durationExpr))
   203→	} else {
   204→		columns = append(columns, "0 as totalAccHours")
   205→	}
   206→
   207→	if need("runningJobs") {
   208→		columns = append(columns, `COUNT(CASE WHEN job.job_state = 'running' THEN 1 END) as runningJobs`)
   209→	} else {
   210→		columns = append(columns, "0 as runningJobs")
   211→	}
   212→
   213→	if need("shortJobs") {
   214→		columns = append(columns, fmt.Sprintf(`COUNT(CASE WHEN job.duration < %d THEN 1 END) as shortJobs`, shortThreshold))
   215→	} else {
   216→		columns = append(columns, "0 as shortJobs")
   217→	}
   218→
   219→	query := sq.Select(columns...).From("job")
   220→	if col != "" {
   221→		query = query.GroupBy(col)
   222→	}
   223→
   224→	for _, f := range filter {
   225→		query = BuildWhereClause(f, query)
   226→	}
   227→
   228→	return query
   229→}
   230→
   231→// JobsStatsGrouped computes job statistics grouped by a dimension (user, project, cluster, or subcluster).
   232→// Only columns listed in reqFields are computed; others return 0. User display names are looked up
   233→// in a separate lightweight query to avoid JOIN overhead on the main aggregation.
   234→func (r *JobRepository) JobsStatsGrouped(
   235→	ctx context.Context,
   236→	filter []*model.JobFilter,
   237→	page *model.PageRequest,
   238→	sortBy *model.SortByAggregate,
   239→	groupBy *model.Aggregate,
   240→	reqFields map[string]bool,
   241→) ([]*model.JobsStatistics, error) {
   242→	col := groupBy2column[*groupBy]
   243→	query := r.buildStatsQuery(filter, col, config.Keys.ShortRunningJobsDuration, reqFields)
   244→
   245→	query, err := SecurityCheck(ctx, query)
   246→	if err != nil {
   247→		return nil, err
   248→	}
   249→
   250→	if sortBy != nil {
   251→		sortBy := sortBy2column[*sortBy]
   252→		query = query.OrderBy(fmt.Sprintf("%s DESC", sortBy))
   253→	}
   254→	if page != nil && page.ItemsPerPage != -1 {
   255→		limit := uint64(page.ItemsPerPage)
   256→		query = query.Offset((uint64(page.Page) - 1) * limit).Limit(limit)
   257→	}
   258→
   259→	rows, err := query.RunWith(r.DB).QueryContext(ctx)
   260→	if err != nil {
   261→		cclog.Warn("Error while querying DB for job statistics")
   262→		return nil, err
   263→	}
   264→	defer rows.Close()
   265→
   266→	stats := make([]*model.JobsStatistics, 0, 100)
   267→
   268→	for rows.Next() {
   269→		var id sql.NullString
   270→		var jobs, users, walltime, nodes, nodeHours, cores, coreHours, accs, accHours, runningJobs, shortJobs sql.NullInt64
   271→		if err := rows.Scan(&id, &jobs, &users, &walltime, &nodes, &nodeHours, &cores, &coreHours, &accs, &accHours, &runningJobs, &shortJobs); err != nil {
   272→			cclog.Warnf("Error while scanning rows: %s", err.Error())
   273→			return nil, err
   274→		}
   275→
   276→		if id.Valid {
   277→			stats = append(stats,
   278→				&model.JobsStatistics{
   279→					ID:             id.String,
   280→					TotalJobs:      int(jobs.Int64),
   281→					TotalUsers:     int(users.Int64),
   282→					TotalWalltime:  int(walltime.Int64),
   283→					TotalNodes:     int(nodes.Int64),
   284→					TotalNodeHours: int(nodeHours.Int64),
   285→					TotalCores:     int(cores.Int64),
   286→					TotalCoreHours: int(coreHours.Int64),
   287→					TotalAccs:      int(accs.Int64),
   288→					TotalAccHours:  int(accHours.Int64),
   289→					RunningJobs:    int(runningJobs.Int64),
   290→					ShortJobs:      int(shortJobs.Int64),
   291→				})
   292→		}
   293→	}
   294→
   295→	if err := rows.Err(); err != nil {
   296→		return nil, err
   297→	}
   298→
   299→	// Post-query name lookup for user grouping (avoids LEFT JOIN on aggregation query)
   300→	if col == "job.hpc_user" && len(stats) > 0 {
   301→		usernames := make([]any, len(stats))
   302→		for i, s := range stats {
   303→			usernames[i] = s.ID
   304→		}
   305→
   306→		nameQuery := sq.Select("username", "name").From("hpc_user").Where(sq.Eq{"username": usernames})
   307→		nameRows, err := nameQuery.RunWith(r.DB).QueryContext(ctx)
   308→		if err != nil {
   309→			cclog.Warnf("Error looking up user names: %s", err.Error())
   310→			// Non-fatal: stats are still valid without display names
   311→		} else {
   312→			defer nameRows.Close()
   313→			nameMap := make(map[string]string, len(stats))
   314→			for nameRows.Next() {
   315→				var username, name string
   316→				if err := nameRows.Scan(&username, &name); err == nil {
   317→					nameMap[username] = name
   318→				}
   319→			}
   320→			for _, s := range stats {
   321→				if name, ok := nameMap[s.ID]; ok {
   322→					s.Name = name
   323→				}
   324→			}
   325→		}
   326→	}
   327→
   328→	return stats, nil
   329→}
   330→
   331→// JobsStats computes overall job statistics across all matching jobs without grouping.
   332→//
   333→// This method provides a single aggregate view of job metrics, useful for dashboard
   334→// summaries and overall system utilization reports.
   335→//
   336→// Parameters:
   337→//   - ctx: Context for security checks and cancellation
   338→//   - filter: Filters to apply (time range, cluster, job state, etc.)
   339→//
   340→// Returns a single-element slice containing aggregate statistics:
   341→//   - totalJobs, totalUsers, totalWalltime
   342→//   - totalNodeHours, totalCoreHours, totalAccHours
   343→//
   344→// Unlike JobsStatsGrouped, this returns overall totals without breaking down by dimension.
   345→// Security checks are applied via SecurityCheck to respect user access levels.
   346→func (r *JobRepository) JobsStats(
   347→	ctx context.Context,
   348→	filter []*model.JobFilter,
   349→	reqFields map[string]bool,
   350→) ([]*model.JobsStatistics, error) {
   351→	start := time.Now()
   352→	query := r.buildStatsQuery(filter, "", config.Keys.ShortRunningJobsDuration, reqFields)
   353→	query, err := SecurityCheck(ctx, query)
   354→	if err != nil {
   355→		return nil, err
   356→	}
   357→
   358→	row := query.RunWith(r.DB).QueryRowContext(ctx)
   359→	stats := make([]*model.JobsStatistics, 0, 1)
   360→
   361→	var jobs, users, walltime, nodes, nodeHours, cores, coreHours, accs, accHours, runningJobs, shortJobs sql.NullInt64
   362→	if err := row.Scan(&jobs, &users, &walltime, &nodes, &nodeHours, &cores, &coreHours, &accs, &accHours, &runningJobs, &shortJobs); err != nil {
   363→		cclog.Warn("Error while scanning rows")
   364→		return nil, err
   365→	}
   366→
   367→	if jobs.Valid {
   368→		var totalNodeHours, totalCoreHours, totalAccHours int
   369→
   370→		if nodeHours.Valid {
   371→			totalNodeHours = int(nodeHours.Int64)
   372→		}
   373→		if coreHours.Valid {
   374→			totalCoreHours = int(coreHours.Int64)
   375→		}
   376→		if accHours.Valid {
   377→			totalAccHours = int(accHours.Int64)
   378→		}
   379→		stats = append(stats,
   380→			&model.JobsStatistics{
   381→				TotalJobs:      int(jobs.Int64),
   382→				TotalUsers:     int(users.Int64),
   383→				TotalWalltime:  int(walltime.Int64),
   384→				TotalNodeHours: totalNodeHours,
   385→				TotalCoreHours: totalCoreHours,
   386→				TotalAccHours:  totalAccHours,
   387→				RunningJobs:    int(runningJobs.Int64),
   388→				ShortJobs:      int(shortJobs.Int64),
   389→			})
   390→	}
   391→
   392→	cclog.Debugf("Timer JobStats %s", time.Since(start))
   393→	return stats, nil
   394→}
   395→
   396→// LoadJobStat retrieves a specific statistic for a metric from a job's statistics.
   397→// Returns 0.0 if the metric is not found or statType is invalid.
   398→//
   399→// Parameters:
   400→//   - job: Job struct with populated Statistics field
   401→//   - metric: Name of the metric to query (e.g., "cpu_load", "mem_used")
   402→//   - statType: Type of statistic: "avg", "min", or "max"
   403→//
   404→// Returns the requested statistic value or 0.0 if not found.
   405→func LoadJobStat(job *schema.Job, metric string, statType string) float64 {
   406→	if stats, ok := job.Statistics[metric]; ok {
   407→		switch statType {
   408→		case "avg":
   409→			return stats.Avg
   410→		case "max":
   411→			return stats.Max
   412→		case "min":
   413→			return stats.Min
   414→		default:
   415→			cclog.Errorf("Unknown stat type %s", statType)
   416→		}
   417→	}
   418→
   419→	return 0.0
   420→}
   421→
   422→// JobCountGrouped counts jobs grouped by a dimension without computing detailed statistics.
   423→//
   424→// This is a lightweight alternative to JobsStatsGrouped when only job counts are needed,
   425→// avoiding the overhead of calculating walltime and resource usage metrics.
   426→//
   427→// Parameters:
   428→//   - ctx: Context for security checks
   429→//   - filter: Filters to apply
   430→//   - groupBy: Grouping dimension (User, Project, Cluster, or SubCluster)
   431→//
   432→// Returns JobsStatistics with only ID and TotalJobs populated for each group.
   433→func (r *JobRepository) JobCountGrouped(
   434→	ctx context.Context,
   435→	filter []*model.JobFilter,
   436→	groupBy *model.Aggregate,
   437→) ([]*model.JobsStatistics, error) {
   438→	start := time.Now()
   439→	col := groupBy2column[*groupBy]
   440→	query := r.buildCountQuery(filter, "", col)
   441→	query, err := SecurityCheck(ctx, query)
   442→	if err != nil {
   443→		return nil, err
   444→	}
   445→	rows, err := query.RunWith(r.DB).QueryContext(ctx)
   446→	if err != nil {
   447→		cclog.Warn("Error while querying DB for job statistics")
   448→		return nil, err
   449→	}
   450→	defer rows.Close()
   451→
   452→	stats := make([]*model.JobsStatistics, 0, 100)
   453→
   454→	for rows.Next() {
   455→		var id sql.NullString
   456→		var cnt sql.NullInt64
   457→		if err := rows.Scan(&id, &cnt); err != nil {
   458→			cclog.Warn("Error while scanning rows")
   459→			return nil, err
   460→		}
   461→		if id.Valid {
   462→			stats = append(stats,
   463→				&model.JobsStatistics{
   464→					ID:        id.String,
   465→					TotalJobs: int(cnt.Int64),
   466→				})
   467→		}
   468→	}
   469→
   470→	if err := rows.Err(); err != nil {
   471→		return nil, err
   472→	}
   473→
   474→	cclog.Debugf("Timer JobCountGrouped %s", time.Since(start))
   475→	return stats, nil
   476→}
   477→
   478→// AddJobCountGrouped augments existing statistics with additional job counts by category.
   479→//
   480→// This method enriches JobsStatistics returned by JobsStatsGrouped or JobCountGrouped
   481→// with counts of running or short-running (based on ShortRunningJobsDuration) jobs, matched by group ID.
   482→//
   483→// Parameters:
   484→//   - ctx: Context for security checks
   485→//   - filter: Filters to apply
   486→//   - groupBy: Grouping dimension (must match the dimension used for stats parameter)
   487→//   - stats: Existing statistics to augment (modified in-place by ID matching)
   488→//   - kind: "running" to add RunningJobs count, "short" to add ShortJobs count
   489→//
   490→// Returns the same stats slice with RunningJobs or ShortJobs fields populated per group.
   491→// Groups without matching jobs will have 0 for the added field.
   492→func (r *JobRepository) AddJobCountGrouped(
   493→	ctx context.Context,
   494→	filter []*model.JobFilter,
   495→	groupBy *model.Aggregate,
   496→	stats []*model.JobsStatistics,
   497→	kind string,
   498→) ([]*model.JobsStatistics, error) {
   499→	start := time.Now()
   500→	col := groupBy2column[*groupBy]
   501→	query := r.buildCountQuery(filter, kind, col)
   502→	query, err := SecurityCheck(ctx, query)
   503→	if err != nil {
   504→		return nil, err
   505→	}
   506→	rows, err := query.RunWith(r.DB).QueryContext(ctx)
   507→	if err != nil {
   508→		cclog.Warn("Error while querying DB for job statistics")
   509→		return nil, err
   510→	}
   511→	defer rows.Close()
   512→
   513→	counts := make(map[string]int)
   514→
   515→	for rows.Next() {
   516→		var id sql.NullString
   517→		var cnt sql.NullInt64
   518→		if err := rows.Scan(&id, &cnt); err != nil {
   519→			cclog.Warn("Error while scanning rows")
   520→			return nil, err
   521→		}
   522→		if id.Valid {
   523→			counts[id.String] = int(cnt.Int64)
   524→		}
   525→	}
   526→
   527→	if err := rows.Err(); err != nil {
   528→		return nil, err
   529→	}
   530→
   531→	switch kind {
   532→	case "running":
   533→		for _, s := range stats {
   534→			s.RunningJobs = counts[s.ID]
   535→		}
   536→	case "short":
   537→		for _, s := range stats {
   538→			s.ShortJobs = counts[s.ID]
   539→		}
   540→	}
   541→
   542→	cclog.Debugf("Timer AddJobCountGrouped %s", time.Since(start))
   543→	return stats, nil
   544→}
   545→
   546→// AddJobCount augments existing overall statistics with additional job counts by category.
   547→//
   548→// Similar to AddJobCountGrouped but for ungrouped statistics. Applies the same count
   549→// to all statistics entries (typically just one).
   550→//
   551→// Parameters:
   552→//   - ctx: Context for security checks
   553→//   - filter: Filters to apply
   554→//   - stats: Existing statistics to augment (modified in-place)
   555→//   - kind: "running" to add RunningJobs count, "short" to add ShortJobs count
   556→//
   557→// Returns the same stats slice with RunningJobs or ShortJobs fields set to the total count.
   558→func (r *JobRepository) AddJobCount(
   559→	ctx context.Context,
   560→	filter []*model.JobFilter,
   561→	stats []*model.JobsStatistics,
   562→	kind string,
   563→) ([]*model.JobsStatistics, error) {
   564→	start := time.Now()
   565→	query := r.buildCountQuery(filter, kind, "")
   566→	query, err := SecurityCheck(ctx, query)
   567→	if err != nil {
   568→		return nil, err
   569→	}
   570→	var cnt sql.NullInt64
   571→	if err := query.RunWith(r.DB).QueryRowContext(ctx).Scan(&cnt); err != nil {
   572→		cclog.Warn("Error while querying DB for job count")
   573→		return nil, err
   574→	}
   575→
   576→	count := int(cnt.Int64)
   577→
   578→	switch kind {
   579→	case "running":
   580→		for _, s := range stats {
   581→			s.RunningJobs = count
   582→		}
   583→	case "short":
   584→		for _, s := range stats {
   585→			s.ShortJobs = count
   586→		}
   587→	}
   588→
   589→	cclog.Debugf("Timer AddJobCount %s", time.Since(start))
   590→	return stats, nil
   591→}
   592→
   593→// AddHistograms augments statistics with distribution histograms for job properties.
   594→//
   595→// Generates histogram data for visualization of job duration, node count, core count,
   596→// and accelerator count distributions. Duration histogram uses intelligent binning based
   597→// on the requested resolution.
   598→//
   599→// Parameters:
   600→//   - ctx: Context for security checks
   601→//   - filter: Filters to apply to jobs included in histograms
   602→//   - stat: Statistics struct to augment (modified in-place)
   603→//   - durationBins: Bin size - "1m", "10m", "1h", "6h", "12h", or "24h" (default)
   604→//
   605→// Populates these fields in stat:
   606→//   - HistDuration: Job duration distribution (zero-padded bins)
   607→//   - HistNumNodes: Node count distribution
   608→//   - HistNumCores: Core (hwthread) count distribution
   609→//   - HistNumAccs: Accelerator count distribution
   610→//
   611→// Duration bins are pre-initialized with zeros to ensure consistent ranges for visualization.
   612→// Bin size determines both the width and maximum duration displayed (e.g., "1h" = 48 bins × 1h = 48h max).
   613→func (r *JobRepository) AddHistograms(
   614→	ctx context.Context,
   615→	filter []*model.JobFilter,
   616→	stat *model.JobsStatistics,
   617→	durationBins *string,
   618→) (*model.JobsStatistics, error) {
   619→	start := time.Now()
   620→
   621→	var targetBinCount int
   622→	var targetBinSize int
   623→	switch *durationBins {
   624→	case "1m": // 1 Minute Bins + Max 60 Bins -> Max 60 Minutes
   625→		targetBinCount = 60
   626→		targetBinSize = 60
   627→	case "10m": // 10 Minute Bins + Max 72 Bins -> Max 12 Hours
   628→		targetBinCount = 72
   629→		targetBinSize = 600
   630→	case "1h": // 1 Hour Bins + Max 48 Bins -> Max 48 Hours
   631→		targetBinCount = 48
   632→		targetBinSize = 3600
   633→	case "6h": // 6 Hour Bins + Max 12 Bins -> Max 3 Days
   634→		targetBinCount = 12
   635→		targetBinSize = 21600
   636→	case "12h": // 12 hour Bins + Max 14 Bins -> Max 7 Days
   637→		targetBinCount = 14
   638→		targetBinSize = 43200
   639→	default: // 24h
   640→		targetBinCount = 24
   641→		targetBinSize = 3600
   642→	}
   643→
   644→	// Run all 4 histogram queries in parallel — each writes a distinct struct field.
   645→	g, gctx := errgroup.WithContext(ctx)
   646→
   647→	value := fmt.Sprintf(`CAST(ROUND(((CASE WHEN job.job_state = 'running' THEN %d - job.start_time ELSE job.duration END) / %d) + 1) as int) as value`, time.Now().Unix(), targetBinSize)
   648→
   649→	g.Go(func() error {
   650→		var err error
   651→		stat.HistDuration, err = r.jobsDurationStatisticsHistogram(gctx, value, filter, targetBinSize, &targetBinCount)
   652→		if err != nil {
   653→			cclog.Warn("Error while loading job statistics histogram: job duration")
   654→		}
   655→		return err
   656→	})
   657→	g.Go(func() error {
   658→		var err error
   659→		stat.HistNumNodes, err = r.jobsStatisticsHistogram(gctx, "job.num_nodes as value", filter)
   660→		if err != nil {
   661→			cclog.Warn("Error while loading job statistics histogram: num nodes")
   662→		}
   663→		return err
   664→	})
   665→	g.Go(func() error {
   666→		var err error
   667→		stat.HistNumCores, err = r.jobsStatisticsHistogram(gctx, "job.num_hwthreads as value", filter)
   668→		if err != nil {
   669→			cclog.Warn("Error while loading job statistics histogram: num hwthreads")
   670→		}
   671→		return err
   672→	})
   673→	g.Go(func() error {
   674→		var err error
   675→		stat.HistNumAccs, err = r.jobsStatisticsHistogram(gctx, "job.num_acc as value", filter)
   676→		if err != nil {
   677→			cclog.Warn("Error while loading job statistics histogram: num acc")
   678→		}
   679→		return err
   680→	})
   681→
   682→	if err := g.Wait(); err != nil {
   683→		return nil, err
   684→	}
   685→
   686→	cclog.Debugf("Timer AddHistograms %s", time.Since(start))
   687→	return stat, nil
   688→}
   689→
   690→// AddMetricHistograms augments statistics with distribution histograms for job metrics.
   691→//
   692→// Generates histogram data for metrics like CPU load, memory usage, etc. Handles running
   693→// and completed jobs differently: running jobs load data from metric backend, completed jobs
   694→// use footprint data from database.
   695→//
   696→// Parameters:
   697→//   - ctx: Context for security checks
   698→//   - filter: Filters to apply (MUST contain State filter for running jobs)
   699→//   - metrics: List of metric names to histogram (e.g., ["cpu_load", "mem_used"])
   700→//   - stat: Statistics struct to augment (modified in-place)
   701→//   - targetBinCount: Number of histogram bins (default: 10)
   702→//
   703→// Populates HistMetrics field in stat with MetricHistoPoints for each metric.
   704→//
   705→// Binning algorithm:
   706→//   - Values normalized to metric's peak value from cluster configuration
   707→//   - Bins evenly distributed from 0 to peak
   708→//   - Pre-initialized with zeros for consistent visualization
   709→//
   710→// Limitations:
   711→//   - Running jobs: Limited to 5000 jobs for performance
   712→//   - Requires valid cluster configuration with metric peak values
   713→//   - Uses footprint statistic (avg/max/min) configured per metric
   714→func (r *JobRepository) AddMetricHistograms(
   715→	ctx context.Context,
   716→	filter []*model.JobFilter,
   717→	metrics []string,
   718→	stat *model.JobsStatistics,
   719→	targetBinCount *int,
   720→) (*model.JobsStatistics, error) {
   721→	start := time.Now()
   722→
   723→	// Running Jobs Only: First query jobdata from sqlite, then query data and make bins
   724→	for _, f := range filter {
   725→		if f.State != nil {
   726→			if len(f.State) == 1 && f.State[0] == "running" {
   727→				stat.HistMetrics = r.runningJobsMetricStatisticsHistogram(ctx, metrics, filter, targetBinCount)
   728→				cclog.Debugf("Timer AddMetricHistograms %s", time.Since(start))
   729→				return stat, nil
   730→			}
   731→		}
   732→	}
   733→
   734→	// All other cases: Query and make bins in sqlite directly
   735→	for _, m := range metrics {
   736→		metricHisto, err := r.jobsMetricStatisticsHistogram(ctx, m, filter, targetBinCount)
   737→		if err != nil {
   738→			cclog.Warnf("Error while loading job metric statistics histogram: %s", m)
   739→			continue
   740→		}
   741→		stat.HistMetrics = append(stat.HistMetrics, metricHisto)
   742→	}
   743→
   744→	cclog.Debugf("Timer AddMetricHistograms %s", time.Since(start))
   745→	return stat, nil
   746→}
   747→
   748→// jobsStatisticsHistogram generates a simple histogram by grouping on a column value.
   749→//
   750→// Used for histograms where the column value directly represents the bin (e.g., node count, core count).
   751→// Unlike duration/metric histograms, this doesn't pre-initialize bins with zeros.
   752→//
   753→// Parameters:
   754→//   - value: SQL expression that produces the histogram value, aliased as "value"
   755→//   - filters: Job filters to apply
   756→//
   757→// Returns histogram points with Value (from column) and Count (number of jobs).
   758→func (r *JobRepository) jobsStatisticsHistogram(
   759→	ctx context.Context,
   760→	value string,
   761→	filters []*model.JobFilter,
   762→) ([]*model.HistoPoint, error) {
   763→	start := time.Now()
   764→	query, qerr := SecurityCheck(ctx,
   765→		sq.Select(value, "COUNT(*) AS count").From("job"))
   766→
   767→	if qerr != nil {
   768→		return nil, qerr
   769→	}
   770→
   771→	for _, f := range filters {
   772→		query = BuildWhereClause(f, query)
   773→	}
   774→
   775→	rows, err := query.GroupBy("value").RunWith(r.DB).QueryContext(ctx)
   776→	if err != nil {
   777→		cclog.Error("Error while running query")
   778→		return nil, err
   779→	}
   780→	defer rows.Close()
   781→
   782→	points := make([]*model.HistoPoint, 0)
   783→	// is it possible to introduce zero values here? requires info about bincount
   784→	for rows.Next() {
   785→		point := model.HistoPoint{}
   786→		if err := rows.Scan(&point.Value, &point.Count); err != nil {
   787→			cclog.Warn("Error while scanning rows")
   788→			return nil, err
   789→		}
   790→
   791→		points = append(points, &point)
   792→	}
   793→
   794→	if err := rows.Err(); err != nil {
   795→		return nil, err
   796→	}
   797→
   798→	cclog.Debugf("Timer jobsStatisticsHistogram %s", time.Since(start))
   799→	return points, nil
   800→}
   801→
   802→// jobsDurationStatisticsHistogram generates a duration histogram with pre-initialized bins.
   803→//
   804→// Bins are zero-padded to provide consistent ranges for visualization, unlike simple
   805→// histograms which only return bins with data. The value parameter should compute
   806→// the bin number from job duration.
   807→//
   808→// Parameters:
   809→//   - value: SQL expression computing bin number from duration, aliased as "value"
   810→//   - filters: Job filters to apply
   811→//   - binSizeSeconds: Width of each bin in seconds
   812→//   - targetBinCount: Number of bins to pre-initialize
   813→//
   814→// Returns histogram points with Value (bin_number × binSizeSeconds) and Count.
   815→// All bins from 1 to targetBinCount are returned, with Count=0 for empty bins.
   816→//
   817→// Algorithm:
   818→//  1. Pre-initialize targetBinCount bins with zero counts
   819→//  2. Query database for actual counts per bin
   820→//  3. Match query results to pre-initialized bins by value
   821→//  4. Bins without matches remain at zero
   822→func (r *JobRepository) jobsDurationStatisticsHistogram(
   823→	ctx context.Context,
   824→	value string,
   825→	filters []*model.JobFilter,
   826→	binSizeSeconds int,
   827→	targetBinCount *int,
   828→) ([]*model.HistoPoint, error) {
   829→	start := time.Now()
   830→	query, qerr := SecurityCheck(ctx,
   831→		sq.Select(value, "COUNT(*) AS count").From("job"))
   832→
   833→	if qerr != nil {
   834→		return nil, qerr
   835→	}
   836→
   837→	// Each bin represents a duration range: bin N = [N*binSizeSeconds, (N+1)*binSizeSeconds)
   838→	// Example: binSizeSeconds=3600 (1 hour), bin 1 = 0-1h, bin 2 = 1-2h, etc.
   839→	points := make([]*model.HistoPoint, 0, *targetBinCount)
   840→	for i := 1; i <= *targetBinCount; i++ {
   841→		point := model.HistoPoint{Value: i * binSizeSeconds, Count: 0}
   842→		points = append(points, &point)
   843→	}
   844→
   845→	// Build a map from bin value (seconds) to slice index for O(1) lookup.
   846→	binMap := make(map[int]int, len(points))
   847→	for i, p := range points {
   848→		binMap[p.Value] = i
   849→	}
   850→
   851→	for _, f := range filters {
   852→		query = BuildWhereClause(f, query)
   853→	}
   854→
   855→	rows, err := query.GroupBy("value").RunWith(r.DB).QueryContext(ctx)
   856→	if err != nil {
   857→		cclog.Error("Error while running query")
   858→		return nil, err
   859→	}
   860→	defer rows.Close()
   861→
   862→	// Match query results to pre-initialized bins using map lookup.
   863→	// point.Value from query is the bin number; multiply by binSizeSeconds to get bin key.
   864→	for rows.Next() {
   865→		point := model.HistoPoint{}
   866→		if err := rows.Scan(&point.Value, &point.Count); err != nil {
   867→			cclog.Warn("Error while scanning rows")
   868→			return nil, err
   869→		}
   870→
   871→		if idx, ok := binMap[point.Value*binSizeSeconds]; ok {
   872→			points[idx].Count = point.Count
   873→		}
   874→	}
   875→
   876→	if err := rows.Err(); err != nil {
   877→		return nil, err
   878→	}
   879→
   880→	cclog.Debugf("Timer jobsStatisticsHistogram %s", time.Since(start))
   881→	return points, nil
   882→}
   883→
   884→// jobsMetricStatisticsHistogram generates a metric histogram using footprint data from completed jobs.
   885→//
   886→// Values are normalized to the metric's peak value and distributed into bins. The algorithm
   887→// is based on SQL histogram generation techniques, extracting metric values from JSON footprint
   888→// and computing bin assignments in SQL.
   889→//
   890→// Parameters:
   891→//   - metric: Metric name (e.g., "cpu_load", "mem_used")
   892→//   - filters: Job filters to apply
   893→//   - bins: Number of bins to generate
   894→//
   895→// Returns MetricHistoPoints with metric name, unit, footprint stat type, and binned data.
   896→//
   897→// Algorithm:
   898→//  1. Determine peak value from cluster configuration (filtered cluster or max across all)
   899→//  2. Generate SQL that extracts footprint value, normalizes to [0,1], multiplies by bin count
   900→//  3. Pre-initialize bins with min/max ranges based on peak value
   901→//  4. Query database for counts per bin
   902→//  5. Match results to pre-initialized bins
   903→//
   904→// Special handling: Values exactly equal to peak are forced into the last bin by multiplying
   905→// peak by 0.999999999 to avoid creating an extra bin.
   906→func (r *JobRepository) jobsMetricStatisticsHistogram(
   907→	ctx context.Context,
   908→	metric string,
   909→	filters []*model.JobFilter,
   910→	bins *int,
   911→) (*model.MetricHistoPoints, error) {
   912→	// Peak value defines the upper bound for binning: values are distributed across
   913→	// bins from 0 to peak. First try to get peak from filtered cluster, otherwise
   914→	// scan all clusters to find the maximum peak value.
   915→	var metricConfig *schema.MetricConfig
   916→	var peak float64
   917→	var unit string
   918→	var footprintStat string
   919→
   920→	// Try to get metric config from filtered cluster
   921→	for _, f := range filters {
   922→		if f.Cluster != nil {
   923→			metricConfig = archive.GetMetricConfig(*f.Cluster.Eq, metric)
   924→			peak = metricConfig.Peak
   925→			unit = metricConfig.Unit.Prefix + metricConfig.Unit.Base
   926→			footprintStat = metricConfig.Footprint
   927→			cclog.Debugf("Cluster %s filter found with peak %f for %s", *f.Cluster.Eq, peak, metric)
   928→		}
   929→	}
   930→
   931→	// If no cluster filter or peak not found, find largest peak across all clusters
   932→	// This ensures histogram can accommodate all possible values
   933→	if peak == 0.0 {
   934→		for _, c := range archive.Clusters {
   935→			for _, m := range c.MetricConfig {
   936→				if m.Name == metric {
   937→					if m.Peak > peak {
   938→						peak = m.Peak
   939→					}
   940→					if unit == "" {
   941→						unit = m.Unit.Prefix + m.Unit.Base
   942→					}
   943→					if footprintStat == "" {
   944→						footprintStat = m.Footprint
   945→					}
   946→				}
   947→			}
   948→		}
   949→	}
   950→
   951→	// Construct SQL histogram bins using normalized values.
   952→	// Algorithm based on: https://jereze.com/code/sql-histogram/ (modified)
   953→	start := time.Now()
   954→
   955→	// Bin calculation formula:
   956→	//   bin_number = CAST( (value / peak) * num_bins AS INTEGER ) + 1
   957→	// Special case: value == peak would create bin N+1, so we test for equality
   958→	// and multiply peak by 0.999999999 to force it into bin N.
   959→	binQuery := fmt.Sprintf(`CAST(
   960→		((case when json_extract(footprint, "$.%s") = %f then %f*0.999999999 else json_extract(footprint, "$.%s") end) / %f)
   961→		* %v as INTEGER )`,
   962→		(metric + "_" + footprintStat), peak, peak, (metric + "_" + footprintStat), peak, *bins)
   963→
   964→	mainQuery := sq.Select(
   965→		fmt.Sprintf(`%s + 1 as bin`, binQuery),
   966→		`count(*) as count`,
   967→	).From("job").Where(
   968→		"JSON_VALID(footprint)",
   969→	).Where(fmt.Sprintf(`json_extract(footprint, "$.%s") is not null and json_extract(footprint, "$.%s") <= %f`, (metric + "_" + footprintStat), (metric + "_" + footprintStat), peak))
   970→
   971→	mainQuery, qerr := SecurityCheck(ctx, mainQuery)
   972→	if qerr != nil {
   973→		return nil, qerr
   974→	}
   975→
   976→	for _, f := range filters {
   977→		mainQuery = BuildWhereClause(f, mainQuery)
   978→	}
   979→
   980→	mainQuery = mainQuery.GroupBy("bin").OrderBy("bin")
   981→
   982→	rows, err := mainQuery.RunWith(r.DB).QueryContext(ctx)
   983→	if err != nil {
   984→		cclog.Errorf("Error while running mainQuery: %s", err)
   985→		return nil, err
   986→	}
   987→	defer rows.Close()
   988→
   989→	// Pre-initialize bins with calculated min/max ranges.
   990→	// Example: peak=1000, bins=10 -> bin 1=[0,100), bin 2=[100,200), ..., bin 10=[900,1000]
   991→	points := make([]*model.MetricHistoPoint, 0, *bins)
   992→	binStep := int(peak) / *bins
   993→	for i := 1; i <= *bins; i++ {
   994→		binMin := (binStep * (i - 1))
   995→		binMax := (binStep * i)
   996→		idx := i
   997→		epoint := model.MetricHistoPoint{Bin: &idx, Count: 0, Min: &binMin, Max: &binMax}
   998→		points = append(points, &epoint)
   999→	}
  1000→
  1001→	// Build a map from bin number to slice index for O(1) lookup.
  1002→	binMap := make(map[int]int, len(points))
  1003→	for i, p := range points {
  1004→		if p.Bin != nil {
  1005→			binMap[*p.Bin] = i
  1006→		}
  1007→	}
  1008→
  1009→	// Match query results to pre-initialized bins using map lookup.
  1010→	for rows.Next() {
  1011→		rpoint := model.MetricHistoPoint{}
  1012→		if err := rows.Scan(&rpoint.Bin, &rpoint.Count); err != nil {
  1013→			cclog.Warnf("Error while scanning rows for %s", metric)
  1014→			return nil, err
  1015→		}
  1016→
  1017→		if rpoint.Bin != nil {
  1018→			if idx, ok := binMap[*rpoint.Bin]; ok {
  1019→				points[idx].Count = rpoint.Count
  1020→			}
  1021→		}
  1022→	}
  1023→
  1024→	if err := rows.Err(); err != nil {
  1025→		return nil, err
  1026→	}
  1027→
  1028→	result := model.MetricHistoPoints{Metric: metric, Unit: unit, Stat: &footprintStat, Data: points}
  1029→
  1030→	cclog.Debugf("Timer jobsStatisticsHistogram %s", time.Since(start))
  1031→	return &result, nil
  1032→}
  1033→
  1034→// runningJobsMetricStatisticsHistogram generates metric histograms for running jobs using live data.
  1035→//
  1036→// Unlike completed jobs which use footprint data from the database, running jobs require
  1037→// fetching current metric averages from the metric backend (via metricdispatch).
  1038→//
  1039→// Parameters:
  1040→//   - metrics: List of metric names
  1041→//   - filters: Job filters (should filter to running jobs only)
  1042→//   - bins: Number of histogram bins
  1043→//
  1044→// Returns slice of MetricHistoPoints, one per metric.
  1045→//
  1046→// Limitations:
  1047→//   - Maximum 5000 jobs (returns nil if more jobs match)
  1048→//   - Requires metric backend availability
  1049→//   - Bins based on metric peak values from cluster configuration
  1050→//
  1051→// Algorithm:
  1052→//  1. Query first 5001 jobs to check count limit
  1053→//  2. Load metric averages for all jobs via metricdispatch
  1054→//  3. For each metric, create bins based on peak value
  1055→//  4. Iterate averages and count jobs per bin
  1056→func (r *JobRepository) runningJobsMetricStatisticsHistogram(
  1057→	ctx context.Context,
  1058→	metrics []string,
  1059→	filters []*model.JobFilter,
  1060→	bins *int,
  1061→) []*model.MetricHistoPoints {
  1062→	// Get Jobs
  1063→	jobs, err := r.QueryJobs(ctx, filters, &model.PageRequest{Page: 1, ItemsPerPage: 5000 + 1}, nil)
  1064→	if err != nil {
  1065→		cclog.Errorf("Error while querying jobs for footprint: %s", err)
  1066→		return nil
  1067→	}
  1068→	if len(jobs) > 5000 {
  1069→		cclog.Errorf("too many jobs matched (max: %d)", 5000)
  1070→		return nil
  1071→	}
  1072→
  1073→	// Get AVGs from metric repo
  1074→	avgs := make([][]schema.Float, len(metrics))
  1075→	for i := range avgs {
  1076→		avgs[i] = make([]schema.Float, 0, len(jobs))
  1077→	}
  1078→
  1079→	for _, job := range jobs {
  1080→		if job.MonitoringStatus == schema.MonitoringStatusDisabled || job.MonitoringStatus == schema.MonitoringStatusArchivingFailed {
  1081→			continue
  1082→		}
  1083→
  1084→		if err := metricdispatch.LoadAverages(job, metrics, avgs, ctx); err != nil {
  1085→			cclog.Errorf("Error while loading averages for histogram: %s", err)
  1086→			return nil
  1087→		}
  1088→	}
  1089→
  1090→	// Iterate metrics to fill endresult
  1091→	data := make([]*model.MetricHistoPoints, 0)
  1092→	for idx, metric := range metrics {
  1093→		// Get specific Peak or largest Peak
  1094→		var metricConfig *schema.MetricConfig
  1095→		var peak float64
  1096→		var unit string
  1097→
  1098→		for _, f := range filters {
  1099→			if f.Cluster != nil {
  1100→				metricConfig = archive.GetMetricConfig(*f.Cluster.Eq, metric)
  1101→				peak = metricConfig.Peak
  1102→				unit = metricConfig.Unit.Prefix + metricConfig.Unit.Base
  1103→			}
  1104→		}
  1105→
  1106→		if peak == 0.0 {
  1107→			for _, c := range archive.Clusters {
  1108→				for _, m := range c.MetricConfig {
  1109→					if m.Name == metric {
  1110→						if m.Peak > peak {
  1111→							peak = m.Peak
  1112→						}
  1113→						if unit == "" {
  1114→							unit = m.Unit.Prefix + m.Unit.Base
  1115→						}
  1116→					}
  1117→				}
  1118→			}
  1119→		}
  1120→
  1121→		// Make and fill bins
  1122→		peakBin := int(peak) / *bins
  1123→
  1124→		points := make([]*model.MetricHistoPoint, 0)
  1125→		for b := 0; b < *bins; b++ {
  1126→			count := 0
  1127→			bindex := b + 1
  1128→			bmin := peakBin * b
  1129→			bmax := peakBin * (b + 1)
  1130→
  1131→			// Iterate AVG values for indexed metric and count for bins
  1132→			for _, val := range avgs[idx] {
  1133→				if int(val) >= bmin && int(val) < bmax {
  1134→					count += 1
  1135→				}
  1136→			}
  1137→
  1138→			// Append Bin to Metric Result Array
  1139→			point := model.MetricHistoPoint{Bin: &bindex, Count: count, Min: &bmin, Max: &bmax}
  1140→			points = append(points, &point)
  1141→		}
  1142→
  1143→		// Append Metric Result Array to final results array
  1144→		result := model.MetricHistoPoints{Metric: metric, Unit: unit, Data: points}
  1145→		data = append(data, &result)
  1146→	}
  1147→
  1148→	return data
  1149→}
  1150→


     1→// Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
     2→// All rights reserved. This file is part of cc-backend.
     3→// Use of this source code is governed by a MIT-style
     4→// license that can be found in the LICENSE file.
     5→
     6→// Package repository provides job query functionality with filtering, pagination,
     7→// and security controls. This file contains the main query builders and security
     8→// checks for job retrieval operations.
     9→package repository
    10→
    11→import (
    12→	"context"
    13→	"errors"
    14→	"fmt"
    15→	"regexp"
    16→	"strings"
    17→	"time"
    18→
    19→	"github.com/ClusterCockpit/cc-backend/internal/config"
    20→	"github.com/ClusterCockpit/cc-backend/internal/graph/model"
    21→	cclog "github.com/ClusterCockpit/cc-lib/v2/ccLogger"
    22→	"github.com/ClusterCockpit/cc-lib/v2/schema"
    23→	sq "github.com/Masterminds/squirrel"
    24→)
    25→
    26→const (
    27→	// Default initial capacity for job result slices
    28→	defaultJobsCapacity = 50
    29→)
    30→
    31→// QueryJobs retrieves jobs from the database with optional filtering, pagination,
    32→// and sorting. Security controls are automatically applied based on the user context.
    33→//
    34→// Parameters:
    35→//   - ctx: Context containing user authentication information
    36→//   - filters: Optional job filters (cluster, state, user, time ranges, etc.)
    37→//   - page: Optional pagination parameters (page number and items per page)
    38→//   - order: Optional sorting specification (column or footprint field)
    39→//
    40→// Returns a slice of jobs matching the criteria, or an error if the query fails.
    41→// The function enforces role-based access control through SecurityCheck.
    42→func (r *JobRepository) QueryJobs(
    43→	ctx context.Context,
    44→	filters []*model.JobFilter,
    45→	page *model.PageRequest,
    46→	order *model.OrderByInput,
    47→) ([]*schema.Job, error) {
    48→	query, qerr := SecurityCheck(ctx, sq.Select(jobColumns...).From("job"))
    49→	if qerr != nil {
    50→		return nil, qerr
    51→	}
    52→
    53→	if order != nil {
    54→		field := toSnakeCase(order.Field)
    55→		if order.Type == "col" {
    56→			switch order.Order {
    57→			case model.SortDirectionEnumAsc:
    58→				query = query.OrderBy(fmt.Sprintf("job.%s ASC", field))
    59→			case model.SortDirectionEnumDesc:
    60→				query = query.OrderBy(fmt.Sprintf("job.%s DESC", field))
    61→			default:
    62→				return nil, errors.New("invalid sorting order for column")
    63→			}
    64→		} else {
    65→			// Order by footprint JSON field values
    66→			query = query.Where("JSON_VALID(footprint)")
    67→			switch order.Order {
    68→			case model.SortDirectionEnumAsc:
    69→				query = query.OrderBy(fmt.Sprintf("JSON_EXTRACT(footprint, \"$.%s\") ASC", field))
    70→			case model.SortDirectionEnumDesc:
    71→				query = query.OrderBy(fmt.Sprintf("JSON_EXTRACT(footprint, \"$.%s\") DESC", field))
    72→			default:
    73→				return nil, errors.New("invalid sorting order for footprint")
    74→			}
    75→		}
    76→	}
    77→
    78→	if page != nil && page.ItemsPerPage != -1 {
    79→		limit := uint64(page.ItemsPerPage)
    80→		query = query.Offset((uint64(page.Page) - 1) * limit).Limit(limit)
    81→	}
    82→
    83→	for _, f := range filters {
    84→		query = BuildWhereClause(f, query)
    85→	}
    86→
    87→	rows, err := query.RunWith(r.stmtCache).QueryContext(ctx)
    88→	if err != nil {
    89→		queryString, queryVars, _ := query.ToSql()
    90→		return nil, fmt.Errorf("query failed [%s] %v: %w", queryString, queryVars, err)
    91→	}
    92→	defer rows.Close()
    93→
    94→	jobs := make([]*schema.Job, 0, defaultJobsCapacity)
    95→	for rows.Next() {
    96→		job, err := scanJob(rows)
    97→		if err != nil {
    98→			cclog.Warnf("Error scanning job row: %v", err)
    99→			return nil, fmt.Errorf("failed to scan job row: %w", err)
   100→		}
   101→		jobs = append(jobs, job)
   102→	}
   103→
   104→	if err := rows.Err(); err != nil {
   105→		return nil, fmt.Errorf("error iterating job rows: %w", err)
   106→	}
   107→
   108→	return jobs, nil
   109→}
   110→
   111→// CountJobs returns the total number of jobs matching the given filters.
   112→// Security controls are automatically applied based on the user context.
   113→// Uses DISTINCT count to handle tag filters correctly (jobs may appear multiple
   114→// times when joined with the tag table).
   115→func (r *JobRepository) CountJobs(
   116→	ctx context.Context,
   117→	filters []*model.JobFilter,
   118→) (int, error) {
   119→	query, qerr := SecurityCheck(ctx, sq.Select("count(DISTINCT job.id)").From("job"))
   120→	if qerr != nil {
   121→		return 0, qerr
   122→	}
   123→
   124→	for _, f := range filters {
   125→		query = BuildWhereClause(f, query)
   126→	}
   127→
   128→	var count int
   129→	if err := query.RunWith(r.DB).QueryRowContext(ctx).Scan(&count); err != nil {
   130→		return 0, fmt.Errorf("failed to count jobs: %w", err)
   131→	}
   132→
   133→	return count, nil
   134→}
   135→
   136→// SecurityCheckWithUser applies role-based access control filters to a job query
   137→// based on the provided user's roles and permissions.
   138→//
   139→// Access rules by role:
   140→//   - API role (exclusive): Full access to all jobs
   141→//   - Admin/Support roles: Full access to all jobs
   142→//   - Manager role: Access to jobs in managed projects plus own jobs
   143→//   - User role: Access only to own jobs
   144→//
   145→// Returns an error if the user is nil or has no recognized roles.
   146→func SecurityCheckWithUser(user *schema.User, query sq.SelectBuilder) (sq.SelectBuilder, error) {
   147→	if user == nil {
   148→		var qnil sq.SelectBuilder
   149→		return qnil, fmt.Errorf("user context is nil")
   150→	}
   151→
   152→	switch {
   153→	case len(user.Roles) == 1 && user.HasRole(schema.RoleAPI):
   154→		return query, nil
   155→	case user.HasAnyRole([]schema.Role{schema.RoleAdmin, schema.RoleSupport}):
   156→		return query, nil
   157→	case user.HasRole(schema.RoleManager):
   158→		if len(user.Projects) != 0 {
   159→			return query.Where(sq.Or{sq.Eq{"job.project": user.Projects}, sq.Eq{"job.hpc_user": user.Username}}), nil
   160→		}
   161→		cclog.Debugf("Manager '%s' has no assigned projects, restricting to personal jobs", user.Username)
   162→		return query.Where("job.hpc_user = ?", user.Username), nil
   163→	case user.HasRole(schema.RoleUser):
   164→		return query.Where("job.hpc_user = ?", user.Username), nil
   165→	default:
   166→		var qnil sq.SelectBuilder
   167→		return qnil, fmt.Errorf("user has no or unknown roles")
   168→	}
   169→}
   170→
   171→// SecurityCheck extracts the user from the context and applies role-based access
   172→// control filters to the query. This is a convenience wrapper around SecurityCheckWithUser.
   173→func SecurityCheck(ctx context.Context, query sq.SelectBuilder) (sq.SelectBuilder, error) {
   174→	user := GetUserFromContext(ctx)
   175→	return SecurityCheckWithUser(user, query)
   176→}
   177→
   178→// BuildWhereClause constructs SQL WHERE conditions from a JobFilter and applies
   179→// them to the query. Supports filtering by job properties (cluster, state, user),
   180→// time ranges, resource usage, tags, and JSON field searches in meta_data,
   181→// footprint, and resources columns.
   182→func BuildWhereClause(filter *model.JobFilter, query sq.SelectBuilder) sq.SelectBuilder {
   183→	// Primary Key
   184→	if filter.DbID != nil {
   185→		dbIDs := make([]string, len(filter.DbID))
   186→		copy(dbIDs, filter.DbID)
   187→		query = query.Where(sq.Eq{"job.id": dbIDs})
   188→	}
   189→	// Explicit indices
   190→	if filter.Cluster != nil {
   191→		query = buildStringCondition("job.cluster", filter.Cluster, query)
   192→	}
   193→	if filter.SubCluster != nil {
   194→		query = buildStringCondition("job.subcluster", filter.SubCluster, query)
   195→	}
   196→	if filter.Partition != nil {
   197→		query = buildStringCondition("job.cluster_partition", filter.Partition, query)
   198→	}
   199→	if filter.State != nil {
   200→		if len(filter.State) == 1 {
   201→			// Inline literal value so SQLite can match partial indexes (WHERE job_state = 'running').
   202→			// Safe: values come from validated GraphQL enum (model.JobState).
   203→			singleStat := string(filter.State[0])
   204→			query = query.Where(fmt.Sprintf("job.job_state = '%s'", singleStat))
   205→		} else {
   206→			states := make([]string, len(filter.State))
   207→			for i, val := range filter.State {
   208→				states[i] = string(val)
   209→			}
   210→			query = query.Where(sq.Eq{"job.job_state": states})
   211→		}
   212→	}
   213→	if filter.Shared != nil {
   214→		query = query.Where("job.shared = ?", *filter.Shared)
   215→	}
   216→	if filter.Project != nil {
   217→		query = buildStringCondition("job.project", filter.Project, query)
   218→	}
   219→	if filter.User != nil {
   220→		query = buildStringCondition("job.hpc_user", filter.User, query)
   221→	}
   222→	if filter.NumNodes != nil {
   223→		query = buildIntCondition("job.num_nodes", filter.NumNodes, query)
   224→	}
   225→	if filter.NumAccelerators != nil {
   226→		query = buildIntCondition("job.num_acc", filter.NumAccelerators, query)
   227→	}
   228→	if filter.NumHWThreads != nil {
   229→		query = buildIntCondition("job.num_hwthreads", filter.NumHWThreads, query)
   230→	}
   231→	if filter.ArrayJobID != nil {
   232→		query = query.Where("job.array_job_id = ?", *filter.ArrayJobID)
   233→	}
   234→	if filter.StartTime != nil {
   235→		query = buildTimeCondition("job.start_time", filter.StartTime, query)
   236→	}
   237→	if filter.Duration != nil {
   238→		query = buildIntCondition("job.duration", filter.Duration, query)
   239→	}
   240→	if filter.Energy != nil {
   241→		query = buildFloatCondition("job.energy", filter.Energy, query)
   242→	}
   243→	// Indices on Tag Table
   244→	if filter.Tags != nil {
   245→		// This is an OR-Logic query: Returns all distinct jobs with at least one of the requested tags; TODO: AND-Logic query?
   246→		query = query.Join("jobtag ON jobtag.job_id = job.id").Where(sq.Eq{"jobtag.tag_id": filter.Tags}).Distinct()
   247→	}
   248→	// No explicit Indices
   249→	if filter.JobID != nil {
   250→		query = buildStringCondition("job.job_id", filter.JobID, query)
   251→	}
   252→	// Queries Within JSONs
   253→	if filter.MetricStats != nil {
   254→		for _, ms := range filter.MetricStats {
   255→			query = buildFloatJSONCondition(ms.MetricName, ms.Range, query)
   256→		}
   257→	}
   258→	if filter.Node != nil {
   259→		query = buildResourceJSONCondition("hostname", filter.Node, query)
   260→	}
   261→	if filter.JobName != nil {
   262→		query = buildMetaJSONCondition("jobName", filter.JobName, query)
   263→	}
   264→	if filter.Schedule != nil {
   265→		interactiveJobname := "interactive"
   266→		switch *filter.Schedule {
   267→		case "interactive":
   268→			iFilter := model.StringInput{Eq: &interactiveJobname}
   269→			query = buildMetaJSONCondition("jobName", &iFilter, query)
   270→		case "batch":
   271→			sFilter := model.StringInput{Neq: &interactiveJobname}
   272→			query = buildMetaJSONCondition("jobName", &sFilter, query)
   273→		}
   274→	}
   275→
   276→	// Configurable Filter to exclude recently started jobs, see config.go: ShortRunningJobsDuration
   277→	if filter.MinRunningFor != nil {
   278→		now := time.Now().Unix()
   279→		// Only jobs whose start timestamp is more than MinRunningFor seconds in the past
   280→		// If a job completed within the configured timeframe, it will still show up after the start_time matches the condition!
   281→		query = query.Where(sq.Lt{"job.start_time": (now - int64(*filter.MinRunningFor))})
   282→	}
   283→	return query
   284→}
   285→
   286→// buildIntCondition creates clauses for integer range filters, using BETWEEN only if required.
   287→func buildIntCondition(field string, cond *config.IntRange, query sq.SelectBuilder) sq.SelectBuilder {
   288→	if cond.From != 1 && cond.To != 0 {
   289→		return query.Where(field+" BETWEEN ? AND ?", cond.From, cond.To)
   290→	} else if cond.From != 1 && cond.To == 0 {
   291→		return query.Where(field+" >= ?", cond.From)
   292→	} else if cond.From == 1 && cond.To != 0 {
   293→		return query.Where(field+" <= ?", cond.To)
   294→	} else {
   295→		return query
   296→	}
   297→}
   298→
   299→// buildFloatCondition creates a clauses for float range filters, using BETWEEN only if required.
   300→func buildFloatCondition(field string, cond *model.FloatRange, query sq.SelectBuilder) sq.SelectBuilder {
   301→	if cond.From != 1.0 && cond.To != 0.0 {
   302→		return query.Where(field+" BETWEEN ? AND ?", cond.From, cond.To)
   303→	} else if cond.From != 1.0 && cond.To == 0.0 {
   304→		return query.Where(field+" >= ?", cond.From)
   305→	} else if cond.From == 1.0 && cond.To != 0.0 {
   306→		return query.Where(field+" <= ?", cond.To)
   307→	} else {
   308→		return query
   309→	}
   310→}
   311→
   312→// buildTimeCondition creates time range filters supporting absolute timestamps,
   313→// relative time ranges (last6h, last24h, last7d, last30d), or open-ended ranges.
   314→// Reminder: BETWEEN Queries are slower and dont use indices as frequently: Only use if both conditions required
   315→func buildTimeCondition(field string, cond *config.TimeRange, query sq.SelectBuilder) sq.SelectBuilder {
   316→	if cond.From != nil && cond.To != nil {
   317→		return query.Where(field+" BETWEEN ? AND ?", cond.From.Unix(), cond.To.Unix())
   318→	} else if cond.From != nil {
   319→		return query.Where(field+" >= ?", cond.From.Unix())
   320→	} else if cond.To != nil {
   321→		return query.Where(field+" <= ?", cond.To.Unix())
   322→	} else if cond.Range != "" {
   323→		now := time.Now().Unix()
   324→		var then int64
   325→		switch cond.Range {
   326→		case "last6h":
   327→			then = now - (60 * 60 * 6)
   328→		case "last24h":
   329→			then = now - (60 * 60 * 24)
   330→		case "last7d":
   331→			then = now - (60 * 60 * 24 * 7)
   332→		case "last30d":
   333→			then = now - (60 * 60 * 24 * 30)
   334→		default:
   335→			cclog.Debugf("No known named timeRange: startTime.range = %s", cond.Range)
   336→			return query
   337→		}
   338→		return query.Where(field+" >= ?", then)
   339→	} else {
   340→		return query
   341→	}
   342→}
   343→
   344→// buildFloatJSONCondition creates a filter on a numeric field within the footprint JSON column, using BETWEEN only if required.
   345→func buildFloatJSONCondition(jsonField string, cond *model.FloatRange, query sq.SelectBuilder) sq.SelectBuilder {
   346→	query = query.Where("JSON_VALID(footprint)")
   347→	if cond.From != 1.0 && cond.To != 0.0 {
   348→		return query.Where("JSON_EXTRACT(footprint, \"$."+jsonField+"\") BETWEEN ? AND ?", cond.From, cond.To)
   349→	} else if cond.From != 1.0 && cond.To == 0.0 {
   350→		return query.Where("JSON_EXTRACT(footprint, \"$."+jsonField+"\") >= ?", cond.From)
   351→	} else if cond.From == 1.0 && cond.To != 0.0 {
   352→		return query.Where("JSON_EXTRACT(footprint, \"$."+jsonField+"\") <= ?", cond.To)
   353→	} else {
   354→		return query
   355→	}
   356→}
   357→
   358→// buildStringCondition creates filters for string fields supporting equality,
   359→// inequality, prefix, suffix, substring, and IN list matching.
   360→func buildStringCondition(field string, cond *model.StringInput, query sq.SelectBuilder) sq.SelectBuilder {
   361→	if cond.Eq != nil {
   362→		return query.Where(field+" = ?", *cond.Eq)
   363→	}
   364→	if cond.Neq != nil {
   365→		return query.Where(field+" != ?", *cond.Neq)
   366→	}
   367→	if cond.StartsWith != nil {
   368→		return query.Where(field+" LIKE ?", fmt.Sprint(*cond.StartsWith, "%"))
   369→	}
   370→	if cond.EndsWith != nil {
   371→		return query.Where(field+" LIKE ?", fmt.Sprint("%", *cond.EndsWith))
   372→	}
   373→	if cond.Contains != nil {
   374→		return query.Where(field+" LIKE ?", fmt.Sprint("%", *cond.Contains, "%"))
   375→	}
   376→	if cond.In != nil {
   377→		queryElements := make([]string, len(cond.In))
   378→		copy(queryElements, cond.In)
   379→		return query.Where(sq.Or{sq.Eq{field: queryElements}})
   380→	}
   381→	return query
   382→}
   383→
   384→// buildMetaJSONCondition creates filters on fields within the meta_data JSON column.
   385→func buildMetaJSONCondition(jsonField string, cond *model.StringInput, query sq.SelectBuilder) sq.SelectBuilder {
   386→	query = query.Where("JSON_VALID(meta_data)")
   387→	if cond.Eq != nil {
   388→		return query.Where("JSON_EXTRACT(meta_data, \"$."+jsonField+"\") = ?", *cond.Eq)
   389→	}
   390→	if cond.Neq != nil {
   391→		return query.Where("JSON_EXTRACT(meta_data, \"$."+jsonField+"\") != ?", *cond.Neq)
   392→	}
   393→	if cond.StartsWith != nil {
   394→		return query.Where("JSON_EXTRACT(meta_data, \"$."+jsonField+"\") LIKE ?", fmt.Sprint(*cond.StartsWith, "%"))
   395→	}
   396→	if cond.EndsWith != nil {
   397→		return query.Where("JSON_EXTRACT(meta_data, \"$."+jsonField+"\") LIKE ?", fmt.Sprint("%", *cond.EndsWith))
   398→	}
   399→	if cond.Contains != nil {
   400→		return query.Where("JSON_EXTRACT(meta_data, \"$."+jsonField+"\") LIKE ?", fmt.Sprint("%", *cond.Contains, "%"))
   401→	}
   402→	return query
   403→}
   404→
   405→// buildResourceJSONCondition creates filters on fields within the resources JSON array column.
   406→// Uses json_each to search within array elements.
   407→func buildResourceJSONCondition(jsonField string, cond *model.StringInput, query sq.SelectBuilder) sq.SelectBuilder {
   408→	query = query.Where("JSON_VALID(resources)")
   409→	if cond.Eq != nil {
   410→		return query.Where("EXISTS (SELECT 1 FROM json_each(job.resources) WHERE json_extract(value, \"$."+jsonField+"\") = ?)", *cond.Eq)
   411→	}
   412→	if cond.Neq != nil { // Currently Unused
   413→		return query.Where("EXISTS (SELECT 1 FROM json_each(job.resources) WHERE json_extract(value, \"$."+jsonField+"\") != ?)", *cond.Neq)
   414→	}
   415→	if cond.StartsWith != nil { // Currently Unused
   416→		return query.Where("EXISTS (SELECT 1 FROM json_each(job.resources) WHERE json_extract(value, \"$."+jsonField+"\")) LIKE ?)", fmt.Sprint(*cond.StartsWith, "%"))
   417→	}
   418→	if cond.EndsWith != nil { // Currently Unused
   419→		return query.Where("EXISTS (SELECT 1 FROM json_each(job.resources) WHERE json_extract(value, \"$."+jsonField+"\") LIKE ?)", fmt.Sprint("%", *cond.EndsWith))
   420→	}
   421→	if cond.Contains != nil {
   422→		return query.Where("EXISTS (SELECT 1 FROM json_each(job.resources) WHERE json_extract(value, \"$."+jsonField+"\") LIKE ?)", fmt.Sprint("%", *cond.Contains, "%"))
   423→	}
   424→	return query
   425→}
   426→
   427→var (
   428→	matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)")
   429→	matchAllCap   = regexp.MustCompile("([a-z0-9])([A-Z])")
   430→)
   431→
   432→// toSnakeCase converts camelCase strings to snake_case for SQL column names.
   433→// Includes security checks to prevent SQL injection attempts.
   434→// Panics if potentially dangerous characters are detected.
   435→func toSnakeCase(str string) string {
   436→	for _, c := range str {
   437→		if c == '\'' || c == '\\' || c == '"' || c == ';' || c == '-' || c == ' ' {
   438→			cclog.Panicf("toSnakeCase: potentially dangerous character detected in input: %q", str)
   439→		}
   440→	}
   441→
   442→	snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}")
   443→	snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}")
   444→	return strings.ToLower(snake)
   445→}
   446→


<persisted-output>
Output too large (49.5KB). Full output saved to: /Users/jan/.claude/projects/-Users-jan-prg-ClusterCockpit-cc-backend/889ef150-c6ca-4f35-81e5-c19dc95fe399/tool-results/REDACTED.txt

Preview (first 2KB):
     1→// Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
     2→// All rights reserved. This file is part of cc-backend.
     3→// Use of this source code is governed by a MIT-style
     4→// license that can be found in the LICENSE file.
     5→
     6→// Package repository provides the data access layer for cc-backend using the repository pattern.
     7→//
     8→// The repository pattern abstracts database operations and provides a clean interface for
     9→// data access. Each major entity (Job, User, Node, Tag) has its own repository with CRUD
    10→// operations and specialized queries.
    11→//
    12→// # Database Connection
    13→//
    14→// Initialize the database connection before using any repository:
    15→//
    16→//	repository.Connect("sqlite3", "./var/job.db")
    17→//
    18→// # Configuration
    19→//
    20→// Optional: Configure repository settings before initialization:
    21→//
    22→//	repository.SetConfig(&repository.RepositoryConfig{
    23→//	    CacheSize: 2 * 1024 * 1024,     // 2MB cache
    24→//	    MaxOpenConnections: 8,           // Connection pool size
    25→//	    MinRunningJobDuration: 300,      // Filter threshold
    26→//	})
    27→//
    28→// If not configured, sensible defaults are used automatically.
    29→//
    30→// # Repositories
    31→//
    32→//   - JobRepository: Job lifecycle management and querying
    33→//   - UserRepository: User management and authentication
    34→//   - NodeRepository: Cluster node state tracking
    35→//   - Tags: Job tagging and categorization
    36→//
    37→// # Caching
    38→//
    39→// Repositories use LRU caching to improve performance. Cache keys are constructed
    40→// as "type:id" (e.g., "metadata:123"). Cache is automatically invalidated on
    41→// mutations to maintain consistency.
    42→//
    43→// # Transaction Support
    44→//
    45→// For batch operations, use transactions:
    46→//
    47→//	t, err := jobRepo.TransactionInit()
...
</persisted-output>


     1→// Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
     2→// All rights reserved. This file is part of cc-backend.
     3→// Use of this source code is governed by a MIT-style
     4→// license that can be found in the LICENSE file.
     5→
     6→package repository
     7→
     8→import (
     9→	"encoding/json"
    10→	"fmt"
    11→
    12→	cclog "github.com/ClusterCockpit/cc-lib/v2/ccLogger"
    13→	"github.com/ClusterCockpit/cc-lib/v2/schema"
    14→	sq "github.com/Masterminds/squirrel"
    15→)
    16→
    17→const NamedJobCacheInsert string = `INSERT INTO job_cache (
    18→	job_id, hpc_user, project, cluster, subcluster, cluster_partition, array_job_id, num_nodes, num_hwthreads, num_acc,
    19→	shared, monitoring_status, smt, job_state, start_time, duration, walltime, footprint, energy, energy_footprint, resources, meta_data
    20→) VALUES (
    21→	:job_id, :hpc_user, :project, :cluster, :subcluster, :cluster_partition, :array_job_id, :num_nodes, :num_hwthreads, :num_acc,
    22→  :shared, :monitoring_status, :smt, :job_state, :start_time, :duration, :walltime, :footprint,  :energy, :energy_footprint, :resources, :meta_data
    23→);`
    24→
    25→const NamedJobInsert string = `INSERT INTO job (
    26→	job_id, hpc_user, project, cluster, subcluster, cluster_partition, array_job_id, num_nodes, num_hwthreads, num_acc,
    27→	shared, monitoring_status, smt, job_state, start_time, duration, walltime, footprint, energy, energy_footprint, resources, meta_data
    28→) VALUES (
    29→	:job_id, :hpc_user, :project, :cluster, :subcluster, :cluster_partition, :array_job_id, :num_nodes, :num_hwthreads, :num_acc,
    30→  :shared, :monitoring_status, :smt, :job_state, :start_time, :duration, :walltime, :footprint,  :energy, :energy_footprint, :resources, :meta_data
    31→);`
    32→
    33→// InsertJobDirect inserts a job directly into the job table (not job_cache).
    34→// Use this when the returned ID will be used for operations on the job table
    35→// (e.g., adding tags), or for imported jobs that are already completed.
    36→func (r *JobRepository) InsertJobDirect(job *schema.Job) (int64, error) {
    37→	r.Mutex.Lock()
    38→	defer r.Mutex.Unlock()
    39→
    40→	res, err := r.DB.NamedExec(NamedJobInsert, job)
    41→	if err != nil {
    42→		cclog.Warn("Error while NamedJobInsert (direct)")
    43→		return 0, err
    44→	}
    45→	id, err := res.LastInsertId()
    46→	if err != nil {
    47→		cclog.Warn("Error while getting last insert ID (direct)")
    48→		return 0, err
    49→	}
    50→
    51→	return id, nil
    52→}
    53→
    54→func (r *JobRepository) InsertJob(job *schema.Job) (int64, error) {
    55→	r.Mutex.Lock()
    56→	defer r.Mutex.Unlock()
    57→
    58→	res, err := r.DB.NamedExec(NamedJobCacheInsert, job)
    59→	if err != nil {
    60→		cclog.Warn("Error while NamedJobInsert")
    61→		return 0, err
    62→	}
    63→	id, err := res.LastInsertId()
    64→	if err != nil {
    65→		cclog.Warn("Error while getting last insert ID")
    66→		return 0, err
    67→	}
    68→
    69→	return id, nil
    70→}
    71→
    72→func (r *JobRepository) SyncJobs() ([]*schema.Job, error) {
    73→	r.Mutex.Lock()
    74→	defer r.Mutex.Unlock()
    75→
    76→	query := sq.Select(jobCacheColumns...).From("job_cache")
    77→
    78→	rows, err := query.RunWith(r.stmtCache).Query()
    79→	if err != nil {
    80→		cclog.Errorf("Error while running query %v", err)
    81→		return nil, err
    82→	}
    83→	defer rows.Close()
    84→
    85→	jobs := make([]*schema.Job, 0, 50)
    86→	for rows.Next() {
    87→		job, err := scanJob(rows)
    88→		if err != nil {
    89→			cclog.Warn("Error while scanning rows")
    90→			return nil, err
    91→		}
    92→		jobs = append(jobs, job)
    93→	}
    94→
    95→	// Use INSERT OR IGNORE to skip jobs already transferred by the stop path
    96→	_, err = r.DB.Exec(
    97→		"INSERT OR IGNORE INTO job (job_id, cluster, subcluster, start_time, hpc_user, project, cluster_partition, array_job_id, num_nodes, num_hwthreads, num_acc, shared, monitoring_status, smt, job_state, duration, walltime, footprint, energy, energy_footprint, resources, meta_data) SELECT job_id, cluster, subcluster, start_time, hpc_user, project, cluster_partition, array_job_id, num_nodes, num_hwthreads, num_acc, shared, monitoring_status, smt, job_state, duration, walltime, footprint, energy, energy_footprint, resources, meta_data FROM job_cache")
    98→	if err != nil {
    99→		cclog.Errorf("Error while Job sync: %v", err)
   100→		return nil, err
   101→	}
   102→
   103→	_, err = r.DB.Exec("DELETE FROM job_cache")
   104→	if err != nil {
   105→		cclog.Errorf("Error while Job cache clean: %v", err)
   106→		return nil, err
   107→	}
   108→
   109→	// Resolve correct job.id from the job table. The IDs read from job_cache
   110→	// are from a different auto-increment sequence and must not be used to
   111→	// query the job table.
   112→	for _, job := range jobs {
   113→		var newID int64
   114→		if err := sq.Select("job.id").From("job").
   115→			Where("job.job_id = ? AND job.cluster = ? AND job.start_time = ?",
   116→				job.JobID, job.Cluster, job.StartTime).
   117→			RunWith(r.stmtCache).QueryRow().Scan(&newID); err != nil {
   118→			cclog.Warnf("SyncJobs: could not resolve job table id for job %d on %s: %v",
   119→				job.JobID, job.Cluster, err)
   120→			continue
   121→		}
   122→		job.ID = &newID
   123→	}
   124→
   125→	return jobs, nil
   126→}
   127→
   128→// TransferCachedJobToMain moves a job from job_cache to the job table.
   129→// Caller must hold r.Mutex. Returns the new job table ID.
   130→func (r *JobRepository) TransferCachedJobToMain(cacheID int64) (int64, error) {
   131→	res, err := r.DB.Exec(
   132→		"INSERT INTO job (job_id, cluster, subcluster, start_time, hpc_user, project, cluster_partition, array_job_id, num_nodes, num_hwthreads, num_acc, shared, monitoring_status, smt, job_state, duration, walltime, footprint, energy, energy_footprint, resources, meta_data) SELECT job_id, cluster, subcluster, start_time, hpc_user, project, cluster_partition, array_job_id, num_nodes, num_hwthreads, num_acc, shared, monitoring_status, smt, job_state, duration, walltime, footprint, energy, energy_footprint, resources, meta_data FROM job_cache WHERE id = ?",
   133→		cacheID)
   134→	if err != nil {
   135→		return 0, fmt.Errorf("transferring cached job %d to main table failed: %w", cacheID, err)
   136→	}
   137→
   138→	newID, err := res.LastInsertId()
   139→	if err != nil {
   140→		return 0, fmt.Errorf("getting new job ID after transfer failed: %w", err)
   141→	}
   142→
   143→	_, err = r.DB.Exec("DELETE FROM job_cache WHERE id = ?", cacheID)
   144→	if err != nil {
   145→		return 0, fmt.Errorf("deleting cached job %d after transfer failed: %w", cacheID, err)
   146→	}
   147→
   148→	return newID, nil
   149→}
   150→
   151→// Start inserts a new job in the table, returning the unique job ID.
   152→// Statistics are not transfered!
   153→func (r *JobRepository) Start(job *schema.Job) (id int64, err error) {
   154→	job.RawFootprint, err = json.Marshal(job.Footprint)
   155→	if err != nil {
   156→		return -1, fmt.Errorf("REPOSITORY/JOB > encoding footprint field failed: %w", err)
   157→	}
   158→
   159→	job.RawResources, err = json.Marshal(job.Resources)
   160→	if err != nil {
   161→		return -1, fmt.Errorf("REPOSITORY/JOB > encoding resources field failed: %w", err)
   162→	}
   163→
   164→	job.RawMetaData, err = json.Marshal(job.MetaData)
   165→	if err != nil {
   166→		return -1, fmt.Errorf("REPOSITORY/JOB > encoding metaData field failed: %w", err)
   167→	}
   168→
   169→	return r.InsertJob(job)
   170→}
   171→
   172→// StartDirect inserts a new job directly into the job table (not job_cache).
   173→// Use this when the returned ID will immediately be used for job table
   174→// operations such as adding tags.
   175→func (r *JobRepository) StartDirect(job *schema.Job) (id int64, err error) {
   176→	job.RawFootprint, err = json.Marshal(job.Footprint)
   177→	if err != nil {
   178→		return -1, fmt.Errorf("REPOSITORY/JOB > encoding footprint field failed: %w", err)
   179→	}
   180→
   181→	job.RawResources, err = json.Marshal(job.Resources)
   182→	if err != nil {
   183→		return -1, fmt.Errorf("REPOSITORY/JOB > encoding resources field failed: %w", err)
   184→	}
   185→
   186→	job.RawMetaData, err = json.Marshal(job.MetaData)
   187→	if err != nil {
   188→		return -1, fmt.Errorf("REPOSITORY/JOB > encoding metaData field failed: %w", err)
   189→	}
   190→
   191→	return r.InsertJobDirect(job)
   192→}
   193→
   194→// Stop updates the job with the database id jobId using the provided arguments.
   195→func (r *JobRepository) Stop(
   196→	jobID int64,
   197→	duration int32,
   198→	state schema.JobState,
   199→	monitoringStatus int32,
   200→) (err error) {
   201→	// Invalidate cache entries as job state is changing
   202→	r.cache.Del(fmt.Sprintf("metadata:%d", jobID))
   203→	r.cache.Del(fmt.Sprintf("energyFootprint:%d", jobID))
   204→
   205→	stmt := sq.Update("job").
   206→		Set("job_state", state).
   207→		Set("duration", duration).
   208→		Set("monitoring_status", monitoringStatus).
   209→		Where("job.id = ?", jobID)
   210→
   211→	_, err = stmt.RunWith(r.stmtCache).Exec()
   212→	return err
   213→}
   214→


     1→// Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
     2→// All rights reserved. This file is part of cc-backend.
     3→// Use of this source code is governed by a MIT-style
     4→// license that can be found in the LICENSE file.
     5→
     6→package repository
     7→
     8→import (
     9→	"context"
    10→	"database/sql"
    11→	"fmt"
    12→	"time"
    13→
    14→	"github.com/ClusterCockpit/cc-backend/internal/graph/model"
    15→	cclog "github.com/ClusterCockpit/cc-lib/v2/ccLogger"
    16→	"github.com/ClusterCockpit/cc-lib/v2/schema"
    17→	sq "github.com/Masterminds/squirrel"
    18→)
    19→
    20→// Find executes a SQL query to find a specific batch job.
    21→// The job is queried using the batch job id, the cluster name,
    22→// and the start time of the job in UNIX epoch time seconds.
    23→// It returns a pointer to a schema.Job data structure and an error variable.
    24→// To check if no job was found test err == sql.ErrNoRows
    25→func (r *JobRepository) Find(
    26→	jobID *int64,
    27→	cluster *string,
    28→	startTime *int64,
    29→) (*schema.Job, error) {
    30→	if jobID == nil {
    31→		return nil, fmt.Errorf("jobID cannot be nil")
    32→	}
    33→
    34→	start := time.Now()
    35→	q := sq.Select(jobColumns...).From("job").
    36→		Where("job.job_id = ?", *jobID)
    37→
    38→	if cluster != nil {
    39→		q = q.Where("job.cluster = ?", *cluster)
    40→	}
    41→	if startTime != nil {
    42→		q = q.Where("job.start_time = ?", *startTime)
    43→	}
    44→
    45→	q = q.OrderBy("job.id DESC").Limit(1) // always use newest matching job by db id if more than one match
    46→
    47→	cclog.Debugf("Timer Find %s", time.Since(start))
    48→	return scanJob(q.RunWith(r.stmtCache).QueryRow())
    49→}
    50→
    51→// FindCached executes a SQL query to find a specific batch job from the job_cache table.
    52→// The job is queried using the batch job id, and optionally filtered by cluster name
    53→// and start time (UNIX epoch time seconds). This method uses cached job data which
    54→// may be stale but provides faster access than Find().
    55→// It returns a pointer to a schema.Job data structure and an error variable.
    56→// To check if no job was found test err == sql.ErrNoRows
    57→func (r *JobRepository) FindCached(
    58→	jobID *int64,
    59→	cluster *string,
    60→	startTime *int64,
    61→) (*schema.Job, error) {
    62→	if jobID == nil {
    63→		return nil, fmt.Errorf("jobID cannot be nil")
    64→	}
    65→
    66→	q := sq.Select(jobCacheColumns...).From("job_cache").
    67→		Where("job_cache.job_id = ?", *jobID)
    68→
    69→	if cluster != nil {
    70→		q = q.Where("job_cache.cluster = ?", *cluster)
    71→	}
    72→	if startTime != nil {
    73→		q = q.Where("job_cache.start_time = ?", *startTime)
    74→	}
    75→
    76→	q = q.OrderBy("job_cache.id DESC").Limit(1) // always use newest matching job by db id if more than one match
    77→
    78→	return scanJob(q.RunWith(r.stmtCache).QueryRow())
    79→}
    80→
    81→// FindAll executes a SQL query to find all batch jobs matching the given criteria.
    82→// Jobs are queried using the batch job id, and optionally filtered by cluster name
    83→// and start time (UNIX epoch time seconds).
    84→// It returns a slice of pointers to schema.Job data structures and an error variable.
    85→// An empty slice is returned if no matching jobs are found.
    86→func (r *JobRepository) FindAll(
    87→	jobID *int64,
    88→	cluster *string,
    89→	startTime *int64,
    90→) ([]*schema.Job, error) {
    91→	if jobID == nil {
    92→		return nil, fmt.Errorf("jobID cannot be nil")
    93→	}
    94→
    95→	start := time.Now()
    96→	q := sq.Select(jobColumns...).From("job").
    97→		Where("job.job_id = ?", *jobID)
    98→
    99→	if cluster != nil {
   100→		q = q.Where("job.cluster = ?", *cluster)
   101→	}
   102→	if startTime != nil {
   103→		q = q.Where("job.start_time = ?", *startTime)
   104→	}
   105→
   106→	rows, err := q.RunWith(r.stmtCache).Query()
   107→	if err != nil {
   108→		cclog.Errorf("Error while running FindAll query for jobID=%d: %v", *jobID, err)
   109→		return nil, fmt.Errorf("failed to execute FindAll query: %w", err)
   110→	}
   111→	defer rows.Close()
   112→
   113→	jobs := make([]*schema.Job, 0, 10)
   114→	for rows.Next() {
   115→		job, err := scanJob(rows)
   116→		if err != nil {
   117→			cclog.Warnf("Error while scanning rows in FindAll: %v", err)
   118→			return nil, fmt.Errorf("failed to scan job row: %w", err)
   119→		}
   120→		jobs = append(jobs, job)
   121→	}
   122→	cclog.Debugf("Timer FindAll %s", time.Since(start))
   123→	return jobs, nil
   124→}
   125→
   126→// GetJobList returns job IDs for non-running jobs.
   127→// This is useful to process large job counts and intended to be used
   128→// together with FindById to process jobs one by one.
   129→// Use limit and offset for pagination. Use limit=0 to get all results (not recommended for large datasets).
   130→func (r *JobRepository) GetJobList(limit int, offset int) ([]int64, error) {
   131→	query := sq.Select("id").From("job").
   132→		Where("job.job_state != 'running'")
   133→
   134→	// Add pagination if limit is specified
   135→	if limit > 0 {
   136→		query = query.Limit(uint64(limit)).Offset(uint64(offset))
   137→	}
   138→
   139→	rows, err := query.RunWith(r.stmtCache).Query()
   140→	if err != nil {
   141→		cclog.Errorf("Error while running GetJobList query (limit=%d, offset=%d): %v", limit, offset, err)
   142→		return nil, fmt.Errorf("failed to execute GetJobList query: %w", err)
   143→	}
   144→	defer rows.Close()
   145→
   146→	jl := make([]int64, 0, 1000)
   147→	for rows.Next() {
   148→		var id int64
   149→		err := rows.Scan(&id)
   150→		if err != nil {
   151→			cclog.Warnf("Error while scanning rows in GetJobList: %v", err)
   152→			return nil, fmt.Errorf("failed to scan job ID: %w", err)
   153→		}
   154→		jl = append(jl, id)
   155→	}
   156→
   157→	cclog.Debugf("JobRepository.GetJobList(): Return job count %d", len(jl))
   158→	return jl, nil
   159→}
   160→
   161→// FindByID executes a SQL query to find a specific batch job.
   162→// The job is queried using the database id.
   163→// It returns a pointer to a schema.Job data structure and an error variable.
   164→// To check if no job was found test err == sql.ErrNoRows
   165→func (r *JobRepository) FindByID(ctx context.Context, jobID int64) (*schema.Job, error) {
   166→	q := sq.Select(jobColumns...).
   167→		From("job").Where("job.id = ?", jobID)
   168→
   169→	q, qerr := SecurityCheck(ctx, q)
   170→	if qerr != nil {
   171→		return nil, qerr
   172→	}
   173→
   174→	return scanJob(q.RunWith(r.stmtCache).QueryRowContext(ctx))
   175→}
   176→
   177→// FindByIDWithUser executes a SQL query to find a specific batch job.
   178→// The job is queried using the database id. The user is passed directly,
   179→// instead as part of the context.
   180→// It returns a pointer to a schema.Job data structure and an error variable.
   181→// To check if no job was found test err == sql.ErrNoRows
   182→func (r *JobRepository) FindByIDWithUser(user *schema.User, jobID int64) (*schema.Job, error) {
   183→	q := sq.Select(jobColumns...).
   184→		From("job").Where("job.id = ?", jobID)
   185→
   186→	q, qerr := SecurityCheckWithUser(user, q)
   187→	if qerr != nil {
   188→		return nil, qerr
   189→	}
   190→
   191→	return scanJob(q.RunWith(r.stmtCache).QueryRow())
   192→}
   193→
   194→// FindByIDDirect executes a SQL query to find a specific batch job.
   195→// The job is queried using the database id.
   196→// It returns a pointer to a schema.Job data structure and an error variable.
   197→// To check if no job was found test err == sql.ErrNoRows
   198→func (r *JobRepository) FindByIDDirect(jobID int64) (*schema.Job, error) {
   199→	q := sq.Select(jobColumns...).
   200→		From("job").Where("job.id = ?", jobID)
   201→	return scanJob(q.RunWith(r.stmtCache).QueryRow())
   202→}
   203→
   204→// FindByJobID executes a SQL query to find a specific batch job.
   205→// The job is queried using the slurm id and the clustername.
   206→// It returns a pointer to a schema.Job data structure and an error variable.
   207→// To check if no job was found test err == sql.ErrNoRows
   208→func (r *JobRepository) FindByJobID(ctx context.Context, jobID int64, startTime int64, cluster string) (*schema.Job, error) {
   209→	q := sq.Select(jobColumns...).
   210→		From("job").
   211→		Where("job.job_id = ?", jobID).
   212→		Where("job.cluster = ?", cluster).
   213→		Where("job.start_time = ?", startTime)
   214→
   215→	q, qerr := SecurityCheck(ctx, q)
   216→	if qerr != nil {
   217→		return nil, qerr
   218→	}
   219→
   220→	return scanJob(q.RunWith(r.stmtCache).QueryRowContext(ctx))
   221→}
   222→
   223→// IsJobOwner checks if the specified user owns the batch job identified by jobID,
   224→// startTime, and cluster. Returns true if the user is the owner, false otherwise.
   225→// This method does not return errors; it returns false for both non-existent jobs
   226→// and jobs owned by other users.
   227→func (r *JobRepository) IsJobOwner(jobID int64, startTime int64, user string, cluster string) bool {
   228→	q := sq.Select("id").
   229→		From("job").
   230→		Where("job.job_id = ?", jobID).
   231→		Where("job.hpc_user = ?", user).
   232→		Where("job.cluster = ?", cluster).
   233→		Where("job.start_time = ?", startTime)
   234→
   235→	_, err := scanJob(q.RunWith(r.stmtCache).QueryRow())
   236→	if err != nil && err != sql.ErrNoRows {
   237→		cclog.Warnf("IsJobOwner: unexpected error for jobID=%d, user=%s, cluster=%s: %v", jobID, user, cluster, err)
   238→	}
   239→	return err != sql.ErrNoRows
   240→}
   241→
   242→func (r *JobRepository) FindConcurrentJobs(
   243→	ctx context.Context,
   244→	job *schema.Job,
   245→) (*model.JobLinkResultList, error) {
   246→	if job == nil {
   247→		return nil, nil
   248→	}
   249→
   250→	query, qerr := SecurityCheck(ctx, sq.Select("job.id", "job.job_id", "job.start_time").From("job"))
   251→	if qerr != nil {
   252→		return nil, qerr
   253→	}
   254→
   255→	query = query.Where("cluster = ?", job.Cluster)
   256→
   257→	if len(job.Resources) == 0 {
   258→		return nil, fmt.Errorf("job has no resources defined")
   259→	}
   260→
   261→	var startTime int64
   262→	var stopTime int64
   263→
   264→	startTime = job.StartTime
   265→	hostname := job.Resources[0].Hostname
   266→
   267→	if job.State == schema.JobStateRunning {
   268→		stopTime = time.Now().Unix()
   269→	} else {
   270→		stopTime = startTime + int64(job.Duration)
   271→	}
   272→
   273→	// Time buffer constant for finding overlapping jobs
   274→	// overlapBufferEnd: 200s buffer at job end to account for scheduling/cleanup overlap
   275→	const overlapBufferEnd = 200
   276→
   277→	stopTimeTail := stopTime - overlapBufferEnd
   278→	startTimeFront := startTime + overlapBufferEnd
   279→
   280→	queryRunning := query.Where("job.job_state = ?", "running").
   281→		Where("job.start_time <= ?", stopTimeTail)
   282→	// Get At Least One Exact Hostname Match from JSON Resources Array in Database
   283→	queryRunning = queryRunning.Where("EXISTS (SELECT 1 FROM json_each(job.resources) WHERE json_extract(value, '$.hostname') = ?)", hostname)
   284→
   285→	query = query.Where("job.job_state != ?", "running").
   286→		Where("job.start_time < ?", stopTimeTail).
   287→		Where("(job.start_time + job.duration) > ?", startTimeFront)
   288→	// Get At Least One Exact Hostname Match from JSON Resources Array in Database
   289→	query = query.Where("EXISTS (SELECT 1 FROM json_each(job.resources) WHERE json_extract(value, '$.hostname') = ?)", hostname)
   290→
   291→	rows, err := query.RunWith(r.stmtCache).Query()
   292→	if err != nil {
   293→		cclog.Errorf("Error while running concurrent jobs query: %v", err)
   294→		return nil, fmt.Errorf("failed to execute concurrent jobs query: %w", err)
   295→	}
   296→	defer rows.Close()
   297→
   298→	items := make([]*model.JobLink, 0, 10)
   299→	queryString := fmt.Sprintf("cluster=%s", job.Cluster)
   300→
   301→	for rows.Next() {
   302→		var id, jobID, startTime sql.NullInt64
   303→
   304→		if err = rows.Scan(&id, &jobID, &startTime); err != nil {
   305→			cclog.Warnf("Error while scanning concurrent job rows: %v", err)
   306→			return nil, fmt.Errorf("failed to scan concurrent job row: %w", err)
   307→		}
   308→
   309→		if id.Valid {
   310→			queryString += fmt.Sprintf("&jobId=%d", int(jobID.Int64))
   311→			items = append(items,
   312→				&model.JobLink{
   313→					ID:    fmt.Sprint(id.Int64),
   314→					JobID: int(jobID.Int64),
   315→				})
   316→		}
   317→	}
   318→
   319→	rows, err = queryRunning.RunWith(r.stmtCache).Query()
   320→	if err != nil {
   321→		cclog.Errorf("Error while running concurrent running jobs query: %v", err)
   322→		return nil, fmt.Errorf("failed to execute concurrent running jobs query: %w", err)
   323→	}
   324→	defer rows.Close()
   325→
   326→	for rows.Next() {
   327→		var id, jobID, startTime sql.NullInt64
   328→
   329→		if err := rows.Scan(&id, &jobID, &startTime); err != nil {
   330→			cclog.Warnf("Error while scanning running concurrent job rows: %v", err)
   331→			return nil, fmt.Errorf("failed to scan running concurrent job row: %w", err)
   332→		}
   333→
   334→		if id.Valid {
   335→			queryString += fmt.Sprintf("&jobId=%d", int(jobID.Int64))
   336→			items = append(items,
   337→				&model.JobLink{
   338→					ID:    fmt.Sprint(id.Int64),
   339→					JobID: int(jobID.Int64),
   340→				})
   341→		}
   342→	}
   343→
   344→	cnt := len(items)
   345→
   346→	return &model.JobLinkResultList{
   347→		ListQuery: &queryString,
   348→		Items:     items,
   349→		Count:     &cnt,
   350→	}, nil
   351→}
   352→


96-//
97-/ The kind parameter enables counting specific job categories:
98://   - "running": Only jobs with job_state = 'running'
99-/   - "short": Only jobs with duration < ShortRunningJobsDuration config value
100-/   - empty: All jobs matching filters
--
114-	switch kind {
115-	case "running":
116:		query = query.Where("job.job_state = ?", "running")
117-	case "short":
118-		query = query.Where("job.duration < ?", config.Keys.ShortRunningJobsDuration)
--
146-		return reqFields == nil || reqFields[field]
147-	}
148:	durationExpr := fmt.Sprintf(`(CASE WHEN job.job_state = 'running' THEN %d - job.start_time ELSE job.duration END)`, now)
149-
150-	// Build column list
--
206-
207-	if need("runningJobs") {
208:		columns = append(columns, `COUNT(CASE WHEN job.job_state = 'running' THEN 1 END) as runningJobs`)
209-	} else {
210-		columns = append(columns, "0 as runningJobs")
--
645-	g, gctx := errgroup.WithContext(ctx)
646-
647:	value := fmt.Sprintf(`CAST(ROUND(((CASE WHEN job.job_state = 'running' THEN %d - job.start_time ELSE job.duration END) / %d) + 1) as int) as value`, time.Now().Unix(), targetBinSize)
648-
649-	g.Go(func() error {


199-	if filter.State != nil {
200-		if len(filter.State) == 1 {
201:			// Inline literal value so SQLite can match partial indexes (WHERE job_state = 'running').
202-			/ Safe: values come from validated GraphQL enum (model.JobState).
203-			singleStat := string(filter.State[0])
204:			query = query.Where(fmt.Sprintf("job.job_state = '%s'", singleStat))
205-		} else {
206-			states := make([]string, len(filter.State))
--
208-				states[i] = string(val)
209-			}
210:			query = query.Where(sq.Eq{"job.job_state": states})
211-		}
212-	}


130-func (r *JobRepository) GetJobList(limit int, offset int) ([]int64, error) {
131-	query := sq.Select("id").From("job").
132:		Where("job.job_state != 'running'")
133-
134-	// Add pagination if limit is specified
--
278-	startTimeFront := startTime + overlapBufferEnd
279-
280:	queryRunning := query.Where("job.job_state = ?", "running").
281-		Where("job.start_time <= ?", stopTimeTail)
282-	// Get At Least One Exact Hostname Match from JSON Resources Array in Database
283-	queryRunning = queryRunning.Where("EXISTS (SELECT 1 FROM json_each(job.resources) WHERE json_extract(value, '$.hostname') = ?)", hostname)
284-
285:	query = query.Where("job.job_state != ?", "running").
286-		Where("job.start_time < ?", stopTimeTail).
287-		Where("(job.start_time + job.duration) > ?", startTimeFront)


Bash

find /Users/jan/prg/ClusterCockpit/cc-backend/internal -name "*.go" -type f ! -path "*/migrations/*" ! -path "*_test.go" | xargs grep -l "job_state" 2>/dev/null | grep -v migration


grep -n "job_state" /Users/jan/prg/ClusterCockpit/cc-backend/internal/repository/job.go | head -20


131:	"job.smt", "job.job_state", "job.duration", "job.walltime", "job.resources",
142:	"job_cache.job_state", "job_cache.duration", "job_cache.walltime", "job_cache.resources",
661:		Where("job.job_state = 'running'").
705://   - job_state: JobStateFailed
718:		Set("job_state", schema.JobStateFailed).
719:		Where("job.job_state = 'running'").
784:		Where("job.job_state = 'running'").
817:		Where("job_state = 'running'")
1066:	// Note: Query expects index on (job_state, start_time) for optimal performance
1069:		Where(sq.Eq{"job.job_state": "running"})