Skip to content

Commit

Permalink
Optimize resource download graph not to fetch all data
Browse files Browse the repository at this point in the history
  • Loading branch information
Zharktas committed Mar 12, 2024
1 parent fc8eea5 commit 58b63af
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
9 changes: 7 additions & 2 deletions ckanext/matomo/helpers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from datetime import date, datetime
from datetime import date, datetime, timedelta
from flask import request
from typing import List, Tuple, Optional
from ckan.plugins.toolkit import render_snippet, config
from ckan.plugins import toolkit as tk
from ckanext.matomo.reports import last_calendar_period, get_report_years
from ckanext.matomo.model import PackageStats, ResourceStats
from ckanext.matomo.model import PackageStats, ResourceStats, get_end_of_last_week, get_beginning_of_next_week
from ckanext.matomo.types import Visits


Expand Down Expand Up @@ -39,6 +39,11 @@ def get_visits_for_dataset(id: str) -> Visits:
def get_visits_for_resource(id: str) -> Visits:
return ResourceStats.get_all_visits(id)

def get_downloads_in_date_range_for_resource(id: str) -> Visits:
end_of_last_week = get_end_of_last_week(datetime.now())
start_date = get_beginning_of_next_week(end_of_last_week.replace(hour=0, minute=0, second=0, microsecond=0)
- timedelta(days=365))
return ResourceStats.get_downloads_in_date_range_by_id(id, start_date, end_of_last_week)

def get_download_count_for_dataset(id: str, time: str) -> int:
start_date, end_date = get_date_range(time)
Expand Down
28 changes: 28 additions & 0 deletions ckanext/matomo/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,34 @@ def get_all_visits_by_id(cls, resource_id) -> Visits:
resource_visits, total_visits, total_downloads)
return visits

@classmethod
def get_downloads_in_date_range_by_id(cls, resource_id: str, start_date: datetime, end_date: datetime):
weekly_visits = ((model.Session.query(
func.date_trunc('week', cls.visit_date).label('weekly'),
func.sum(cls.downloads).label('weekly_downloads'))
.filter(cls.resource_id == resource_id,
cls.visit_date >= start_date,
cls.visit_date <= end_date))
.group_by('weekly').order_by('weekly').all())


total_visits_and_downloads = model.Session.query(func.sum(cls.visits).label('visits'),
func.sum(cls.downloads).label('downloads')).filter(
cls.resource_id == resource_id).first()

visits: Visits = []

for week_visits in weekly_visits:
visits.append({'year': week_visits.weekly.year, 'week': week_visits.weekly.isocalendar()[1],
'downloads': week_visits.weekly_downloads})


results = {'visits': visits,
'total_visits': total_visits_and_downloads.visits,
'total_downloads': total_visits_and_downloads.downloads}

return results

@classmethod
def get_stat_counts_by_id_and_date_range(cls, resource_id: str,
start_date: Optional[datetime] = None,
Expand Down
3 changes: 2 additions & 1 deletion ckanext/matomo/plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def get_helpers(self):
'get_date_range': helpers.get_date_range,
'get_years': helpers.get_years,
'matomo_show_download_graph': helpers.show_download_graph,
'get_current_date': helpers.get_current_date
'get_current_date': helpers.get_current_date,
'get_downloads_in_date_range_for_resource': helpers.get_downloads_in_date_range_for_resource
}

# IClick
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ <h2 class="module-heading">{{ _('Stats') }}</h2>
<div id="chart_div"></div>
{% endif %}

{% set visits = h.get_visits_for_resource(res.id) %}
{% set visits = h.get_downloads_in_date_range_for_resource(res.id) %}
{% set visit_count_last_12_month = h.get_visit_count_for_resource(res.id, 'year') %}
{% set visit_count_last_30_days = h.get_visit_count_for_resource(res.id, 'month') %}
{% set download_count_last_12_month = h.get_download_count_for_resource(res.id, 'year') %}
Expand Down

0 comments on commit 58b63af

Please sign in to comment.