-
Notifications
You must be signed in to change notification settings - Fork 5
/
stats.go
345 lines (275 loc) · 8.48 KB
/
stats.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package sncli
import (
"fmt"
"sort"
"time"
"github.com/alexeyco/simpletable"
"github.com/dustin/go-humanize"
// "github.com/fatih/color".
"github.com/gookit/color"
"github.com/jonhadfield/gosn-v2/cache"
"github.com/jonhadfield/gosn-v2/common"
"github.com/jonhadfield/gosn-v2/items"
"github.com/ryanuber/columnize"
)
type StatsData struct {
CoreTypeCounter typeCounter
OtherTypeCounter typeCounter
LargestNotes []*items.Note
ItemsOrphanedRefs []ItemOrphanedRefs
LastUpdatedNote *items.Note
NewestNote *items.Note
OldestNote *items.Note
}
func (i *StatsConfig) GetData() (StatsData, error) {
var err error
var so cache.SyncOutput
so, err = Sync(cache.SyncInput{
Session: &i.Session,
}, true)
if err != nil {
return StatsData{}, err
}
var allPersistedItems cache.Items
if err = so.DB.All(&allPersistedItems); err != nil {
return StatsData{}, err
}
var gitems items.Items
gitems, err = allPersistedItems.ToItems(&i.Session)
if err != nil {
return StatsData{}, err
}
var notes items.Items
var itemsOrphanedRefs []ItemOrphanedRefs
var missingItemsKey []string
var missingContentUUIDs []string
var missingContentTypeUUIDs []string
allUUIDs := make([]string, len(allPersistedItems))
for x := range allPersistedItems {
allUUIDs = append(allUUIDs, allPersistedItems[x].UUID)
}
var ctCounter, otCounter typeCounter
ctCounter.counts = make(map[string]int64)
otCounter.counts = make(map[string]int64)
var oldestNoteTime, newestNoteTime, lastUpdatedNoteTime time.Time
var oldestNote, newestNote, lastUpdatedNote *items.Note
for _, item := range gitems {
// check if item is trashed note
var isTrashedNote bool
if item.GetContentType() == common.SNItemTypeNote {
n := item.(*items.Note)
if n.Content.Trashed != nil && *n.Content.Trashed {
isTrashedNote = true
}
}
switch {
case isTrashedNote:
ctCounter.update("Notes (In Trash)")
case item.GetContentType() == common.SNItemTypeNote:
ctCounter.update(common.SNItemTypeNote)
case item.GetContentType() == common.SNItemTypeTag:
ctCounter.update(common.SNItemTypeTag)
default:
otCounter.update(item.GetContentType())
}
if item.GetItemsKeyID() == "" {
missingItemsKey = append(missingItemsKey, fmt.Sprintf("- type: %s uuid: %s %s", item.GetContentType(), item.GetUUID(), item.GetItemsKeyID()))
}
if item.GetContentType() == "" {
missingContentTypeUUIDs = append(missingContentTypeUUIDs, item.GetUUID())
}
refs := item.GetContent().References()
for _, ref := range refs {
if !StringInSlice(ref.UUID, allUUIDs, false) {
itemsOrphanedRefs = append(itemsOrphanedRefs, ItemOrphanedRefs{
ContentType: item.GetContentType(),
Item: item,
OrphanedRefs: []string{ref.UUID},
})
break
}
}
if item.GetContentType() == common.SNItemTypeNote && !isTrashedNote {
if item.GetContent() == nil {
missingContentUUIDs = append(missingContentUUIDs, item.GetUUID())
}
var cTime time.Time
cTime, err = time.Parse(timeLayout, item.GetCreatedAt())
if err != nil {
return StatsData{}, err
}
var uTime time.Time
uTime, err = time.Parse(timeLayout, item.GetUpdatedAt())
if err != nil {
return StatsData{}, err
}
if oldestNoteTime.IsZero() || cTime.Before(oldestNoteTime) {
oldestNote = item.(*items.Note)
oldestNoteTime = cTime
}
if newestNoteTime.IsZero() || cTime.After(newestNoteTime) {
newestNote = item.(*items.Note)
newestNoteTime = cTime
}
if lastUpdatedNoteTime.IsZero() || uTime.After(lastUpdatedNoteTime) {
lastUpdatedNote = item.(*items.Note)
lastUpdatedNoteTime = uTime
}
// create slice of notes with non-zero content size
if item.GetContentSize() > 0 {
notes = append(notes, item)
}
}
}
sort.Slice(notes, func(i, j int) bool {
return notes[i].GetContentSize() > notes[j].GetContentSize()
})
var largestNotes []*items.Note
if len(notes) > 0 {
finalItem := len(notes)
if len(notes) >= 5 {
finalItem = 4
}
for x := 0; x < finalItem; x++ {
largestNotes = append(largestNotes, notes[x].(*items.Note))
}
}
return StatsData{
CoreTypeCounter: ctCounter,
OtherTypeCounter: otCounter,
LargestNotes: largestNotes,
ItemsOrphanedRefs: itemsOrphanedRefs,
LastUpdatedNote: lastUpdatedNote,
NewestNote: newestNote,
OldestNote: oldestNote,
}, nil
}
type ItemOrphanedRefs struct {
ContentType string
Item items.Item
OrphanedRefs []string
}
func showNoteHistory(data StatsData) {
table := simpletable.New()
table.Header = &simpletable.Header{
Cells: []*simpletable.Cell{
{Align: simpletable.AlignLeft, Text: color.Bold.Sprintf("")},
{Align: simpletable.AlignLeft, Text: color.Bold.Sprintf("Title")},
{Align: simpletable.AlignLeft, Text: color.Bold.Sprintf("Time")},
},
}
if data.OldestNote != nil {
data.OldestNote.Content = items.NoteContent{}
}
table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
{Text: "Newest"},
{Text: fmt.Sprintf("%s", data.NewestNote.Content.Title)},
{Text: fmt.Sprintf("%s", humanize.Time(time.UnixMicro(data.NewestNote.CreatedAtTimestamp)))},
})
table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
{Text: "Most Recently Updated"},
{Text: fmt.Sprintf("%s", data.LastUpdatedNote.Content.Title)},
{Text: fmt.Sprintf("%s", humanize.Time(time.UnixMicro(data.LastUpdatedNote.UpdatedAtTimestamp)))},
})
color.Bold.Println("Note History")
table.Println()
}
func showItemCounts(data StatsData) {
table := simpletable.New()
table.Header = &simpletable.Header{
Cells: []*simpletable.Cell{
{Align: simpletable.AlignLeft, Text: color.Bold.Sprintf("Type")},
{Align: simpletable.AlignLeft, Text: color.Bold.Sprintf("Count")},
},
}
table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
{Text: "Notes"},
{Text: fmt.Sprintf("%s", humanize.Comma(data.CoreTypeCounter.counts[common.SNItemTypeNote]))},
})
table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
{Text: "Tags"},
{Text: fmt.Sprintf("%s", humanize.Comma(data.CoreTypeCounter.counts[common.SNItemTypeTag]))},
})
table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
{Text: "----------------"},
{Text: "------"},
})
table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
{Text: "Notes (In Trash)"},
{Text: fmt.Sprintf("%s", humanize.Comma(data.CoreTypeCounter.counts["Notes (In Trash)"]))},
})
table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
{Text: "----------------"},
{Text: "------"},
})
var keys []string
for key := range data.OtherTypeCounter.counts {
keys = append(keys, key)
}
sort.SliceStable(keys, func(i, j int) bool {
return data.OtherTypeCounter.counts[keys[i]] > data.OtherTypeCounter.counts[keys[j]]
})
for _, k := range keys {
table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
{Text: k},
{Text: fmt.Sprintf("%s", humanize.Comma(data.OtherTypeCounter.counts[k]))},
})
}
color.Bold.Println("Item Counts")
table.Println()
}
func showLargestNotes(data StatsData) {
table := simpletable.New()
table.Header = &simpletable.Header{
Cells: []*simpletable.Cell{
{Align: simpletable.AlignLeft, Text: color.Bold.Sprintf("Size")},
{Align: simpletable.AlignLeft, Text: color.Bold.Sprintf("Title")},
},
}
for _, note := range data.LargestNotes {
table.Body.Cells = append(table.Body.Cells, []*simpletable.Cell{
{Text: fmt.Sprintf("%s", humanize.Bytes(uint64(note.GetContentSize())))},
{Text: fmt.Sprintf("%s", note.Content.Title)},
})
}
color.Bold.Println("Largest Notes")
table.Println()
}
func (i *StatsConfig) Run() error {
data, err := i.GetData()
if err != nil {
return err
}
showItemCounts(data)
showNoteHistory(data)
showLargestNotes(data)
return err
}
type typeCounter struct {
counts map[string]int64
}
func (in *typeCounter) update(itemType string) {
var found bool
for name := range in.counts {
if name == itemType {
found = true
in.counts[name]++
}
}
if !found {
in.counts[itemType] = 1
}
}
func (in *typeCounter) present() {
var lines []string
lines = append(lines, fmt.Sprintf("Notes ^ %d", in.counts[common.SNItemTypeNote]))
lines = append(lines, fmt.Sprintf("Tags ^ %d", in.counts[common.SNItemTypeTag]))
for name, count := range in.counts {
if name != common.SNItemTypeTag && name != common.SNItemTypeNote && name != "Deleted" {
lines = append(lines, fmt.Sprintf("%s ^ %d", name, count))
}
}
config := columnize.DefaultConfig()
config.Delim = "^"
fmt.Println(columnize.Format(lines, config))
}