Skip to content

Commit

Permalink
feat: ca marche de ouf
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud AMBROSELLI committed Feb 6, 2024
1 parent 16bcd46 commit 7d079d0
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 43 deletions.
1 change: 0 additions & 1 deletion dashboard/src/components/EvolutiveStatsSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useState } from 'react';
import SelectCustom from './SelectCustom';
import { components } from 'react-select';
import { dayjsInstance } from '../services/date';
import DatePicker from './DatePicker';
import type { FilterField } from '../types/field';
Expand Down
19 changes: 8 additions & 11 deletions dashboard/src/components/EvolutiveStatsViewer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useRecoilValue } from 'recoil';
import { evolutiveStatsIndicatorsBaseSelector, evolutiveStatsPersonSelector } from '../recoil/evolutiveStats';
import { evolutiveStatsIndicatorsBaseSelector, evolutiveStatsPersonSelector, startHistoryFeatureDate } from '../recoil/evolutiveStats';
import type { PersonPopulated } from '../types/person';
import type { IndicatorsSelection } from '../types/evolutivesStats';
import { dayjsInstance } from '../services/date';
Expand All @@ -25,24 +25,21 @@ export default function EvolutiveStatsViewer({ evolutiveStatsIndicators, period,
})
);
const indicatorsBase = useRecoilValue(evolutiveStatsIndicatorsBaseSelector);
if (!evolutiveStatsIndicators.length) return null;
const indicator = evolutiveStatsIndicators[0];

console.log({
evolutiveStatsPerson,
startDate,
endDate,
evolutiveStatsIndicators,
indicator,
});

if (!evolutiveStatsIndicators.length) return null;

const indicator = evolutiveStatsIndicators[0];

if (!indicator.fieldName) return null;
if (!startDate) return null;
if (!endDate) return null;
if (!endDate) return null;

const startDateFormatted = dayjsInstance(startDate);
const endDateFormatted = dayjsInstance(endDate);
const startDateFormatted = dayjsInstance(startDate ?? startHistoryFeatureDate);
const endDateFormatted = endDate ? dayjsInstance(endDate) : dayjsInstance();

if (startDateFormatted.isSame(endDateFormatted)) return null;

Expand Down Expand Up @@ -114,7 +111,7 @@ function MyResponsiveStream({ indicator, evolutiveStatsPerson, startDateFormatte
const legend = [];
const fieldData = evolutiveStatsPerson[indicator.fieldName];
const daysDiff = endDateFormatted.diff(startDateFormatted, 'days');
const spacing = Math.round(Math.max(1, daysDiff / 12));
const spacing = Math.floor(Math.max(1, daysDiff / 12));
for (let i = 0; i < daysDiff; i += spacing) {
const date = startDateFormatted.add(i, 'days');
legend.push(date.format('DD/MM/YYYY'));
Expand Down
89 changes: 59 additions & 30 deletions dashboard/src/recoil/evolutiveStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@ import type { PersonInstance } from '../types/person';
import type { CustomOrPredefinedField } from '../types/field';
import type { EvolutiveStatsPersonFields, EvolutiveStatOption, EvolutiveStatDateYYYYMMDD } from '../types/evolutivesStats';
import { dayjsInstance } from '../services/date';
import { personFieldsIncludingCustomFieldsSelector, personsState } from './persons';
import { personFieldsIncludingCustomFieldsSelector } from './persons';

export const evolutiveStatsIndicatorsBaseSelector = selector({
key: 'evolutiveStatsIndicatorsBaseSelector',
get: ({ get }) => {
const now = Date.now();
const allFields = get(personFieldsIncludingCustomFieldsSelector);
const indicatorsBase = allFields.filter((f) => {
if (f.name === 'history') return false;
if (f.name === 'documents') return false;
switch (f.type) {
case 'text':
case 'textarea':
case 'number':
case 'date':
case 'date-with-time':
case 'multi-choice':
case 'boolean':
return false;
case 'multi-choice':
case 'number':
case 'yes-no':
case 'enum':
case 'boolean':
default:
return f.filterable;
}
Expand All @@ -34,6 +33,8 @@ export const evolutiveStatsIndicatorsBaseSelector = selector({
},
});

export const startHistoryFeatureDate = '2022-09-23';

export const evolutiveStatsPersonSelector = selectorFamily({
key: 'evolutiveStatsPersonSelector',
get:
Expand Down Expand Up @@ -63,7 +64,7 @@ export const evolutiveStatsPersonSelector = selectorFamily({
return ['Non renseigné'];
}

function getValueByField(fieldName: CustomOrPredefinedField['name'], value: any): string {
function getValueByField(fieldName: CustomOrPredefinedField['name'], value: any): string | Array<string> {
if (!fieldName) return '';
const current = fieldsMap[fieldName];
if (!current) return '';
Expand All @@ -72,21 +73,24 @@ export const evolutiveStatsPersonSelector = selectorFamily({
return 'Non';
}
if (['boolean'].includes(current.type)) {
if (value === true) return 'Oui';
if (value === true || value === 'Oui') return 'Oui';
return 'Non';
}
if (current?.name === 'outOfActiveList') {
if (value === true) return 'Oui';
return 'Non';
}
if (value == null || value === '') return 'Non renseigné'; // we cover the case of undefined, null, empty string
if (value == null || value === '') {
if (current.type === 'multi-choice') return [];
return 'Non renseigné'; // we cover the case of undefined, null, empty string
}
if (value.includes('Choisissez un genre')) return 'Non renseigné';
return value;
}

// we take the years since the history began, let's say early 2023
const dates: Record<EvolutiveStatDateYYYYMMDD, number> = {};
const minimumDateForEvolutiveStats = dayjsInstance(startDate ?? '2022-09-23').format('YYYYMMDD');
const minimumDateForEvolutiveStats = dayjsInstance(startDate ?? startHistoryFeatureDate).format('YYYYMMDD');
let date = minimumDateForEvolutiveStats;
const today = dayjsInstance().format('YYYYMMDD');
while (date !== today) {
Expand All @@ -104,22 +108,27 @@ export const evolutiveStatsPersonSelector = selectorFamily({
}
}

for (const person of persons) {
for (const [index, person] of Object.entries(persons)) {
const followedSince = dayjsInstance(person.followedSince || person.createdAt).format('YYYYMMDD');
const minimumDate = followedSince < minimumDateForEvolutiveStats ? minimumDateForEvolutiveStats : followedSince;
let currentDate = today;
let currentPerson = structuredClone(person);
for (const field of indicatorsBase) {
const value = getValueByField(field.name, currentPerson[field.name]);
if (value === '') continue;
try {
if (!personsFieldsInHistoryObject[field.name][value][currentDate]) {
personsFieldsInHistoryObject[field.name][value][currentDate] = 0;
const rawValue = getValueByField(field.name, currentPerson[field.name]);
if (rawValue === '') continue;
const valueToLoop = Array.isArray(rawValue) ? rawValue : [rawValue];
for (const value of valueToLoop) {
try {
if (!personsFieldsInHistoryObject[field.name][value]) {
personsFieldsInHistoryObject[field.name][value] = { ...dates };
}
if (!personsFieldsInHistoryObject[field.name][value][currentDate]) {
personsFieldsInHistoryObject[field.name][value][currentDate] = 0;
}
personsFieldsInHistoryObject[field.name][value][currentDate]++;
} catch (error) {
capture(error, { extra: { person, field, value, currentDate } });
}
personsFieldsInHistoryObject[field.name][value][currentDate]++;
} catch (error) {
capture(error, { extra: { person, field, value, currentDate } });
return personsFieldsInHistoryObject;
}
}
const history = person.history;
Expand All @@ -130,19 +139,29 @@ export const evolutiveStatsPersonSelector = selectorFamily({
while (currentDate > historyDate && currentDate > minimumDate) {
currentDate = dayjsInstance(currentDate).subtract(1, 'day').format('YYYYMMDD');
for (const field of indicatorsBase) {
const value = getValueByField(field.name, currentPerson[field.name]);
if (value === '') continue;
if (!personsFieldsInHistoryObject[field.name][value][currentDate]) {
personsFieldsInHistoryObject[field.name][value][currentDate] = 0;
const rawValue = getValueByField(field.name, currentPerson[field.name]);
if (rawValue === '') continue;
const valueToLoop = Array.isArray(rawValue) ? rawValue : [rawValue];
for (const value of valueToLoop) {
try {
if (!personsFieldsInHistoryObject[field.name][value]) {
personsFieldsInHistoryObject[field.name][value] = { ...dates };
}
if (!personsFieldsInHistoryObject[field.name][value][currentDate]) {
personsFieldsInHistoryObject[field.name][value][currentDate] = 0;
}
personsFieldsInHistoryObject[field.name][value][currentDate]++;
} catch (error) {
capture(error, { extra: { person, field, value, currentDate } });
}
}
personsFieldsInHistoryObject[field.name][value][currentDate]++;
}
}
for (const historyChangeField of Object.keys(historyItem.data)) {
const oldValue = getValueByField(historyChangeField, historyItem.data[historyChangeField].oldValue);
const historyNewValue = getValueByField(historyChangeField, historyItem.data[historyChangeField].newValue);
const currentPersonValue = getValueByField(historyChangeField, currentPerson[historyChangeField]);
if (historyNewValue !== currentPersonValue) {
if (JSON.stringify(historyNewValue) !== JSON.stringify(currentPersonValue)) {
capture(new Error('Incoherent history'), {
extra: {
person,
Expand All @@ -163,12 +182,22 @@ export const evolutiveStatsPersonSelector = selectorFamily({
while (currentDate >= minimumDate) {
currentDate = dayjsInstance(currentDate).subtract(1, 'day').format('YYYYMMDD');
for (const field of indicatorsBase) {
const value = getValueByField(field.name, currentPerson[field.name]);
if (value === '') continue;
if (!personsFieldsInHistoryObject[field.name][value][currentDate]) {
personsFieldsInHistoryObject[field.name][value][currentDate] = 0;
const rawValue = getValueByField(field.name, currentPerson[field.name]);
if (rawValue === '') continue;
const valueToLoop = Array.isArray(rawValue) ? rawValue : [rawValue];
for (const value of valueToLoop) {
try {
if (!personsFieldsInHistoryObject[field.name][value]) {
personsFieldsInHistoryObject[field.name][value] = { ...dates };
}
if (!personsFieldsInHistoryObject[field.name][value][currentDate]) {
personsFieldsInHistoryObject[field.name][value][currentDate] = 0;
}
personsFieldsInHistoryObject[field.name][value][currentDate]++;
} catch (error) {
capture(error, { extra: { person, field, value, currentDate } });
}
}
personsFieldsInHistoryObject[field.name][value][currentDate]++;
}
}
}
Expand Down
1 change: 0 additions & 1 deletion dashboard/src/scenes/stats/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,6 @@ const Stats = () => {
viewAllOrganisationData,
})
);

const filterableActionsCategories = useMemo(() => {
if (!actionsCategoriesGroups.length) return ['-- Aucune --', ...allCategories];
return groupsCategories
Expand Down

0 comments on commit 7d079d0

Please sign in to comment.