@lightdash/cli 0.1425.3 → 0.1425.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,5 +4,6 @@ export type DownloadHandlerOptions = {
4
4
  dashboards: string[];
5
5
  force: boolean;
6
6
  };
7
+ export declare const downloadContent: (ids: string[], type: 'charts' | 'dashboards', projectId: string) => Promise<[number, string[]]>;
7
8
  export declare const downloadHandler: (options: DownloadHandlerOptions) => Promise<void>;
8
9
  export declare const uploadHandler: (options: DownloadHandlerOptions) => Promise<void>;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.uploadHandler = exports.downloadHandler = void 0;
3
+ exports.uploadHandler = exports.downloadHandler = exports.downloadContent = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  /* eslint-disable no-await-in-loop */
6
6
  /* eslint-disable no-param-reassign */
@@ -34,7 +34,7 @@ const dumpIntoFiles = async (folder, items) => {
34
34
  // Make directory
35
35
  const created = await fs_1.promises.mkdir(outputDir, { recursive: true });
36
36
  if (created)
37
- console.info(`Created new folder: ${outputDir} `);
37
+ console.info(`\nCreated new folder: ${outputDir} `);
38
38
  for (const item of items) {
39
39
  const itemPath = path.join(outputDir, `${item.slug}.yml`);
40
40
  const chartYml = yaml.dump(item, {
@@ -87,6 +87,48 @@ const readCodeFiles = async (folder) => {
87
87
  }
88
88
  return items;
89
89
  };
90
+ const downloadContent = async (ids, // slug, uuid or url
91
+ type, projectId) => {
92
+ const spinner = globalState_1.default.getActiveSpinner();
93
+ const contentFilters = parseContentFilters(ids);
94
+ let contentAsCode;
95
+ let offset = 0;
96
+ let chartSlugs = [];
97
+ do {
98
+ globalState_1.default.debug(`Downloading ${type} with offset "${offset}" and filters "${contentFilters}"`);
99
+ const queryParams = contentFilters
100
+ ? `${contentFilters}&offset=${offset}`
101
+ : `?offset=${offset}`;
102
+ contentAsCode = await (0, apiClient_1.lightdashApi)({
103
+ method: 'GET',
104
+ url: `/api/v1/projects/${projectId}/${type}/code${queryParams}`,
105
+ body: undefined,
106
+ });
107
+ spinner?.start(`Downloaded ${contentAsCode.offset} of ${contentAsCode.total} ${type}`);
108
+ contentAsCode.missingIds.forEach((missingId) => {
109
+ console.warn(styles.warning(`\nNo chart with id "${missingId}"`));
110
+ });
111
+ if ('dashboards' in contentAsCode) {
112
+ await dumpIntoFiles('dashboards', contentAsCode.dashboards);
113
+ // Extract chart slugs from dashboards
114
+ chartSlugs = contentAsCode.dashboards.reduce((acc, dashboard) => {
115
+ const slugs = dashboard.tiles.map((chart) => 'chartSlug' in chart.properties
116
+ ? chart.properties.chartSlug
117
+ : undefined);
118
+ return [
119
+ ...acc,
120
+ ...slugs.filter((slug) => slug !== undefined),
121
+ ];
122
+ }, chartSlugs);
123
+ }
124
+ else {
125
+ await dumpIntoFiles('charts', contentAsCode.charts);
126
+ }
127
+ offset = contentAsCode.offset;
128
+ } while (contentAsCode.offset < contentAsCode.total);
129
+ return [contentAsCode.total, [...new Set(chartSlugs)]];
130
+ };
131
+ exports.downloadContent = downloadContent;
90
132
  const downloadHandler = async (options) => {
91
133
  globalState_1.default.setVerbose(options.verbose);
92
134
  await (0, apiClient_1.checkLightdashVersion)();
@@ -120,28 +162,8 @@ const downloadHandler = async (options) => {
120
162
  }
121
163
  else {
122
164
  const spinner = globalState_1.default.startSpinner(`Downloading charts`);
123
- const chartFilters = parseContentFilters(options.charts);
124
- let chartsAsCode;
125
- let offset = 0;
126
- do {
127
- globalState_1.default.debug(`Downloading charts with offset "${offset}" and filters "${chartFilters}"`);
128
- const queryParams = chartFilters
129
- ? `${chartFilters}&offset=${offset}`
130
- : `?offset=${offset}`;
131
- chartsAsCode = await (0, apiClient_1.lightdashApi)({
132
- method: 'GET',
133
- url: `/api/v1/projects/${projectId}/charts/code${queryParams}`,
134
- body: undefined,
135
- });
136
- spinner.start(`Downloaded ${chartsAsCode.offset} of ${chartsAsCode.total} charts`);
137
- chartsAsCode.missingIds.forEach((missingId) => {
138
- console.warn(styles.warning(`\nNo chart with id "${missingId}"`));
139
- });
140
- await dumpIntoFiles('charts', chartsAsCode.charts);
141
- offset = chartsAsCode.offset;
142
- } while (chartsAsCode.offset < chartsAsCode.total);
143
- chartTotal = chartsAsCode.total;
144
- spinner.succeed(`Downloaded ${chartsAsCode.total} charts`);
165
+ [chartTotal] = await (0, exports.downloadContent)(options.charts, 'charts', projectId);
166
+ spinner.succeed(`Downloaded ${chartTotal} charts`);
145
167
  }
146
168
  // Download dashboards
147
169
  if (hasFilters && options.dashboards.length === 0) {
@@ -149,28 +171,16 @@ const downloadHandler = async (options) => {
149
171
  }
150
172
  else {
151
173
  const spinner = globalState_1.default.startSpinner(`Downloading dashboards`);
152
- const dashboardFilters = parseContentFilters(options.dashboards);
153
- let offset = 0;
154
- let dashboardsAsCode;
155
- do {
156
- globalState_1.default.debug(`Downloading dashboards with offset "${offset}" and filters "${dashboardFilters}"`);
157
- const queryParams = dashboardFilters
158
- ? `${dashboardFilters}&offset=${offset}`
159
- : `?offset=${offset}`;
160
- dashboardsAsCode = await (0, apiClient_1.lightdashApi)({
161
- method: 'GET',
162
- url: `/api/v1/projects/${projectId}/dashboards/code${queryParams}`,
163
- body: undefined,
164
- });
165
- dashboardsAsCode.missingIds.forEach((missingId) => {
166
- console.warn(styles.warning(`\nNo dashboard with id "${missingId}"`));
167
- });
168
- spinner?.start(`Downloaded ${dashboardsAsCode.offset} of ${dashboardsAsCode.total} dashboards`);
169
- await dumpIntoFiles('dashboards', dashboardsAsCode.dashboards);
170
- offset = dashboardsAsCode.offset;
171
- } while (dashboardsAsCode.offset < dashboardsAsCode.total);
172
- dashboardTotal = dashboardsAsCode.total;
173
- spinner.succeed(`Downloaded ${dashboardsAsCode.total} dashboards`);
174
+ let chartSlugs = [];
175
+ [dashboardTotal, chartSlugs] = await (0, exports.downloadContent)(options.dashboards, 'dashboards', projectId);
176
+ spinner.succeed(`Downloaded ${dashboardTotal} dashboards`);
177
+ // If any filter is provided, we download all charts for these dashboard
178
+ // We don't need to do this if we download everything (no filters)
179
+ if (hasFilters) {
180
+ spinner.start(`Downloading ${chartSlugs.length} charts linked to dashboards`);
181
+ const [totalCharts] = await (0, exports.downloadContent)(chartSlugs, 'charts', projectId);
182
+ spinner.succeed(`Downloaded ${totalCharts} charts linked to dashboards`);
183
+ }
174
184
  }
175
185
  const end = Date.now();
176
186
  await analytics_1.LightdashAnalytics.track({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lightdash/cli",
3
- "version": "0.1425.3",
3
+ "version": "0.1425.5",
4
4
  "license": "MIT",
5
5
  "bin": {
6
6
  "lightdash": "dist/index.js"
@@ -11,8 +11,8 @@
11
11
  ],
12
12
  "dependencies": {
13
13
  "@actions/core": "^1.11.1",
14
- "@lightdash/common": "^0.1425.3",
15
- "@lightdash/warehouses": "^0.1425.3",
14
+ "@lightdash/common": "^0.1425.5",
15
+ "@lightdash/warehouses": "^0.1425.5",
16
16
  "@types/columnify": "^1.5.1",
17
17
  "ajv": "^8.11.0",
18
18
  "ajv-formats": "^2.1.1",