@lightdash/common 0.1386.4 → 0.1387.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,8 +1,9 @@
1
- import type { SavedChart } from '..';
1
+ import type { PromotionChanges, SavedChart } from '..';
2
2
  export declare const currentVersion = 1;
3
- export type ChartAsCode = Pick<SavedChart, 'name' | 'description' | 'tableName' | 'metricQuery' | 'chartConfig' | 'tableConfig' | 'slug' | 'dashboardUuid' | 'colorPalette' | 'updatedAt'> & {
3
+ export type ChartAsCode = Pick<SavedChart, 'name' | 'description' | 'tableName' | 'metricQuery' | 'chartConfig' | 'tableConfig' | 'slug' | 'dashboardUuid' | 'updatedAt'> & {
4
4
  version: number;
5
5
  spaceSlug: string;
6
+ downloadedAt?: Date;
6
7
  };
7
8
  export type ApiChartAsCodeListResponse = {
8
9
  status: 'ok';
@@ -10,8 +11,5 @@ export type ApiChartAsCodeListResponse = {
10
11
  };
11
12
  export type ApiChartAsCodeUpsertResponse = {
12
13
  status: 'ok';
13
- results: {
14
- chart: ChartAsCode;
15
- created: boolean;
16
- };
14
+ results: PromotionChanges;
17
15
  };
@@ -5,6 +5,7 @@ export declare enum MetricExplorerComparison {
5
5
  PREVIOUS_PERIOD = "previous_period",
6
6
  DIFFERENT_METRIC = "different_metric"
7
7
  }
8
+ export type MetricExplorerDateRange = [Date | null, Date | null];
8
9
  export type MetricExplorerComparisonType = {
9
10
  type: MetricExplorerComparison.NONE;
10
11
  } | {
@@ -1,8 +1,10 @@
1
1
  import { ConditionalOperator } from '../types/conditionalRule';
2
2
  import { type DateFilterSettings, type FieldTarget, type FilterRule } from '../types/filter';
3
+ import type { MetricExplorerDateRange } from '../types/metricsExplorer';
3
4
  import { TimeFrames } from '../types/timeFrames';
4
5
  type DateFilter = FilterRule<ConditionalOperator, FieldTarget, unknown, DateFilterSettings>;
5
- export declare const getMetricExplorerDimensionCurrentFilters: (exploreName: string, dimensionName: string, timeInterval: TimeFrames | undefined) => DateFilter[];
6
- export declare const getMetricExplorerDimensionPreviousFilters: (exploreName: string, dimensionName: string, timeInterval: TimeFrames | undefined) => DateFilter[];
7
6
  export declare const getFieldIdForDateDimension: (fieldId: string, timeframe: TimeFrames) => string;
7
+ export declare const getMetricExplorerDefaultGrainFilters: (exploreName: string, dimensionName: string, timeInterval: TimeFrames | undefined) => DateFilter[];
8
+ export declare const getMetricExplorerDimensionPreviousFilters: (exploreName: string, dimensionName: string, timeInterval: TimeFrames | undefined) => DateFilter[];
9
+ export declare const getMetricExplorerDateRangeFilters: (exploreName: string, dimensionName: string, [startDate, endDate]: MetricExplorerDateRange) => DateFilter[];
8
10
  export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getFieldIdForDateDimension = exports.getMetricExplorerDimensionPreviousFilters = exports.getMetricExplorerDimensionCurrentFilters = void 0;
3
+ exports.getMetricExplorerDateRangeFilters = exports.getMetricExplorerDimensionPreviousFilters = exports.getMetricExplorerDefaultGrainFilters = exports.getFieldIdForDateDimension = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const uuid_1 = require("uuid");
6
6
  const conditionalRule_1 = require("../types/conditionalRule");
@@ -33,14 +33,32 @@ const assertUnimplementedTimeframe = (timeframe) => {
33
33
  return (0, assertUnreachable_1.default)(timeframe, `Unknown time interval: "${timeframe}"`);
34
34
  }
35
35
  };
36
+ const getFieldIdForDateDimension = (fieldId, timeframe) => {
37
+ switch (timeframe) {
38
+ case timeFrames_1.TimeFrames.DAY:
39
+ return `${fieldId}_day`;
40
+ case timeFrames_1.TimeFrames.WEEK:
41
+ return `${fieldId}_week`;
42
+ case timeFrames_1.TimeFrames.MONTH:
43
+ return `${fieldId}_month`;
44
+ case timeFrames_1.TimeFrames.YEAR:
45
+ return `${fieldId}_year`;
46
+ default:
47
+ return assertUnimplementedTimeframe(timeframe);
48
+ }
49
+ };
50
+ exports.getFieldIdForDateDimension = getFieldIdForDateDimension;
36
51
  // Time grain Year: -> past 5 years (i.e. 5 completed years + this uncompleted year)
37
52
  // Time grain Month -> past 12 months (i.e. 12 completed months + this uncompleted month)
38
53
  // Time grain Week -> past 12 weeks (i.e. 12 completed weeks + this uncompleted week)
39
54
  // Time grain Day -> past 30 days (i.e. 30 completed days + this uncompleted day)
40
- const getMetricExplorerDimensionCurrentFilters = (exploreName, dimensionName, timeInterval) => {
55
+ const getMetricExplorerDefaultGrainFilters = (exploreName, dimensionName, timeInterval) => {
56
+ const fieldWithGrain = timeInterval
57
+ ? (0, exports.getFieldIdForDateDimension)(dimensionName, timeInterval)
58
+ : dimensionName;
41
59
  const targetFieldId = (0, item_1.getItemId)({
42
60
  table: exploreName,
43
- name: dimensionName,
61
+ name: fieldWithGrain,
44
62
  });
45
63
  if (!timeInterval) {
46
64
  throw new Error('Time interval is required to get relevant filter');
@@ -127,7 +145,7 @@ const getMetricExplorerDimensionCurrentFilters = (exploreName, dimensionName, ti
127
145
  return assertUnimplementedTimeframe(timeInterval);
128
146
  }
129
147
  };
130
- exports.getMetricExplorerDimensionCurrentFilters = getMetricExplorerDimensionCurrentFilters;
148
+ exports.getMetricExplorerDefaultGrainFilters = getMetricExplorerDefaultGrainFilters;
131
149
  // Time grain Year: -> 10 years ago - 5 years ago
132
150
  // Time grain Month -> 24 months ago - 12 months ago
133
151
  // Time grain Week -> 24 weeks ago - 12 weeks ago
@@ -242,18 +260,40 @@ const getMetricExplorerDimensionPreviousFilters = (exploreName, dimensionName, t
242
260
  }
243
261
  };
244
262
  exports.getMetricExplorerDimensionPreviousFilters = getMetricExplorerDimensionPreviousFilters;
245
- const getFieldIdForDateDimension = (fieldId, timeframe) => {
246
- switch (timeframe) {
247
- case timeFrames_1.TimeFrames.DAY:
248
- return `${fieldId}_day`;
249
- case timeFrames_1.TimeFrames.WEEK:
250
- return `${fieldId}_week`;
251
- case timeFrames_1.TimeFrames.MONTH:
252
- return `${fieldId}_month`;
253
- case timeFrames_1.TimeFrames.YEAR:
254
- return `${fieldId}_year`;
255
- default:
256
- return assertUnimplementedTimeframe(timeframe);
263
+ // Time grain Year: -> past 5 years (i.e. 5 completed years + this uncompleted year)
264
+ // Time grain Month -> past 12 months (i.e. 12 completed months + this uncompleted month)
265
+ // Time grain Week -> past 12 weeks (i.e. 12 completed weeks + this uncompleted week)
266
+ // Time grain Day -> past 30 days (i.e. 30 completed days + this uncompleted day)
267
+ const getGrainForDateRange = (dateRange) => {
268
+ const diff = dateRange[1].getTime() - dateRange[0].getTime();
269
+ const days = diff / (1000 * 60 * 60 * 24);
270
+ if (days <= 31) {
271
+ return timeFrames_1.TimeFrames.DAY;
272
+ }
273
+ if (days <= 7 * 12) {
274
+ return timeFrames_1.TimeFrames.WEEK;
257
275
  }
276
+ if (days <= 366) {
277
+ return timeFrames_1.TimeFrames.MONTH;
278
+ }
279
+ return timeFrames_1.TimeFrames.YEAR;
258
280
  };
259
- exports.getFieldIdForDateDimension = getFieldIdForDateDimension;
281
+ const getMetricExplorerDateRangeFilters = (exploreName, dimensionName, [startDate, endDate]) => {
282
+ if (!startDate || !endDate) {
283
+ return [];
284
+ }
285
+ const defaultGrain = getGrainForDateRange([startDate, endDate]);
286
+ const targetFieldId = (0, item_1.getItemId)({
287
+ table: exploreName,
288
+ name: (0, exports.getFieldIdForDateDimension)(dimensionName, defaultGrain),
289
+ });
290
+ return [
291
+ {
292
+ id: (0, uuid_1.v4)(),
293
+ target: { fieldId: targetFieldId },
294
+ operator: conditionalRule_1.ConditionalOperator.IN_BETWEEN,
295
+ values: [startDate, endDate],
296
+ },
297
+ ];
298
+ };
299
+ exports.getMetricExplorerDateRangeFilters = getMetricExplorerDateRangeFilters;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lightdash/common",
3
- "version": "0.1386.4",
3
+ "version": "0.1387.1",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [