Skip to content
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
58 changes: 58 additions & 0 deletions src/commands/profile/dictionary/dictionary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Account } from "@tago-io/sdk";

import { editAutoDictionaryContent, IDictionaryContent } from "./edit-dictionary-content";
import { generateDictionaryKey } from "./generate-dictionary-key";
import { getDashboardDictionary } from "./get-dashboard-dictionary";
import { getAutoDictionaryID } from "./get-dictionary-id";
import { getWidgetInfo } from "./get-widget-info";
import { removeDuplicatesAndEmptyStrings } from "./remove-strings";

async function dictionary() {
const accountToken = "YOUR-ACCOUNT-TOKEN";

Check failure

Code scanning / CodeQL

Hard-coded credentials

The hard-coded value "YOUR-ACCOUNT-TOKEN" is used as [authorization header](1).
const account = new Account({ token: accountToken });

const dashboardList = await account.dashboards.list();

// const dictionaryContent: IDictionaryContent = {};
// const dashboardDictionaryContent: IDictionaryContent = {};
// const widgetDictionaryContent: IDictionaryContent = {};

const allDashboardDictionaries: string[] = [];
const allWidgetDictionaries: string[] = [];
const allDictionaryContent: IDictionaryContent = {};

for (const dashboard of dashboardList) {
const dashboardInfo = await account.dashboards.info(dashboard.id);

const { dashboardDictionaries } = await getDashboardDictionary(dashboardInfo, account);
allDashboardDictionaries.push(...dashboardDictionaries);

if (!dashboardInfo.arrangement) {
console.log(`No arrangement found for dashboard ${dashboard.label}: ID-${dashboard.id}`);
continue;
}

for (const widget of dashboardInfo.arrangement) {
const widgetInfo = await getWidgetInfo(dashboard.id, widget.widget_id, accountToken);
// console.log(`Widget ${widgetInfo.label} found in dashboard ${dashboard.label}: ID-${dashboard.id}`);
console.dir(widgetInfo, { depth: null });

// }
}

const dictionaries = [...allDashboardDictionaries, ...allWidgetDictionaries];
const dictSet = removeDuplicatesAndEmptyStrings(dictionaries);
console.dir(dictSet, { depth: null });

for (const dict of dictSet) {
const dictKey = generateDictionaryKey(dict);

allDictionaryContent[dictKey] = dict;
}

const dictID = await getAutoDictionaryID(account);
await editAutoDictionaryContent(account, dictID, allDictionaryContent);
}
}

export { dictionary };
17 changes: 17 additions & 0 deletions src/commands/profile/dictionary/edit-dictionary-content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Account } from "@tago-io/sdk";

interface IDictionaryContent {
[key: string]: string;
}

async function editAutoDictionaryContent(account: Account, dictionaryID: string, content: IDictionaryContent) {
//get the current values in the dictionary
const dictionaryLanguageInfo = await account.dictionaries.languageInfo(dictionaryID, "en-US");

//TODO: Maybe sort the dictionary alphabetically before saving it

//overwrite the dictionary with the new values in the object
await account.dictionaries.languageEdit(dictionaryID, "en-US", { active: true, dictionary: { ...dictionaryLanguageInfo, ...content } });
}

export { editAutoDictionaryContent, IDictionaryContent };
22 changes: 22 additions & 0 deletions src/commands/profile/dictionary/edit-widget-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import axios, { AxiosRequestConfig } from "axios";

async function editWidgetInfo(accountToken: string, dashboardID: string, widgetID: string, widgetInfo: any) {
const config: AxiosRequestConfig = {
method: "PUT",
url: `https://api.tago.io/dashboard/${dashboardID}/widget/${widgetID}`,
headers: {
"Content-Type": "application/json",
Authorization: accountToken,
},
data: widgetInfo,
};

return axios(config)
.then((r) => r.data.result)
.catch((error) => {
console.error(config);
throw error.response.data;
});
}

export { editWidgetInfo };
9 changes: 9 additions & 0 deletions src/commands/profile/dictionary/generate-dictionary-key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function generateDictionaryKey(string: string): string {
return string
.toUpperCase()
.replaceAll(/[[\]{}().,:;!?#]+/g, " ")
.trim()
.replaceAll(/\s+/g, "_");
}

export { generateDictionaryKey };
69 changes: 69 additions & 0 deletions src/commands/profile/dictionary/get-dashboard-dictionary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Account } from "@tago-io/sdk";
import { DashboardInfo } from "@tago-io/sdk/lib/types";

import { generateDictionaryKey } from "./generate-dictionary-key";
import { isDictionaryString } from "./is-dictionary-string";

async function getDashboardDictionary(dashboardInfo: DashboardInfo, account: Account) {
const dashboardDictionaries: string[] = [];

// Dashboard Name (label)
const dashboardLabel = dashboardInfo.label;
const isDashLabelDictionary = isDictionaryString(dashboardLabel);
if (!isDashLabelDictionary) {
dashboardDictionaries.push(dashboardLabel);
const dictKey = generateDictionaryKey(dashboardLabel);
dashboardInfo.label = `#AUTO.${dictKey}#`;
}

// Dashboard Tabs Names (value)
const dashboardTabsValues = [];

for (const tab of dashboardInfo.tabs) {
if (tab.value !== undefined) {
const isDashTabValueDictionary = isDictionaryString(tab.value);

if (!isDashTabValueDictionary) {
dashboardTabsValues.push(tab?.value);
const dictKey = generateDictionaryKey(tab.value);
tab.value = `#AUTO.${dictKey}#`;
}
}
}

// Dashboard Blueprint Devices (label and placeholder)
const dashboardBlueprintDevicesLabel = [];
const dashboardBlueprintDevicesPlaceholder = [];

for (const device of dashboardInfo.blueprint_devices) {
if (device.label !== undefined) {
const isDashBlueprintDeviceLabelDictionary = isDictionaryString(device.label);

if (!isDashBlueprintDeviceLabelDictionary) {
dashboardBlueprintDevicesLabel.push(device.label);
const dictKey = generateDictionaryKey(device.label);
device.label = `#AUTO.${dictKey}#`;
}
}

if (device.placeholder !== undefined) {
const isDashBlueprintDevicePlaceholderDictionary = isDictionaryString(device.placeholder);

if (!isDashBlueprintDevicePlaceholderDictionary) {
dashboardBlueprintDevicesPlaceholder.push(device.placeholder);
const dictKey = generateDictionaryKey(device.placeholder);
device.placeholder = `#AUTO.${dictKey}#`;
}
}
}

// console.log({ dashboardLabel, dashboardTabsValues, dashboardBlueprintDevicesLabel, dashboardBlueprintDevicesPlaceholder });

dashboardDictionaries.push(...dashboardTabsValues, ...dashboardBlueprintDevicesLabel, ...dashboardBlueprintDevicesPlaceholder);

await account.dashboards.edit(dashboardInfo.id, dashboardInfo);

return { dashboardDictionaries };
}

export { getDashboardDictionary };
24 changes: 24 additions & 0 deletions src/commands/profile/dictionary/get-dictionary-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Account } from "@tago-io/sdk";

async function getAutoDictionaryID(account: Account) {
const dictionaries = await account.dictionaries.list();

// console.log(`Found ${dictionaries.length} dictionaries`);
// console.dir(dictionaries, { depth: null });

const isAutoDictionary = dictionaries.some((dictionary) => dictionary.slug === "AUTO");
let autoDictionaryID = "";

if (!isAutoDictionary) {
const { dictionary: dictionaryID } = await account.dictionaries.create({ name: "TagoIO Automatic Dictionary", slug: "AUTO", fallback: "en-US" });

autoDictionaryID = dictionaryID;
console.log({ dictionaryID });
} else {
autoDictionaryID = dictionaries.find((dictionary) => dictionary.slug === "AUTO")?.id as string;
}

return autoDictionaryID;
}

export { getAutoDictionaryID };
Loading