This repository has been archived by the owner on Mar 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.go
146 lines (139 loc) · 3.26 KB
/
graph.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package studio_statistics
import (
"encoding/csv"
"fmt"
"github.com/wcharczuk/go-chart"
"io"
"regexp"
"strconv"
"time"
)
var alphanumeric = regexp.MustCompile("(?m)[^a-zA-Z0-9, \\-:.]+")
func MakeGraph(r io.Reader, w io.Writer) error {
rows, err := csv.NewReader(r).ReadAll()
if err != nil {
return err
}
fmt.Println("Getting rid of identical rows...")
before := len(rows)
rows = eliminateIdenticalTimestamps(rows)
after := len(rows)
fmt.Println("Eliminated", before-after, "rows!")
fmt.Println("Parsing dataset")
times := make([]time.Time, 0, len(rows))
opens := make([]float64, 0, len(rows))
switchStates := make([]float64, 0, len(rows))
motions := make([]float64, 0, len(rows))
for i := 0; i < len(rows); i++ {
for j := 0; j < len(rows[i]); j++ {
rows[i][j] = alphanumeric.ReplaceAllString(rows[i][j], "")
}
t, err := parseTime(rows[i][0])
if t.Add(time.Hour * 24 * 7).Before(time.Now()) {
continue
}
open, err := strconv.ParseBool(rows[i][1])
if err != nil {
return err
}
switchState, err := strconv.ParseInt(rows[i][2], 10, 32)
if err != nil {
return err
}
motion, err := strconv.ParseBool(rows[i][3])
if err != nil {
return err
}
times = append(times, t)
opens = append(opens, btof(open))
switchStates = append(switchStates, float64(switchState))
motions = append(motions, btof(motion))
}
fmt.Println("Done parsing data")
width := int(times[len(times)-1].Sub(times[0]).Seconds() / 30.0)
graph := chart.Chart{
Title: "Design Studio Statistics",
Width: width,
Height: 600,
XAxis: chart.XAxis{
Name: "Time",
NameStyle: chart.StyleShow(),
Style: chart.StyleShow(),
ValueFormatter: chart.TimeValueFormatterWithFormat("Mon Jan _2 3:04PM"),
},
YAxis: chart.YAxis{
Name: "Value",
NameStyle: chart.StyleShow(),
Style: chart.StyleShow(),
},
Series: []chart.Series{
chart.TimeSeries{
Name: "Time Open",
XValues: times,
YValues: opens,
},
chart.TimeSeries{
Name: "Switch States (0 = normal, 1 = forced open, 2 = forced closed)",
XValues: times,
YValues: switchStates,
},
chart.TimeSeries{
Name: "Motion Detected",
XValues: times,
YValues: motions,
},
},
}
graph.Elements = []chart.Renderable{
chart.Legend(&graph),
}
fmt.Println("Rendering graph")
err = graph.Render(chart.PNG, w)
if err != nil {
return err
}
return nil
}
func btof(v bool) float64 {
if v {
return 1.0
}
return 0.0
}
func eliminateIdenticalTimestamps(rows [][]string) (newRows [][]string) {
if rows == nil || len(rows) < 3 {
newRows = rows
return
}
for i := 0; i < len(rows); i++ {
lastIdenticalIndex := i
for j := i; j < len(rows); j++ {
identical := true
for k := 1; k < len(rows[i]); k++ {
if rows[i][k] != rows[j][k] {
identical = false
break
}
}
if identical {
lastIdenticalIndex = j
} else {
break
}
}
if lastIdenticalIndex != i {
newRows = append(newRows, rows[i], rows[lastIdenticalIndex])
i = lastIdenticalIndex
} else {
newRows = append(newRows, rows[i])
}
}
return
}
func parseTime(timeString string) (t time.Time, err error) {
t, err= time.Parse(time.RFC822, timeString)
if err != nil {
t, err = time.Parse(time.RFC3339Nano, timeString)
}
return
}