-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
245 lines (219 loc) · 6.62 KB
/
manager.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package godid
import (
"errors"
"regexp"
"strconv"
"time"
"github.com/samber/lo"
"github.com/sirupsen/logrus"
)
const (
flatEntriesPlaceholder = "all entries"
rootBucketName = "root"
)
var (
lastDurationPattern = regexp.MustCompile(`^(\d+)d$`)
)
var (
store entryStore
flatAggregation = func(entries []entry) (any, error) {
return lo.Map(entries, func(e entry, _ int) string {
return string(e.Content)
}), nil
}
perDayAggregation = func(entries []entry) (any, error) {
result := make(map[string][]string)
for _, entry := range entries {
content := string(entry.Content)
bucket, err := getBucketFromEntry(entry)
if err != nil {
return nil, err
}
result[bucket] = append(result[bucket], content)
}
return result, nil
}
)
// Init initialises godid
func Init() {
cfg, err := getConfig()
if err != nil {
panic(err)
}
if cfg == nil {
panic(errors.New("null config"))
}
store, err = newBoltStore(*cfg)
if err != nil {
panic(err)
}
}
// Close closes godid
func Close() {
store.Close()
}
// AddEntry adds an entry to the underlying store in the root bucket
func AddEntry(what string) error {
return AddEntryToBucket(rootBucketName, what)
}
// AddEntryToBucket adds an entry to the underlying store in the specified parent bucket
func AddEntryToBucket(bucket string, what string) error {
e := entry{
Content: []byte(what),
Timestamp: time.Now(),
}
err := store.Put(bucket, e)
if err != nil {
getLogger().WithFields(logrus.Fields{
"component": "manager",
"method": "AddEntry",
"entry": what,
}).WithError(err).Error("failed to put entry")
}
return err
}
// GetToday retrieves all entries logged today from the root bucket
func GetToday() ([]string, error) {
return GetTodayFromBucket(rootBucketName)
}
// GetTodayFromBucket retrieves all entries logged today from the specified bucket
func GetTodayFromBucket(bucketName string) ([]string, error) {
start := time.Now()
result, err := getRange(bucketName, start, start, true)
if err != nil {
getLogger().WithFields(logrus.Fields{
"component": "manager",
"method": "GetToday",
"timestamp": start,
}).WithError(err).Error("failed to get entries")
return nil, err
}
return result[flatEntriesPlaceholder], nil
}
// GetYesterday retrieves all entries logged yesterday from the root bucket
func GetYesterday() ([]string, error) {
return GetYesterdayFromBucket(rootBucketName)
}
// GetYesterdayFromBucket retrieves all entries logged yesterday from the specified bucket
func GetYesterdayFromBucket(bucketName string) ([]string, error) {
start := time.Now().AddDate(0, 0, -1)
result, err := getRange(bucketName, start, start, true)
if err != nil {
getLogger().WithFields(logrus.Fields{
"component": "manager",
"method": "GetYesterday",
"timestamp": start,
}).WithError(err).Error("failed to get entries")
return nil, err
}
return result[flatEntriesPlaceholder], nil
}
// GetThisWeek returns all entries from the current week from the root bucket
func GetThisWeek(flat bool) (map[string][]string, error) {
return GetThisWeekFromBucket(rootBucketName, flat)
}
// GetThisWeekFromBucket returns all entries from the current week from the specified bucket
func GetThisWeekFromBucket(bucketName string, flat bool) (map[string][]string, error) {
start, end := getWeekInterval(time.Now())
result, err := getRange(bucketName, start, end, flat)
if err != nil {
getLogger().WithFields(logrus.Fields{
"component": "manager",
"method": "GetThisWeek",
"start": start,
"end": end,
"flat": flat,
}).WithError(err).Error("failed to get entries")
}
return result, err
}
// GetLastWeek returns all entries from the previous week from the root bucket
func GetLastWeek(flat bool) (map[string][]string, error) {
return GetLastWeekFromBucket(rootBucketName, flat)
}
// GetLastWeekFromBucket returns all entries from the previous week from the specified bucket
func GetLastWeekFromBucket(bucketName string, flat bool) (map[string][]string, error) {
aWeekBefore := time.Now().AddDate(0, 0, -7)
start, end := getWeekInterval(aWeekBefore)
result, err := getRange(bucketName, start, end, flat)
if err != nil {
getLogger().WithFields(logrus.Fields{
"component": "manager",
"method": "GetThisWeek",
"start": start,
"end": end,
"flat": flat,
}).WithError(err).Error("failed to get entries")
}
return result, err
}
// GetLastDuration retrives all the entries from the custom previous duration from the root bucket
func GetLastDuration(durationString string, flat bool) (map[string][]string, error) {
return GetLastDurationFromBucket(rootBucketName, durationString, flat)
}
// GetLastDurationFromBucket retrives all the entries from the custom previous duration from the specified bucket
func GetLastDurationFromBucket(bucketName string, durationString string, flat bool) (map[string][]string, error) {
d, err := parseDuration(durationString)
if err != nil {
return nil, err
}
now := time.Now()
result, err := getRange(bucketName, now.Add(-1*d), now, flat)
if err != nil {
getLogger().WithFields(logrus.Fields{
"component": "manager",
"method": "GetThisWeek",
"start": now.Add(-1 * d),
"end": now,
"flat": flat,
}).WithError(err).Error("failed to get entries")
}
return result, err
}
func parseDuration(durationString string) (time.Duration, error) {
match := lastDurationPattern.FindStringSubmatch(durationString)
if match == nil {
return 0, didErrorf("invalid duration string %s", durationString)
}
d, err := strconv.Atoi(match[1])
if err != nil {
return 0, err
}
return 24 * time.Duration(d) * time.Hour, nil
}
func getWeekInterval(reference time.Time) (time.Time, time.Time) {
weekDay := int(reference.Weekday())
if reference.Weekday() == time.Sunday {
weekDay = int(time.Saturday) + 1
}
start := reference.AddDate(0, 0, -1*(weekDay-int(time.Monday)))
end := reference.AddDate(0, 0, int(time.Saturday)+1-weekDay)
return start, end
}
func getRange(bucketName string, start, end time.Time, flat bool) (map[string][]string, error) {
var agg aggregationFunction
if flat {
agg = flatAggregation
} else {
agg = perDayAggregation
}
entries, err := store.GetRangeWithAggregation(bucketName, start, end, agg)
if err != nil {
return nil, err
}
result := make(map[string][]string)
if flat {
flatEntries, ok := entries.([]string)
if !ok {
return nil, errors.New("internal error, cannot convert result")
}
result[flatEntriesPlaceholder] = flatEntries
} else {
var ok bool
result, ok = entries.(map[string][]string)
if !ok {
return nil, errors.New("internal error, cannot convert result")
}
}
return result, nil
}