4
4
"context"
5
5
"time"
6
6
7
+ "github.com/google/uuid"
7
8
"github.com/prometheus/client_golang/prometheus"
8
9
"golang.org/x/sync/errgroup"
9
10
"golang.org/x/xerrors"
@@ -58,7 +59,7 @@ func (mc *MetricsCollector) Run(ctx context.Context) (func(), error) {
58
59
59
60
// TODO collect iteration time
60
61
61
- var templateInsights database.GetTemplateInsightsRow
62
+ var userActivity [] database.GetUserActivityInsightsRow
62
63
var appInsights []database.GetTemplateAppInsightsRow
63
64
var parameterInsights []database.GetTemplateParameterInsightsRow
64
65
@@ -68,7 +69,7 @@ func (mc *MetricsCollector) Run(ctx context.Context) (func(), error) {
68
69
69
70
eg .Go (func () error {
70
71
var err error
71
- templateInsights , err = mc .database .GetTemplateInsights (egCtx , database.GetTemplateInsightsParams {
72
+ userActivity , err = mc .database .GetUserActivityInsights (egCtx , database.GetUserActivityInsightsParams {
72
73
StartTime : startTime ,
73
74
EndTime : endTime ,
74
75
})
@@ -105,9 +106,17 @@ func (mc *MetricsCollector) Run(ctx context.Context) (func(), error) {
105
106
return
106
107
}
107
108
108
- // Phase 2: Collect resource IDs (templates, applications, parameters), and fetch relevant details
109
+ // Phase 2: Collect template IDs, and fetch relevant details
110
+ templateIDs := uniqueTemplateIDs (userActivity , appInsights , parameterInsights )
111
+ templates , err := mc .database .GetTemplatesWithFilter (ctx , database.GetTemplatesWithFilterParams {
112
+ IDs : templateIDs ,
113
+ })
114
+ if err != nil {
115
+ mc .logger .Error (ctx , "unable to fetch template details from database" , slog .Error (err ))
116
+ return
117
+ }
109
118
110
- // TODO
119
+ templateNames := onlyTemplateNames ( templates )
111
120
}
112
121
113
122
go func () {
@@ -139,3 +148,41 @@ func (mc *MetricsCollector) Collect(metricsCh chan<- prometheus.Metric) {
139
148
140
149
// TODO
141
150
}
151
+
152
+ // Helper functions below.
153
+
154
+ func uniqueTemplateIDs (userActivity []database.GetUserActivityInsightsRow , appInsights []database.GetTemplateAppInsightsRow , parameterInsights []database.GetTemplateParameterInsightsRow ) []uuid.UUID {
155
+ tids := map [uuid.UUID ]bool {}
156
+ for _ , t := range userActivity {
157
+ for _ , tid := range t .TemplateIDs {
158
+ tids [tid ] = true
159
+ }
160
+ }
161
+
162
+ for _ , a := range appInsights {
163
+ for _ , tid := range a .TemplateIDs {
164
+ tids [tid ] = true
165
+ }
166
+ }
167
+
168
+ for _ , p := range parameterInsights {
169
+ for _ , tid := range p .TemplateIDs {
170
+ tids [tid ] = true
171
+ }
172
+ }
173
+
174
+ uniqueUUIDs := make ([]uuid.UUID , len (tids ))
175
+ var i int
176
+ for t := range tids {
177
+ uniqueUUIDs [i ] = t
178
+ }
179
+ return uniqueUUIDs
180
+ }
181
+
182
+ func onlyTemplateNames (templates []database.Template ) map [uuid.UUID ]string {
183
+ m := map [uuid.UUID ]string {}
184
+ for _ , t := range templates {
185
+ m [t .ID ] = t .Name
186
+ }
187
+ return m
188
+ }
0 commit comments