@@ -2,6 +2,7 @@ package insights
2
2
3
3
import (
4
4
"context"
5
+ "sync/atomic"
5
6
"time"
6
7
7
8
"github.com/google/uuid"
@@ -24,6 +25,16 @@ type MetricsCollector struct {
24
25
database database.Store
25
26
logger slog.Logger
26
27
duration time.Duration
28
+
29
+ data atomic.Pointer [insightsData ]
30
+ }
31
+
32
+ type insightsData struct {
33
+ templates []database.GetTemplateInsightsByTemplateRow
34
+ apps []database.GetTemplateAppInsightsRow
35
+ parameters []database.GetTemplateParameterInsightsRow
36
+
37
+ templateNames map [uuid.UUID ]string
27
38
}
28
39
29
40
var _ prometheus.Collector = new (MetricsCollector )
@@ -59,7 +70,7 @@ func (mc *MetricsCollector) Run(ctx context.Context) (func(), error) {
59
70
60
71
// TODO collect iteration time
61
72
62
- var userActivity []database.GetUserActivityInsightsRow
73
+ var templateInsights []database.GetTemplateInsightsByTemplateRow
63
74
var appInsights []database.GetTemplateAppInsightsRow
64
75
var parameterInsights []database.GetTemplateParameterInsightsRow
65
76
@@ -69,7 +80,7 @@ func (mc *MetricsCollector) Run(ctx context.Context) (func(), error) {
69
80
70
81
eg .Go (func () error {
71
82
var err error
72
- userActivity , err = mc .database .GetUserActivityInsights (egCtx , database.GetUserActivityInsightsParams {
83
+ templateInsights , err = mc .database .GetTemplateInsightsByTemplate (egCtx , database.GetTemplateInsightsByTemplateParams {
73
84
StartTime : startTime ,
74
85
EndTime : endTime ,
75
86
})
@@ -107,7 +118,7 @@ func (mc *MetricsCollector) Run(ctx context.Context) (func(), error) {
107
118
}
108
119
109
120
// Phase 2: Collect template IDs, and fetch relevant details
110
- templateIDs := uniqueTemplateIDs (userActivity , appInsights , parameterInsights )
121
+ templateIDs := uniqueTemplateIDs (templateInsights , appInsights , parameterInsights )
111
122
templates , err := mc .database .GetTemplatesWithFilter (ctx , database.GetTemplatesWithFilterParams {
112
123
IDs : templateIDs ,
113
124
})
@@ -116,8 +127,15 @@ func (mc *MetricsCollector) Run(ctx context.Context) (func(), error) {
116
127
return
117
128
}
118
129
119
- /*templateNames := */
120
- onlyTemplateNames (templates )
130
+ templateNames := onlyTemplateNames (templates )
131
+
132
+ mc .data .Store (& insightsData {
133
+ templates : templateInsights ,
134
+ apps : appInsights ,
135
+ parameters : parameterInsights ,
136
+
137
+ templateNames : templateNames ,
138
+ })
121
139
}
122
140
123
141
go func () {
@@ -147,17 +165,24 @@ func (*MetricsCollector) Describe(descCh chan<- *prometheus.Desc) {
147
165
func (mc * MetricsCollector ) Collect (metricsCh chan <- prometheus.Metric ) {
148
166
// Phase 3: Collect metrics
149
167
150
- // TODO
168
+ data := mc .data .Load ()
169
+ if data == nil {
170
+ return // insights data not loaded yet
171
+ }
172
+
173
+ for _ , templateRow := range data .templates {
174
+ metricsCh <- prometheus .MustNewConstMetric (activeUsersDesc , prometheus .GaugeValue , float64 (templateRow .ActiveUsers ), data .templateNames [templateRow .TemplateID ])
175
+
176
+ // TODO applicationsUsageSeconds, parameters
177
+ }
151
178
}
152
179
153
180
// Helper functions below.
154
181
155
- func uniqueTemplateIDs (userActivity []database.GetUserActivityInsightsRow , appInsights []database.GetTemplateAppInsightsRow , parameterInsights []database.GetTemplateParameterInsightsRow ) []uuid.UUID {
182
+ func uniqueTemplateIDs (templateInsights []database.GetTemplateInsightsByTemplateRow , appInsights []database.GetTemplateAppInsightsRow , parameterInsights []database.GetTemplateParameterInsightsRow ) []uuid.UUID {
156
183
tids := map [uuid.UUID ]bool {}
157
- for _ , t := range userActivity {
158
- for _ , tid := range t .TemplateIDs {
159
- tids [tid ] = true
160
- }
184
+ for _ , t := range templateInsights {
185
+ tids [t .TemplateID ] = true
161
186
}
162
187
163
188
for _ , a := range appInsights {
0 commit comments