Skip to content

Commit ff670df

Browse files
William Chrispwilliamchrisp
authored andcommitted
Fix guardrail_activation bug and add test
1 parent 951255c commit ff670df

File tree

3 files changed

+70
-20
lines changed

3 files changed

+70
-20
lines changed

infrastructure/dashboard.json

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
"type": "timeseries"
132132
},
133133
{
134-
"description": "Total Guardrail Activation Count",
134+
"description": "Guardrail Total Activation Count per Resource and Guardrail ID.",
135135
"fieldConfig": {
136136
"defaults": {
137137
"color": {
@@ -145,13 +145,17 @@
145145
"color": "green",
146146
"value": null
147147
},
148+
{
149+
"color": "#EAB839",
150+
"value": 25
151+
},
148152
{
149153
"color": "red",
150-
"value": 80
154+
"value": 50
151155
}
152156
]
153157
},
154-
"unit": "Activations"
158+
"unit": "none"
155159
},
156160
"overrides": []
157161
},
@@ -169,7 +173,7 @@
169173
"calcs": [
170174
"sum"
171175
],
172-
"fields": "/^measure_value::double$/",
176+
"fields": "/^total_activations$/",
173177
"values": true
174178
},
175179
"showUnfilled": true
@@ -183,15 +187,16 @@
183187
"uid": "4tnTKWb4z"
184188
},
185189
"measure": "guardrail_activation_count",
186-
"rawQuery": "SELECT \"aggregate_id\", \"measure_value\"::double FROM $__database.$__table\nWHERE \"measure_name\" = '$__measure' AND $__timeFilter\nORDER BY \"time\" ASC",
190+
"rawQuery": "SELECT \"aggregate_id\", \"guardrail_id\", MAX(\"measure_value\"::double) as \"total_activations\" FROM $__database.$__table\nWHERE \"measure_name\" = '$__measure' AND $__timeFilter\nGROUP BY \"aggregate_id\", \"guardrail_id\"\nORDER BY \"total_activations\" DESC",
187191
"refId": "A",
188192
"table": "\"metrics_table\""
189193
}
190194
],
191-
"title": "Guardrail Activation Count",
195+
"title": "Guardrail Total Activation Count",
192196
"type": "bargauge"
193197
},
194198
{
199+
"description": "Average lead time from last pertinent activation to resolution.",
195200
"fieldConfig": {
196201
"defaults": {
197202
"color": {
@@ -223,16 +228,17 @@
223228
},
224229
"id": 16,
225230
"options": {
226-
"orientation": "auto",
231+
"displayMode": "gradient",
232+
"orientation": "horizontal",
227233
"reduceOptions": {
228234
"calcs": [
229-
"mean"
235+
"lastNotNull"
230236
],
231-
"fields": "/^measure_value::double$/",
232-
"values": false
237+
"fields": "/^average_lead_time$/",
238+
"values": true
233239
},
234-
"showThresholdLabels": false,
235-
"showThresholdMarkers": true
240+
"showUnfilled": true,
241+
"text": {}
236242
},
237243
"pluginVersion": "8.4.7",
238244
"targets": [
@@ -243,13 +249,13 @@
243249
"uid": "4tnTKWb4z"
244250
},
245251
"measure": "guardrail_lead_time",
246-
"rawQuery": "SELECT \"aggregate_id\", \"measure_value\"::double FROM $__database.$__table\nWHERE \"measure_name\" = '$__measure' AND $__timeFilter\nORDER BY \"time\" ASC",
252+
"rawQuery": "SELECT \"aggregate_id\", \"guardrail_id\", AVG(\"measure_value\"::double) as \"average_lead_time\" FROM $__database.$__table\nWHERE \"measure_name\" = '$__measure' AND $__timeFilter\nGROUP BY \"aggregate_id\", \"guardrail_id\"\nORDER BY \"average_lead_time\" DESC",
247253
"refId": "A",
248254
"table": "\"metrics_table\""
249255
}
250256
],
251257
"title": "Guardrail Lead Time Average",
252-
"type": "gauge"
258+
"type": "bargauge"
253259
},
254260
{
255261
"description": "Percentage based on the number of patch compliant instances.",
@@ -588,13 +594,13 @@
588594
"list": []
589595
},
590596
"time": {
591-
"from": "now-5m",
597+
"from": "now-15m",
592598
"to": "now"
593599
},
594600
"timepicker": {},
595601
"timezone": "",
596602
"title": "Flight Controller dashboard",
597603
"uid": "test",
598-
"version": 2,
604+
"version": 10,
599605
"weekStart": ""
600-
}
606+
}

src/usecases/guardrail.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,13 @@ def handle_guardrail_passed(
6565
) -> Tuple[GuardrailPassed, List[Union[GuardrailLeadTime, GuardrailMaxActivation]]]:
6666
event = _convert_payload_to_event(event, len(aggregate_events))
6767
last_compliant_event = 0
68-
max_activations = 0
6968
activations_since_last_pass = 0
7069
for i, aggregate_event in enumerate(aggregate_events):
7170
if isinstance(aggregate_event, GuardrailPassed) and aggregate_event.payload.guardrail_id == event.payload.guardrail_id:
7271
last_compliant_event = i
7372
activations_since_last_pass = 0
7473
elif isinstance(aggregate_event, GuardrailActivated) and aggregate_event.payload.guardrail_id == event.payload.guardrail_id:
7574
activations_since_last_pass += 1
76-
max_activations = max(max_activations, activations_since_last_pass)
7775
for aggregate_event in aggregate_events[last_compliant_event:]:
7876
if isinstance(aggregate_event, GuardrailActivated) and aggregate_event.payload.guardrail_id == event.payload.guardrail_id:
7977
return (
@@ -95,7 +93,7 @@ def handle_guardrail_passed(
9593
dimension_names=["guardrail_id"],
9694
guardrail_id=event.payload.guardrail_id,
9795
),
98-
metric_value=max_activations,
96+
metric_value=activations_since_last_pass,
9997
),
10098
],
10199
)

tests/src/usecases/test_guardrail.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,49 @@ def test_guardrail_passed_returns_correct_max_activation_from_last_pertinent_act
359359
)[1][1].metric_value
360360
== 2
361361
)
362+
363+
364+
def test_guardrail_passed_with_history_returns_correct_max_activation_from_last_pertinent_activated_with_a_max_less_then_previous():
365+
second_activated_aggregate_event = GuardrailActivated(
366+
aggregate_id=activated_aggregate_event.aggregate_id,
367+
event_id=str(uuid4()),
368+
event_type="guardrail_activated",
369+
aggregate_version=2,
370+
payload=GuardrailActivatedPayload(
371+
guardrail_id=activated_aggregate_event.payload.guardrail_id,
372+
timestamp=int(activated_aggregate_event.payload.timestamp + 2),
373+
),
374+
)
375+
third_passed_aggregate_event = GuardrailPassed(
376+
aggregate_id=activated_aggregate_event.aggregate_id,
377+
event_id=str(uuid4()),
378+
event_type="guardrail_passed",
379+
aggregate_version=3,
380+
payload=GuardrailPassedPayload(
381+
guardrail_id=activated_aggregate_event.payload.guardrail_id,
382+
timestamp=int(activated_aggregate_event.payload.timestamp + 4),
383+
),
384+
)
385+
fourth_activated_aggregate_event = GuardrailActivated(
386+
aggregate_id=activated_aggregate_event.aggregate_id,
387+
event_id=str(uuid4()),
388+
event_type="guardrail_activated",
389+
aggregate_version=4,
390+
payload=GuardrailActivatedPayload(
391+
guardrail_id=activated_aggregate_event.payload.guardrail_id,
392+
timestamp=int(activated_aggregate_event.payload.timestamp + 6),
393+
),
394+
)
395+
assert (
396+
handle_guardrail_passed(
397+
passed_event,
398+
[
399+
activated_aggregate_event,
400+
second_activated_aggregate_event,
401+
third_passed_aggregate_event,
402+
fourth_activated_aggregate_event,
403+
404+
],
405+
)[1][1].metric_value
406+
== 1
407+
)

0 commit comments

Comments
 (0)