def Update(self, drink, force=False): previous = None if not force and self.stats: previous = protoutil.DictToProtoMessage(self.stats, models_pb2.Stats()) builder = self.STATS_BUILDER(drink, previous) self.stats = protoutil.ProtoMessageToDict(builder.Build()) self.save()
def _getEmptyStats(self): s = models_pb2.Stats() s.last_drink_id = "0" s.total_volume_ml = 0.0 s.total_pours = 0 s.average_volume_ml = 0.0 s.greatest_volume_ml = 0.0 s.greatest_volume_id = "0" return s
def Build(self): self.stats = models_pb2.Stats() if not self.drink: return self.stats self.drinks = self._AllDrinks() if self.previous: self.stats.MergeFrom(self.previous) for statname, fn in self.STAT_MAP.iteritems(): fn() return self.stats
def GetStats(self): try: stats = SystemStats.objects.get(site=self).stats except SystemStats.DoesNotExist: stats = {} return protoutil.DictToProtoMessage(stats, models_pb2.Stats())
def GetStats(self): try: stats = UserStats.objects.get(user=self).stats except UserStats.DoesNotExist: stats = {} return protoutil.DictToProtoMessage(stats, models_pb2.Stats())
def testStuff(self): builder = stats.SystemStatsBuilder(None) empty_stats = models_pb2.Stats() system_stats_d0 = builder.Build() self.assertEquals(empty_stats, system_stats_d0) # Record a new drink and verify stats. pour_time = datetime.datetime(2011, 05, 01, 12, 00) self.backend.RecordDrink('kegboard.flow0', ticks=100, volume_ml=100, username='******', pour_time=pour_time, do_postprocess=False) drink1 = models.Drink.objects.get(seqn=1) expected = self._getEmptyStats() expected.last_drink_id = "1" expected.total_volume_ml = 100.0 expected.total_pours = 1 expected.average_volume_ml = 100.0 expected.greatest_volume_ml = 100.0 expected.greatest_volume_id = "1" expected.has_guest_pour = False d = expected.volume_by_day_of_week.add() d.weekday = "0" # Sunday d.volume_ml = 100 u = expected.volume_by_drinker.add() u.username = "******" u.volume_ml = 100 y = expected.volume_by_year.add() y.year = 2011 y.volume_ml = 100 expected.registered_drinkers.append("user1") expected.sessions_count = 1 system_stats_d1 = stats.SystemStatsBuilder(drink1).Build() self.assertProtosEqual(expected, system_stats_d1) # Pour another drink self.backend.RecordDrink('kegboard.flow0', ticks=200, volume_ml=200, username='******', pour_time=pour_time, do_postprocess=False) drink2 = models.Drink.objects.get(seqn=2) # Adjust stats expected.last_drink_id = "2" expected.total_volume_ml = 300.0 expected.total_pours = 2 expected.average_volume_ml = 150.0 expected.greatest_volume_ml = 200.0 expected.greatest_volume_id = "2" d = expected.volume_by_day_of_week[0] d.volume_ml += 200 u = expected.volume_by_drinker[0] u.volume_ml += 200 y = expected.volume_by_year[0] y.volume_ml += 200 system_stats_d2 = stats.SystemStatsBuilder(drink2).Build() self.assertProtosEqual(expected, system_stats_d2) # Build the same stats incrementally and verify identical result. system_stats_d2_inc = stats.SystemStatsBuilder(drink2, system_stats_d1).Build() self.assertProtosEqual(system_stats_d2, system_stats_d2_inc)
def SystemStatsToProto(record, full=False): stats = record.stats ret = models_pb2.Stats() for k, v in stats.iteritems(): setattr(ret, k, v) return ret