api/statistics: add service_ou filter when not group-by (#86179)
gitea/authentic/pipeline/head This commit looks good Details

This commit is contained in:
Yann Weber 2024-01-31 10:49:07 +01:00 committed by Yann Weber
parent 4ba169c4c1
commit dbed18fd82
3 changed files with 35 additions and 3 deletions

View File

@ -1737,6 +1737,8 @@ class StatisticsAPI(ViewSet):
allowed_filters = ('services_ou', 'users_ou', 'service')
subfilters = self.get_additional_filters(allowed_filters)
elif data['group_by'] == 'global':
allowed_filters = ('services_ou',)
subfilters = self.get_additional_filters(allowed_filters)
kwargs['y_label'] = getattr(self, self.action).name
else:
allowed_filters = getattr(self, self.action).filters

View File

@ -159,8 +159,16 @@ class EventTypeDefinition(metaclass=EventTypeDefinitionMeta):
return qs.order_by(group_by_time)
@classmethod
def get_global_statistics(cls, group_by_time, y_label, start=None, end=None):
qs = cls.get_statistics(group_by_time=group_by_time, start=start, end=end)
def get_global_statistics(cls, group_by_time, y_label, services_ou=None, start=None, end=None):
which_ref = None
if services_ou:
from authentic2.models import Service
services = Service.objects.filter(ou=services_ou)
# look for the Service child and parent instances, see #68390 and #64853
which_ref = [] if not services else [services, list(services.select_subclasses())]
qs = cls.get_statistics(group_by_time=group_by_time, which_references=which_ref, start=start, end=end)
stats = Statistics(qs, time_interval=group_by_time)
for stat in qs:

View File

@ -257,7 +257,15 @@ def test_api_statistics_subfilters(app, admin, endpoint):
assert len(resp.json['data']['subfilters']) == 0
resp = app.get('/api/statistics/%s/' % endpoint, headers=headers)
assert len(resp.json['data']['subfilters']) == 0
assert len(resp.json['data']['subfilters']) == 1
assert resp.json['data']['subfilters'][0] == {
'id': 'services_ou',
'label': 'Services organizational unit',
'options': [
{'id': 'default', 'label': 'Default organizational unit'},
{'id': 'second', 'label': 'Second OU'},
],
}
@pytest.mark.parametrize(
@ -398,6 +406,20 @@ def test_api_statistics(app, admin, freezer, event_type_name, event_name):
assert resp.json['data']['series'][0]['data'] == [2, 2]
assert 'count' in resp.json['data']['series'][0]['label']
params = {'services_ou': 'second', **params}
resp = app.get(url, headers=headers, params=params)
assert resp.json['data']['x_labels'] == ['2020-02', '2020-03']
assert len(resp.json['data']['series']) == 1
assert resp.json['data']['series'][0]['data'] == [1, 1]
assert resp.json['data']['series'][0]['label'].endswith(' count')
params = {'services_ou': 'default', **params}
resp = app.get(url, headers=headers, params=params)
assert resp.json['data']['x_labels'] == ['2020-02', '2020-03']
assert len(resp.json['data']['series']) == 1
assert resp.json['data']['series'][0]['data'] == [1, 1]
assert resp.json['data']['series'][0]['label'].endswith(' count')
@pytest.mark.parametrize(
'event_type_name,event_name,event_description',