Add classroom dust levels to /charts page

This commit is contained in:
Tanner Collin 2021-07-02 03:50:14 +00:00
parent a7b1009d64
commit 8fa5131364
2 changed files with 51 additions and 1 deletions

View File

@ -4,16 +4,19 @@ import { Button, Container, Checkbox, Dimmer, Divider, Dropdown, Form, Grid, Hea
import { BarChart, Bar, LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer, ReferenceLine } from 'recharts'; import { BarChart, Bar, LineChart, Line, XAxis, YAxis, CartesianGrid, Tooltip, Legend, ResponsiveContainer, ReferenceLine } from 'recharts';
import { apiUrl, statusColor, BasicTable, staticUrl, requester } from './utils.js'; import { apiUrl, statusColor, BasicTable, staticUrl, requester } from './utils.js';
import { NotFound } from './Misc.js'; import { NotFound } from './Misc.js';
import moment from 'moment-timezone';
let memberCountCache = false; let memberCountCache = false;
let signupCountCache = false; let signupCountCache = false;
let spaceActivityCache = false; let spaceActivityCache = false;
let dustLevelCache = false;
export function Charts(props) { export function Charts(props) {
const [memberCount, setMemberCount] = useState(memberCountCache); const [memberCount, setMemberCount] = useState(memberCountCache);
const [signupCount, setSignupCount] = useState(signupCountCache); const [signupCount, setSignupCount] = useState(signupCountCache);
const [spaceActivity, setSpaceActivity] = useState(spaceActivityCache); const [spaceActivity, setSpaceActivity] = useState(spaceActivityCache);
const [fullActivity, setFullActivity] = useState(false); const [fullActivity, setFullActivity] = useState(false);
const [dustLevel, setDustLevel] = useState(dustLevelCache);
useEffect(() => { useEffect(() => {
requester('/charts/membercount/', 'GET') requester('/charts/membercount/', 'GET')
@ -42,6 +45,15 @@ export function Charts(props) {
.catch(err => { .catch(err => {
console.log(err); console.log(err);
}); });
requester('https://ps-iot.dns.t0.vc/sensors/air/0/pm25/week', 'GET')
.then(res => {
setDustLevel(res.result);
dustLevelCache = res.result;
})
.catch(err => {
console.log(err);
});
}, []); }, []);
return ( return (
@ -281,6 +293,37 @@ export function Charts(props) {
<p>Retained Count: number of those signups who are still a member currently.</p> <p>Retained Count: number of those signups who are still a member currently.</p>
<Header size='medium'>Dust Level</Header>
<p>Averaged every 15 minutes for the past week.</p>
<p>
{dustLevel &&
<ResponsiveContainer width='100%' height={300}>
<LineChart data={dustLevel}>
<XAxis dataKey='time' tickFormatter={(t) => moment(t).format('ddd h:mm a')} minTickGap={10} />
<YAxis />
<CartesianGrid strokeDasharray='3 3'/>
<Tooltip formatter={v => v.toFixed(2) + ' μg/m³'} labelFormatter={t => 'Time: ' + moment(t).format('ddd h:mm a')} />
<Legend />
<Line
type='monotone'
dataKey='value'
name='Classroom PM2.5'
stroke='#8884d8'
strokeWidth={2}
dot={false}
animationDuration={1000}
/>
</LineChart>
</ResponsiveContainer>
}
</p>
<p>Classroom PM2.5: Amount of PM2.5 particles measured from the classroom ceiling. Units are μg/.</p>
</Container> </Container>
); );
}; };

View File

@ -32,6 +32,7 @@ export const BasicTable = (props) => (
export const requester = (route, method, token, data) => { export const requester = (route, method, token, data) => {
let options = {headers: {}}; let options = {headers: {}};
let url = '';
if (token) { if (token) {
options.headers.Authorization = 'Token ' + token; options.headers.Authorization = 'Token ' + token;
@ -65,7 +66,13 @@ export const requester = (route, method, token, data) => {
return error; return error;
} }
return fetch(apiUrl + route, options) if (route.startsWith('http')) {
url = route;
} else {
url = apiUrl + route;
}
return fetch(url, options)
.then(response => { .then(response => {
if (!response.ok) { if (!response.ok) {
throw customError(response); throw customError(response);