Skip to content

Commit

Permalink
importer: Add support for aged z database
Browse files Browse the repository at this point in the history
  • Loading branch information
mafredri committed Mar 19, 2019
1 parent 346537d commit 3df104c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
3 changes: 3 additions & 0 deletions importer/testdata/z-aged.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/|90.3972|1553005180
/home|8657.77|1553005185
/var/log|79.4688|1553005178
5 changes: 3 additions & 2 deletions importer/z.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package importer
import (
"fmt"
"io"
"math"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -94,7 +95,7 @@ func (i *z) newEntryFromLine(line string) (*scoring.Entry, error) {
}

path := parts[0]
weight, err := strconv.ParseInt(parts[1], 10, 64)
weight, err := strconv.ParseFloat(parts[1], 64)
if err != nil {
return nil, err
}
Expand All @@ -106,7 +107,7 @@ func (i *z) newEntryFromLine(line string) (*scoring.Entry, error) {
return &scoring.Entry{
Path: path,
Score: &scoring.Score{
Weight: weight,
Weight: int64(math.Round(weight)), // TODO(mafredri): Figure out a fair weight.
Age: time.Unix(epoch, 0),
},
}, nil
Expand Down
29 changes: 29 additions & 0 deletions importer/z_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,32 @@ func TestZ(t *testing.T) {
assert.True(t, conf.Entries[i].CalculateScore() <= conf.Entries[j].CalculateScore())
}
}

func TestZAged(t *testing.T) {
conf := &config.Testing{}
configPath := p.Join(td, "z-aged.txt")

imp := Z(conf, configPath)

err := imp.Import(nil)
assert.Nil(t, err)

assert.
Len(t, 3, conf.Entries).
// 0
Equal(t, "/var/log", conf.Entries[0].Path).
Equal(t, 79, conf.Entries[0].Score.Weight).
Equal(t, time.Unix(1553005178, 0), conf.Entries[0].Score.Age).
// 1
Equal(t, "/", conf.Entries[1].Path).
Equal(t, 90, conf.Entries[1].Score.Weight).
Equal(t, time.Unix(1553005180, 0), conf.Entries[1].Score.Age).
// 2
Equal(t, "/home", conf.Entries[2].Path).
Equal(t, 8658, conf.Entries[2].Score.Weight).
Equal(t, time.Unix(1553005185, 0), conf.Entries[2].Score.Age)

for i, j := 0, 1; i < len(conf.Entries)-1; i, j = i+1, j+1 {
assert.True(t, conf.Entries[i].CalculateScore() <= conf.Entries[j].CalculateScore())
}
}

0 comments on commit 3df104c

Please # to comment.