Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/fix/export-dashboard-enable-to-use-export-tag #15

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## What does PR do?

Brief description of the Pull request

### JIRA cards.

- Add Jira Card name

## Type of alteration

- [ ] Bug fix
- [x] New feature
- [ ] Breaking change
- [ ] Documentation update

## Tests

- Added: 0
- Removed: 0
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Run Unit Tests Linter and Build to JS

on: pull_request

jobs:
build:
runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x]

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: npm ci --ignore-scripts
- run: npm run linter
- run: npm run test
- run: npm run build
730 changes: 380 additions & 350 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions src/commands/devices/data-get.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import kleur from "kleur";

import { Account, Device, Utils } from "@tago-io/sdk";
import { Data, DataQuery } from "@tago-io/sdk/lib/types";
import { Data, DataQuery, DataQueryDefault } from "@tago-io/sdk/lib/types";

// import { DataQuery } from "@tago-io/sdk/lib/types";
import { getEnvironmentConfig } from "../../lib/config-file";
import { errorHandler, infoMSG, successMSG } from "../../lib/messages";
import { pickDeviceIDFromTagoIO } from "../../prompt/pick-device-id-from-tagoio";
Expand Down Expand Up @@ -42,7 +41,7 @@ async function getDevice(idOrToken: string, account: Account) {
* @param options - The options to create the data filter from.
* @returns The data filter object.
*/
function _createDataFilter(options: IOptions): DataQuery {
function _createDataFilter(options: IOptions): DataQueryDefault {
const filter: DataQuery = {};
if (options.var) {
filter.variables = options.var;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/devices/device-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async function deviceInfo(idOrToken: string, options: { environment: string; raw
const device = new Device({ token: idOrToken });
deviceInfo = await device
.info()
.then((r) => r as DeviceInfo)
.then((r) => r as unknown as DeviceInfo)
.catch(() => null);

if (!deviceInfo) {
Expand Down
6 changes: 3 additions & 3 deletions src/commands/devices/device-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ function mapTags(tags: TagsObj[], opt: { [key: string]: any }): any[] {
* @param opt.raw - If true, returns the ISO string representation of the date.
* @returns A formatted string representation of the date, or undefined if the date is null.
*/
function mapDate(date: Date | null, opt: { [key: string]: any }): string | undefined {
function mapDate(date: Date | null | undefined, opt: { [key: string]: any }): string | undefined {
if (opt.raw) {
return date?.toISOString();
return date?.toISOString() ?? undefined;
}
return date ? `${date?.toLocaleDateString()} ${date?.toLocaleTimeString()}` : undefined;
}
Expand Down Expand Up @@ -91,7 +91,7 @@ async function deviceList(options: IOptions) {
const resultList = deviceList.map((x) => ({
...x,
tags: options.json || options.stringify ? mapTags(x.tags, options) : x.tags.length,
last_input: mapDate(x.last_input, options),
last_input: mapDate(x?.last_input, options),
}));

if (options.stringify) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/devices/device-live-inspector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ async function inspectorConnection(deviceIdOrToken: string, options: IOptions) {
const device = new Device({ token: deviceIdOrToken, region: "usa-1" });
deviceInfo = await device
.info()
.then((r) => r as DeviceInfo)
.then((r) => r as unknown as DeviceInfo)
.catch(() => null);

if (!deviceInfo) {
Expand Down
1 change: 1 addition & 0 deletions src/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ async function handleOTPLogin({ otp_autosend }: { otp_autosend: OTPType }, { ema
*/
async function loginWithEmailPassword(email: string, password: string) {
try {
// @ts-expect-error - not required pin_code
const loginResult = await Account.login({ email, password });
return loginResult;
} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/profile/export/collect-ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,18 @@ async function getDeviceTokens(list: (DeviceListItem & { token?: string })[], ac
}

async function collectIDs(account: Account, import_account: Account, entity: Entity, export_holder: IExportHolder) {
// @ts-expect-error ts don't know what kind of tagsobj we are using
let list = await account[entity].list({
page: 1,
amount: 99,
fields: ["id", "tags"] as any,
// @ts-expect-error ts don't know what kind of tagsobj we are using
filter: { tags: [{ key: "export_id" }] },
});
// @ts-expect-error ts don't know what kind of tagsobj we are using
let import_list = await import_account[entity].list({
page: 1,
amount: 99,
fields: ["id", "tags"] as any,
// @ts-expect-error ts don't know what kind of tagsobj we are using
filter: { tags: [{ key: "export_id" }] },
});

Expand Down
2 changes: 1 addition & 1 deletion src/commands/profile/export/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ async function startExport(options: IExportOptions) {
idCollection.push("devices");
export_holder = await collectIDs(account, import_account, "devices", export_holder);
}
export_holder = await dashboardExport(account, import_account, export_holder, options);
export_holder = await dashboardExport(account, import_account, export_holder, options, userConfig.export_tag);
idCollection.push("dashboards");
break;
case "access":
Expand Down
14 changes: 7 additions & 7 deletions src/commands/profile/export/services/analysis-export.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import zlib from "zlib";
import axios from "axios";
import prompts from "prompts";
import zlib from "zlib";

import { Account } from "@tago-io/sdk";
import { AnalysisInfo } from "@tago-io/sdk/out/modules/Account/analysis.types";
import { AnalysisListItem } from "@tago-io/sdk/lib/types";

import { IExportHolder } from "../types";
import { infoMSG } from "../../../../lib/messages";
import { replaceObj } from "../../../../lib/replace-obj";
import { IExportHolder } from "../types";

/**
* Choose one of the values for your Environment Variable
Expand All @@ -29,7 +28,7 @@ async function choose_variable(key: string, values: string[]) {
return variable;
}

function separate_variable_with_duplicate_values(export_analysis: AnalysisInfo[], import_analysis: AnalysisInfo[]) {
function separate_variable_with_duplicate_values(export_analysis: AnalysisListItem[], import_analysis: AnalysisListItem[]) {
const values_by_keys: any = {};
for (const item of [...export_analysis, ...import_analysis]) {
if (!item.variables) {
Expand All @@ -55,8 +54,8 @@ function separate_variable_with_duplicate_values(export_analysis: AnalysisInfo[]

async function fixEnvironmentVariables(
import_account: Account,
export_analysis: AnalysisInfo[],
import_analysis: AnalysisInfo[],
export_analysis: AnalysisListItem[],
import_analysis: AnalysisListItem[],
analysis_info: { id: string; variables: { key: string; value: string } }[]
) {
const variables_with_duplicate_values = separate_variable_with_duplicate_values(export_analysis, import_analysis);
Expand Down Expand Up @@ -89,6 +88,7 @@ async function analysisExport(account: Account, import_account: Account, export_
// @ts-expect-error we are looking only for keys
.list({ amount: 99, fields: ["id", "name", "tags", "variables"], filter: { tags: [{ key: "export_id" }] } })
.then((r) => r.reverse());

// @ts-expect-error we are looking only for keys
const import_list = await import_account.analysis.list({ amount: 99, fields: ["id", "tags", "variables"], filter: { tags: [{ key: "export_id" }] } });

Expand Down
5 changes: 3 additions & 2 deletions src/commands/profile/export/services/collect-ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,19 @@ async function getDeviceTokens(list: (DeviceListItem & { token?: string })[], ac
}

async function collectIDs(account: Account, import_account: Account, entity: Entity, export_holder: IExportHolder) {
// @ts-expect-error ts don't know what kind of tagsobj we are using
let list = await account[entity].list({
page: 1,
amount: 99,
fields: ["id", "tags"] as any,
// @ts-expect-error ts don't know what kind of tagsobj we are using
filter: { tags: [{ key: "export_id" }] },
});

// @ts-expect-error ts don't know what kind of tagsobj we are using
let import_list = await import_account[entity].list({
page: 1,
amount: 99,
fields: ["id", "tags"] as any,
// @ts-expect-error ts don't know what kind of tagsobj we are using
filter: { tags: [{ key: "export_id" }] },
});

Expand Down
19 changes: 10 additions & 9 deletions src/commands/profile/export/services/dashboards-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ interface IQueue {
export_holder: IExportHolder;
importAccount: Account;
exportAccount: Account;
export_tag: string;
}
async function updateDashboard({ label, dash_id, import_list, export_holder, exportAccount, importAccount }: IQueue) {
async function updateDashboard({ label, dash_id, import_list, export_holder, exportAccount, importAccount, export_tag }: IQueue) {
console.info(`Exporting dashboard ${label}...`);
const exportDash = await exportAccount.dashboards.info(dash_id).catch((error) => {
throw `Error on dashboard ${label} in export account: ${error}`;
});
const export_id = exportDash.tags?.find((tag) => tag.key === "export_id")?.value;
const export_id = exportDash.tags?.find((tag) => tag.key === export_tag)?.value;
if (!export_id) {
return;
}
await storeExportBackup("original", "dashboards", exportDash);

const importDash = await resolveDashboardTarget(importAccount, export_id, import_list, exportDash);
const importDash = await resolveDashboardTarget(importAccount, export_id, import_list, exportDash, export_tag);
if (importDash.id === exportDash.id) {
throw `Dashboard ${label} ID is the same as the export account`;
}
Expand All @@ -42,9 +43,9 @@ async function updateDashboard({ label, dash_id, import_list, export_holder, exp
export_holder.dashboards[dash_id] = importDash.id;
}

async function resolveDashboardTarget(importAccount: Account, export_id: string, import_list: DashboardInfo[], content: DashboardInfo) {
async function resolveDashboardTarget(importAccount: Account, export_id: string, import_list: DashboardInfo[], content: DashboardInfo, export_tag: string) {
const import_dashboard = import_list.find((dash) => {
const import_id = dash.tags?.find((tag) => tag.key === "export_id")?.value;
const import_id = dash.tags?.find((tag) => tag.key === export_tag)?.value;
return import_id && import_id === export_id;
});

Expand Down Expand Up @@ -76,11 +77,11 @@ async function resolveDashboardTarget(importAccount: Account, export_id: string,
return importAccount.dashboards.info(dashboard_id);
}

async function dashboardExport(exportAccount: Account, importAccount: Account, export_holder: IExportHolder, options: IExportOptions) {
async function dashboardExport(exportAccount: Account, importAccount: Account, export_holder: IExportHolder, options: IExportOptions, export_tag: string) {
console.info("Exporting dashboard: started");

// @ts-expect-error we are looking only for keys
let exportList = await exportAccount.dashboards.list({ page: 1, amount: 99, fields: ["id", "label", "tags"], filter: { tags: [{ key: "export_id" }] } });
let exportList = await exportAccount.dashboards.list({ page: 1, amount: 99, fields: ["id", "label", "tags"], filter: { tags: [{ key: export_tag }] } });
if (exportList.length > 0 && options.pick) {
const choices = exportList.map((item) => ({ title: item.label, value: item }));
exportList = await chooseFromList(choices, "Choose the dashboards you want to export:");
Expand All @@ -90,13 +91,13 @@ async function dashboardExport(exportAccount: Account, importAccount: Account, e
}

// @ts-expect-error we are looking only for keys
const import_list = await importAccount.dashboards.list({ page: 1, amount: 99, fields: ["id", "label", "tags"], filter: { tags: [{ key: "export_id" }] } });
const import_list = await importAccount.dashboards.list({ page: 1, amount: 99, fields: ["id", "label", "tags"], filter: { tags: [{ key: export_tag }] } });

const dashboardQueue = queue(updateDashboard, 3);
dashboardQueue.error(errorHandler);

for (const { id: dash_id, label } of exportList) {
void dashboardQueue.push({ dash_id, label, import_list, importAccount, export_holder, exportAccount }).catch(null);
void dashboardQueue.push({ dash_id, label, import_list, importAccount, export_holder, exportAccount, export_tag }).catch(null);
}

await dashboardQueue.drain();
Expand Down
6 changes: 3 additions & 3 deletions src/commands/start-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import prompts, { Choice } from "prompts";
import stringComparison from "string-comparison";

import { Account } from "@tago-io/sdk";
import { AnalysisInfo } from "@tago-io/sdk/lib/types";
import { AnalysisInfo, AnalysisListItem } from "@tago-io/sdk/lib/types";

import { getConfigFile, IEnvironment, writeConfigFileEnv, writeToConfigFile } from "../lib/config-file";
import { errorHandler, highlightMSG, infoMSG } from "../lib/messages";
Expand Down Expand Up @@ -69,10 +69,10 @@ async function getAnalysisList(account: Account, oldList: IEnvironment["analysis
return [];
}

const getName = (analysis: AnalysisInfo) => `[${analysis.id}] ${analysis.name}`;
const getName = (analysis: AnalysisListItem<"name" | "id" | "tags">) => `[${analysis.id}] ${analysis.name}`;

const oldIDList = new Set(oldList.map((x) => x.id));
const configList: AnalysisInfo[] = analysisList.filter((x) => oldIDList.has(x.id));
const configList: AnalysisListItem<"name" | "id" | "tags">[] = analysisList.filter((x) => oldIDList.has(x.id));

const analysisOptions = analysisList.map((x) => ({ title: getName(x), selected: configList.some((y) => y.id === x.id), value: x }));
const response = await chooseAnalysis(analysisOptions);
Expand Down