-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
169 lines (148 loc) · 2.97 KB
/
main.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"bytes"
"encoding/json"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/svg"
"github.com/wcharczuk/go-chart"
"html/template"
"log"
"os"
"time"
)
type CountMaxMin struct {
Count int
Max int
Min int
}
type CountPercent struct {
Count float64
Percent float64
}
type Metadata struct {
Visitors CountMaxMin
Hits CountMaxMin
Data struct {
Unique int
}
}
type HitsVisitorsData []struct {
Hits CountPercent
Visitors CountPercent
Data string
}
type Visitors struct {
Metadata Metadata
Data HitsVisitorsData // date in form "20180610"
}
type Requests struct {
Metadata Metadata
Data HitsVisitorsData // path that was visited e.g. "/home/path/"
}
type Report struct {
General struct {
StartDate string `json:"start_date"`
EndDate string `json:"end_date"`
DateTime string `json:"date_time"`
TotalRequests int `json:"total_requests"`
}
Visitors Visitors
Requests Requests
Hosts struct {
Metadata Metadata
Data []struct {
Hits CountPercent
Visitors CountPercent
Data string
Country string
}
}
OS struct {
Metadata Metadata
Data []struct {
Hits CountPercent
Visitors CountPercent
Data string
Items HitsVisitorsData
}
}
}
type TemplateContext struct {
Visitors template.HTML
}
func getBaseChart() *chart.Chart {
return &chart.Chart{
// Width: 1920, //this overrides the default.
Background: chart.Style{
Padding: chart.Box{
Top: 40,
Left: 80,
},
},
Series: []chart.Series{},
XAxis: chart.XAxis{
Name: "Date",
NameStyle: chart.StyleShow(),
Style: chart.StyleShow(),
},
YAxisSecondary: chart.YAxis{
Name: "Hits",
NameStyle: chart.StyleShow(),
Style: chart.StyleShow(),
},
YAxis: chart.YAxis{
Name: "Visitors",
NameStyle: chart.StyleShow(),
Style: chart.StyleShow(),
},
}
}
func getVisitorsChart(v Visitors) *chart.Chart {
var y1, y2 []float64
var x []time.Time
for _, d := range v.Data {
y1 = append(y1, d.Visitors.Count)
y2 = append(y2, d.Hits.Count)
t, err := time.Parse("20060102", d.Data)
if err != nil {
log.Fatal(err)
}
x = append(x, t)
}
c := getBaseChart()
c.Series = []chart.Series{
chart.TimeSeries{
XValues: x,
YValues: y1,
},
chart.TimeSeries{
XValues: x,
YValues: y2,
YAxis: chart.YAxisSecondary,
},
}
return c
}
func ChartAsHTML(c *chart.Chart) template.HTML {
markup := bytes.Buffer{}
c.Render(chart.SVG, &markup)
m := minify.New()
m.AddFunc("image/svg", svg.Minify)
gb, _ := m.Bytes("image/svg", markup.Bytes())
return template.HTML(gb)
}
func main() {
var d Report
err := json.NewDecoder(os.Stdin).Decode(&d)
if err != nil {
log.Fatal(err)
}
t := template.Must(template.ParseFiles("template.html"))
ctx := TemplateContext{
Visitors: ChartAsHTML(getVisitorsChart(d.Visitors)),
}
err = t.ExecuteTemplate(os.Stdout, "template.html", &ctx)
if err != nil {
log.Fatal(err)
}
}