From: Ross Jones Date: Wed, 09 Jan 2013 10:10:35 +0000 Subject: Only show the months for Downloads that we have X-Git-Url: http://maxious.lambdacomplex.org/git/?p=ckanext-ga-report.git&a=commitdiff&h=c3167f269d99f8ef981488dc03671f27f8f1c4b9 --- Only show the months for Downloads that we have --- --- a/ckanext/ga_report/controller.py +++ b/ckanext/ga_report/controller.py @@ -21,7 +21,7 @@ return '%s %s' % (calendar.month_name[d.tm_mon], d.tm_year) -def _month_details(cls): +def _month_details(cls, stat_key=None): ''' Returns a list of all the periods for which we have data, unfortunately knows too much about the type of the cls being passed as GA_Url has a @@ -32,9 +32,12 @@ months = [] day = None - vals = model.Session.query(cls.period_name,cls.period_complete_day)\ - .filter(cls.period_name!='All').distinct(cls.period_name)\ - .order_by("period_name desc").all() + q = model.Session.query(cls.period_name,cls.period_complete_day)\ + .filter(cls.period_name!='All').distinct(cls.period_name) + if stat_key: + q= q.filter(cls.stat_name==stat_key) + + vals = q.order_by("period_name desc").all() if vals and vals[0][1]: day = int(vals[0][1]) ordinal = 'th' if 11 <= day <= 13 \ @@ -203,7 +206,7 @@ # Get the month details by fetching distinct values and determining the # month names from the values. - c.months, c.day = _month_details(GA_Stat) + c.months, c.day = _month_details(GA_Stat, "Downloads") # Work out which month to show, based on query params of the first item c.month_desc = 'all months' @@ -216,12 +219,15 @@ q = q.filter(GA_Stat.period_name==c.month) if c.month else q q = q.order_by("ga_stat.value::int desc") + data = collections.defaultdict(int) for entry in q.all(): r = model.Session.query(model.Resource).filter(model.Resource.url==entry.key).first() - if r: - c.downloads.append((r,entry.value)) - else: - log.info("Failed to find resource for %s" % entry.key) + if not r: + continue + data[r] += int(entry.value) + + c.downloads = [(k,v,) for k,v in data.iteritems()] + c.downloads = sorted(c.downloads, key=operator.itemgetter(1), reverse=True) return render('ga_report/site/downloads.html') --- a/ckanext/ga_report/download_analytics.py +++ b/ckanext/ga_report/download_analytics.py @@ -304,6 +304,12 @@ max_results=10000, end_date=end_date).execute() result_data = results.get('rows') + if not result_data: + # We may not have data for this time period, so we need to bail + # early. + log.info("There is no download data for this time period") + return + # [[url, count], [url],count] data = {} for result in result_data: --- a/ckanext/ga_report/templates/ga_report/ga_util.html +++ b/ckanext/ga_report/templates/ga_report/ga_util.html @@ -46,7 +46,7 @@ - +
NameDataset and resource Downloads