-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
55 lines (46 loc) · 1.14 KB
/
types.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
package main
import (
"strings"
"time"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/influxdata/influxdb-client-go/v2/api/write"
)
type Kvitto struct {
Timestamp int64 `json:"timestamp"`
Sold []SoldProduct `json:"sold"`
SoldBy string `json:"sold_by"`
Raw []byte `json:"-"`
}
type SoldProduct struct {
Category string `json:"category"`
Name string `json:"name"`
Count int `json:"count"`
}
func (k Kvitto) Time() time.Time {
return time.Unix(k.Timestamp, 0)
}
func (k Kvitto) toPoints() []*write.Point {
rawP := influxdb2.NewPoint("kvitto_raw",
map[string]string{
"sold_by": strings.ToLower(k.SoldBy),
},
map[string]interface{}{
"json": string(k.Raw),
},
k.Time(),
)
points := make([]*write.Point, 0, len(k.Sold)+1)
points = append(points, rawP)
for _, s := range k.Sold {
tags := map[string]string{
"category": s.Category,
"sold_by": strings.ToLower(k.SoldBy),
"product": s.Name,
}
fields := map[string]interface{}{
"count": s.Count,
}
points = append(points, write.NewPoint("kvitto", tags, fields, k.Time()))
}
return points
}