Skip to content

Commit

Permalink
feat(dashboard): stats for passages (#614)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudambro authored Apr 13, 2022
1 parent f09fd57 commit 7bc04ef
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions dashboard/src/scenes/stats/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useMemo, useState } from 'react';
import styled from 'styled-components';
import { Col, Label, Nav, NavItem, NavLink, Row, TabContent, TabPane } from 'reactstrap';
import { useRecoilValue, useSetRecoilState } from 'recoil';
Expand Down Expand Up @@ -26,7 +26,7 @@ import { reportsState } from '../../recoil/reports';
import ExportData from '../data-import-export/ExportData';
import SelectCustom from '../../components/SelectCustom';
import { territoriesState } from '../../recoil/territory';
import { getIsDayWithinHoursOffsetOfPeriod } from '../../services/date';
import { dayjsInstance, getIsDayWithinHoursOffsetOfPeriod } from '../../services/date';
import { loadingState, refreshTriggerState } from '../../components/Loader';
import { passagesState } from '../../recoil/passages';
import useTitle from '../../services/useTitle';
Expand All @@ -43,13 +43,12 @@ const getDataForPeriod = (data, { startDate, endDate }, currentTeam, viewAllOrga
);
};

const tabs = ['Général', 'Accueil', 'Actions', 'Personnes suivies', 'Observations', 'Comptes-rendus'];
const tabs = ['Général', 'Accueil', 'Actions', 'Personnes suivies', 'Passages', 'Observations', 'Comptes-rendus'];
const Stats = () => {
const organisation = useRecoilValue(organisationState);
const user = useRecoilValue(userState);
const currentTeam = useRecoilValue(currentTeamState);
const teams = useRecoilValue(teamsState);
useTitle('Statistiques');

const allPersons = useRecoilValue(personsState);
const allActions = useRecoilValue(actionsState);
Expand All @@ -69,12 +68,12 @@ const Stats = () => {
const [period, setPeriod] = useState({ startDate: null, endDate: null });
const [actionsStatuses, setActionsStatuses] = useState(DONE);

useTitle(`${tabs[activeTab]} - Statistiques`);

const addFilter = ({ field, value }) => {
setFilterPersons((filters) => [...filters, { field, value }]);
};

if (loading) return <Loading />;

const persons = getDataForPeriod(
allPersons.filter((e) => viewAllOrganisationData || (e.assignedTeams || []).includes(currentTeam._id)),
period,
Expand Down Expand Up @@ -105,6 +104,27 @@ const Stats = () => {
viewAllOrganisationData,
{ field: 'date' }
);
const personsInPassagesBeforePeriod = useMemo(() => {
if (!period?.startDate) return [];
const passagesIds = passages.map((p) => p._id);
const passagesNotIncludedInPeriod = allPassages
.filter((p) => !passagesIds.includes(p._id))
.filter((p) => dayjsInstance(p.date).isBefore(period.startDate));
return passagesNotIncludedInPeriod.reduce((personsIds, passage) => {
if (!passage.person) return personsIds;
if (personsIds.includes(passage.person)) return personsIds;
return [...personsIds, passage.person];
}, []);
}, [allPassages, passages, period.startDate]);
const personsInPassagesOfPeriod = useMemo(
() =>
passages.reduce((personsIds, passage) => {
if (!passage.person) return personsIds;
if (personsIds.includes(passage.person)) return personsIds;
return [...personsIds, passage.person];
}, []),
[passages]
);

const reports = getDataForPeriod(
allreports.filter((e) => viewAllOrganisationData || e.team === currentTeam._id),
Expand All @@ -123,6 +143,8 @@ const Stats = () => {
...customFieldsPersonsMedical.filter((a) => a.enabled).map((a) => ({ field: a.name, ...a })),
];

if (loading) return <Loading />;

return (
<>
<Header
Expand Down Expand Up @@ -356,6 +378,21 @@ const Stats = () => {
}
</TabPane>
<TabPane tabId={4}>
<Title>Statistiques des passages</Title>
<Row>
<Block data={passages.length} title="Nombre de passages" />
<Block data={passages.filter((p) => !p.person).length} title="Nombre de passages anonymes" />
<Block data={passages.filter((p) => !!p.person).length} title="Nombre de passages non anonymes" />
</Row>
<Row>
<Block data={personsInPassagesOfPeriod.length} title="Nombre de personnes différentes passées (passages anonymes exclus)" />
<Block
data={personsInPassagesOfPeriod.filter((personId) => !personsInPassagesBeforePeriod.includes(personId)).length}
title="Nombre de nouvelles personnes passées (passages anonymes exclus)"
/>
</Row>
</TabPane>
<TabPane tabId={5}>
<Title>Statistiques des observations de territoire</Title>
<div style={{ maxWidth: '350px', marginBottom: '2rem' }}>
<Label htmlFor="filter-territory">Filter par territoire</Label>
Expand Down Expand Up @@ -407,7 +444,7 @@ const Stats = () => {
))}
</Row>
</TabPane>
<TabPane tabId={5}>
<TabPane tabId={6}>
<Title>Statistiques des comptes-rendus</Title>
<CustomResponsivePie
title="Répartition des comptes-rendus par collaboration"
Expand Down

0 comments on commit 7bc04ef

Please sign in to comment.