mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	feat(1-3260): impl function
This commit is contained in:
		
							parent
							
								
									44ee534b9c
								
							
						
					
					
						commit
						ef5209fe57
					
				@ -204,7 +204,7 @@ test('can query for monthly aggregation of data for a specified range', async ()
 | 
			
		||||
 | 
			
		||||
    // fill in with data for the last 13 months
 | 
			
		||||
    for (let i = 0; i <= 12; i++) {
 | 
			
		||||
        const month = subMonths(now, i).getMonth();
 | 
			
		||||
        const then = subMonths(now, i);
 | 
			
		||||
        let monthAggregateA = 0;
 | 
			
		||||
        let monthAggregateB = 0;
 | 
			
		||||
        for (let day = 1; day <= 5; day++) {
 | 
			
		||||
@ -213,14 +213,14 @@ test('can query for monthly aggregation of data for a specified range', async ()
 | 
			
		||||
            monthAggregateA += dayValue;
 | 
			
		||||
            monthAggregateB += dayValueB;
 | 
			
		||||
            const dataA = {
 | 
			
		||||
                day: new Date(2024, month, day),
 | 
			
		||||
                day: new Date(then.getFullYear(), then.getMonth(), day),
 | 
			
		||||
                trafficGroup: 'groupA',
 | 
			
		||||
                statusCodeSeries: 200,
 | 
			
		||||
                count: dayValue,
 | 
			
		||||
            };
 | 
			
		||||
            await trafficDataUsageStore.upsert(dataA);
 | 
			
		||||
            const dataB = {
 | 
			
		||||
                day: new Date(2024, month, day),
 | 
			
		||||
                day: new Date(then.getFullYear(), then.getMonth(), day),
 | 
			
		||||
                trafficGroup: 'groupB',
 | 
			
		||||
                statusCodeSeries: 200,
 | 
			
		||||
                count: dayValueB,
 | 
			
		||||
@ -233,14 +233,12 @@ test('can query for monthly aggregation of data for a specified range', async ()
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    console.log(expectedValues);
 | 
			
		||||
 | 
			
		||||
    for (const monthsBack of [3, 6, 12]) {
 | 
			
		||||
        const result =
 | 
			
		||||
            await trafficDataUsageStore.getTrafficDataForMonthRange(monthsBack);
 | 
			
		||||
 | 
			
		||||
        // should have the current month and the preceding n months
 | 
			
		||||
        expect(result.length).toBe(monthsBack + 1);
 | 
			
		||||
        expect(result.length).toBe((monthsBack + 1) * 2);
 | 
			
		||||
 | 
			
		||||
        for (const entry of result) {
 | 
			
		||||
            const index = differenceInCalendarMonths(
 | 
			
		||||
 | 
			
		||||
@ -56,7 +56,7 @@ export class TrafficDataUsageStore implements ITrafficDataUsageStore {
 | 
			
		||||
    }
 | 
			
		||||
    async exists(key: IStatTrafficUsageKey): Promise<boolean> {
 | 
			
		||||
        const result = await this.db.raw(
 | 
			
		||||
            `SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE 
 | 
			
		||||
            `SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE
 | 
			
		||||
                day = ? AND
 | 
			
		||||
                traffic_group = ? AND
 | 
			
		||||
                status_code_series ?) AS present`,
 | 
			
		||||
@ -102,6 +102,33 @@ export class TrafficDataUsageStore implements ITrafficDataUsageStore {
 | 
			
		||||
    async getTrafficDataForMonthRange(
 | 
			
		||||
        monthsBack: number,
 | 
			
		||||
    ): Promise<IStatMonthlyTrafficUsage[]> {
 | 
			
		||||
        return [];
 | 
			
		||||
        const rows = await this.db(TABLE)
 | 
			
		||||
            .select(
 | 
			
		||||
                'traffic_group',
 | 
			
		||||
                'status_code_series',
 | 
			
		||||
                this.db.raw(`to_char(day, 'YYYY-MM') AS month`),
 | 
			
		||||
                this.db.raw(`SUM(count) AS count`),
 | 
			
		||||
            )
 | 
			
		||||
            .whereRaw(
 | 
			
		||||
                `day >= date_trunc('month', CURRENT_DATE) - make_interval(months := ?)`,
 | 
			
		||||
                [monthsBack],
 | 
			
		||||
            )
 | 
			
		||||
            .groupBy([
 | 
			
		||||
                'traffic_group',
 | 
			
		||||
                this.db.raw(`to_char(day, 'YYYY-MM')`),
 | 
			
		||||
                'status_code_series',
 | 
			
		||||
            ])
 | 
			
		||||
            .orderBy([
 | 
			
		||||
                { column: 'month', order: 'desc' },
 | 
			
		||||
                { column: 'traffic_group', order: 'asc' },
 | 
			
		||||
            ]);
 | 
			
		||||
        return rows.map(
 | 
			
		||||
            ({ traffic_group, status_code_series, month, count }) => ({
 | 
			
		||||
                trafficGroup: traffic_group,
 | 
			
		||||
                statusCodeSeries: status_code_series,
 | 
			
		||||
                month,
 | 
			
		||||
                count: Number.parseInt(count),
 | 
			
		||||
            }),
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user