diff --git a/manifest.webapp b/manifest.webapp index 51a32d6ba6d1d48c397d20effe3a92fbbf05b20e..2ea181515f204d761c9df6232ed3881ffbc8befb 100644 --- a/manifest.webapp +++ b/manifest.webapp @@ -3,7 +3,7 @@ "slug": "ecolyo", "icon": "icon.svg", "categories": ["energy"], - "version": "1.0.9", + "version": "1.1.0", "licence": "AGPL-3.0", "editor": "Métropole de Lyon", "default_locale": "fr", @@ -98,10 +98,6 @@ "type": "node", "file": "services/monthlyReportNotification/ecolyo.js", "trigger": "@cron 0 0 10 3 * *" - }, - "aggregatorUsageEvents": { - "type": "node", - "file": "services/aggregatorUsageEvents/ecolyo.js" } }, "permissions": { diff --git a/package.json b/package.json index ca6ab144a7e9f49e0a5abc92ad0d004bb2562daa..d57c05d8263d3cb15d797babf2f98dec3d5ff00e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ecolyo", - "version": "1.0.9", + "version": "1.1.0", "scripts": { "tx": "tx pull --all || true", "lint": "yarn lint:js && yarn lint:styles", diff --git a/src/components/Duel/DuelUnlocked.tsx b/src/components/Duel/DuelUnlocked.tsx index 2de27790be53696616a5469fbdef5c0ba5440728..48e8f92e272213882b811384d5e868acaccfffea 100644 --- a/src/components/Duel/DuelUnlocked.tsx +++ b/src/components/Duel/DuelUnlocked.tsx @@ -13,6 +13,9 @@ import defaultIcon from 'assets/icons/visu/duel/default.svg' import StyledIcon from 'components/CommonKit/Icon/StyledIcon' import Button from '@material-ui/core/Button' import { UserChallengeUpdateFlag } from 'enum/userChallenge.enum' +import { UsageEventType } from 'enum/usageEvent.enum' +import UsageEventService from 'services/usageEvent.service' + interface DuelUnlockedProps { userChallenge: UserChallenge } @@ -42,6 +45,11 @@ const DuelUnlocked: React.FC<DuelUnlockedProps> = ({ const dataloads = await challengeService.getUserChallengeDataload( updatedChallenge ) + await UsageEventService.addEvent(client, { + type: UsageEventType.DUEL_LAUNCH_EVENT, + target: userChallenge.duel.id, + context: userChallenge.id, + }) dispatch(setChallengeConsumption(updatedChallenge, dataloads)) }, [client, dispatch, userChallenge]) diff --git a/src/enum/usageEvent.enum.ts b/src/enum/usageEvent.enum.ts index a913011809290cd01c561b2bfacca40c24319971..66a5ecc88fb670fb2d59ff8a035df31a8d78d06f 100644 --- a/src/enum/usageEvent.enum.ts +++ b/src/enum/usageEvent.enum.ts @@ -7,6 +7,7 @@ export enum UsageEventType { CONSUMPTION_CHANGE_TIMESTEP_EVENT = 'ConsumptionChangeTimeStepEvent', CHALLENGE_LAUNCH_EVENT = 'ChallengeLaunchEvent', CHALLENGE_END_EVENT = 'ChallengeEndEvent', + DUEL_LAUNCH_EVENT = 'DuelLaunchEvent', DUEL_END_EVENT = 'DuelEndEvent', QUIZ_END_EVENT = 'QuizEndEvent', EXPLORATION_END_EVENT = 'ExplorationEndEvent', diff --git a/src/targets/services/aggregatorUsageEvents.ts b/src/targets/services/aggregatorUsageEvents.ts index e8b24406293717c552366b86183020dd346e6cf9..c5ad07b4dfa22bf8aa34a033df8c6eeac1979878 100644 --- a/src/targets/services/aggregatorUsageEvents.ts +++ b/src/targets/services/aggregatorUsageEvents.ts @@ -2,10 +2,12 @@ import logger from 'cozy-logger' import { Client } from 'cozy-client' import { runService } from './service' import UsageEventService from 'services/usageEvent.service' -import { UsageEvent } from 'models' +import { FluidStatus, UsageEvent } from 'models' import { UsageEventType } from 'enum/usageEvent.enum' import { DateTime } from 'luxon' import { toNumber, uniq } from 'lodash' +import FluidService from 'services/fluid.service' +import { FluidState, FluidType } from 'enum/fluid.enum' const log = logger.namespace('aggregatorUsageEvents') @@ -18,8 +20,8 @@ interface Indicator { name: string startDate: string endDate: string - value?: number - groups: { [key: string]: string } + value: number + groups: { [key: string]: string | boolean } } // Store all id of events read during the process @@ -144,6 +146,7 @@ const sendNoAggregatedEvent = async ( name: eventType, startDate: startDate, endDate: endDate, + value: 1, groups: group, } const result = await sendIndicator(indicator, client) @@ -215,6 +218,9 @@ const calculPeriodBetweenFirstConnectionAndFirstChallenge = async ( name: 'PeriodBetweenFirstConnectionAndFirstChallenge', startDate: firstConnectionEvent.eventDate.toString(), endDate: challengeLaunchEvents[0].eventDate.toString(), + value: challengeLaunchEvents[0].eventDate.diff( + firstConnectionEvent.eventDate + ).seconds, groups: {}, } await sendIndicator(periodIndicator, client) @@ -254,6 +260,9 @@ const calculPeriodBetweenChallenge = async ( previousEndedChallengeIndex ].eventDate.toString(), endDate: event.eventDate.toString(), + value: event.eventDate.diff( + allEndedChallengeEvents[previousEndedChallengeIndex].eventDate + ).seconds, groups: { target: event.target, context: prevChallengeId }, } await sendIndicator(periodChallengeIndicator, client) @@ -263,6 +272,83 @@ const calculPeriodBetweenChallenge = async ( } } +const getDuelLaunchIndicator = async (events: UsageEvent[], client: Client) => { + const duelLaunchEvents: UsageEvent[] = events.filter( + (event: UsageEvent) => event.type === UsageEventType.DUEL_LAUNCH_EVENT + ) + const progressDateEvents: DateTime[] = events + .filter( + (event: UsageEvent) => + event.type === UsageEventType.QUIZ_END_EVENT || + event.type === UsageEventType.ACTION_END_EVENT || + event.type === UsageEventType.EXPLORATION_END_EVENT + ) + .map(event => event.eventDate) + + if (duelLaunchEvents.length > 0) { + for (const duelLaunch of duelLaunchEvents) { + const duelLaunchIndicator: Indicator = { + source: 'ecolyo', + name: 'DuelLaunchEvent', + startDate: duelLaunch.eventDate.toString(), + endDate: duelLaunch.eventDate.toString(), + value: 1, + groups: { + result: progressDateEvents.includes( + duelLaunch.eventDate.startOf('day') + ) + ? true + : false, + target: duelLaunch.target ? duelLaunch.target : '', + context: duelLaunch.context ? duelLaunch.context : '', + }, + } + const result = await sendIndicator(duelLaunchIndicator, client) + + if (result) { + readUsageEvents.push(duelLaunch) + } + } + } +} + +const calculateConnectedKonnectorPerDay = async (client: Client) => { + const fluidService: FluidService = new FluidService(client) + const fluidStatus: FluidStatus[] = await fluidService.getFluidStatus() + const connectedKonnectors = fluidStatus.filter( + fluid => fluid.status === FluidState.DONE + ) + if (connectedKonnectors.length > 0) { + const konnectorfluidTypes: FluidType[] = [] + for (const konnector of connectedKonnectors) { + konnectorfluidTypes.push(konnector.fluidType) + } + const KonnectorConnectedPerDayIndicator: Indicator = { + source: 'ecolyo', + name: 'KonnectorConnectedPerDay', + startDate: DateTime.local() + .setZone('utc', { + keepLocalTime: true, + }) + .startOf('day') + .toISO(), + endDate: DateTime.local() + .setZone('utc', { + keepLocalTime: true, + }) + .startOf('day') + .toISO(), + value: connectedKonnectors.length, + groups: { + electricity: konnectorfluidTypes.includes(FluidType.ELECTRICITY), + gaz: konnectorfluidTypes.includes(FluidType.GAS), + water: konnectorfluidTypes.includes(FluidType.WATER), + }, + } + await sendIndicator(KonnectorConnectedPerDayIndicator, client) + } +} + const aggregateEvents = async ( events: UsageEvent[], eventType: UsageEventType, @@ -289,6 +375,9 @@ const aggregateEvents = async ( case UsageEventType.DUEL_END_EVENT: await sendNoAggregatedEvent(events, eventType, true, client) break + case UsageEventType.DUEL_LAUNCH_EVENT: + await getDuelLaunchIndicator(events, client) + break case UsageEventType.CHALLENGE_LAUNCH_EVENT: await sendAggregatedEventByDay(events, eventType, client) await calculPeriodBetweenFirstConnectionAndFirstChallenge( @@ -324,6 +413,7 @@ const AggregatorUsageEvents = async ({ if (events.length > 0) { log('info', `Fetching Sessions`) calculSessionTime(events, client) + calculateConnectedKonnectorPerDay(client) for (const eventType of Object.values(UsageEventType)) { log('info', `Fetching ${eventType}`) const filteredEvents: UsageEvent[] = events.filter(