@lightdash/common 0.1419.0 → 0.1419.1
Sign up to get free protection for your applications and to get access to all the features.
@@ -12,7 +12,7 @@ type ImpelemntedTimeframe = TimeFrames.DAY | TimeFrames.WEEK | TimeFrames.MONTH
|
|
12
12
|
type UnimplementedTimeframe = Exclude<TimeFrames, ImpelemntedTimeframe>;
|
13
13
|
export declare const assertUnimplementedTimeframe: (timeframe: UnimplementedTimeframe) => never;
|
14
14
|
export declare const getFieldIdForDateDimension: (fieldId: string, timeframe: TimeFrames) => string;
|
15
|
-
export declare const getDateCalcUtils: (timeFrame: TimeFrames) => {
|
15
|
+
export declare const getDateCalcUtils: (timeFrame: TimeFrames, grain?: TimeFrames) => {
|
16
16
|
forward: (date: Date) => Date;
|
17
17
|
back: (date: Date) => Date;
|
18
18
|
};
|
@@ -25,7 +25,7 @@ export declare const getMetricExplorerDataPoints: (dimension: Dimension, metric:
|
|
25
25
|
dataPoints: Array<MetricExploreDataPoint>;
|
26
26
|
isSegmentDimensionFiltered: boolean;
|
27
27
|
};
|
28
|
-
export declare const getMetricExplorerDataPointsWithCompare: (dimension: Dimension, compareDimension: Dimension, metric: MetricWithAssociatedTimeDimension, metricRows: ResultRow[], compareMetricRows: ResultRow[], query: MetricExplorerQuery) => {
|
28
|
+
export declare const getMetricExplorerDataPointsWithCompare: (dimension: Dimension, compareDimension: Dimension, metric: MetricWithAssociatedTimeDimension, metricRows: ResultRow[], compareMetricRows: ResultRow[], query: MetricExplorerQuery, timeFrame: TimeFrames) => {
|
29
29
|
dataPoints: Array<MetricExploreDataPoint>;
|
30
30
|
};
|
31
31
|
/**
|
@@ -3,6 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getAvailableCompareMetrics = exports.getAvailableSegmentDimensions = exports.getAvailableTimeDimensionsFromTables = exports.getDefaultTimeDimension = exports.getFirstAvailableTimeDimension = exports.DEFAULT_METRICS_EXPLORER_TIME_INTERVAL = exports.getDefaultDateRangeForMetricTotal = exports.getDefaultDateRangeFromInterval = exports.getMetricExplorerDataPointsWithCompare = exports.getMetricExplorerDataPoints = exports.MAX_SEGMENT_DIMENSION_UNIQUE_VALUES = exports.getMetricExplorerDateRangeFilters = exports.getGrainForDateRange = exports.getDateRangeFromString = exports.METRICS_EXPLORER_DATE_FORMAT = exports.getDateCalcUtils = exports.getFieldIdForDateDimension = exports.assertUnimplementedTimeframe = void 0;
|
4
4
|
const tslib_1 = require("tslib");
|
5
5
|
const dayjs_1 = tslib_1.__importDefault(require("dayjs"));
|
6
|
+
const isoWeek_1 = tslib_1.__importDefault(require("dayjs/plugin/isoWeek"));
|
7
|
+
const weekOfYear_1 = tslib_1.__importDefault(require("dayjs/plugin/weekOfYear"));
|
6
8
|
const lodash_1 = require("lodash");
|
7
9
|
const uuid_1 = require("uuid");
|
8
10
|
const conditionalRule_1 = require("../types/conditionalRule");
|
@@ -11,6 +13,8 @@ const metricsExplorer_1 = require("../types/metricsExplorer");
|
|
11
13
|
const timeFrames_1 = require("../types/timeFrames");
|
12
14
|
const assertUnreachable_1 = tslib_1.__importDefault(require("./assertUnreachable"));
|
13
15
|
const item_1 = require("./item");
|
16
|
+
dayjs_1.default.extend(isoWeek_1.default);
|
17
|
+
dayjs_1.default.extend(weekOfYear_1.default);
|
14
18
|
const assertUnimplementedTimeframe = (timeframe) => {
|
15
19
|
switch (timeframe) {
|
16
20
|
case timeFrames_1.TimeFrames.RAW:
|
@@ -52,28 +56,38 @@ const getFieldIdForDateDimension = (fieldId, timeframe) => {
|
|
52
56
|
}
|
53
57
|
};
|
54
58
|
exports.getFieldIdForDateDimension = getFieldIdForDateDimension;
|
55
|
-
const getDateCalcUtils = (timeFrame) => {
|
59
|
+
const getDateCalcUtils = (timeFrame, grain) => {
|
56
60
|
switch (timeFrame) {
|
57
|
-
case timeFrames_1.TimeFrames.DAY:
|
58
|
-
return {
|
59
|
-
forward: (date) => (0, dayjs_1.default)(date).add(1, 'day').toDate(),
|
60
|
-
back: (date) => (0, dayjs_1.default)(date).subtract(1, 'day').toDate(),
|
61
|
-
};
|
62
|
-
case timeFrames_1.TimeFrames.WEEK:
|
63
|
-
return {
|
64
|
-
forward: (date) => (0, dayjs_1.default)(date).add(1, 'week').toDate(),
|
65
|
-
back: (date) => (0, dayjs_1.default)(date).subtract(1, 'week').toDate(),
|
66
|
-
};
|
67
61
|
case timeFrames_1.TimeFrames.MONTH:
|
62
|
+
if (grain)
|
63
|
+
throw new Error(`Timeframe "${grain}" is not supported yet for this timeframe "${timeFrame}"`);
|
68
64
|
return {
|
69
65
|
forward: (date) => (0, dayjs_1.default)(date).add(1, 'month').toDate(),
|
70
66
|
back: (date) => (0, dayjs_1.default)(date).subtract(1, 'month').toDate(),
|
71
67
|
};
|
72
68
|
case timeFrames_1.TimeFrames.YEAR:
|
69
|
+
// Handle week shift for previous year comparison and subtract what the amount of weeks is in a year
|
70
|
+
if (grain === timeFrames_1.TimeFrames.WEEK) {
|
71
|
+
return {
|
72
|
+
forward: (date) => (0, dayjs_1.default)(date)
|
73
|
+
// 52 weeks in a year
|
74
|
+
.add(52, 'weeks')
|
75
|
+
.startOf('isoWeek')
|
76
|
+
.toDate(),
|
77
|
+
back: (date) => (0, dayjs_1.default)(date)
|
78
|
+
// 52 weeks in a year
|
79
|
+
.subtract(52, 'weeks')
|
80
|
+
.startOf('isoWeek')
|
81
|
+
.toDate(),
|
82
|
+
};
|
83
|
+
}
|
73
84
|
return {
|
74
85
|
forward: (date) => (0, dayjs_1.default)(date).add(1, 'year').toDate(),
|
75
86
|
back: (date) => (0, dayjs_1.default)(date).subtract(1, 'year').toDate(),
|
76
87
|
};
|
88
|
+
case timeFrames_1.TimeFrames.DAY:
|
89
|
+
case timeFrames_1.TimeFrames.WEEK:
|
90
|
+
throw new Error(`Timeframe "${timeFrame}" is not supported yet`);
|
77
91
|
default:
|
78
92
|
return (0, exports.assertUnimplementedTimeframe)(timeFrame);
|
79
93
|
}
|
@@ -176,7 +190,18 @@ const getMetricExplorerDataPoints = (dimension, metric, metricRows, segmentDimen
|
|
176
190
|
};
|
177
191
|
};
|
178
192
|
exports.getMetricExplorerDataPoints = getMetricExplorerDataPoints;
|
179
|
-
|
193
|
+
function offsetWeekCompareDates(groupByCompareMetricRows, timeFrame) {
|
194
|
+
return Object.fromEntries(Object.keys(groupByCompareMetricRows).map((compareDate) => {
|
195
|
+
const compareDateWeek = (0, dayjs_1.default)(compareDate).week();
|
196
|
+
const compareDateNextYear = (0, exports.getDateCalcUtils)(timeFrames_1.TimeFrames.YEAR, timeFrame).forward(new Date(compareDate));
|
197
|
+
const adjustedCompareDate = (0, dayjs_1.default)(compareDateNextYear)
|
198
|
+
.week(compareDateWeek)
|
199
|
+
.startOf('isoWeek')
|
200
|
+
.toISOString();
|
201
|
+
return [compareDate, adjustedCompareDate];
|
202
|
+
}));
|
203
|
+
}
|
204
|
+
const getMetricExplorerDataPointsWithCompare = (dimension, compareDimension, metric, metricRows, compareMetricRows, query, timeFrame) => {
|
180
205
|
if (query.comparison === metricsExplorer_1.MetricExplorerComparison.NONE) {
|
181
206
|
throw new Error('Comparison type is required');
|
182
207
|
}
|
@@ -185,11 +210,17 @@ const getMetricExplorerDataPointsWithCompare = (dimension, compareDimension, met
|
|
185
210
|
const compareDimensionId = (0, item_1.getItemId)(compareDimension);
|
186
211
|
const groupByMetricRows = (0, lodash_1.groupBy)(metricRows, (row) => new Date(String(row[dimensionId].value.raw)).toISOString());
|
187
212
|
const groupByCompareMetricRows = (0, lodash_1.groupBy)(compareMetricRows, (row) => new Date(String(row[compareDimensionId].value.raw)).toISOString());
|
188
|
-
const offsetGroupByCompareMetricRows = (0, lodash_1.mapKeys)(groupByCompareMetricRows, (_, date) =>
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
213
|
+
const offsetGroupByCompareMetricRows = (0, lodash_1.mapKeys)(groupByCompareMetricRows, (_, date) => {
|
214
|
+
if (query.comparison === metricsExplorer_1.MetricExplorerComparison.PREVIOUS_PERIOD) {
|
215
|
+
if (timeFrame === timeFrames_1.TimeFrames.WEEK) {
|
216
|
+
return offsetWeekCompareDates(groupByCompareMetricRows, timeFrame)[date];
|
217
|
+
}
|
218
|
+
return (0, exports.getDateCalcUtils)(timeFrames_1.TimeFrames.YEAR, timeFrame)
|
219
|
+
.forward(new Date(date))
|
220
|
+
.toISOString();
|
221
|
+
}
|
222
|
+
return date;
|
223
|
+
});
|
193
224
|
const dates = new Set([
|
194
225
|
...Object.keys(groupByMetricRows),
|
195
226
|
...Object.keys(offsetGroupByCompareMetricRows),
|
@@ -245,7 +276,7 @@ const getDefaultDateRangeFromInterval = (timeInterval) => {
|
|
245
276
|
];
|
246
277
|
case timeFrames_1.TimeFrames.WEEK:
|
247
278
|
return [
|
248
|
-
now.subtract(11, 'week').startOf('
|
279
|
+
now.subtract(11, 'week').startOf('isoWeek').toDate(),
|
249
280
|
now.toDate(),
|
250
281
|
];
|
251
282
|
case timeFrames_1.TimeFrames.MONTH:
|
@@ -269,7 +300,10 @@ const getDefaultDateRangeForMetricTotal = (timeFrame) => {
|
|
269
300
|
case timeFrames_1.TimeFrames.DAY:
|
270
301
|
return [now.startOf('day').toDate(), now.endOf('day').toDate()];
|
271
302
|
case timeFrames_1.TimeFrames.WEEK:
|
272
|
-
return [
|
303
|
+
return [
|
304
|
+
now.startOf('isoWeek').toDate(),
|
305
|
+
now.endOf('isoWeek').toDate(),
|
306
|
+
];
|
273
307
|
case timeFrames_1.TimeFrames.MONTH:
|
274
308
|
return [now.startOf('month').toDate(), now.endOf('month').toDate()];
|
275
309
|
case timeFrames_1.TimeFrames.YEAR:
|