Skip to content

Commit 96585ee

Browse files
committed
Add drink sales to Charts page
1 parent efea882 commit 96585ee

File tree

3 files changed

+78
-1
lines changed

3 files changed

+78
-1
lines changed

apiserver/apiserver/api/management/commands/run_hourly.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ def generate_stats(self):
3030
defaults=dict(signup_count=signup_count),
3131
)
3232

33+
utils_stats.calc_drink_sales()
34+
3335
utils_stats.calc_card_scans()
3436

3537
utils.gen_search_strings()

apiserver/apiserver/api/utils_stats.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
'closing': {},
3333
'printer3d': {},
3434
'solar': {},
35+
'drinks_6mo': [],
3536
}
3637

3738
if secrets.MUMBLE:
@@ -196,6 +197,35 @@ def calc_card_scans():
196197
defaults=dict(card_scans=count),
197198
)
198199

200+
def calc_drink_sales():
201+
six_months_ago = utils.today_alberta_tz() - timedelta(days=183)
202+
203+
drinks = {
204+
'1': 'Coke',
205+
'2': 'Coke Zero',
206+
'3': 'Root Beer',
207+
'4': 'Iced Tea',
208+
'5': 'Crush Pop',
209+
'6': 'Dr Pepper',
210+
'7': 'Arizona Tea',
211+
'8': 'Cherry Coke',
212+
}
213+
results = []
214+
215+
txs = models.Transaction.objects
216+
217+
for number, name in drinks.items():
218+
count = txs.filter(
219+
category='Snacks',
220+
memo__contains='pop vending machine item #' + number,
221+
date__gte=six_months_ago,
222+
).count()
223+
224+
results.append(dict(name=name, count=count))
225+
226+
cache.set('drinks_6mo', results)
227+
228+
199229
def get_progress(request_id):
200230
return cache.get('request-progress-' + request_id, [])
201231

webclient/src/Charts.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useState, useEffect } from 'react';
22
import { Statistic, Button, Container, Header } from 'semantic-ui-react';
33
import { BarChart, Bar, LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer, ReferenceLine } from 'recharts';
4-
import { requester } from './utils.js';
4+
import { requester, useIsMobile } from './utils.js';
55
import moment from 'moment-timezone';
66

77
let memberCountCache = false;
@@ -14,6 +14,8 @@ export function Charts(props) {
1414
const [spaceActivity, setSpaceActivity] = useState(spaceActivityCache);
1515
const [fullActivity, setFullActivity] = useState(false);
1616
const [fullSignups, setFullSignups] = useState(false);
17+
const [stats, setStats] = useState(false);
18+
const isMobile = useIsMobile();
1719

1820
useEffect(() => {
1921
requester('/charts/membercount/', 'GET')
@@ -42,6 +44,16 @@ export function Charts(props) {
4244
.catch(err => {
4345
console.log(err);
4446
});
47+
48+
requester('/stats/', 'GET')
49+
.then(res => {
50+
setStats(res);
51+
localStorage.setItem('stats', JSON.stringify(res));
52+
})
53+
.catch(err => {
54+
console.log(err);
55+
setStats(false);
56+
});
4557
}, []);
4658

4759
return (
@@ -111,6 +123,39 @@ export function Charts(props) {
111123
</>
112124
}
113125

126+
<Header size='medium'>Drink Sales</Header>
127+
128+
<p>Drinks sold over the last six months.</p>
129+
130+
<p>
131+
{stats && stats.drinks_6mo &&
132+
<ResponsiveContainer width='100%' height={300}>
133+
<BarChart
134+
margin={isMobile? {bottom: 50} : {}}
135+
data={stats.drinks_6mo.map((x, i) => (
136+
{...x, fill: ['#e7223a', 'black', '#9a4423', '#1582ae', '#d77a2d', '#6f0e21', '#3fad96', '#ab316e'][i]}
137+
))}
138+
>
139+
<XAxis dataKey='name' interval={0} angle={isMobile ? -45 : 0} textAnchor={isMobile ? 'end' : 'middle'} />
140+
<YAxis />
141+
<CartesianGrid strokeDasharray='3 3'/>
142+
<Tooltip />
143+
144+
<Bar
145+
type='monotone'
146+
dataKey='count'
147+
name='Cans'
148+
fill='#2185d0'
149+
maxBarSize={40}
150+
animationDuration={250}
151+
/>
152+
</BarChart>
153+
</ResponsiveContainer>
154+
}
155+
</p>
156+
157+
<p>Count: number of cans of pop sold over the last six months.</p>
158+
114159
<Header size='medium'>Member Counts</Header>
115160

116161
<p>Daily since March 2nd, 2020.</p>

0 commit comments

Comments
 (0)