-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperceived-load.go
76 lines (63 loc) · 1.5 KB
/
perceived-load.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strconv"
"time"
"github.com/jessevdk/go-flags"
)
func getTimeSeries() *TimeSeries {
var opts struct {
DB string `long:"db" value-name:"FILE" description:"database file to use"`
}
args, err := flags.ParseArgs(&opts, os.Args[1:])
if err != nil {
log.Fatal(err)
}
if len(args) > 1 {
log.Fatalf("Unexpected parameters: %v\n", args[1:])
}
if opts.DB == "" {
name := filepath.FromSlash("${HOME}/.config/perceived-load.csv")
opts.DB = os.ExpandEnv(name)
}
ts := NewTimeSeries()
if err := ts.Read(opts.DB); err != nil {
log.Fatal(err)
}
if len(args) > 0 {
value, err := strconv.ParseFloat(args[0], 64)
if err != nil {
log.Fatal(err)
}
ts.Add(time.Now(), value)
if err := ts.Write(opts.DB); err != nil {
log.Fatal(err)
}
} else if len(ts.records) > 0 {
ts.Add(time.Now(), ts.records[len(ts.records)-1].Datum)
}
return ts
}
func main() {
ts := getTimeSeries()
days := []int{1, 5, 15}
today := time.Now()
avgs := averages(ts, &today, days...)
const separator = ", "
var dayList string
for _, lookback := range days {
dayList += fmt.Sprintf("%d%s", lookback, separator)
}
dayList = dayList[:len(dayList)-len(separator)]
var avgList string
for _, avg := range avgs {
avgList += fmt.Sprintf("%.1f%s", avg, separator)
}
avgList = avgList[:len(avgList)-len(separator)]
fmt.Printf("Perceived task load average (%s days): %s\n",
dayList, avgList)
fmt.Println("Optimum is 1.0; higher values mean delayed tasks")
}