Skip to content

Commit

Permalink
fixed metrics bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jabelone committed Jun 1, 2024
1 parent 4a8fd13 commit bfd7f5c
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 24 deletions.
24 changes: 24 additions & 0 deletions memberportal/api_general/tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from celery import signals
import logging

logger = logging.getLogger("api_general:tasks")


@signals.task_failure.connect
@signals.task_revoked.connect
def on_task_failure(**kwargs):
"""Abort transaction on task errors."""
# celery exceptions will not be published to `sys.excepthook`. therefore we have to create another handler here.
from traceback import format_tb

logger.error(
"[task:%s:%s]"
% (
kwargs.get("task_id"),
kwargs["sender"].request.correlation_id,
)
+ "\n"
+ "".join(format_tb(kwargs.get("traceback", [])))
+ "\n"
+ str(kwargs.get("exception", ""))
)
33 changes: 15 additions & 18 deletions memberportal/api_metrics/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from django.db.models import Count
from constance import config
import logging
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync

logger = logging.getLogger("celery:api_metrics")

Expand All @@ -28,29 +26,28 @@ def calculate_metrics():

# get the count of all the different member profile states
logger.debug("Calculating member count total")
profile_states = (
Profile.objects.all()
.values("state")
.annotate(total=Count("state"))
.order_by("total")
)
profile_states = []

for state in (
Profile.objects.values("state").annotate(count=Count("pk")).order_by("count")
):
profile_states.append({"state": state["state"], "total": state["count"]})

Metric.objects.create(
name=Metric.MetricName.MEMBER_COUNT_TOTAL, data=profile_states
).full_clean()

# get the count of all the different subscription states
logger.debug("Calculating subscription count total")
subscription_states = (
Profile.objects.all()
.values("subscription_status")
.annotate(total=Count("subscription_status"))
.order_by("total")
)
subscription_states_data = [
{"state": item["subscription_status"], "total": item["total"]}
for item in subscription_states
]
subscription_states_data = []
for state in (
Profile.objects.values("subscription_status")
.annotate(count=Count("pk"))
.order_by("count")
):
subscription_states_data.append(
{"state": state["subscription_status"], "total": state["count"]}
)
Metric.objects.create(
name=Metric.MetricName.SUBSCRIPTION_COUNT_TOTAL,
data=subscription_states_data,
Expand Down
8 changes: 2 additions & 6 deletions memberportal/api_metrics/views.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import logging
import json

from django.db.models import Count
from rest_framework import permissions

import api_metrics.metrics
from api_metrics.models import Metric
from api_general.models import SiteSession

from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import permissions
import logging

logger = logging.getLogger("metrics")

Expand Down
25 changes: 25 additions & 0 deletions memberportal/membermatters/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,31 @@
"level": os.environ.get("MM_LOG_LEVEL_SMS", "INFO"),
"propagate": False,
},
"api_general:tasks": {
"handlers": ["console", "file"],
"level": os.environ.get("MM_LOG_LEVEL_GENERAL_TASKS", "INFO"),
"propagate": False,
},
"celery:api_metrics": {
"handlers": ["console", "file"],
"level": os.environ.get("MM_LOG_LEVEL_CELERY_METRICS", "INFO"),
"propagate": False,
},
"metrics": {
"handlers": ["console", "file"],
"level": os.environ.get("MM_LOG_LEVEL_METRICS", "INFO"),
"propagate": False,
},
"celery:celeryapp": {
"handlers": ["console", "file"],
"level": os.environ.get("MM_LOG_LEVEL_CELERY_APP", "INFO"),
"propagate": False,
},
"oidc_provider": {
"handlers": ["console", "file"],
"level": os.environ.get("MM_LOG_LEVEL_OIDC_PROVIDER", "INFO"),
"propagate": False,
},
"daphne": {"handlers": ["console", "file"], "level": "WARNING"},
},
}
Expand Down

0 comments on commit bfd7f5c

Please sign in to comment.