###################################################################### # Copyright (c) 2007, Petteri Aimonen # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # * Redistributions of source code must retain the above copyright # notice and this list of conditions. # * Redistributions in binary form must reproduce the above copyright # notice and this list of conditions in the documentation and/or other # materials provided with the distribution. '''Compare groupfeed and per-animal amounts''' import wx import sys from pymisc import _ from database import db_conn from dataaccess import getvalvelist, getgroupspervalve, animals_in_group from dataaccess import date_todb, getlastdate import settings import datetime class GroupFeedPanel(wx.Panel): def __init__(self, *args, **kwargs): wx.Panel.__init__(self, *args, **kwargs) self.heading = wx.StaticText(self) self.listctrl = wx.ListCtrl(self, style = wx.LC_REPORT | wx.LC_HRULES | wx.LC_VRULES) self.sizer = wx.BoxSizer(wx.VERTICAL) self.sizer.Add(self.heading, flag = wx.EXPAND | wx.ALL, border = 5) self.sizer.Add(self.listctrl, flag = wx.EXPAND | wx.ALL, border = 5, proportion = 1) self.SetAutoLayout(True) self.SetSizer(self.sizer) self.Layout() self.cursor = db_conn.cursor() self.update() self.Bind(wx.EVT_SIZE, self.OnSize) def OnSize(self, evt = None): cwidth = self.GetSize()[0] // 6 - 10 for col in range(6): self.listctrl.SetColumnWidth(col, cwidth) if evt: evt.Skip() def update(self, fromdate = None, todate = None): '''Reload groupfeed list from database. If timeframe is not given, use previous n days, n coming from settings.''' if not todate: todate = getlastdate() if not fromdate: days = settings.parser.getint("NOTICES", "groupfeedperiod") fromdate = todate - datetime.timedelta(days) p = {'from': fromdate.strftime("%d.%m.%Y"), 'to': todate.strftime("%d.%m.%Y")} text = _("Groupfeed daily sums from %(from)s to %(to)s:") % p self.heading.SetLabel(text) self.listctrl.ClearAll() format = wx.LIST_FORMAT_RIGHT self.listctrl.InsertColumn(0, _('Valve'), format) self.listctrl.InsertColumn(1, _('Groupfeed / kg'), format) self.listctrl.InsertColumn(2, _('Per-animal / kg'), format) self.listctrl.InsertColumn(3, _('Difference / kg'), format) self.listctrl.InsertColumn(4, _('Groupfeed animals'), format) self.listctrl.InsertColumn(5, _('Per-animal animals'), format) for valve in getvalvelist(): row = self.getrow(valve, fromdate, todate) self.addrow(row) def getrow(self, valve, fromdate, todate): '''Get (valve, groupfeed, per-animal, difference, gfeedcount, afeedcount) row for valve.''' # Gfeed amount self.cursor.execute("SELECT AVG(grams) FROM groupfeed " \ "WHERE date>=? AND date<=? AND valve=?", (date_todb(fromdate), date_todb(todate), valve)) groupfeed = self.cursor.fetchone()[0] if groupfeed is None: groupfeed = 0. else: groupfeed /= 1000. # Afeed amount groups = ', '.join([str(int(gid)) for gid in getgroupspervalve(valve)]) self.cursor.execute("SELECT SUM(grams) FROM feeds WHERE date>=? AND " \ "date<=? AND animalid IN (SELECT animalid FROM animals WHERE " \ "groupid IN (%s))" % groups, (date_todb(fromdate), date_todb(todate))) afeed = self.cursor.fetchone()[0] if afeed is None: afeed = 0. else: afeed /= float((todate - fromdate).days) afeed /= 1000. # Gfeed count self.cursor.execute("SELECT count FROM groupfeed WHERE valve=? " \ "ORDER BY date DESC LIMIT 1", (valve, )) row = self.cursor.fetchone() if row: gcount = row[0] else: gcount = 0 acount = 0 for groupid in getgroupspervalve(valve): acount += len(animals_in_group(groupid)) if afeed > 0. and acount > 0: difference = _('%0.1f') % abs(groupfeed - afeed) else: difference = '' groupfeed = _('%0.1f') % groupfeed afeed = _('%0.1f') % afeed return (str(valve), groupfeed, afeed, difference, str(gcount), str(acount)) def addrow(self, row): '''Add row to list''' rowindex = self.listctrl.InsertStringItem(sys.maxint, row[0]) for i in range(1, len(row)): self.listctrl.SetStringItem(rowindex, i, row[i]) if __name__ == '__main__': print "Unit testing" class MyWindow(wx.Frame): def __init__(self): wx.Frame.__init__(self, None) self.q = GroupFeedPanel(self) class MyApp(wx.App): def OnInit(self): w = MyWindow() w.SetSize((500,500)) w.Show() return True app = MyApp(0) app.MainLoop()