     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→package repository
     6→
     7→import (
     8→	"encoding/json"
     9→	"maps"
    10→	"sync"
    11→	"time"
    12→
    13→	"github.com/ClusterCockpit/cc-backend/internal/config"
    14→	"github.com/ClusterCockpit/cc-backend/web"
    15→	cclog "github.com/ClusterCockpit/cc-lib/v2/ccLogger"
    16→	"github.com/ClusterCockpit/cc-lib/v2/lrucache"
    17→	"github.com/ClusterCockpit/cc-lib/v2/schema"
    18→	"github.com/jmoiron/sqlx"
    19→)
    20→
    21→var (
    22→	userCfgRepoOnce     sync.Once
    23→	userCfgRepoInstance *UserCfgRepo
    24→)
    25→
    26→type UserCfgRepo struct {
    27→	DB         *sqlx.DB
    28→	Lookup     *sqlx.Stmt
    29→	uiDefaults map[string]any
    30→	cache      *lrucache.Cache
    31→	lock       sync.RWMutex
    32→}
    33→
    34→func GetUserCfgRepo() *UserCfgRepo {
    35→	userCfgRepoOnce.Do(func() {
    36→		db := GetConnection()
    37→
    38→		lookupConfigStmt, err := db.DB.Preparex(`SELECT confkey, value FROM configuration WHERE configuration.username = ?`)
    39→		if err != nil {
    40→			cclog.Fatalf("User Config: Call 'db.DB.Preparex()' failed.\nError: %s\n", err.Error())
    41→		}
    42→
    43→		userCfgRepoInstance = &UserCfgRepo{
    44→			DB:         db.DB,
    45→			Lookup:     lookupConfigStmt,
    46→			uiDefaults: web.UIDefaultsMap,
    47→			cache:      lrucache.New(1024),
    48→		}
    49→	})
    50→
    51→	return userCfgRepoInstance
    52→}
    53→
    54→// Return the personalised UI config for the currently authenticated
    55→// user or return the plain default config.
    56→func (uCfg *UserCfgRepo) GetUIConfig(user *schema.User) (map[string]any, error) {
    57→	if user == nil {
    58→		copy := make(map[string]any, len(uCfg.uiDefaults))
    59→		maps.Copy(copy, uCfg.uiDefaults)
    60→		return copy, nil
    61→	}
    62→
    63→	// Is the cache invalidated in case the options are changed?
    64→	data := uCfg.cache.Get(user.Username, func() (any, time.Duration, int) {
    65→		uiconfig := make(map[string]any, len(uCfg.uiDefaults))
    66→		maps.Copy(uiconfig, uCfg.uiDefaults)
    67→
    68→		rows, err := uCfg.Lookup.Query(user.Username)
    69→		if err != nil {
    70→			cclog.Warnf("Error while looking up user uiconfig for user '%v'", user.Username)
    71→			return err, 0, 0
    72→		}
    73→
    74→		size := 0
    75→		defer rows.Close()
    76→		for rows.Next() {
    77→			var key, rawval string
    78→			if err := rows.Scan(&key, &rawval); err != nil {
    79→				cclog.Warn("Error while scanning user uiconfig values")
    80→				return err, 0, 0
    81→			}
    82→
    83→			var val any
    84→			if err := json.Unmarshal([]byte(rawval), &val); err != nil {
    85→				cclog.Warn("Error while unmarshaling raw user uiconfig json")
    86→				return err, 0, 0
    87→			}
    88→
    89→			size += len(key)
    90→			size += len(rawval)
    91→			uiconfig[key] = val
    92→		}
    93→
    94→		// Add global ShortRunningJobsDuration setting as jobList_hideShortRunningJobs
    95→		uiconfig["jobList_hideShortRunningJobs"] = config.Keys.ShortRunningJobsDuration
    96→
    97→		return uiconfig, 24 * time.Hour, size
    98→	})
    99→	if err, ok := data.(error); ok {
   100→		cclog.Error("Error in returned dataset")
   101→		return nil, err
   102→	}
   103→
   104→	return data.(map[string]any), nil
   105→}
   106→
   107→// If the context does not have a user, update the global ui configuration
   108→// without persisting it!  If there is a (authenticated) user, update only his
   109→// configuration.
   110→func (uCfg *UserCfgRepo) UpdateConfig(
   111→	key, value string,
   112→	user *schema.User,
   113→) error {
   114→	if user == nil {
   115→		return nil
   116→	}
   117→
   118→	if _, err := uCfg.DB.Exec(`REPLACE INTO configuration (username, confkey, value) VALUES (?, ?, ?)`, user.Username, key, value); err != nil {
   119→		cclog.Warnf("Error while replacing user config in DB for user '%v'", user.Username)
   120→		return err
   121→	}
   122→
   123→	uCfg.cache.Del(user.Username)
   124→	return nil
   125→}
   126→


     1→CREATE TABLE IF NOT EXISTS job (
     2→id                INTEGER PRIMARY KEY,
     3→job_id            BIGINT NOT NULL,
     4→cluster           VARCHAR(255) NOT NULL,
     5→subcluster        VARCHAR(255) NOT NULL,
     6→start_time        BIGINT NOT NULL, -- Unix timestamp
     7→user              VARCHAR(255) NOT NULL,
     8→project           VARCHAR(255) NOT NULL,
     9→partition         VARCHAR(255),
    10→array_job_id      BIGINT,
    11→duration          INT NOT NULL,
    12→walltime          INT NOT NULL,
    13→job_state         VARCHAR(255) NOT NULL
    14→CHECK(job_state IN ('running', 'completed', 'failed', 'canceled', 'stopped', 'timeout')),
    15→meta_data         TEXT,          -- JSON
    16→resources         TEXT NOT NULL, -- JSON
    17→num_nodes         INT NOT NULL,
    18→num_hwthreads     INT,
    19→num_acc           INT,
    20→smt               TINYINT NOT NULL DEFAULT 1 CHECK(smt               IN (0, 1   )),
    21→exclusive         TINYINT NOT NULL DEFAULT 1 CHECK(exclusive         IN (0, 1, 2)),
    22→monitoring_status TINYINT NOT NULL DEFAULT 1 CHECK(monitoring_status IN (0, 1, 2, 3)),
    23→mem_used_max        REAL NOT NULL DEFAULT 0.0,
    24→flops_any_avg       REAL NOT NULL DEFAULT 0.0,
    25→mem_bw_avg          REAL NOT NULL DEFAULT 0.0,
    26→load_avg            REAL NOT NULL DEFAULT 0.0,
    27→net_bw_avg          REAL NOT NULL DEFAULT 0.0,
    28→net_data_vol_total  REAL NOT NULL DEFAULT 0.0,
    29→file_bw_avg         REAL NOT NULL DEFAULT 0.0,
    30→file_data_vol_total REAL NOT NULL DEFAULT 0.0);
    31→
    32→CREATE TABLE IF NOT EXISTS tag (
    33→id       INTEGER PRIMARY KEY,
    34→tag_type VARCHAR(255) NOT NULL,
    35→tag_name VARCHAR(255) NOT NULL,
    36→UNIQUE (tag_type, tag_name));
    37→
    38→CREATE TABLE IF NOT EXISTS jobtag (
    39→job_id INTEGER,
    40→tag_id INTEGER,
    41→PRIMARY KEY (job_id, tag_id),
    42→FOREIGN KEY (job_id) REFERENCES job (id) ON DELETE CASCADE,
    43→FOREIGN KEY (tag_id) REFERENCES tag (id) ON DELETE CASCADE);
    44→
    45→CREATE TABLE IF NOT EXISTS user (
    46→username varchar(255) PRIMARY KEY NOT NULL,
    47→password varchar(255) DEFAULT NULL,
    48→ldap     tinyint      NOT NULL DEFAULT 0, /* col called "ldap" for historic reasons, fills the "AuthSource" */
    49→name     varchar(255) DEFAULT NULL,
    50→roles    varchar(255) NOT NULL DEFAULT "[]",
    51→email    varchar(255) DEFAULT NULL);
    52→
    53→CREATE TABLE IF NOT EXISTS configuration (
    54→username varchar(255),
    55→confkey  varchar(255),
    56→value    varchar(255),
    57→PRIMARY KEY (username, confkey),
    58→FOREIGN KEY (username) REFERENCES user (username) ON DELETE CASCADE ON UPDATE NO ACTION);
    59→
    60→
    61→


     1→CREATE INDEX IF NOT EXISTS job_stats        ON job (cluster,subcluster,user);
     2→CREATE INDEX IF NOT EXISTS job_by_user      ON job (user);
     3→CREATE INDEX IF NOT EXISTS job_by_starttime ON job (start_time);
     4→CREATE INDEX IF NOT EXISTS job_by_job_id    ON job (job_id, cluster, start_time);
     5→CREATE INDEX IF NOT EXISTS job_list         ON job (cluster, job_state);
     6→CREATE INDEX IF NOT EXISTS job_list_user    ON job (user, cluster, job_state);
     7→CREATE INDEX IF NOT EXISTS job_list_users   ON job (user, job_state);
     8→CREATE INDEX IF NOT EXISTS job_list_users_start ON job (start_time, user, job_state);
     9→


     1→ALTER TABLE user ADD COLUMN projects varchar(255) NOT NULL DEFAULT "[]";
     2→


     1→CREATE TABLE IF NOT EXISTS job_new (
     2→id                INTEGER PRIMARY KEY,
     3→job_id            BIGINT NOT NULL,
     4→cluster           VARCHAR(255) NOT NULL,
     5→subcluster        VARCHAR(255) NOT NULL,
     6→start_time        BIGINT NOT NULL, -- Unix timestamp
     7→user              VARCHAR(255) NOT NULL,
     8→project           VARCHAR(255) NOT NULL,
     9→partition         VARCHAR(255),
    10→array_job_id      BIGINT,
    11→duration          INT NOT NULL,
    12→walltime          INT NOT NULL,
    13→job_state         VARCHAR(255) NOT NULL
    14→CHECK(job_state IN ('running', 'completed', 'failed', 'cancelled', 'stopped', 'timeout', 'preempted', 'out_of_memory')),
    15→meta_data         TEXT,          -- JSON
    16→resources         TEXT NOT NULL, -- JSON
    17→num_nodes         INT NOT NULL,
    18→num_hwthreads     INT,
    19→num_acc           INT,
    20→smt               TINYINT NOT NULL DEFAULT 1 CHECK(smt               IN (0, 1   )),
    21→exclusive         TINYINT NOT NULL DEFAULT 1 CHECK(exclusive         IN (0, 1, 2)),
    22→monitoring_status TINYINT NOT NULL DEFAULT 1 CHECK(monitoring_status IN (0, 1, 2, 3)),
    23→mem_used_max        REAL NOT NULL DEFAULT 0.0,
    24→flops_any_avg       REAL NOT NULL DEFAULT 0.0,
    25→mem_bw_avg          REAL NOT NULL DEFAULT 0.0,
    26→load_avg            REAL NOT NULL DEFAULT 0.0,
    27→net_bw_avg          REAL NOT NULL DEFAULT 0.0,
    28→net_data_vol_total  REAL NOT NULL DEFAULT 0.0,
    29→file_bw_avg         REAL NOT NULL DEFAULT 0.0,
    30→file_data_vol_total REAL NOT NULL DEFAULT 0.0,
    31→UNIQUE (job_id, cluster, start_time));
    32→
    33→
    34→UPDATE job SET job_state='cancelled' WHERE job_state='canceled';
    35→INSERT INTO job_new SELECT * FROM job;
    36→DROP TABLE job;
    37→ALTER TABLE job_new RENAME TO job;
    38→
    39→CREATE INDEX IF NOT EXISTS job_stats        ON job (cluster,subcluster,user);
    40→CREATE INDEX IF NOT EXISTS job_by_user      ON job (user);
    41→CREATE INDEX IF NOT EXISTS job_by_starttime ON job (start_time);
    42→CREATE INDEX IF NOT EXISTS job_by_job_id    ON job (job_id, cluster, start_time);
    43→CREATE INDEX IF NOT EXISTS job_list         ON job (cluster, job_state);
    44→CREATE INDEX IF NOT EXISTS job_list_user    ON job (user, cluster, job_state);
    45→CREATE INDEX IF NOT EXISTS job_list_users   ON job (user, job_state);
    46→CREATE INDEX IF NOT EXISTS job_list_users_start ON job (start_time, user, job_state);
    47→


     1→ALTER TABLE tag ADD COLUMN insert_ts TEXT DEFAULT NULL /* replace me */;
     2→ALTER TABLE jobtag ADD COLUMN insert_ts TEXT DEFAULT NULL /* replace me */;
     3→UPDATE tag SET insert_ts = CURRENT_TIMESTAMP;
     4→UPDATE jobtag SET insert_ts = CURRENT_TIMESTAMP;
     5→PRAGMA writable_schema = on;
     6→
     7→UPDATE sqlite_master
     8→SET sql = replace(sql, 'DEFAULT NULL /* replace me */',
     9→                       'DEFAULT CURRENT_TIMESTAMP')
    10→WHERE type = 'table'
    11→  AND name = 'tag';
    12→UPDATE sqlite_master
    13→SET sql = replace(sql, 'DEFAULT NULL /* replace me */',
    14→                       'DEFAULT CURRENT_TIMESTAMP')
    15→WHERE type = 'table'
    16→  AND name = 'jobtag';
    17→
    18→PRAGMA writable_schema = off;
    19→


     1→CREATE TABLE IF NOT EXISTS configuration_new (
     2→username varchar(255),
     3→confkey  varchar(255),
     4→value    text,
     5→PRIMARY KEY (username, confkey),
     6→FOREIGN KEY (username) REFERENCES user (username) ON DELETE CASCADE ON UPDATE NO ACTION);
     7→
     8→INSERT INTO configuration_new SELECT * FROM configuration;
     9→DROP TABLE configuration;
    10→ALTER TABLE configuration_new RENAME TO configuration;
    11→


<system-reminder>Warning: the file exists but is shorter than the provided offset (1). The file has 1 lines.</system-reminder>


     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.Warnf("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.Warnf("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→
   215→


     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).QueryRow())
   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).QueryRow())
   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→


     1→DROP INDEX IF EXISTS job_stats;
     2→DROP INDEX IF EXISTS job_by_user;
     3→DROP INDEX IF EXISTS job_by_starttime;
     4→DROP INDEX IF EXISTS job_by_job_id;
     5→DROP INDEX IF EXISTS job_list;
     6→DROP INDEX IF EXISTS job_list_user;
     7→DROP INDEX IF EXISTS job_list_users;
     8→DROP INDEX IF EXISTS job_list_users_start;
     9→
    10→ALTER TABLE job ADD COLUMN energy REAL NOT NULL DEFAULT 0.0;
    11→ALTER TABLE job ADD COLUMN energy_footprint TEXT DEFAULT NULL;
    12→
    13→ALTER TABLE job ADD COLUMN footprint TEXT DEFAULT NULL;
    14→ALTER TABLE tag ADD COLUMN tag_scope TEXT NOT NULL DEFAULT 'global';
    15→
    16→-- Do not use reserved keywords anymore
    17→ALTER TABLE "user" RENAME TO hpc_user;
    18→ALTER TABLE job RENAME COLUMN "user" TO hpc_user;
    19→ALTER TABLE job RENAME COLUMN "partition" TO cluster_partition;
    20→
    21→UPDATE job SET footprint = '{"flops_any_avg": 0.0}';
    22→UPDATE job SET footprint = json_replace(footprint, '$.flops_any_avg', job.flops_any_avg);
    23→UPDATE job SET footprint = json_insert(footprint, '$.mem_bw_avg', job.mem_bw_avg);
    24→UPDATE job SET footprint = json_insert(footprint, '$.mem_used_max', job.mem_used_max);
    25→UPDATE job SET footprint = json_insert(footprint, '$.cpu_load_avg', job.load_avg);
    26→UPDATE job SET footprint = json_insert(footprint, '$.net_bw_avg', job.net_bw_avg) WHERE job.net_bw_avg != 0;
    27→UPDATE job SET footprint = json_insert(footprint, '$.net_data_vol_total', job.net_data_vol_total) WHERE job.net_data_vol_total != 0;
    28→UPDATE job SET footprint = json_insert(footprint, '$.file_bw_avg', job.file_bw_avg) WHERE job.file_bw_avg != 0;
    29→UPDATE job SET footprint = json_insert(footprint, '$.file_data_vol_total', job.file_data_vol_total) WHERE job.file_data_vol_total != 0;
    30→
    31→ALTER TABLE job DROP flops_any_avg;
    32→ALTER TABLE job DROP mem_bw_avg;
    33→ALTER TABLE job DROP mem_used_max;
    34→ALTER TABLE job DROP load_avg;
    35→ALTER TABLE job DROP net_bw_avg;
    36→ALTER TABLE job DROP net_data_vol_total;
    37→ALTER TABLE job DROP file_bw_avg;
    38→ALTER TABLE job DROP file_data_vol_total;
    39→
    40→-- Indices for: Single filters, combined filters, sorting, sorting with filters
    41→-- Cluster Filter
    42→CREATE INDEX IF NOT EXISTS jobs_cluster ON job (cluster);
    43→CREATE INDEX IF NOT EXISTS jobs_cluster_user ON job (cluster, hpc_user);
    44→CREATE INDEX IF NOT EXISTS jobs_cluster_project ON job (cluster, project);
    45→CREATE INDEX IF NOT EXISTS jobs_cluster_subcluster ON job (cluster, subcluster);
    46→-- Cluster Filter Sorting
    47→CREATE INDEX IF NOT EXISTS jobs_cluster_starttime ON job (cluster, start_time);
    48→CREATE INDEX IF NOT EXISTS jobs_cluster_duration ON job (cluster, duration);
    49→CREATE INDEX IF NOT EXISTS jobs_cluster_numnodes ON job (cluster, num_nodes);
    50→CREATE INDEX IF NOT EXISTS jobs_cluster_numhwthreads ON job (cluster, num_hwthreads);
    51→CREATE INDEX IF NOT EXISTS jobs_cluster_numacc ON job (cluster, num_acc);
    52→CREATE INDEX IF NOT EXISTS jobs_cluster_energy ON job (cluster, energy);
    53→
    54→-- Cluster+Partition Filter
    55→CREATE INDEX IF NOT EXISTS jobs_cluster_partition ON job (cluster, cluster_partition);
    56→-- Cluster+Partition Filter Sorting
    57→CREATE INDEX IF NOT EXISTS jobs_cluster_partition_starttime ON job (cluster, cluster_partition, start_time);
    58→CREATE INDEX IF NOT EXISTS jobs_cluster_partition_duration ON job (cluster, cluster_partition, duration);
    59→CREATE INDEX IF NOT EXISTS jobs_cluster_partition_numnodes ON job (cluster, cluster_partition, num_nodes);
    60→CREATE INDEX IF NOT EXISTS jobs_cluster_partition_numhwthreads ON job (cluster, cluster_partition, num_hwthreads);
    61→CREATE INDEX IF NOT EXISTS jobs_cluster_partition_numacc ON job (cluster, cluster_partition, num_acc);
    62→CREATE INDEX IF NOT EXISTS jobs_cluster_partition_energy ON job (cluster, cluster_partition, energy);
    63→
    64→-- Cluster+Partition+Jobstate Filter
    65→CREATE INDEX IF NOT EXISTS jobs_cluster_partition_jobstate ON job (cluster, cluster_partition, job_state);
    66→CREATE INDEX IF NOT EXISTS jobs_cluster_partition_jobstate_user ON job (cluster, cluster_partition, job_state, hpc_user);
    67→CREATE INDEX IF NOT EXISTS jobs_cluster_partition_jobstate_project ON job (cluster, cluster_partition, job_state, project);
    68→-- Cluster+Partition+Jobstate Filter Sorting
    69→CREATE INDEX IF NOT EXISTS jobs_cluster_partition_jobstate_starttime ON job (cluster, cluster_partition, job_state, start_time);
    70→CREATE INDEX IF NOT EXISTS jobs_cluster_partition_jobstate_duration ON job (cluster, cluster_partition, job_state, duration);
    71→CREATE INDEX IF NOT EXISTS jobs_cluster_partition_jobstate_numnodes ON job (cluster, cluster_partition, job_state, num_nodes);
    72→CREATE INDEX IF NOT EXISTS jobs_cluster_partition_jobstate_numhwthreads ON job (cluster, cluster_partition, job_state, num_hwthreads);
    73→CREATE INDEX IF NOT EXISTS jobs_cluster_partition_jobstate_numacc ON job (cluster, cluster_partition, job_state, num_acc);
    74→CREATE INDEX IF NOT EXISTS jobs_cluster_partition_jobstate_energy ON job (cluster, cluster_partition, job_state, energy);
    75→
    76→-- Cluster+JobState Filter
    77→CREATE INDEX IF NOT EXISTS jobs_cluster_jobstate ON job (cluster, job_state);
    78→CREATE INDEX IF NOT EXISTS jobs_cluster_jobstate_user ON job (cluster, job_state, hpc_user);
    79→CREATE INDEX IF NOT EXISTS jobs_cluster_jobstate_project ON job (cluster, job_state, project);
    80→-- Cluster+JobState Filter Sorting
    81→CREATE INDEX IF NOT EXISTS jobs_cluster_jobstate_starttime ON job (cluster, job_state, start_time);
    82→CREATE INDEX IF NOT EXISTS jobs_cluster_jobstate_duration ON job (cluster, job_state, duration);
    83→CREATE INDEX IF NOT EXISTS jobs_cluster_jobstate_numnodes ON job (cluster, job_state, num_nodes);
    84→CREATE INDEX IF NOT EXISTS jobs_cluster_jobstate_numhwthreads ON job (cluster, job_state, num_hwthreads);
    85→CREATE INDEX IF NOT EXISTS jobs_cluster_jobstate_numacc ON job (cluster, job_state, num_acc);
    86→CREATE INDEX IF NOT EXISTS jobs_cluster_jobstate_energy ON job (cluster, job_state, energy);
    87→
    88→-- User Filter
    89→CREATE INDEX IF NOT EXISTS jobs_user ON job (hpc_user);
    90→-- User Filter Sorting
    91→CREATE INDEX IF NOT EXISTS jobs_user_starttime ON job (hpc_user, start_time);
    92→CREATE INDEX IF NOT EXISTS jobs_user_duration ON job (hpc_user, duration);
    93→CREATE INDEX IF NOT EXISTS jobs_user_numnodes ON job (hpc_user, num_nodes);
    94→CREATE INDEX IF NOT EXISTS jobs_user_numhwthreads ON job (hpc_user, num_hwthreads);
    95→CREATE INDEX IF NOT EXISTS jobs_user_numacc ON job (hpc_user, num_acc);
    96→CREATE INDEX IF NOT EXISTS jobs_user_energy ON job (hpc_user, energy);
    97→
    98→-- Project Filter
    99→CREATE INDEX IF NOT EXISTS jobs_project ON job (project);
   100→CREATE INDEX IF NOT EXISTS jobs_project_user ON job (project, hpc_user);
   101→-- Project Filter Sorting
   102→CREATE INDEX IF NOT EXISTS jobs_project_starttime ON job (project, start_time);
   103→CREATE INDEX IF NOT EXISTS jobs_project_duration ON job (project, duration);
   104→CREATE INDEX IF NOT EXISTS jobs_project_numnodes ON job (project, num_nodes);
   105→CREATE INDEX IF NOT EXISTS jobs_project_numhwthreads ON job (project, num_hwthreads);
   106→CREATE INDEX IF NOT EXISTS jobs_project_numacc ON job (project, num_acc);
   107→CREATE INDEX IF NOT EXISTS jobs_project_energy ON job (project, energy);
   108→
   109→-- JobState Filter
   110→CREATE INDEX IF NOT EXISTS jobs_jobstate ON job (job_state);
   111→CREATE INDEX IF NOT EXISTS jobs_jobstate_user ON job (job_state, hpc_user);
   112→CREATE INDEX IF NOT EXISTS jobs_jobstate_project ON job (job_state, project);
   113→CREATE INDEX IF NOT EXISTS jobs_jobstate_cluster ON job (job_state, cluster);
   114→-- JobState Filter Sorting
   115→CREATE INDEX IF NOT EXISTS jobs_jobstate_starttime ON job (job_state, start_time);
   116→CREATE INDEX IF NOT EXISTS jobs_jobstate_duration ON job (job_state, duration);
   117→CREATE INDEX IF NOT EXISTS jobs_jobstate_numnodes ON job (job_state, num_nodes);
   118→CREATE INDEX IF NOT EXISTS jobs_jobstate_numhwthreads ON job (job_state, num_hwthreads);
   119→CREATE INDEX IF NOT EXISTS jobs_jobstate_numacc ON job (job_state, num_acc);
   120→CREATE INDEX IF NOT EXISTS jobs_jobstate_energy ON job (job_state, energy);
   121→
   122→-- ArrayJob Filter
   123→CREATE INDEX IF NOT EXISTS jobs_arrayjobid_starttime ON job (array_job_id, start_time);
   124→CREATE INDEX IF NOT EXISTS jobs_cluster_arrayjobid_starttime ON job (cluster, array_job_id, start_time);
   125→
   126→-- Sorting without active filters
   127→CREATE INDEX IF NOT EXISTS jobs_starttime ON job (start_time);
   128→CREATE INDEX IF NOT EXISTS jobs_duration ON job (duration);
   129→CREATE INDEX IF NOT EXISTS jobs_numnodes ON job (num_nodes);
   130→CREATE INDEX IF NOT EXISTS jobs_numhwthreads ON job (num_hwthreads);
   131→CREATE INDEX IF NOT EXISTS jobs_numacc ON job (num_acc);
   132→CREATE INDEX IF NOT EXISTS jobs_energy ON job (energy);
   133→
   134→-- Single filters with default starttime sorting
   135→CREATE INDEX IF NOT EXISTS jobs_duration_starttime ON job (duration, start_time);
   136→CREATE INDEX IF NOT EXISTS jobs_numnodes_starttime ON job (num_nodes, start_time);
   137→CREATE INDEX IF NOT EXISTS jobs_numhwthreads_starttime ON job (num_hwthreads, start_time);
   138→CREATE INDEX IF NOT EXISTS jobs_numacc_starttime ON job (num_acc, start_time);
   139→CREATE INDEX IF NOT EXISTS jobs_energy_starttime ON job (energy, start_time);
   140→
   141→-- Optimize DB index usage
   142→PRAGMA optimize;
   143→


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


<persisted-output>
Output too large (49.5KB). Full output saved to: /Users/jan/.claude/projects/-Users-jan-prg-CC-cc-backend/42401d2e-7d1c-4c0e-abe6-356cb2d48747/tool-results/toolu_01Er2KCQdp4jKnnfzYsLRdVY.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>