def get_values(value_type): '''Returns a list containing 1 list for each hour range. Each inner list contains a 14-day moving average of values for each day since store opening''' hours = [9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,0,1,2,3] values = [[] for i in hour_idx_range] dates = [] day_infos = db.get_daily_sales() window_size = 14 # number of days to use in running avg running_avgs = [[0.0 for w in range(window_size)] for i in hour_idx_range] # indexed by hour-range-index then 0 to window_size-1 window_idx = 0 for i,key in enumerate(sorted(day_infos.keys())): # go through days in order day_info = day_infos[key] daily_sales = day_info.get_gross_sales() dates.append(day_info.get_date()) hourly_sales = [0.0 for h in hour_idx_range] for hour_info in day_info.get_hourly_sales(hours): idx = find_index(hour_info.get_hour()) if (value_type == 'sales'): hourly_sales[idx] += hour_info.get_gross_sales() else: hourly_sales[idx] += hour_info.get_customers() if daily_sales > low_threshold: for hour,val in enumerate(hourly_sales): running_avgs[hour][window_idx] = val num_avgs = float(min(i+1,window_size)) to_append = map(lambda l: sum(l)/num_avgs, running_avgs) # avg each of the running_avgs lists to get the value for each hour range for the day window_idx = (window_idx + 1) % window_size else: to_append = hourly_sales for idx,val in enumerate(to_append): values[idx].append(val) return dates,values
def main(): print_headers() print """Note: this values may be slightly inflated as they are the sum of sales including $ from taxes, however LINK transactions are not charged taxes, but this does not take that into consideration. Use sales_tax.py if you need precise values eg for reporting sales taxes <br /> <br />""" form = cgi.FieldStorage() today = datetime.datetime.now() if "days" in form: days = int(form.getvalue("days")) if days == -1: start_date = db.FIRST_SALES else: start_date = today - datetime.timedelta(days=days) else: start_date = today - datetime.timedelta(days=7) start_date += datetime.timedelta( hours=(-start_date.hour + db.DAY_START_HOUR) ) # subtract current hour so reporting starts at midnight DAYS days ago. add db.DAY_START_HOUR because this is the hour we want reporting to start. print """<form name="dates" action="hours.py" method="get">""" print """Show data for:""" print """<select name="days">""" print """<option value="7">Last 7 days</option>""" print """<option value="30">Last 30 days</option>""" print """<option value="-1">Since store open</option>""" print """</select>""" print """<input type="submit" value="Change date range" />""" print """<br /><br />""" print "<table border='0' cellspacing='0'>\n" print "<tr><th>Date</th><th>Dayname</th><th>Customers</th><th>$/ring</th><th>Gross</th>\n" hours = [7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 0, 1, 2, 3] for i in hours: print "<th style='padding-left: 1px; padding-right: 1px;' class=''>%d</th>" % (i,) print "</tr>\n" day_infos = db.get_daily_sales(start_date, today) for i, key in enumerate(sorted(day_infos.keys())): day_info = day_infos[key] print "<tr>" print "<td>%s</td>" % (day_info.get_date_str(),) print "<td>%s</td>" % (day_info.get_dayname(),) print """<td><div class='bar' style='width: %dpx;'>%d</td>""" % ( day_info.get_customers_total() / 2, day_info.get_customers_total(), ) print """<td><div class='bar' style='width: %dpx;'>%.2f</td>""" % ( day_info.get_avg_sales() * 10, day_info.get_avg_sales(), ) print """<td><div class='bar' style='width: %dpx;'>%.2f<div style='width: 1px; margin-top: -1em; margin-left: 110px; border-top: 1em solid black'></div></div></td>""" % ( day_info.get_gross_sales() / 10, day_info.get_gross_sales(), ) for h in day_info.get_hourly_sales(hours): print """<td class='tiny' style='background-color: rgb(200,%d,200);'>%d<br/>%.1f<br/><b>$%d</b></td>\n""" % ( 255 - h.get_gross_sales() * 1.3, h.get_customers(), h.get_avg_sales(), h.get_gross_sales(), ) if i == 0: # print arrows on the first row print "<td class='tiny' style='border: none;'>←custs<br/>←\$/ring<br/>←gross</td>" print "</tr>\n" print """</table>""" print """</body></html>"""
# (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import op_db_library as db hours = [0,1,2,3,23] outputfilename = '../site/latenight_sales.csv' outputfile = open(outputfilename, 'w') daily_sales = db.get_daily_sales() format = "%Y%m%d" outputfile.write('"Date",Gross $,Customers\n') for key in sorted(daily_sales.keys()): day_info = daily_sales[key] date = day_info.get_date().strftime(format) customers = 0 gross = 0 for h in day_info.get_hourly_sales(hours): customers += h.get_customers() gross += h.get_gross_sales() outputfile.write("%s,%.2f,%d\n" % (date,gross,customers))