-
Notifications
You must be signed in to change notification settings - Fork 1
Automatic Dictionary Feature #13
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
Open
cotrin
wants to merge
2
commits into
master
Choose a base branch
from
feat/automatic-dictionary
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"; | ||
| 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
17
src/commands/profile/dictionary/edit-dictionary-content.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
69
src/commands/profile/dictionary/get-dashboard-dictionary.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Hard-coded credentials