Skip to content

Commit

Permalink
feat: Add feedback anonymity metric to Grafana dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
zusorio committed Oct 10, 2024
1 parent 874a04f commit 736c25b
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 15 deletions.
22 changes: 18 additions & 4 deletions backend/capellacollab/feedback/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,27 @@

def count_feedback_by_rating(
db: orm.Session,
) -> dict[models.FeedbackRating, int]:
) -> dict[tuple[models.FeedbackRating, bool], int]:
return {
value[0]: value[1]
(value[0], value[1]): value[2]
for value in db.execute(
sa.select(
models.DatabaseFeedback.rating, sa.func.count()
).group_by(models.DatabaseFeedback.rating)
models.DatabaseFeedback.rating,
sa.case(
# pylint: disable=singleton-comparison
(models.DatabaseFeedback.user_id != None, False),
else_=True,
).label("anonymous"),
# pylint: disable=not-callable
sa.func.count(),
).group_by(
models.DatabaseFeedback.rating,
sa.case(
# pylint: disable=singleton-comparison
(models.DatabaseFeedback.user_id != None, False),
else_=True,
),
)
).all()
}

Expand Down
9 changes: 7 additions & 2 deletions backend/capellacollab/feedback/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ def collect(self) -> t.Iterable[prometheus_client.core.Metric]:
metric = prometheus_client.core.GaugeMetricFamily(
"feedback_count",
"Submitted feedback",
labels=["rating"],
labels=["rating", "anonymous"],
)

with database.SessionLocal() as db:
feedback = crud.count_feedback_by_rating(db)

for rating in models.FeedbackRating:
metric.add_metric([str(rating.value)], feedback.get(rating, 0))
metric.add_metric(
[str(rating.value), "true"], feedback.get((rating, True), 0)
)
metric.add_metric(
[str(rating.value), "false"], feedback.get((rating, False), 0)
)

yield metric

Expand Down
4 changes: 2 additions & 2 deletions backend/tests/test_feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,10 @@ def test_feedback_metric(db: orm.Session):
collector = feedback_metrics.FeedbackCollector()
data = list(collector.collect())

assert len(data[0].samples) == 3
assert len(data[0].samples) == 6

bad_sample = data[0].samples[0]
good_sample = data[0].samples[2]
good_sample = data[0].samples[4]

assert bad_sample.labels["rating"] == "bad"
assert bad_sample.name == "feedback_count"
Expand Down
98 changes: 91 additions & 7 deletions helm/config/grafana/dashboards/feedback.json
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@
"id": 13,
"options": {
"legend": {
"calcs": ["lastNotNull"],
"calcs": [
"lastNotNull"
],
"displayMode": "list",
"placement": "bottom",
"showLegend": false
Expand Down Expand Up @@ -259,13 +261,15 @@
},
"gridPos": {
"h": 6,
"w": 5,
"w": 4,
"x": 0,
"y": 8
},
"id": 8,
"options": {
"displayLabels": ["percent"],
"displayLabels": [
"percent"
],
"legend": {
"displayMode": "list",
"placement": "bottom",
Expand All @@ -274,7 +278,9 @@
},
"pieType": "pie",
"reduceOptions": {
"calcs": ["lastNotNull"],
"calcs": [
"lastNotNull"
],
"fields": "",
"values": false
},
Expand Down Expand Up @@ -333,8 +339,8 @@
},
"gridPos": {
"h": 6,
"w": 5,
"x": 5,
"w": 3,
"x": 4,
"y": 8
},
"id": 12,
Expand All @@ -343,7 +349,9 @@
"minVizWidth": 75,
"orientation": "auto",
"reduceOptions": {
"calcs": ["lastNotNull"],
"calcs": [
"lastNotNull"
],
"fields": "",
"values": false
},
Expand Down Expand Up @@ -378,6 +386,82 @@
],
"type": "gauge"
},
{
"datasource": {
"type": "prometheus",
"uid": "prometheus_ccm"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "thresholds"
},
"fieldMinMax": false,
"mappings": [],
"max": 100,
"min": 0,
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 50
}
]
},
"unit": "percent"
},
"overrides": []
},
"gridPos": {
"h": 6,
"w": 3,
"x": 7,
"y": 8
},
"id": 15,
"options": {
"minVizHeight": 75,
"minVizWidth": 75,
"orientation": "auto",
"reduceOptions": {
"calcs": [
"lastNotNull"
],
"fields": "",
"values": false
},
"showThresholdLabels": false,
"showThresholdMarkers": false,
"sizing": "auto",
"text": {}
},
"pluginVersion": "11.1.0",
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "prometheus_ccm"
},
"disableTextWrap": false,
"editorMode": "code",
"expr": "(sum(feedback_count{anonymous=\"True\"}) / sum(feedback_count)) * 100",
"fullMetaSearch": false,
"includeNullMetadata": true,
"instant": false,
"legendFormat": "__auto",
"range": true,
"refId": "A",
"useBackend": false
}
],
"title": "Anonymity Index",
"type": "gauge"
},
{
"datasource": {
"type": "prometheus",
Expand Down

0 comments on commit 736c25b

Please sign in to comment.