Commit 84f859eb authored by Hugo NOUTS's avatar Hugo NOUTS
Browse files

Merge branch 'feat/Add_new_events' into 'dev'

Add 2 indicators for duel and konnector

See merge request web-et-numerique/llle_project/ecolyo!354
parents b2833c7b d75a806e
......@@ -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": {
......
{
"name": "ecolyo",
"version": "1.0.9",
"version": "1.1.0",
"scripts": {
"tx": "tx pull --all || true",
"lint": "yarn lint:js && yarn lint:styles",
......
......@@ -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])
......
......@@ -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',
......
......@@ -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(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment