Commit ff051d17 authored by Guilhem CARRON's avatar Guilhem CARRON
Browse files

feat(dacc): Add recalculation for 2 indicators since january

- This modification is temporary and needs to be removed once the service has run
parent 88095961
......@@ -381,16 +381,24 @@ const calculateConnectedKonnectorPerDay = async (client: Client) => {
*/
const buildProfileWithFuildType = async (
client: Client,
fluidType: FluidType
fluidType: FluidType,
monthToAggregate?: number
): Promise<string> => {
let formatedProfile = ''
const profile = await new ProfileService(client).getProfile()
// If profile is not filled, return empty string
if (profile && !profile.isProfileTypeCompleted) return formatedProfile
const profileType = await new ProfileTypeEntityService(
client
).getProfileType()
if (profile && !profile.isProfileTypeCompleted) return formatedProfile
const date = monthToAggregate
? DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.set({ month: monthToAggregate })
: null
const profileType = await new ProfileTypeEntityService(client).getProfileType(
date
)
if (fluidType === FluidType.ELECTRICITY) {
if (
profile &&
......@@ -473,10 +481,15 @@ const buildProfileWithFuildType = async (
const getConsumptionValue = async (
client: Client,
fluidType: FluidType[]
fluidType: FluidType[],
monthToAggregate?: number
): Promise<PerformanceIndicator[]> => {
const consumptionService = new ConsumptionService(client)
const analysisDate = DateTime.local().setZone('utc', { keepLocalTime: true })
const analysisDate = monthToAggregate
? DateTime.local()
.setZone('utc', { keepLocalTime: true })
.set({ month: monthToAggregate })
: DateTime.local().setZone('utc', { keepLocalTime: true })
const periods = {
timePeriod: {
startDate: analysisDate.minus({ month: 1 }).startOf('month'),
......@@ -501,13 +514,16 @@ const getConsumptionValue = async (
* @param client
* @group [{ slug }, { seniority (in month) }, { profile (ECS, chauffage, etc...) }],
*/
const calculateConsumptionVariation = async (client: Client) => {
const calculateConsumptionVariation = async (
client: Client,
monthToAggregate?: number
) => {
log('info', `calculateConsumptionVariation`)
const consumptionData = await getConsumptionValue(client, [
FluidType.ELECTRICITY,
FluidType.GAS,
FluidType.WATER,
])
const consumptionData = await getConsumptionValue(
client,
[FluidType.ELECTRICITY, FluidType.GAS, FluidType.WATER],
monthToAggregate
)
for (const fluidType in [
FluidType.ELECTRICITY,
FluidType.GAS,
......@@ -521,21 +537,40 @@ const calculateConsumptionVariation = async (client: Client) => {
})
if (firstConnectionEvent) {
const seniority = DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.diff(firstConnectionEvent.eventDate).months
const seniority = 0
// Seniority is set to 0, otherwise the indicator is too specific
// monthToAggregate
// ? DateTime.local()
// .setZone('utc', {
// keepLocalTime: true,
// })
// .set({ month: monthToAggregate })
// .diff(firstConnectionEvent.eventDate, 'month')
// .get('month')
// : DateTime.local()
// .setZone('utc', {
// keepLocalTime: true,
// })
// .diff(firstConnectionEvent.eventDate, 'month')
// .get('month')
const consumptionVariationIndicator: Indicator = {
createdBy: 'ecolyo',
measureName: DaccEvent.CONSUMPTION_VARIATION_MONTHLY,
startDate: DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.startOf('day')
.toISODate(),
startDate: monthToAggregate
? DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.set({ month: monthToAggregate })
.startOf('day')
.toISODate()
: DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.startOf('day')
.toISODate(),
value:
consumptionData[fluidType] &&
consumptionData[fluidType].percentageVariation
......@@ -544,37 +579,63 @@ const calculateConsumptionVariation = async (client: Client) => {
// eslint-disable-next-line @typescript-eslint/camelcase
group1: { fluid_type: FluidType[fluidType].toLowerCase() },
// eslint-disable-next-line @typescript-eslint/camelcase
group2: { seniority: seniority.toString() },
group2: { seniority: Math.round(seniority).toString() },
group3: {
// eslint-disable-next-line @typescript-eslint/camelcase
fluid_usage: await buildProfileWithFuildType(
client,
getFluidType(FluidType[fluidType])
getFluidType(FluidType[fluidType]),
monthToAggregate
),
},
}
// if user wasnt connected during current month, dont send indicator
const events: UsageEvent[] = await UsageEventService.getEvents(client, {
eventDate: {
$lt: DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.endOf('month')
.minus({ month: 1 })
.toString(),
$gt: DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.startOf('month')
.minus({ month: 1 })
.toString(),
},
eventDate: monthToAggregate
? {
$lt: DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.set({ month: monthToAggregate })
.endOf('month')
.minus({ month: 1 })
.toString(),
$gt: DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.set({ month: monthToAggregate })
.startOf('month')
.minus({ month: 1 })
.toString(),
}
: {
$lt: DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.endOf('month')
.minus({ month: 1 })
.toString(),
$gt: DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.startOf('month')
.minus({ month: 1 })
.toString(),
},
})
if (events.length > 0 && consumptionVariationIndicator.value !== 0) {
log(
'info',
`Send variation indicator for ${monthToAggregate} : ${JSON.stringify(
consumptionVariationIndicator
)}`
)
await sendIndicator(consumptionVariationIndicator, client)
}
}
......@@ -582,27 +643,49 @@ const calculateConsumptionVariation = async (client: Client) => {
}
}
const sendConnectionCount = async (client: Client) => {
const sendConnectionCount = async (
client: Client,
monthToAggregate?: number
) => {
log('info', `sendConnectionCount`)
// Get daily connexion
const events: UsageEvent[] = await UsageEventService.getEvents(client, {
type: UsageEventType.CONNECTION_EVENT,
eventDate: {
$lt: DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.endOf('month')
.minus({ month: 1 })
.toString(),
$gt: DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.startOf('month')
.minus({ month: 1 })
.toString(),
},
eventDate: monthToAggregate
? {
$lt: DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.set({ month: monthToAggregate })
.minus({ month: 1 })
.endOf('month')
.toString(),
$gt: DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.set({ month: monthToAggregate })
.minus({ month: 1 })
.startOf('month')
.toString(),
}
: {
$lt: DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.endOf('month')
.minus({ month: 1 })
.toString(),
$gt: DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.startOf('month')
.minus({ month: 1 })
.toString(),
},
})
let uniqueDates = 0
......@@ -616,14 +699,28 @@ const sendConnectionCount = async (client: Client) => {
const connectionMonthly: Indicator = {
createdBy: 'ecolyo',
measureName: DaccEvent.CONNECTION_COUNT_MONTHLY,
startDate: DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.startOf('day')
.toISODate(),
startDate: monthToAggregate
? DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.set({ month: monthToAggregate })
.startOf('day')
.toISODate()
: DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.startOf('day')
.toISODate(),
value: uniqueDates,
}
log(
'info',
`Send connectionMonthly indicator for ${monthToAggregate} : ${JSON.stringify(
connectionMonthly
)}`
)
await sendIndicator(connectionMonthly, client)
}
......@@ -1226,14 +1323,30 @@ const AggregatorUsageEvents = async ({
})
.startOf('day').day === profile.monthlyAnalysisDate.day
) {
sendKonnectorAttemptsMonthly(client)
calculateConsumptionVariation(client)
sendEmailSubscription(client)
sendHalfHourConsumption(client)
sendKonnectorEvents(client)
sendConnectionCount(client)
sendProfileCount(client)
await sendKonnectorAttemptsMonthly(client)
await calculateConsumptionVariation(client)
await sendEmailSubscription(client)
await sendHalfHourConsumption(client)
await sendKonnectorEvents(client)
await sendConnectionCount(client)
await sendProfileCount(client)
}
log('info', `Recalculates errored indicators`)
const today: DateTime = DateTime.local()
.setZone('utc', {
keepLocalTime: true,
})
.startOf('day')
const lastMonth = 5
if (today.day === 3 && today.month === lastMonth) {
let monthToAggregate = 1
for (monthToAggregate; monthToAggregate <= lastMonth; monthToAggregate++) {
await calculateConsumptionVariation(client, monthToAggregate)
await sendConnectionCount(client, monthToAggregate)
}
}
const uniqueReadUsageEvents: UsageEvent[] = uniq(readUsageEvents)
log(
'info',
......
Supports Markdown
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