@vasrefil/api-toolkit 1.4.3 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/utilities/index.js
CHANGED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
interface ChartDataInput {
|
|
2
|
+
_id: {
|
|
3
|
+
date: string;
|
|
4
|
+
groupByField: string | number;
|
|
5
|
+
};
|
|
6
|
+
count: number;
|
|
7
|
+
}
|
|
8
|
+
export declare class ReportUtil {
|
|
9
|
+
static getDateGroupFormat(query: any): "%Y-%m" | "%Y-%m-%d";
|
|
10
|
+
static daysBetween(date1: Date, date2: Date): number;
|
|
11
|
+
/**
|
|
12
|
+
* Formats aggregated data for chart display.
|
|
13
|
+
* @param data - The aggregated data from MongoDB.
|
|
14
|
+
* Expected format: [{ _id: { date: 'YYYY-MM-DD', groupByField: 'someValue' }, count: 123 }]
|
|
15
|
+
* @returns An object with `series` and `categories` for charting libraries.
|
|
16
|
+
*/
|
|
17
|
+
static formatChartData(data: ChartDataInput[]): {
|
|
18
|
+
series: {
|
|
19
|
+
name: string;
|
|
20
|
+
data: number[];
|
|
21
|
+
}[];
|
|
22
|
+
categories: string[];
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ReportUtil = void 0;
|
|
4
|
+
class ReportUtil {
|
|
5
|
+
static getDateGroupFormat(query) {
|
|
6
|
+
const { date_type, date_range } = query;
|
|
7
|
+
if (date_type === 'custom') {
|
|
8
|
+
const daterange = date_range ? JSON.parse(date_range) : {};
|
|
9
|
+
if (daterange.start_date && daterange.end_date) {
|
|
10
|
+
const numOfDays = this.daysBetween(new Date(daterange.start_date), new Date(daterange.end_date));
|
|
11
|
+
if (numOfDays > 30) {
|
|
12
|
+
return '%Y-%m'; // Group by month for long ranges
|
|
13
|
+
}
|
|
14
|
+
return '%Y-%m-%d'; // Group by day for short ranges
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const dateGroupFormat = ['this_year', 'no_date'].includes(date_type) ? '%Y-%m' : '%Y-%m-%d';
|
|
18
|
+
return dateGroupFormat;
|
|
19
|
+
}
|
|
20
|
+
static daysBetween(date1, date2) {
|
|
21
|
+
const msPerDay = 1000 * 60 * 60 * 24;
|
|
22
|
+
return Math.abs(Math.floor((date2.getTime() - date1.getTime()) / msPerDay));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Formats aggregated data for chart display.
|
|
26
|
+
* @param data - The aggregated data from MongoDB.
|
|
27
|
+
* Expected format: [{ _id: { date: 'YYYY-MM-DD', groupByField: 'someValue' }, count: 123 }]
|
|
28
|
+
* @returns An object with `series` and `categories` for charting libraries.
|
|
29
|
+
*/
|
|
30
|
+
static formatChartData(data) {
|
|
31
|
+
const categoriesSet = new Set();
|
|
32
|
+
const groupedData = {};
|
|
33
|
+
data.forEach(({ _id, count }) => {
|
|
34
|
+
const groupByValue = String(_id.groupByField);
|
|
35
|
+
categoriesSet.add(_id.date);
|
|
36
|
+
if (!groupedData[groupByValue]) {
|
|
37
|
+
groupedData[groupByValue] = {};
|
|
38
|
+
}
|
|
39
|
+
groupedData[groupByValue][_id.date] = count;
|
|
40
|
+
});
|
|
41
|
+
const categories = Array.from(categoriesSet).sort();
|
|
42
|
+
const series = Object.keys(groupedData).map(key => {
|
|
43
|
+
return { name: key, data: categories.map((date) => groupedData[key][date] || 0) };
|
|
44
|
+
});
|
|
45
|
+
return { series, categories };
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.ReportUtil = ReportUtil;
|