@lightdash/common 0.1398.0 → 0.1399.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -14,6 +14,7 @@ export type MetricExplorerComparisonType = {
14
14
  type: MetricExplorerComparison.PREVIOUS_PERIOD;
15
15
  } | {
16
16
  type: MetricExplorerComparison.DIFFERENT_METRIC;
17
+ metricTable: string;
17
18
  metricName: string;
18
19
  };
19
20
  export type MetricExploreDataPoint = {
@@ -4,7 +4,7 @@ import { type CompiledTable } from '../types/explore';
4
4
  import type { Dimension } from '../types/field';
5
5
  import { DimensionType, type CompiledDimension, type CompiledMetric } from '../types/field';
6
6
  import { type DateFilterSettings, type FieldTarget, type FilterRule } from '../types/filter';
7
- import type { MetricExploreDataPoint, MetricExplorerDateRange } from '../types/metricsExplorer';
7
+ import { type MetricExploreDataPoint, type MetricExplorerComparisonType, type MetricExplorerDateRange } from '../types/metricsExplorer';
8
8
  import type { ResultRow } from '../types/results';
9
9
  import { TimeFrames, type DefaultTimeDimension } from '../types/timeFrames';
10
10
  type DateFilter = FilterRule<ConditionalOperator, FieldTarget, unknown, DateFilterSettings>;
@@ -15,7 +15,7 @@ export declare const oneYearForward: (date: Date) => Date;
15
15
  export declare const getGrainForDateRange: (dateRange: [Date, Date]) => ImpelemntedTimeframe;
16
16
  export declare const getMetricExplorerDateRangeFilters: (exploreName: string, dimensionName: string, dateRange: MetricExplorerDateRange) => DateFilter[];
17
17
  export declare const getMetricExplorerDataPoints: (dimension: Dimension, metric: MetricWithAssociatedTimeDimension, metricRows: ResultRow[]) => Array<MetricExploreDataPoint>;
18
- export declare const getMetricExplorerDataPointsWithCompare: (dimension: Dimension, metric: MetricWithAssociatedTimeDimension, metricRows: ResultRow[], compareMetricRows: ResultRow[], offsetFunction?: (date: Date) => Date) => Array<MetricExploreDataPoint>;
18
+ export declare const getMetricExplorerDataPointsWithCompare: (dimension: Dimension, metric: MetricWithAssociatedTimeDimension, metricRows: ResultRow[], compareMetricRows: ResultRow[], comparison: MetricExplorerComparisonType) => Array<MetricExploreDataPoint>;
19
19
  /**
20
20
  * Get the date range for a given time interval, based on the current date and the time interval
21
21
  * Time grain Year: -> past 5 years (i.e. 5 completed years + this uncompleted year)
@@ -7,6 +7,7 @@ const lodash_1 = require("lodash");
7
7
  const uuid_1 = require("uuid");
8
8
  const conditionalRule_1 = require("../types/conditionalRule");
9
9
  const field_1 = require("../types/field");
10
+ const metricsExplorer_1 = require("../types/metricsExplorer");
10
11
  const timeFrames_1 = require("../types/timeFrames");
11
12
  const assertUnreachable_1 = tslib_1.__importDefault(require("./assertUnreachable"));
12
13
  const item_1 = require("./item");
@@ -106,22 +107,33 @@ const getMetricExplorerDataPoints = (dimension, metric, metricRows) => {
106
107
  }));
107
108
  };
108
109
  exports.getMetricExplorerDataPoints = getMetricExplorerDataPoints;
109
- const getMetricExplorerDataPointsWithCompare = (dimension, metric, metricRows, compareMetricRows, offsetFunction = exports.oneYearForward) => {
110
+ const getMetricExplorerDataPointsWithCompare = (dimension, metric, metricRows, compareMetricRows, comparison) => {
111
+ if (comparison.type === metricsExplorer_1.MetricExplorerComparison.NONE) {
112
+ throw new Error('Comparison type is required');
113
+ }
110
114
  const dimensionId = (0, item_1.getItemId)(dimension);
111
115
  const metricId = (0, item_1.getItemId)(metric);
112
116
  const mapDateField = (row) => new Date(String(row[dimensionId].value.raw)).toString();
113
117
  const groupByMetricRows = (0, lodash_1.groupBy)(metricRows, (row) => mapDateField(row));
114
118
  const groupByCompareMetricRows = (0, lodash_1.groupBy)(compareMetricRows, (row) => mapDateField(row));
115
- const offsetGroupByCompareMetricRows = (0, lodash_1.mapKeys)(groupByCompareMetricRows, (_, date) => offsetFunction(new Date(date)).toString());
119
+ const offsetGroupByCompareMetricRows = (0, lodash_1.mapKeys)(groupByCompareMetricRows, (_, date) => comparison.type === metricsExplorer_1.MetricExplorerComparison.PREVIOUS_PERIOD
120
+ ? (0, exports.oneYearForward)(new Date(date)).toString()
121
+ : date);
116
122
  const dates = new Set([
117
123
  ...Object.keys(groupByMetricRows),
118
124
  ...Object.keys(offsetGroupByCompareMetricRows),
119
125
  ]);
126
+ const compareMetricId = comparison.type === metricsExplorer_1.MetricExplorerComparison.PREVIOUS_PERIOD
127
+ ? metricId
128
+ : (0, item_1.getItemId)({
129
+ table: comparison.metricTable,
130
+ name: comparison.metricName,
131
+ });
120
132
  return Array.from(dates).map((date) => ({
121
133
  date: new Date(date),
122
134
  metric: groupByMetricRows[date]?.[0]?.[metricId]?.value.raw ?? null,
123
- compareMetric: offsetGroupByCompareMetricRows[date]?.[0]?.[metricId]?.value.raw ??
124
- null,
135
+ compareMetric: offsetGroupByCompareMetricRows[date]?.[0]?.[compareMetricId]?.value
136
+ .raw ?? null,
125
137
  }));
126
138
  };
127
139
  exports.getMetricExplorerDataPointsWithCompare = getMetricExplorerDataPointsWithCompare;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lightdash/common",
3
- "version": "0.1398.0",
3
+ "version": "0.1399.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [