def get(self): """ Chart about specified counters @param names: comma separated list of counter names @param captions: (optional) comma separated list of user-readable names (should match counter names) @param title: (optional) Ex: "?names=email_bounce,email_unsubscribe&captions=Bounces,Unsubscribes&title=Emails Stats" """ names = self.request.get('names') if not names: self.response.content_type = 'text/plain' self.response.write("Invalid Input.\n\n" + self.get.__doc__) return names = [n.strip() for n in names.split(',')] captions = self.request.get('captions') prefix = None if captions: captions = [n.strip() for n in captions.split(',')] if captions and len(captions) != len(names): self.response.content_type = 'text/plain' self.response.write("Invalid Input.\n\n" + self.get.__doc__) return else: for name in names: if prefix: common = '' for cn, cp in zip(name, prefix): if cn == cp: common += cn prefix = common else: prefix = name if prefix: captions = [name[len(prefix):] for name in names] else: captions = names title = self.request.get('title') or prefix or 'Counters Chart' counterFuts = [Counter.getCurrent(name) for name in names] counters = [fut.get_result() for fut in counterFuts] slices = set() for counter in counters: slices.update(counter.bySlice) slices = sorted(slices) chartData = [["On"] + captions] for item in slices: row = [item] for counter in counters: row.append(counter.bySlice.get(item, 0)) chartData.append(row) self.writeTemplate("countersChart.html", {"chartData": json.dumps(chartData), "title": title})
def testMaxNameLength(self): longCounterName = 't' * Counter.NAME_MAX_LENGTH incrementSuccess = Counter.increment(longCounterName, 1).get_result() self.assertTrue(incrementSuccess) counter = Counter.getCurrent(longCounterName).get_result() self.assertEqual(counter.name, longCounterName, "Name integrity was not maintained")
def testSlices(self): """ Test using default and custom slices while counting """ # Default slice today = datetime.datetime.utcnow().strftime("%Y-%m-%d") Counter.increment('testDefault', 1).get_result() counter = Counter.getCurrent('testDefault').get_result() self.assertEqual(counter.value, 1) self.assertDictEqual(counter.bySlice, {today: 1}) # Custom slice futs = [Counter.increment('testCustom', 1, sliceId="13")] futs.append(Counter.increment('testCustom', 2, sliceId="12")) [fut.get_result() for fut in futs] counter = Counter.getCurrent('testCustom').get_result() self.assertEqual(counter.value, 3) self.assertDictEqual(counter.bySlice, {"12": 2, "13": 1})
def testShards(self): """ Test that 'getCurrent' correctly retrieves values from different shards """ futs = [] for _ in xrange(10): # Because of NDB issue I can't perform all at the same time # https://code.google.com/p/googleappengine/issues/detail?id=10617 futs.append(Counter.increment('testShards', nbShards=3).get_result()) successes = futs # getAllResults(futs) self.assertEqual(10, sum(successes)) counter = Counter.getCurrent('testShards', nbShards=3).get_result() self.assertEqual(counter.value, 10)