diff --git a/r2/r2/config/templates.py b/r2/r2/config/templates.py index 1919dfb95..0b7ff7f90 100644 --- a/r2/r2/config/templates.py +++ b/r2/r2/config/templates.py @@ -44,3 +44,4 @@ api('listing', ListingJsonTemplate) api('usertableitem', UserItemJsonTemplate) api('organiclisting', OrganicListingJsonTemplate) +api('reddittraffic', TrafficJsonTemplate) diff --git a/r2/r2/lib/jsontemplates.py b/r2/r2/lib/jsontemplates.py index 20f0ff9f6..c96d78e10 100644 --- a/r2/r2/lib/jsontemplates.py +++ b/r2/r2/lib/jsontemplates.py @@ -347,3 +347,12 @@ class ListingJsonTemplate(ThingJsonTemplate): class OrganicListingJsonTemplate(ListingJsonTemplate): def kind(self, wrapped): return "OrganicListing" + +class TrafficJsonTemplate(JsonTemplate): + def render(self, thing, *a, **kw): + res = {} + for ival in ("hour", "day", "month"): + if hasattr(thing, ival + "_data"): + res[ival] = [[time.mktime(date.timetuple())] + list(data) + for date, data in getattr(thing, ival+"_data")] + return res diff --git a/r2/r2/lib/pages/graph.py b/r2/r2/lib/pages/graph.py index 4d53dbce6..a3184aa25 100644 --- a/r2/r2/lib/pages/graph.py +++ b/r2/r2/lib/pages/graph.py @@ -28,7 +28,7 @@ def google_extended(n): "0123456789-.") base = len(numerals) assert(0 <= n <= base ** 2) - q, r = divmod(n, base) + q, r = divmod(int(n), base) return numerals[q] + numerals[r] def make_date_axis_labels(series): @@ -101,7 +101,10 @@ class DataSeries(list): return DataSeries(data) def toBarX(self): - delta = self[-1] - self[-2] + if len(self) > 1: + delta = self[-1] - self[-2] + else: + delta = 0 data = self.toBarY() return DataSeries(data[1:] + [data[-1] + delta]) diff --git a/r2/r2/lib/services.py b/r2/r2/lib/services.py index 5f44f7d40..04401efed 100644 --- a/r2/r2/lib/services.py +++ b/r2/r2/lib/services.py @@ -359,10 +359,16 @@ def process_info(proc_ids = [], name = '', exe = "/usr/bin/env ps"): n = ' '.join(line[ageid+1:]) if (n.startswith(name) and (not proc_ids or int(pid) in proc_ids)): + age = line[ageid].split(':')[0] + # patch for > 24 hour old processes + if '-' in age: + days, hours = age.split('-') + age = float(days) * 24 + float(hours) + else: + age = float(age) res[pid] = dict(cpu = float(line[cpuid]), mem = float(line[memid]), - age = float(line[ageid].split(':')[0]), - name = n) + age = age, name = n) except (ValueError, IndexError): pass return res diff --git a/r2/r2/lib/traffic.py b/r2/r2/lib/traffic.py index 873093654..71bd5477b 100644 --- a/r2/r2/lib/traffic.py +++ b/r2/r2/lib/traffic.py @@ -69,10 +69,15 @@ def load_traffic(interval, what, iden, npoints = npoints) if res and isinstance(res[0][0], datetime.datetime): - res = zip(*res) - res[0] = [x.replace(tzinfo=None) - datetime.timedelta(0, time.timezone) - for x in res[0]] - res = zip(*res) + dates, data = zip(*res) + if interval == 'hour': + # shift hourly totals into local time zone. + dates = [x.replace(tzinfo=None) - + datetime.timedelta(0, time.timezone) for x in dates] + else: + # we don't care about the hours + dates = [x.date() for x in dates] + res = zip(dates, data) return res diff --git a/r2/r2/templates/reddittraffic.html b/r2/r2/templates/reddittraffic.html index bad853461..260721f43 100644 --- a/r2/r2/templates/reddittraffic.html +++ b/r2/r2/templates/reddittraffic.html @@ -28,7 +28,10 @@ <%def name="daily_summary()"> <% - umin = min(data[0] for date, data in filter(None, thing.day_data)) + thing.day_data = filter(None, thing.day_data) + umin = min(data[0] for date, data in thing.day_data) + if len(filter(lambda x: x[1][0] == umin, thing.day_data)) > 1: + umin = -1 umax = max(data[0] for date, data in filter(None, thing.day_data)) %>