-
Notifications
You must be signed in to change notification settings - Fork 0
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 #4 from NorwegianRefugeeCouncil/all_token_usage
Refactor
- Loading branch information
Showing
7 changed files
with
194 additions
and
113 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__VERSION__ = '0.3.0' | ||
__VERSION__ = '0.3.2' |
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,5 @@ | ||
# flake8: noqa: F401 | ||
|
||
from ckanext.tracking.queries.data.all import all_token_usage_data | ||
from ckanext.tracking.queries.data.dataset import most_accessed_dataset_with_token_data | ||
from ckanext.tracking.queries.data.token import most_accessed_token_data |
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,70 @@ | ||
""" | ||
Post-preocessed data after the DB queries and before the CSV generation | ||
""" | ||
|
||
import logging | ||
from ckan import model | ||
from ckan.plugins import toolkit | ||
|
||
from ckanext.tracking.queries.api import get_all_token_usage | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def all_token_usage_data(limit=1000): | ||
""" Get all tokens usage """ | ||
|
||
data = get_all_token_usage(limit=limit) | ||
# Create CSV including package details | ||
rows = [] | ||
|
||
for row in data: | ||
user_id = row['user_id'] | ||
user = model.User.get(user_id) | ||
if user: | ||
user_name = user.fullname or user.name | ||
else: | ||
user_name = None | ||
object_id = row['object_id'] | ||
object_type = row['object_type'] | ||
organization_url = None | ||
organization_title = None | ||
obj = None | ||
obj_title = None | ||
object_url = None | ||
if object_id: | ||
if object_type == 'dataset': | ||
obj = model.Package.get(object_id) | ||
obj_title = obj.title | ||
object_url = toolkit.url_for('dataset.read', id=obj.id) | ||
pkg = toolkit.get_action('package_show')({'ignore_auth': True}, {'id': obj.id}) | ||
owner_org = pkg.get('organization', {}) | ||
organization_title = owner_org.get('title') | ||
organization_url = toolkit.url_for('organization.read', id=owner_org.get('id')) | ||
elif object_type == 'resource': | ||
obj = model.Resource.get(object_id) | ||
obj_title = obj.name | ||
object_url = toolkit.url_for('dataset_resource.read', id=obj.package_id, resource_id=obj.id) | ||
elif object_type == 'organization': | ||
obj = model.Organization.get(object_id) | ||
obj_title = obj.title | ||
object_url = toolkit.url_for('organization.read', id=obj.id) | ||
|
||
rows.append({ | ||
'timestamp': row['timestamp'], | ||
'user_id': user_id, | ||
'user_name': user_name, | ||
'user_fullname': user.fullname if user else None, | ||
'token_name': row['token_name'], | ||
'tracking_type': row['tracking_type'], | ||
'tracking_sub_type': row['tracking_sub_type'], | ||
'object_type': object_type, | ||
'object_id': object_id, | ||
'object_title': obj_title, | ||
'object_url': object_url, | ||
'organization_url': organization_url, | ||
'organization_title': organization_title, | ||
}) | ||
|
||
return rows |
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,37 @@ | ||
""" | ||
Post-preocessed data after the DB queries and before the CSV generation | ||
""" | ||
|
||
import logging | ||
from ckan import model | ||
from ckan.plugins import toolkit | ||
|
||
from ckanext.tracking.queries.api import get_most_accessed_dataset_with_token | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def most_accessed_dataset_with_token_data(limit=10): | ||
data = get_most_accessed_dataset_with_token(limit=limit) | ||
|
||
# Create CSV including package details | ||
rows = [] | ||
for row in data: | ||
object_id = row['object_id'] | ||
obj = model.Package.get(object_id) | ||
if obj: | ||
obj_title = obj.title | ||
object_url = toolkit.url_for('dataset.read', id=obj.name, qualified=True) | ||
else: | ||
obj_title = None | ||
object_url = None | ||
|
||
rows.append({ | ||
'dataset_id': object_id, | ||
'dataset_title': obj_title, | ||
'dataset_url': object_url, | ||
'total': row['total'], | ||
}) | ||
|
||
return rows |
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,41 @@ | ||
""" | ||
Post-preocessed data after the DB queries and before the CSV generation | ||
""" | ||
|
||
import logging | ||
from ckan import model | ||
from ckan.plugins import toolkit | ||
|
||
from ckanext.tracking.queries.api import get_most_accessed_token | ||
|
||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def most_accessed_token_data(limit=10): | ||
""" Get most accessed tokens """ | ||
data = get_most_accessed_token(limit=limit) | ||
# Create CSV including package details | ||
rows = [] | ||
for row in data: | ||
user_id = row['user_id'] | ||
user = model.User.get(user_id) | ||
if user: | ||
user_title = user.fullname | ||
user_name = user.name | ||
user_url = toolkit.url_for('user.read', id=user.name, qualified=True) | ||
else: | ||
user_title = None | ||
user_name = None | ||
user_url = None | ||
|
||
rows.append({ | ||
'user_id': user_id, | ||
'user_fullname': user_title, | ||
'user_name': user_name, | ||
'user_url': user_url, | ||
'token_name': row['token_name'], | ||
'total': row['total'], | ||
}) | ||
|
||
return rows |
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