mirror of
https://github.com/ClusterCockpit/cc-backend
synced 2025-01-24 10:29:06 +01:00
Refactor
This commit is contained in:
parent
5404be3ca7
commit
066c4553b4
@ -7,7 +7,6 @@ package importer
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -15,7 +14,6 @@ import (
|
|||||||
"github.com/ClusterCockpit/cc-backend/pkg/archive"
|
"github.com/ClusterCockpit/cc-backend/pkg/archive"
|
||||||
"github.com/ClusterCockpit/cc-backend/pkg/log"
|
"github.com/ClusterCockpit/cc-backend/pkg/log"
|
||||||
"github.com/ClusterCockpit/cc-backend/pkg/schema"
|
"github.com/ClusterCockpit/cc-backend/pkg/schema"
|
||||||
ccunits "github.com/ClusterCockpit/cc-units"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Delete the tables "job", "tag" and "jobtag" from the database and
|
// 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
|
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 {
|
func checkJobData(d *schema.JobData) error {
|
||||||
for _, scopes := range *d {
|
for _, scopes := range *d {
|
||||||
// var newUnit schema.Unit
|
// var newUnit schema.Unit
|
||||||
|
58
internal/importer/normalize.go
Normal file
58
internal/importer/normalize.go
Normal file
@ -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
|
||||||
|
}
|
@ -31,7 +31,7 @@ func TestNormalizeFactor(t *testing.T) {
|
|||||||
|
|
||||||
fmt.Printf("Prefix %e Short %s\n", float64(np), np.Prefix())
|
fmt.Printf("Prefix %e Short %s\n", float64(np), np.Prefix())
|
||||||
|
|
||||||
p := NewPrefixFromFactor(np, e)
|
p := newPrefixFromFactor(np, e)
|
||||||
|
|
||||||
if p.Prefix() != "G" {
|
if p.Prefix() != "G" {
|
||||||
t.Errorf("Failed Prefix or unit: Want G, Got %s", p.Prefix())
|
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())
|
fmt.Printf("Prefix %e Short %s\n", float64(np), np.Prefix())
|
||||||
|
|
||||||
p := NewPrefixFromFactor(np, e)
|
p := newPrefixFromFactor(np, e)
|
||||||
|
|
||||||
if p.Prefix() != "G" {
|
if p.Prefix() != "G" {
|
||||||
t.Errorf("Failed Prefix or unit: Want G, Got %s", p.Prefix())
|
t.Errorf("Failed Prefix or unit: Want G, Got %s", p.Prefix())
|
@ -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) {
|
func (r *JobRepository) FindColumnValue(user *auth.User, searchterm string, table string, selectColumn string, whereColumn string, isLike bool) (result string, err error) {
|
||||||
compareStr := " = ?"
|
compareStr := " = ?"
|
||||||
query := searchterm
|
query := searchterm
|
||||||
if isLike == true {
|
if isLike {
|
||||||
compareStr = " LIKE ?"
|
compareStr = " LIKE ?"
|
||||||
query = "%" + searchterm + "%"
|
query = "%" + searchterm + "%"
|
||||||
}
|
}
|
||||||
@ -909,7 +909,6 @@ func (r *JobRepository) jobsStatisticsHistogram(ctx context.Context,
|
|||||||
value string, filters []*model.JobFilter, id, col string) ([]*model.HistoPoint, error) {
|
value string, filters []*model.JobFilter, id, col string) ([]*model.HistoPoint, error) {
|
||||||
|
|
||||||
start := time.Now()
|
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"))
|
query, qerr := SecurityCheck(ctx, sq.Select(value, "COUNT(job.id) AS count").From("job"))
|
||||||
|
|
||||||
if qerr != nil {
|
if qerr != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user