-
Notifications
You must be signed in to change notification settings - Fork 11
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 #227 from OnroerendErfgoed/214_collect_usage_stats
214 collect usage stats
- Loading branch information
Showing
14 changed files
with
338 additions
and
13 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""visit_log | ||
Revision ID: 1ad2b6fbcf22 | ||
Revises: 441c5a16ef8 | ||
Create Date: 2015-07-27 13:29:04.840631 | ||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = '1ad2b6fbcf22' | ||
down_revision = '441c5a16ef8' | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
def upgrade(): | ||
op.create_table('conceptscheme_visit_log', | ||
sa.Column('id', sa.Integer(), primary_key=True, autoincrement=True), | ||
sa.Column('conceptscheme_id', sa.String(), nullable=False), | ||
sa.Column('visited_at', sa.DateTime(), nullable=False), | ||
sa.Column('origin', sa.String, nullable=False) | ||
) | ||
op.create_table('concept_visit_log', | ||
sa.Column('id', sa.Integer(), primary_key=True, autoincrement=True), | ||
sa.Column('concept_id', sa.Integer(), nullable=False), | ||
sa.Column('conceptscheme_id', sa.String(), nullable=False), | ||
sa.Column('visited_at', sa.DateTime(), nullable=False), | ||
sa.Column('origin', sa.String, nullable=False) | ||
) | ||
|
||
|
||
def downgrade(): | ||
op.drop_table('concept_visit_log') | ||
op.drop_table('conceptscheme_visit_log') |
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,81 @@ | ||
# -*- coding: utf-8 -*- | ||
from atramhasis.data.models import ( | ||
ConceptschemeVisitLog, | ||
ConceptVisitLog | ||
) | ||
from pyramid.response import Response | ||
import logging | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
|
||
def _origin_from_request(request): | ||
if request.url.endswith('.csv'): | ||
return 'CSV' | ||
elif request.url.endswith('.rdf') \ | ||
or 'application/rdf+xml' in request.accept \ | ||
or 'text/turtle' in request.accept \ | ||
or 'application/x-turtle' in request.accept: | ||
return 'RDF' | ||
elif 'text/html' in request.accept: | ||
return 'HTML' | ||
elif 'application/json' in request.accept: | ||
return 'REST' | ||
else: | ||
return None | ||
|
||
|
||
def _origin_from_response(response): | ||
if response.content_type == 'text/html': | ||
return 'HTML' | ||
elif response.content_type == 'application/json': | ||
return 'REST' | ||
elif response.content_type == 'application/rdf+xml' \ | ||
or response.content_type == 'text/turtle' \ | ||
or response.content_type == 'application/x-turtle': | ||
return 'RDF' | ||
elif response.content_type == 'text/csv': | ||
return 'CSV' | ||
else: | ||
return None | ||
|
||
|
||
def audit(fn): | ||
''' | ||
use this decorator to audit an operation and to log the visit | ||
* CSV routes with .csv extensions accept all mime types, | ||
the response is not of the `pyramid.response.Response` type, | ||
the origin is derived from the `pyramid.request.Request.url` extension. | ||
* RDF routes with .rdf, .ttl extensions accept all mime types, | ||
the origin is derived form the response content type. | ||
* REST and HTML the view results are not of the `pyramid.response.Response` type, | ||
the origin is derived from the accept header. | ||
''' | ||
|
||
def advice(parent_object, *args, **kw): | ||
request = parent_object.request | ||
audit_manager = request.data_managers['audit_manager'] | ||
|
||
if 'c_id' in request.matchdict.keys(): | ||
visit_log = ConceptVisitLog( | ||
concept_id=request.matchdict['c_id'], | ||
conceptscheme_id=request.matchdict['scheme_id'] | ||
) | ||
elif 'scheme_id' not in request.matchdict.keys(): | ||
log.error('Misuse of the audit decorator. The url must at least contain a {scheme_id} parameter') | ||
return fn(parent_object, *args, **kw) | ||
else: | ||
visit_log = ConceptschemeVisitLog(conceptscheme_id=request.matchdict['scheme_id']) | ||
response = fn(parent_object, *args, **kw) | ||
|
||
if isinstance(response, Response): | ||
visit_log.origin = _origin_from_response(response) | ||
else: | ||
visit_log.origin = _origin_from_request(request) | ||
|
||
audit_manager.save(visit_log) | ||
|
||
return response | ||
|
||
return advice |
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
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,3 +1,28 @@ | ||
from sqlalchemy.ext.declarative import declarative_base | ||
from sqlalchemy import ( | ||
Column, | ||
Integer, | ||
String, | ||
DateTime) | ||
from sqlalchemy.sql import ( | ||
func | ||
) | ||
|
||
Base = declarative_base() | ||
Base = declarative_base() | ||
|
||
|
||
class ConceptschemeVisitLog(Base): | ||
__tablename__ = 'conceptscheme_visit_log' | ||
id = Column(Integer, primary_key=True, autoincrement=True) | ||
conceptscheme_id = Column(String(25), nullable=False) | ||
visited_at = Column(DateTime, default=func.now(), nullable=False) | ||
origin = Column(String(25), nullable=False) | ||
|
||
|
||
class ConceptVisitLog(Base): | ||
__tablename__ = 'concept_visit_log' | ||
id = Column(Integer, primary_key=True, autoincrement=True) | ||
concept_id = Column(Integer, nullable=False) | ||
conceptscheme_id = Column(String(25), nullable=False) | ||
visited_at = Column(DateTime, default=func.now(), nullable=False) | ||
origin = Column(String(25), nullable=False) |
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
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
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
Oops, something went wrong.