-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsheet.go
712 lines (611 loc) · 21.3 KB
/
sheet.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
// Copyright 2022 Blues Inc. All rights reserved.
// Use of this source code is governed by licenses granted by the
// copyright holder including that found in the LICENSE file.
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"sort"
"strings"
"time"
"github.com/xuri/excelize/v2"
)
// Trace
const sheetTrace = true
// The route to our sheet handler
const sheetRoute = "/file/"
// Handler to retrieve a sheet
func inboundWebSheetHandler(w http.ResponseWriter, r *http.Request) {
// Open the file
filename := r.RequestURI[len(sheetRoute):]
file := configDataDirectory + filename
contents, err := os.ReadFile(file)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
// Write the file to the HTTPS client as binary, with its original filename
w.Header().Set("Content-Disposition", "attachment; filename="+filename)
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(contents)))
// Copy the file to output
io.Copy(w, bytes.NewReader(contents))
}
// Add all the tabs for this service type
func sheetAddTabs(serviceType string, hs *HostStats, ss serviceSummary, handlers map[string]AppHandler, f *excelize.File) (response string) {
var sn int
if sheetTrace {
fmt.Printf("sheetAddTabs: %s\n", serviceType)
}
keys := make([]string, 0, len(hs.Stats))
for key := range hs.Stats {
keys = append(keys, key)
}
sort.Strings(keys)
for _, siid := range keys {
// Generate the sheet name
s := strings.Split(siid, ":")
ht := "unknown-service-type"
if len(s) == 2 {
ht = s[1]
}
// Skip if it's not what we're looking for
if ht != serviceType {
continue
}
// Bump the sheet number
sn++
// Generate the title
var sheetName string
switch ht {
case DcServiceNameNoteDiscovery:
sheetName = fmt.Sprintf("Discover%d", sn)
case DcServiceNameNoteboard:
sheetName = fmt.Sprintf("Noteboard%d", sn)
case DcServiceNameNotehandlerTCP:
sheetName = fmt.Sprintf("Handler%d", sn)
default:
sheetName = fmt.Sprintf("%s%d", ht, sn)
}
// Generate the sheet for this service instance
response = sheetAddTab(f, sheetName, siid, ss, handlers[siid], hs.Stats[siid])
if response != "" {
break
}
}
return
}
// Generate a sheet for this host
func sheetGetHostStats(hostname string, hostaddr string) (response string) {
// Update with the most recent stats, ignoring errors
if sheetTrace {
fmt.Printf("sheetGetHostStats: get stats for %s\n", hostname)
}
ss, handlers, err := statsUpdateHost(hostname, hostaddr, false)
if err != nil {
response = fmt.Sprintf("sheetGetHostStats: error updating %s: %s\n", hostname, err)
return
}
// Get the entire set of stats available in-memory
if sheetTrace {
fmt.Printf("sheetGetHostStats: extract stats (%d handlers)\n", len(handlers))
}
hs, exists := statsExtract(hostname, 0, 0)
if !exists {
response = fmt.Sprintf("unknown host: %s", hostname)
return
}
if sheetTrace {
fmt.Printf("sheetGetHostStats: extracted and retrieved stats from %d handlers\n", len(hs.Stats))
}
// Create a new spreadsheet
f := excelize.NewFile()
// Generate the summary tab
sheetAddTab(f, "Summary", "summary", ss, AppHandler{}, statsAggregateAsStatsStat(hs.Stats, hs.BucketMins*60))
// Generate a page within the sheet for each service instance
if response == "" {
response = sheetAddTabs(DcServiceNameNotehandlerTCP, &hs, ss, handlers, f)
}
if response == "" {
response = sheetAddTabs(DcServiceNameNoteDiscovery, &hs, ss, handlers, f)
}
if response == "" {
response = sheetAddTabs(DcServiceNameNoteboard, &hs, ss, handlers, f)
}
if response == "" {
response = sheetAddTabs("", &hs, ss, handlers, f)
}
if response != "" {
return
}
// Delete the default sheet
f.DeleteSheet("Sheet1")
// Save the spreadsheet to a temp file
if sheetTrace {
fmt.Printf("sheetGetHostStats: saving sheet\n")
}
hostCleaned := strings.TrimSuffix(hostaddr, ".blues.tools")
hostCleaned = strings.TrimPrefix(hostCleaned, "api.")
hostCleaned = strings.TrimPrefix(hostCleaned, "a.")
hostCleaned = strings.TrimPrefix(hostCleaned, "i.")
if hostCleaned == "notefile.net" {
hostCleaned = "prod"
}
filename := fmt.Sprintf("%s-%s.xlsx", hostCleaned, time.Now().UTC().Format("20060102-150405"))
err = f.SaveAs(configDataDirectory + filename)
if err != nil {
return err.Error()
}
// Change file permissions to 444 so we can read it
err = os.Chmod(configDataDirectory+filename, 0444)
if err != nil {
return err.Error()
}
// Generate response
response += "```"
response += fmt.Sprintf(" host: %s\n", hostCleaned)
response += fmt.Sprintf(" version: %s\n", ss.ServiceVersion)
response += fmt.Sprintf(" nodes: %d\n", len(ss.ServiceInstanceIDs))
response += fmt.Sprintf(" handlers: %d (continuous:%d notification:%d ephemeral:%d discovery:%d)\n",
ss.ContinuousHandlers+ss.NotificationHandlers+ss.EphemeralHandlers+ss.DiscoveryHandlers,
ss.ContinuousHandlers, ss.NotificationHandlers, ss.EphemeralHandlers, ss.DiscoveryHandlers)
response += "```" + "\n"
response += fmt.Sprintf("<%s%s%s|%s>", Config.HostURL, sheetRoute, filename, filename)
// Done
if sheetTrace {
fmt.Printf("sheetGetHostStats: done\n")
}
return
}
// Add the stats for a service instance as a tabbed sheet within the xlsx
func sheetAddTab(f *excelize.File, sheetName string, siid string, ss serviceSummary, handler AppHandler, stats []StatsStat) (errstr string) {
// Determine if summary sheet, for special treatment
isSummarySheet := siid == "summary"
// Debug
fmt.Printf("sheet: adding '%s'\n", sheetName)
// Generate the sheet
f.NewSheet(sheetName)
// Generate styles
styleMetric, _ := f.NewStyle(`{"font":{"color":"00007f"}}`)
styleCategory, _ := f.NewStyle(`{"font":{"color":"ff0000","bold":true,"italic":true}}`)
styleSubcategory, _ := f.NewStyle(`{"font":{"color":"007f00","bold":true,"italic":false}}`)
styleRightAligned, _ := f.NewStyle(`{"alignment":{"horizontal":"right"}}`)
styleLeftAligned, _ := f.NewStyle(`{"alignment":{"horizontal":"left"}}`)
// Base for dynamic info
row := 1
col := 1
colname, _ := excelize.ColumnNumberToName(col)
f.SetColWidth(sheetName, colname, colname, 32)
// Freeze panes
f.SetPanes(sheetName, `{"freeze":true,"x_split":1,"y_split":2,"top_left_cell":"B3","active_pane":"bottomRight","panes":[{"pane":"topLeft"},{"pane":"topRight"},{"pane":"bottomLeft"},{"active_cell":"B3", "sqref":"B3", "pane":"bottomRight"}]}`)
// Node info
f.SetCellValue(sheetName, cell(col, row), "Node")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleCategory)
f.SetCellValue(sheetName, cell(col+1, row), siid)
row++
f.SetCellValue(sheetName, cell(col, row), "Version")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleCategory)
f.SetCellValue(sheetName, cell(col+1, row), ss.ServiceVersion)
row++
if !isSummarySheet {
f.SetCellValue(sheetName, cell(col, row), "Node Tags")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleCategory)
s := ""
for _, t := range handler.NodeTags {
if !strings.Contains(t, "/") {
if s != "" {
s += ", "
}
s += t
}
}
f.SetCellValue(sheetName, cell(col+1, row), s)
}
row++
if !isSummarySheet {
f.SetCellValue(sheetName, cell(col, row), "Started")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleCategory)
if handler.NodeStarted == 0 {
f.SetCellValue(sheetName, cell(col+1, row), "unknown")
} else {
f.SetCellValue(sheetName, cell(col+1, row), time.Unix(handler.NodeStarted, 0).Format("01-02 15:04:05"))
}
}
row++
if !isSummarySheet {
f.SetCellValue(sheetName, cell(col, row), "IPv4")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleCategory)
f.SetCellValue(sheetName, cell(col+1, row), handler.Ipv4)
f.SetCellValue(sheetName, cell(col+2, row), "tcp")
f.SetCellStyle(sheetName, cell(col+2, row), cell(col+2, row), styleRightAligned)
f.SetCellValue(sheetName, cell(col+3, row), handler.TCPPort)
f.SetCellStyle(sheetName, cell(col+3, row), cell(col+3, row), styleLeftAligned)
f.SetCellValue(sheetName, cell(col+4, row), "tcps")
f.SetCellStyle(sheetName, cell(col+4, row), cell(col+4, row), styleRightAligned)
f.SetCellValue(sheetName, cell(col+5, row), handler.TCPSPort)
f.SetCellStyle(sheetName, cell(col+5, row), cell(col+5, row), styleLeftAligned)
f.SetCellValue(sheetName, cell(col+6, row), "http")
f.SetCellStyle(sheetName, cell(col+6, row), cell(col+6, row), styleRightAligned)
f.SetCellValue(sheetName, cell(col+7, row), handler.HTTPPort)
f.SetCellStyle(sheetName, cell(col+7, row), cell(col+7, row), styleLeftAligned)
f.SetCellValue(sheetName, cell(col+8, row), "https")
f.SetCellStyle(sheetName, cell(col+8, row), cell(col+8, row), styleRightAligned)
f.SetCellValue(sheetName, cell(col+9, row), handler.HTTPSPort)
f.SetCellStyle(sheetName, cell(col+9, row), cell(col+9, row), styleLeftAligned)
}
row++
if !isSummarySheet {
f.SetCellValue(sheetName, cell(col, row), "Public IPv4")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleCategory)
f.SetCellValue(sheetName, cell(col+1, row), handler.PublicIpv4)
}
row++
row++
// Exit if no stats
if len(stats) == 0 {
return
}
// Bucket parameters are assumed to be uniform
buckets := len(stats)
bucketMins := int(ss.BucketSecs / 60)
// OS stats
f.SetCellValue(sheetName, cell(col, row), "OS (MiB)")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleCategory)
timeHeader(f, sheetName, col+1, row, bucketMins, buckets)
row++
f.SetCellValue(sheetName, cell(col, row), "sampled UTC")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
if stat.SnapshotTaken != 0 {
f.SetCellValue(sheetName, cell(col+1+i, row), time.Unix(stat.SnapshotTaken, 0))
colname, _ := excelize.ColumnNumberToName(col + 1 + i)
f.SetColWidth(sheetName, colname, colname, 13)
}
}
row++
f.SetCellValue(sheetName, cell(col, row), "malloc mb")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
if stat.OSMemTotal != 0 {
f.SetCellValue(sheetName, cell(col+1+i, row), (stat.OSMemTotal-stat.OSMemFree)/(1024*1024))
}
}
row++
f.SetCellValue(sheetName, cell(col, row), "mtotal mb")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
if stat.OSMemTotal != 0 {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.OSMemTotal/(1024*1024))
}
}
row++
f.SetCellValue(sheetName, cell(col, row), "diskrd")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.OSDiskRead/(1024*1024))
}
row++
f.SetCellValue(sheetName, cell(col, row), "diskwr")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.OSDiskWrite/(1024*1024))
}
row++
f.SetCellValue(sheetName, cell(col, row), "netrcv mb")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.OSNetReceived/(1024*1024))
}
row++
f.SetCellValue(sheetName, cell(col, row), "netsnd mb")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.OSNetSent/(1024*1024))
}
row++
f.SetCellValue(sheetName, cell(col, row), "httpcon")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.HttpConnTotal)
}
row++
f.SetCellValue(sheetName, cell(col, row), "httpconru")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.HttpConnReused)
}
row++
row++
// Handler stats
f.SetCellValue(sheetName, cell(col, row), "Total Handlers Active")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleCategory)
timeHeader(f, sheetName, col+1, row, bucketMins, buckets)
row++
f.SetCellValue(sheetName, cell(col, row), "continuous")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.ContinuousHandlersDeactivated)
}
row++
f.SetCellValue(sheetName, cell(col, row), "notification")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.NotificationHandlersDeactivated)
}
row++
f.SetCellValue(sheetName, cell(col, row), "ephemeral")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.EphemeralHandlersDeactivated)
}
row++
f.SetCellValue(sheetName, cell(col, row), "discovery")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.DiscoveryHandlersDeactivated)
}
row++
row++
// Handler stats
f.SetCellValue(sheetName, cell(col, row), "Handlers Activated in Period")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleCategory)
timeHeader(f, sheetName, col+1, row, bucketMins, buckets)
row++
f.SetCellValue(sheetName, cell(col, row), "continuous")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.ContinuousHandlersActivated)
}
row++
f.SetCellValue(sheetName, cell(col, row), "notification")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.NotificationHandlersActivated)
}
row++
f.SetCellValue(sheetName, cell(col, row), "ephemeral")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.EphemeralHandlersActivated)
}
row++
f.SetCellValue(sheetName, cell(col, row), "discovery")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.DiscoveryHandlersActivated)
}
row++
row++
// Event stats
f.SetCellValue(sheetName, cell(col, row), "Events")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleCategory)
timeHeader(f, sheetName, col+1, row, bucketMins, buckets)
row++
f.SetCellValue(sheetName, cell(col, row), "queued")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.EventsEnqueued)
}
row++
f.SetCellValue(sheetName, cell(col, row), "routed")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.EventsRouted)
}
row++
row++
// Fatals stats
km := map[string]bool{}
for _, stat := range stats {
for k := range stat.Fatals {
km[k] = true
}
}
keys := make([]string, 0, len(km))
for k := range km {
keys = append(keys, k)
}
sort.Strings(keys)
if len(keys) > 0 {
f.SetCellValue(sheetName, cell(col, row), "Fatals")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleCategory)
timeHeader(f, sheetName, col+1, row, bucketMins, buckets)
row++
}
for _, k := range keys {
f.SetCellValue(sheetName, cell(col, row), k)
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleSubcategory)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.Fatals[k])
}
row++
}
if len(keys) > 0 {
row++
}
// Cache stats
km = map[string]bool{}
for _, stat := range stats {
for k := range stat.Caches {
km[k] = true
}
}
keys = make([]string, 0, len(km))
for k := range km {
keys = append(keys, k)
}
sort.Strings(keys)
if len(keys) > 0 {
f.SetCellValue(sheetName, cell(col, row), "Caches")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleCategory)
row++
}
for _, k := range keys {
row++
f.SetCellValue(sheetName, cell(col, row), k+" cache")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleSubcategory)
timeHeader(f, sheetName, col+1, row, bucketMins, buckets)
row++
f.SetCellValue(sheetName, cell(col, row), "refreshed")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.Caches[k].Invalidations)
}
row++
f.SetCellValue(sheetName, cell(col, row), "entries")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.Caches[k].Entries)
}
row++
f.SetCellValue(sheetName, cell(col, row), "entriesHWM")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.Caches[k].EntriesHWM)
}
row++
}
if len(keys) > 0 {
row++
}
// API stats
km = map[string]bool{}
for _, stat := range stats {
for k := range stat.API {
km[k] = true
}
}
keys = make([]string, 0, len(km))
for k := range km {
keys = append(keys, k)
}
sort.Strings(keys)
if len(keys) > 0 {
f.SetCellValue(sheetName, cell(col, row), "API")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleCategory)
row++
}
for _, k := range keys {
row++
f.SetCellValue(sheetName, cell(col, row), k)
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleSubcategory)
row++
f.SetCellValue(sheetName, cell(col, row), "api")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
timeHeader(f, sheetName, col+1, row, bucketMins, buckets)
row++
f.SetCellValue(sheetName, cell(col, row), "calls")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.API[k])
}
}
if len(keys) > 0 {
row++
row++
}
// Database stats (display the ones beginning with "app" at the end)
kmApps := map[string]bool{}
kmNonApps := map[string]bool{}
for _, stat := range stats {
for k := range stat.Databases {
if strings.HasPrefix(k, "app:") {
kmApps[k] = true
} else {
kmNonApps[k] = true
}
}
}
apps := make([]string, 0, len(kmApps))
for k := range kmApps {
apps = append(apps, k)
}
sort.Strings(apps)
nonapps := make([]string, 0, len(kmNonApps))
for k := range kmNonApps {
nonapps = append(nonapps, k)
}
sort.Strings(nonapps)
keys = append(nonapps, apps...)
if len(keys) > 0 {
f.SetCellValue(sheetName, cell(col, row), "Databases")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleCategory)
row++
}
for _, k := range keys {
row++
f.SetCellValue(sheetName, cell(col, row), k)
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleSubcategory)
row++
f.SetCellValue(sheetName, cell(col, row), "database")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
timeHeader(f, sheetName, col+1, row, bucketMins, buckets)
row++
f.SetCellValue(sheetName, cell(col, row), "queries")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.Databases[k].Reads)
}
row++
f.SetCellValue(sheetName, cell(col, row), "execs")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.Databases[k].Writes)
}
row++
f.SetCellValue(sheetName, cell(col, row), "queryMsAvg")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.Databases[k].ReadMs)
}
row++
f.SetCellValue(sheetName, cell(col, row), "execMsAvg")
f.SetCellStyle(sheetName, cell(col, row), cell(col, row), styleMetric)
for i, stat := range stats {
f.SetCellValue(sheetName, cell(col+1+i, row), stat.Databases[k].WriteMs)
}
row++
}
if len(keys) > 0 {
row++
}
// Done
return
}
// Generate an uptime string
func uptimeStr(started int64, now int64) (s string) {
uptimeSecs := now - started
if uptimeSecs < 0 {
uptimeSecs = -uptimeSecs
}
uptimeDays := uptimeSecs / (24 * 60 * 60)
uptimeSecs -= uptimeDays * (24 * 60 * 60)
uptimeHours := uptimeSecs / (60 * 60)
uptimeSecs -= uptimeHours * (60 * 60)
uptimeMins := uptimeSecs / 60
uptimeSecs -= uptimeMins * 60
if uptimeDays > 0 {
s = fmt.Sprintf("%dd %dh %dm", uptimeDays, uptimeHours, uptimeMins)
} else if uptimeHours > 0 {
s = fmt.Sprintf("%dh %dm", uptimeHours, uptimeMins)
} else {
s = fmt.Sprintf("%dm", uptimeMins)
}
return s
}
// Get the cell address for a given 1-based coordinate
func cell(col int, row int) string {
cell, _ := excelize.CoordinatesToCellName(col, row)
return cell
}
// Generate a time header at the specified col/row
func timeHeader(f *excelize.File, sheetName string, col int, row int, bucketMins int, buckets int) {
style, _ := f.NewStyle(`{"alignment":{"horizontal":"right"},"font":{"color":"0000ff","bold":true,"italic":true}}`)
for i := 0; i < buckets; i++ {
f.SetCellValue(sheetName, cell(col+i, row), uptimeStr(0, (int64(i)+1)*int64(bucketMins)*60))
f.SetCellStyle(sheetName, cell(col+i, row), cell(col+i, row), style)
}
}