Changed to pageviews instead of unique pageviews
[ckanext-ga-report.git] / ckanext / ga_report / ga_model.py
blob:a/ckanext/ga_report/ga_model.py -> blob:b/ckanext/ga_report/ga_model.py
--- a/ckanext/ga_report/ga_model.py
+++ b/ckanext/ga_report/ga_model.py
@@ -111,9 +111,7 @@
     >>> normalize_url('http://data.gov.uk/dataset/weekly_fuel_prices')
     '/dataset/weekly_fuel_prices'
     '''
-    # Deliberately leaving a /
-    url = url.replace('http:/','')
-    return '/' + '/'.join(url.split('/')[2:])
+    return '/' + '/'.join(url.split('/')[3:])
 
 
 def _get_package_and_publisher(url):
@@ -165,7 +163,11 @@
 
 
 def update_url_stats(period_name, period_complete_day, url_data):
-
+    '''
+    Given a list of urls and number of hits for each during a given period,
+    stores them in GA_Url under the period and recalculates the totals for
+    the 'All' period.
+    '''
     for url, views, visits in url_data:
         package, publisher = _get_package_and_publisher(url)
 
@@ -341,3 +343,21 @@
         q.delete()
     model.Session.commit()
 
+def get_score_for_dataset(dataset_name):
+    import datetime
+    now = datetime.datetime.now()
+    period_names = ['%s-%02d' % (now.year, now.month),
+                    '%s-%02d' % (now.year, now.month-1)]
+
+    entry = model.Session.query(GA_Url)\
+        .filter(GA_Url.period_name==period_names[0])\
+        .filter(GA_Url.package_id==dataset_name).first()
+    score = int(entry.pageviews) if entry else 0
+
+    entry = model.Session.query(GA_Url)\
+        .filter(GA_Url.period_name==period_names[1])\
+        .filter(GA_Url.package_id==dataset_name).first()
+    val = int(entry.pageviews) if entry else 0
+    score += val/2 if val else 0
+
+    return score