@@ -153,51 +153,23 @@ func initObservationScraper(db *db.Database, logger *zap.Logger) {
153
153
case <- ctx .Done ():
154
154
return nil
155
155
case <- t .C :
156
- messages , err := db .QueryMessagesByIndex (false , common . ExpiryDuration )
156
+ messages , err := db .QueryMessagesByIndex (false , 15 * time . Second )
157
157
if err != nil {
158
158
logger .Error ("QueryMessagesByIndex error" , zap .Error (err ))
159
159
continue
160
160
}
161
161
162
162
// Tally the number of messages for each chain
163
- messagesPerChain := make (map [string ]int )
164
- for _ , message := range messages {
165
- chainID := message .MessageID .ChainID ()
166
- messagesPerChain [chainID ]++
167
- }
163
+ messagesPerChain := historical_uptime .TallyMessagesPerChain (messages )
168
164
169
165
// Initialize the missing observations count for each guardian for each chain
170
- guardianMissingObservations := make (map [string ]map [string ]int )
171
- for _ , message := range messages {
172
- chainID := message .MessageID .ChainID ()
173
- for _ , guardianName := range common .GetGuardianIndexToNameMap () {
174
- if guardianMissingObservations [guardianName ] == nil {
175
- guardianMissingObservations [guardianName ] = make (map [string ]int )
176
- }
177
- // Initialize the count for this chain for each guardian with the number of messages for that chain
178
- guardianMissingObservations [guardianName ][chainID ] = messagesPerChain [chainID ]
179
- }
180
- }
166
+ guardianMissingObservations := historical_uptime .InitializeMissingObservationsCount (messages , messagesPerChain )
181
167
182
168
// Decrement the missing observations count for each observed message
183
- for _ , message := range messages {
184
- chainID := message .MessageID .ChainID ()
185
- for _ , observation := range message .Observations {
186
- guardianName , ok := common .GetGuardianName (observation .GuardianID )
187
- if ! ok {
188
- logger .Error ("Unknown guardian address" , zap .String ("guardianAddr" , observation .GuardianID ))
189
- continue
190
- }
191
- guardianMissingObservations [guardianName ][chainID ]--
192
- }
193
- }
169
+ historical_uptime .DecrementMissingObservationsCount (logger , guardianMissingObservations , messages )
194
170
195
171
// Update the metrics with the final count of missing observations
196
- for guardianName , chains := range guardianMissingObservations {
197
- for chainID , missingCount := range chains {
198
- guardianMissedObservations .WithLabelValues (guardianName , chainID ).Add (float64 (missingCount ))
199
- }
200
- }
172
+ historical_uptime .UpdateMetrics (guardianMissedObservations , guardianMissingObservations )
201
173
}
202
174
}
203
175
})
0 commit comments