Add backend type and adopt cast expressions

Fixes #67
This commit is contained in:
Jan Eitzinger 2023-02-21 15:22:05 +01:00
parent 33bfc6dbc6
commit 999609a17a
2 changed files with 17 additions and 7 deletions

View File

@ -23,6 +23,7 @@ var (
type DBConnection struct { type DBConnection struct {
DB *sqlx.DB DB *sqlx.DB
Driver string
} }
func Connect(driver string, db string) { func Connect(driver string, db string) {
@ -54,7 +55,7 @@ func Connect(driver string, db string) {
log.Fatalf("unsupported database driver: %s", driver) log.Fatalf("unsupported database driver: %s", driver)
} }
dbConnInstance = &DBConnection{DB: dbHandle} dbConnInstance = &DBConnection{DB: dbHandle, Driver: driver}
checkDBVersion(driver, dbHandle.DB) checkDBVersion(driver, dbHandle.DB)
}) })
} }

View File

@ -33,6 +33,7 @@ var (
type JobRepository struct { type JobRepository struct {
DB *sqlx.DB DB *sqlx.DB
driver string
stmtCache *sq.StmtCache stmtCache *sq.StmtCache
cache *lrucache.Cache cache *lrucache.Cache
@ -47,6 +48,7 @@ func GetJobRepository() *JobRepository {
jobRepoInstance = &JobRepository{ jobRepoInstance = &JobRepository{
DB: db.DB, DB: db.DB,
driver: db.Driver,
stmtCache: sq.NewStmtCache(db.DB), stmtCache: sq.NewStmtCache(db.DB),
cache: lrucache.New(1024 * 1024), cache: lrucache.New(1024 * 1024),
archiveChannel: make(chan *schema.Job, 128), archiveChannel: make(chan *schema.Job, 128),
@ -674,17 +676,24 @@ func (r *JobRepository) JobsStatistics(ctx context.Context,
start := time.Now() start := time.Now()
// In case `groupBy` is nil (not used), the model.JobsStatistics used is at the key '' (empty string) // In case `groupBy` is nil (not used), the model.JobsStatistics used is at the key '' (empty string)
stats := map[string]*model.JobsStatistics{} stats := map[string]*model.JobsStatistics{}
var castType string
if r.driver == "sqlite3" {
castType = "int"
} else if r.driver == "mysql" {
castType = "unsigned"
}
// `socketsPerNode` and `coresPerSocket` can differ from cluster to cluster, so we need to explicitly loop over those. // `socketsPerNode` and `coresPerSocket` can differ from cluster to cluster, so we need to explicitly loop over those.
for _, cluster := range archive.Clusters { for _, cluster := range archive.Clusters {
for _, subcluster := range cluster.SubClusters { for _, subcluster := range cluster.SubClusters {
corehoursCol := fmt.Sprintf("CAST(ROUND(SUM(job.duration * job.num_nodes * %d * %d) / 3600) as int)", subcluster.SocketsPerNode, subcluster.CoresPerSocket) corehoursCol := fmt.Sprintf("CAST(ROUND(SUM(job.duration * job.num_nodes * %d * %d) / 3600) as %s)", subcluster.SocketsPerNode, subcluster.CoresPerSocket, castType)
var query sq.SelectBuilder var query sq.SelectBuilder
if groupBy == nil { if groupBy == nil {
query = sq.Select( query = sq.Select(
"''", "''",
"COUNT(job.id)", "COUNT(job.id)",
"CAST(ROUND(SUM(job.duration) / 3600) as int)", fmt.Sprintf("CAST(ROUND(SUM(job.duration) / 3600) as %s)", castType),
corehoursCol, corehoursCol,
).From("job") ).From("job")
} else { } else {
@ -692,7 +701,7 @@ func (r *JobRepository) JobsStatistics(ctx context.Context,
query = sq.Select( query = sq.Select(
col, col,
"COUNT(job.id)", "COUNT(job.id)",
"CAST(ROUND(SUM(job.duration) / 3600) as int)", fmt.Sprintf("CAST(ROUND(SUM(job.duration) / 3600) as %s)", castType),
corehoursCol, corehoursCol,
).From("job").GroupBy(col) ).From("job").GroupBy(col)
} }
@ -797,7 +806,7 @@ func (r *JobRepository) JobsStatistics(ctx context.Context,
if histogramsNeeded { if histogramsNeeded {
var err error var err error
value := fmt.Sprintf(`CAST(ROUND((CASE WHEN job.job_state = "running" THEN %d - job.start_time ELSE job.duration END) / 3600) as int) as value`, time.Now().Unix()) value := fmt.Sprintf(`CAST(ROUND((CASE WHEN job.job_state = "running" THEN %d - job.start_time ELSE job.duration END) / 3600) as %s) as value`, time.Now().Unix(), castType)
stat.HistDuration, err = r.jobsStatisticsHistogram(ctx, value, filter, id, col) stat.HistDuration, err = r.jobsStatisticsHistogram(ctx, value, filter, id, col)
if err != nil { if err != nil {
log.Warn("Error while loading job statistics histogram: running jobs") log.Warn("Error while loading job statistics histogram: running jobs")