-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1858 from DSD-DBS/store-feedback
feat(monitoring): Add Grafana dashboard for feedback
- Loading branch information
Showing
16 changed files
with
719 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
backend/capellacollab/alembic/versions/7cf3357ddd7b_add_feedback_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
"""Add feedback table | ||
Revision ID: 7cf3357ddd7b | ||
Revises: abddaf015966 | ||
Create Date: 2024-09-30 19:47:36.253187 | ||
""" | ||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "7cf3357ddd7b" | ||
down_revision = "abddaf015966" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.create_table( | ||
"feedback", | ||
sa.Column("id", sa.Integer(), nullable=False), | ||
sa.Column( | ||
"rating", | ||
sa.Enum("BAD", "OKAY", "GOOD", name="feedbackrating"), | ||
nullable=False, | ||
), | ||
sa.Column("feedback_text", sa.String(), nullable=True), | ||
sa.Column("user_id", sa.Integer(), nullable=True), | ||
sa.Column("trigger", sa.String(), nullable=True), | ||
sa.Column("created_at", sa.DateTime(), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["user_id"], | ||
["users.id"], | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
op.create_index(op.f("ix_feedback_id"), "feedback", ["id"], unique=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import datetime | ||
|
||
import sqlalchemy as sa | ||
from sqlalchemy import orm | ||
|
||
from capellacollab.users import models as users_models | ||
|
||
from . import models | ||
|
||
|
||
def count_feedback_by_rating( | ||
db: orm.Session, | ||
) -> dict[models.FeedbackRating, int]: | ||
return { | ||
value[0]: value[1] | ||
for value in db.execute( | ||
sa.select( | ||
models.DatabaseFeedback.rating, sa.func.count() | ||
).group_by(models.DatabaseFeedback.rating) | ||
).all() | ||
} | ||
|
||
|
||
def save_feedback( | ||
db: orm.Session, | ||
rating: models.FeedbackRating, | ||
user: users_models.DatabaseUser | None, | ||
feedback_text: str | None, | ||
created_at: datetime.datetime, | ||
trigger: str | None, | ||
) -> models.DatabaseFeedback: | ||
model = models.DatabaseFeedback( | ||
rating=rating, | ||
user=user, | ||
feedback_text=feedback_text, | ||
created_at=created_at, | ||
trigger=trigger, | ||
) | ||
|
||
db.add(model) | ||
db.commit() | ||
db.refresh(model) | ||
return model | ||
|
||
|
||
def anonymize_feedback_of_user( | ||
db: orm.Session, user: users_models.DatabaseUser | ||
): | ||
db.execute( | ||
sa.update(models.DatabaseFeedback) | ||
.where(models.DatabaseFeedback.user_id == user.id) | ||
.values(user_id=None) | ||
) | ||
db.commit() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# SPDX-FileCopyrightText: Copyright DB InfraGO AG and contributors | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import typing as t | ||
|
||
import prometheus_client | ||
import prometheus_client.core | ||
from prometheus_client import registry as prometheus_registry | ||
|
||
from capellacollab.core import database | ||
|
||
from . import crud, models | ||
|
||
|
||
class FeedbackCollector(prometheus_registry.Collector): | ||
def collect(self) -> t.Iterable[prometheus_client.core.Metric]: | ||
metric = prometheus_client.core.GaugeMetricFamily( | ||
"feedback_count", | ||
"Submitted feedback", | ||
labels=["rating"], | ||
) | ||
|
||
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)) | ||
|
||
yield metric | ||
|
||
|
||
def register() -> None: | ||
prometheus_client.REGISTRY.register(FeedbackCollector()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.