diff --git a/src/targets/services/aggregatorUsageEvents.ts b/src/targets/services/aggregatorUsageEvents.ts index 4f526df8cf8d605e29e594c7bf319c0e5e9418c2..59427ed6eb2a65de2809cc8fda102c720c58ae77 100644 --- a/src/targets/services/aggregatorUsageEvents.ts +++ b/src/targets/services/aggregatorUsageEvents.ts @@ -381,21 +381,16 @@ const calculateConnectedKonnectorPerDay = async (client: Client) => { */ const buildProfileWithFuildType = async ( client: Client, - fluidType: FluidType, - monthToAggregate?: number + fluidType: FluidType ): 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 date = monthToAggregate - ? DateTime.local() - .setZone('utc', { - keepLocalTime: true, - }) - .set({ month: monthToAggregate }) - : null + const date = DateTime.local().setZone('utc', { + keepLocalTime: true, + }) const profileType = await new ProfileTypeEntityService(client).getProfileType( date ) @@ -481,15 +476,10 @@ const buildProfileWithFuildType = async ( const getConsumptionValue = async ( client: Client, - fluidType: FluidType[], - monthToAggregate?: number + fluidType: FluidType[] ): Promise<PerformanceIndicator[]> => { const consumptionService = new ConsumptionService(client) - const analysisDate = monthToAggregate - ? DateTime.local() - .setZone('utc', { keepLocalTime: true }) - .set({ month: monthToAggregate }) - : DateTime.local().setZone('utc', { keepLocalTime: true }) + const analysisDate = DateTime.local().setZone('utc', { keepLocalTime: true }) const periods = { timePeriod: { startDate: analysisDate.minus({ month: 1 }).startOf('month'), @@ -514,16 +504,13 @@ const getConsumptionValue = async ( * @param client * @group [{ slug }, { seniority (in month) }, { profile (ECS, chauffage, etc...) }], */ -const calculateConsumptionVariation = async ( - client: Client, - monthToAggregate?: number -) => { +const calculateConsumptionVariation = async (client: Client) => { log('info', `calculateConsumptionVariation`) - const consumptionData = await getConsumptionValue( - client, - [FluidType.ELECTRICITY, FluidType.GAS, FluidType.WATER], - monthToAggregate - ) + const consumptionData = await getConsumptionValue(client, [ + FluidType.ELECTRICITY, + FluidType.GAS, + FluidType.WATER, + ]) for (const fluidType in [ FluidType.ELECTRICITY, FluidType.GAS, @@ -539,15 +526,7 @@ const calculateConsumptionVariation = async ( if (firstConnectionEvent) { 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() + // DateTime.local() // .setZone('utc', { // keepLocalTime: true, // }) @@ -557,20 +536,12 @@ const calculateConsumptionVariation = async ( const consumptionVariationIndicator: Indicator = { createdBy: 'ecolyo', measureName: DaccEvent.CONSUMPTION_VARIATION_MONTHLY, - startDate: monthToAggregate - ? DateTime.local() - .setZone('utc', { - keepLocalTime: true, - }) - .set({ month: monthToAggregate }) - .startOf('day') - .toISODate() - : DateTime.local() - .setZone('utc', { - keepLocalTime: true, - }) - .startOf('day') - .toISODate(), + startDate: DateTime.local() + .setZone('utc', { + keepLocalTime: true, + }) + .startOf('day') + .toISODate(), value: consumptionData[fluidType] && consumptionData[fluidType].percentageVariation @@ -584,55 +555,35 @@ const calculateConsumptionVariation = async ( // eslint-disable-next-line @typescript-eslint/camelcase fluid_usage: await buildProfileWithFuildType( client, - getFluidType(FluidType[fluidType]), - monthToAggregate + getFluidType(FluidType[fluidType]) ), }, } // if user wasnt connected during current month, dont send indicator const events: UsageEvent[] = await UsageEventService.getEvents(client, { - 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(), - }, + 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(), + }, }) if (events.length > 0 && consumptionVariationIndicator.value !== 0) { log( 'info', - `Send variation indicator for ${monthToAggregate} : ${JSON.stringify( + `Send variation indicator : ${JSON.stringify( consumptionVariationIndicator )}` ) @@ -643,49 +594,27 @@ const calculateConsumptionVariation = async ( } } -const sendConnectionCount = async ( - client: Client, - monthToAggregate?: number -) => { +const sendConnectionCount = async (client: Client) => { log('info', `sendConnectionCount`) // Get daily connexion const events: UsageEvent[] = await UsageEventService.getEvents(client, { type: UsageEventType.CONNECTION_EVENT, - 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(), - }, + 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(), + }, }) let uniqueDates = 0 @@ -699,27 +628,17 @@ const sendConnectionCount = async ( const connectionMonthly: Indicator = { createdBy: 'ecolyo', measureName: DaccEvent.CONNECTION_COUNT_MONTHLY, - startDate: monthToAggregate - ? DateTime.local() - .setZone('utc', { - keepLocalTime: true, - }) - .set({ month: monthToAggregate }) - .startOf('day') - .toISODate() - : DateTime.local() - .setZone('utc', { - keepLocalTime: true, - }) - .startOf('day') - .toISODate(), + startDate: DateTime.local() + .setZone('utc', { + keepLocalTime: true, + }) + .startOf('day') + .toISODate(), value: uniqueDates, } log( 'info', - `Send connectionMonthly indicator for ${monthToAggregate} : ${JSON.stringify( - connectionMonthly - )}` + `Send connectionMonthly indicator : ${JSON.stringify(connectionMonthly)}` ) await sendIndicator(connectionMonthly, client) } @@ -1332,21 +1251,6 @@ const AggregatorUsageEvents = async ({ 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 === 11 && 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',