Add Table flush on init

This commit is contained in:
Jan Eitzinger 2023-05-04 16:03:04 +02:00
parent d9b18d1763
commit c5b67d64d2
2 changed files with 27 additions and 7 deletions

View File

@ -22,6 +22,10 @@ import (
// repopulate them using the jobs found in `archive`. // repopulate them using the jobs found in `archive`.
func InitDB() error { func InitDB() error {
r := repository.GetJobRepository() r := repository.GetJobRepository()
if err := r.Flush(); err != nil {
log.Errorf("repository initDB(): %v", err)
return err
}
starttime := time.Now() starttime := time.Now()
log.Print("Building job table...") log.Print("Building job table...")
@ -184,17 +188,17 @@ func getExponent(p float64) int {
return count * 3 return count * 3
} }
func NewPrefixFromFactor(op ccunits.Prefix, e int) ccunits.Prefix { func newPrefixFromFactor(op ccunits.Prefix, e int) ccunits.Prefix {
f := float64(op) f := float64(op)
exp := math.Pow10(getExponent(f) - e) exp := math.Pow10(getExponent(f) - e)
return ccunits.Prefix(exp) return ccunits.Prefix(exp)
} }
func normalize(avg float64, p string) (float64, string) { func Normalize(avg float64, p string) (float64, string) {
f, e := getNormalizationFactor(avg) f, e := getNormalizationFactor(avg)
if e != 0 { if e != 0 {
np := NewPrefixFromFactor(ccunits.NewPrefix(p), e) np := newPrefixFromFactor(ccunits.NewPrefix(p), e)
return f, np.Prefix() return f, np.Prefix()
} }
@ -217,7 +221,7 @@ func checkJobData(d *schema.JobData) error {
} }
avg := sum / float64(len(metric.Series)) avg := sum / float64(len(metric.Series))
f, p := normalize(avg, metric.Unit.Prefix) f, p := Normalize(avg, metric.Unit.Prefix)
if p != metric.Unit.Prefix { if p != metric.Unit.Prefix {

View File

@ -101,12 +101,28 @@ func (r *JobRepository) Flush() error {
switch r.driver { switch r.driver {
case "sqlite3": case "sqlite3":
_, err = r.DB.Exec(`DELETE FROM job`) if _, err = r.DB.Exec(`DELETE FROM jobtag`); err != nil {
return err
}
if _, err = r.DB.Exec(`DELETE FROM tag`); err != nil {
return err
}
if _, err = r.DB.Exec(`DELETE FROM job`); err != nil {
return err
}
case "mysql": case "mysql":
_, err = r.DB.Exec(`TRUNCATE TABLE job`) if _, err = r.DB.Exec(`TRUNCATE TABLE jobtag`); err != nil {
return err
}
if _, err = r.DB.Exec(`TRUNCATE TABLE tag`); err != nil {
return err
}
if _, err = r.DB.Exec(`TRUNCATE TABLE job`); err != nil {
return err
}
} }
return err return nil
} }
func (r *JobRepository) FetchJobName(job *schema.Job) (*string, error) { func (r *JobRepository) FetchJobName(job *schema.Job) (*string, error) {