Skip to content

Commit

Permalink
Merge pull request #4149 from kobotoolbox/fix-service-usage-endpoint
Browse files Browse the repository at this point in the history
Fix service usage endpoint all time calculation
  • Loading branch information
jnm authored Nov 21, 2022
2 parents c3ba93d + 537366b commit e69efe0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
5 changes: 5 additions & 0 deletions kpi/deployment_backends/base_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ def __init__(self, asset):
def active(self):
return self.get_data('active', False)

@property
@abc.abstractmethod
def all_time_submission_count(self):
pass

@property
@abc.abstractmethod
def attachment_storage_bytes(self):
Expand Down
20 changes: 17 additions & 3 deletions kpi/deployment_backends/kobocat_backend.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# coding: utf-8

import copy
import io
import json
Expand All @@ -11,7 +10,6 @@
from typing import Generator, Optional, Union
from urllib.parse import urlparse
from xml.etree import ElementTree as ET

try:
from zoneinfo import ZoneInfo
except ImportError:
Expand All @@ -22,6 +20,7 @@
from django.core.exceptions import ImproperlyConfigured
from lxml import etree
from django.core.files import File
from django.db.models import Sum
from django.db.models.query import QuerySet
from django.utils import timezone
from django.utils.translation import gettext_lazy as t
Expand Down Expand Up @@ -75,7 +74,6 @@
from kobo.apps.subsequences.utils import stream_with_extras



class KobocatDeploymentBackend(BaseDeploymentBackend):
"""
Used to deploy a project into KoBoCAT. Stores the project identifiers in the
Expand All @@ -97,6 +95,22 @@ class KobocatDeploymentBackend(BaseDeploymentBackend):
r'[a-z\d]{8}-([a-z\d]{4}-){3}[a-z\d]{12}'
)

@property
def all_time_submission_count(self):
try:
xform_id = self.xform_id
except InvalidXFormException:
return 0

result = ReadOnlyKobocatMonthlyXFormSubmissionCounter.objects.filter(
xform_id=xform_id
).aggregate(Sum('counter'))

if count := result['counter__sum']:
return count

return 0

@property
def attachment_storage_bytes(self):
try:
Expand Down
11 changes: 11 additions & 0 deletions kpi/deployment_backends/mock_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ class MockDeploymentBackend(BaseDeploymentBackend):
'meta',
]

@property
def all_time_submission_count(self):
# FIXME, does not reproduce KoBoCAT behaviour.
# Deleted submissions are not taken into account but they should be
monthly_counter = len(
self.get_submissions(self.asset.owner)
)
return monthly_counter

@property
def attachment_storage_bytes(self):
submissions = self.get_submissions(self.asset.owner)
Expand Down Expand Up @@ -146,6 +155,8 @@ def connect(self, active=False):

@property
def current_month_submission_count(self):
# FIXME, does not reproduce KoBoCAT behaviour.
# Deleted submissions are not taken into account but they should be
monthly_counter = len(
self.get_submissions(self.asset.owner)
)
Expand Down
2 changes: 1 addition & 1 deletion kpi/serializers/v2/service_usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_submission_count_all_time(self, asset):
if not asset.has_deployment:
return 0

return asset.deployment.submission_count
return asset.deployment.all_time_submission_count

def get_storage_bytes(self, asset):
# Get value from asset deployment (if it has deployment)
Expand Down
3 changes: 0 additions & 3 deletions kpi/views/v2/service_usage.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# coding: utf-8
from rest_framework import (
mixins,
renderers,
serializers,
viewsets,
)
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from kpi.models import Asset
from kpi.serializers.v2.service_usage import ServiceUsageSerializer
from kpi.utils.object_permission import get_database_user

Expand Down

0 comments on commit e69efe0

Please sign in to comment.