-
Notifications
You must be signed in to change notification settings - Fork 13
/
metrics.go
76 lines (57 loc) · 1.68 KB
/
metrics.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
package metrics
import (
"context"
"encoding/json"
"github.com/latolukasz/beeorm"
"github.com/coretrix/hitrix/pkg/dto/metrics"
"github.com/coretrix/hitrix/pkg/entity"
"github.com/coretrix/hitrix/service"
)
func Get(ctx context.Context) map[string]metrics.Series {
configService := service.DI().Config()
xAxisTitle, has := configService.StringMap("metrics.xAxis.title")
if !has {
panic("Metrics xAxisTitle are required")
}
ormService := service.DI().OrmEngineForContext(ctx)
query := beeorm.NewWhere("1 ORDER BY ID DESC")
pager := beeorm.NewPager(1, 10000)
var allMetricsEntities []*entity.MetricsEntity
for {
var metricsEntities []*entity.MetricsEntity
ormService.Search(query, pager, &metricsEntities)
allMetricsEntities = append(allMetricsEntities, metricsEntities...)
if len(metricsEntities) < pager.PageSize {
break
}
pager.IncrementPage()
}
result := map[string]metrics.Series{}
//{
// "Memory" : {"admin-api: [{date, val}]
// }
for _, metricsEntity := range allMetricsEntities {
memStats := &map[string]interface{}{}
err := json.Unmarshal([]byte(metricsEntity.Metrics), memStats)
if err != nil {
panic(err)
}
for k, v := range *memStats {
if _, ok := result[k]; !ok {
result[k] = metrics.Series{
XAxisTitle: xAxisTitle[k],
Data: map[string][]metrics.Row{},
}
}
if _, ok := result[k].Data[metricsEntity.AppName]; !ok {
result[k].Data[metricsEntity.AppName] = make([]metrics.Row, 0)
} else {
result[k].Data[metricsEntity.AppName] = append(result[k].Data[metricsEntity.AppName], metrics.Row{
Value: v,
CreatedAt: metricsEntity.CreatedAt.UnixMilli(),
})
}
}
}
return result
}