From 066c4553b4cca6a9a8bbc68a996f72d8bc6c2173 Mon Sep 17 00:00:00 2001 From: Jan Eitzinger Date: Thu, 4 May 2023 16:27:30 +0200 Subject: [PATCH] Refactor --- internal/importer/initDB.go | 49 ---------------- internal/importer/normalize.go | 58 +++++++++++++++++++ .../{initDB_test.go => normalize_test.go} | 4 +- internal/repository/job.go | 3 +- 4 files changed, 61 insertions(+), 53 deletions(-) create mode 100644 internal/importer/normalize.go rename internal/importer/{initDB_test.go => normalize_test.go} (95%) diff --git a/internal/importer/initDB.go b/internal/importer/initDB.go index 82a6155..cce9ebf 100644 --- a/internal/importer/initDB.go +++ b/internal/importer/initDB.go @@ -7,7 +7,6 @@ package importer import ( "encoding/json" "fmt" - "math" "strings" "time" @@ -15,7 +14,6 @@ import ( "github.com/ClusterCockpit/cc-backend/pkg/archive" "github.com/ClusterCockpit/cc-backend/pkg/log" "github.com/ClusterCockpit/cc-backend/pkg/schema" - ccunits "github.com/ClusterCockpit/cc-units" ) // Delete the tables "job", "tag" and "jobtag" from the database and @@ -158,53 +156,6 @@ func loadJobStat(job *schema.JobMeta, metric string) float64 { return 0.0 } -func getNormalizationFactor(v float64) (float64, int) { - count := 0 - scale := -3 - - if v > 1000.0 { - for v > 1000.0 { - v *= 1e-3 - count++ - } - } else { - for v < 1.0 { - v *= 1e3 - count++ - } - scale = 3 - } - return math.Pow10(count * scale), count * scale -} - -func getExponent(p float64) int { - count := 0 - - for p > 1.0 { - p = p / 1000.0 - count++ - } - - return count * 3 -} - -func newPrefixFromFactor(op ccunits.Prefix, e int) ccunits.Prefix { - f := float64(op) - exp := math.Pow10(getExponent(f) - e) - return ccunits.Prefix(exp) -} - -func Normalize(avg float64, p string) (float64, string) { - f, e := getNormalizationFactor(avg) - - if e != 0 { - np := newPrefixFromFactor(ccunits.NewPrefix(p), e) - return f, np.Prefix() - } - - return f, p -} - func checkJobData(d *schema.JobData) error { for _, scopes := range *d { // var newUnit schema.Unit diff --git a/internal/importer/normalize.go b/internal/importer/normalize.go new file mode 100644 index 0000000..a2efac3 --- /dev/null +++ b/internal/importer/normalize.go @@ -0,0 +1,58 @@ +// Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg. +// All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. +package importer + +import ( + "math" + + ccunits "github.com/ClusterCockpit/cc-units" +) + +func getNormalizationFactor(v float64) (float64, int) { + count := 0 + scale := -3 + + if v > 1000.0 { + for v > 1000.0 { + v *= 1e-3 + count++ + } + } else { + for v < 1.0 { + v *= 1e3 + count++ + } + scale = 3 + } + return math.Pow10(count * scale), count * scale +} + +func getExponent(p float64) int { + count := 0 + + for p > 1.0 { + p = p / 1000.0 + count++ + } + + return count * 3 +} + +func newPrefixFromFactor(op ccunits.Prefix, e int) ccunits.Prefix { + f := float64(op) + exp := math.Pow10(getExponent(f) - e) + return ccunits.Prefix(exp) +} + +func Normalize(avg float64, p string) (float64, string) { + f, e := getNormalizationFactor(avg) + + if e != 0 { + np := newPrefixFromFactor(ccunits.NewPrefix(p), e) + return f, np.Prefix() + } + + return f, p +} diff --git a/internal/importer/initDB_test.go b/internal/importer/normalize_test.go similarity index 95% rename from internal/importer/initDB_test.go rename to internal/importer/normalize_test.go index 29a5e84..544000a 100644 --- a/internal/importer/initDB_test.go +++ b/internal/importer/normalize_test.go @@ -31,7 +31,7 @@ func TestNormalizeFactor(t *testing.T) { fmt.Printf("Prefix %e Short %s\n", float64(np), np.Prefix()) - p := NewPrefixFromFactor(np, e) + p := newPrefixFromFactor(np, e) if p.Prefix() != "G" { t.Errorf("Failed Prefix or unit: Want G, Got %s", p.Prefix()) @@ -56,7 +56,7 @@ func TestNormalizeKeep(t *testing.T) { fmt.Printf("Prefix %e Short %s\n", float64(np), np.Prefix()) - p := NewPrefixFromFactor(np, e) + p := newPrefixFromFactor(np, e) if p.Prefix() != "G" { t.Errorf("Failed Prefix or unit: Want G, Got %s", p.Prefix()) diff --git a/internal/repository/job.go b/internal/repository/job.go index e5f879b..86eecfb 100644 --- a/internal/repository/job.go +++ b/internal/repository/job.go @@ -566,7 +566,7 @@ func (r *JobRepository) FindUserOrProjectOrJobname(ctx context.Context, searchte func (r *JobRepository) FindColumnValue(user *auth.User, searchterm string, table string, selectColumn string, whereColumn string, isLike bool) (result string, err error) { compareStr := " = ?" query := searchterm - if isLike == true { + if isLike { compareStr = " LIKE ?" query = "%" + searchterm + "%" } @@ -909,7 +909,6 @@ func (r *JobRepository) jobsStatisticsHistogram(ctx context.Context, value string, filters []*model.JobFilter, id, col string) ([]*model.HistoPoint, error) { start := time.Now() - query := sq.Select(value, "COUNT(job.id) AS count").From("job") query, qerr := SecurityCheck(ctx, sq.Select(value, "COUNT(job.id) AS count").From("job")) if qerr != nil {