Skip to content

Commit

Permalink
Merge pull request #377 from shamoon/unified-infowidget-settings
Browse files Browse the repository at this point in the history
Feature: Unified info widget settings
  • Loading branch information
benphelps committed Oct 13, 2022
2 parents c7d1c05 + 48a09e5 commit 4ea2798
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/components/widgets/unifi_console/unifi_console.jsx
Expand Up @@ -10,7 +10,7 @@ export default function Widget({ options }) {

// eslint-disable-next-line no-param-reassign
options.type = "unifi_console";
const { data: statsData, error: statsError } = useWidgetAPI(options, "stat/sites");
const { data: statsData, error: statsError } = useWidgetAPI(options, "stat/sites", { index: options.index });

if (statsError || statsData?.error) {
return (
Expand Down
26 changes: 7 additions & 19 deletions src/pages/api/widgets/glances.js
@@ -1,29 +1,17 @@
import { httpProxy } from "utils/proxy/http";
import createLogger from "utils/logger";
import { getSettings } from "utils/config/config";
import { getPrivateWidgetOptions } from "utils/config/widget-helpers";

const logger = createLogger("glances");

export default async function handler(req, res) {
const { id } = req.query;
const { index } = req.query;

let errorMessage;

let instanceID = "glances";
if (id) { // multiple instances
instanceID = id;
}
const settings = getSettings();
const instanceSettings = settings[instanceID];
if (!instanceSettings) {
errorMessage = id ? `There is no glances section with id '${id}' in settings.yaml` : "There is no glances section in settings.yaml";
logger.error(errorMessage);
return res.status(400).json({ error: errorMessage });
}
const privateWidgetOptions = await getPrivateWidgetOptions("glances", index);

const url = instanceSettings?.url;
const url = privateWidgetOptions?.url;
if (!url) {
errorMessage = "Missing Glances URL";
const errorMessage = "Missing Glances URL";
logger.error(errorMessage);
return res.status(400).json({ error: errorMessage });
}
Expand All @@ -32,8 +20,8 @@ export default async function handler(req, res) {
const headers = {
"Accept-Encoding": "application/json"
};
if (instanceSettings.username && instanceSettings.password) {
headers.Authorization = `Basic ${Buffer.from(`${instanceSettings.username}:${instanceSettings.password}`).toString("base64")}`
if (privateWidgetOptions.username && privateWidgetOptions.password) {
headers.Authorization = `Basic ${Buffer.from(`${privateWidgetOptions.username}:${privateWidgetOptions.password}`).toString("base64")}`
}
const params = { method: "GET", headers };

Expand Down
23 changes: 10 additions & 13 deletions src/utils/config/api-response.js
Expand Up @@ -6,6 +6,7 @@ import yaml from "js-yaml";

import checkAndCopyConfig from "utils/config/config";
import { servicesFromConfig, servicesFromDocker, cleanServiceGroups } from "utils/config/service-helpers";
import { cleanWidgetGroups, widgetsFromConfig } from "utils/config/widget-helpers";

export async function bookmarksResponse() {
checkAndCopyConfig("bookmarks.yaml");
Expand All @@ -29,21 +30,17 @@ export async function bookmarksResponse() {
}

export async function widgetsResponse() {
checkAndCopyConfig("widgets.yaml");
let configuredWidgets;

const widgetsYaml = path.join(process.cwd(), "config", "widgets.yaml");
const fileContents = await fs.readFile(widgetsYaml, "utf8");
const widgets = yaml.load(fileContents);

if (!widgets) return [];

// map easy to write YAML objects into easy to consume JS arrays
const widgetsArray = widgets.map((group) => ({
type: Object.keys(group)[0],
options: { ...group[Object.keys(group)[0]] },
}));
try {
configuredWidgets = cleanWidgetGroups(await widgetsFromConfig());
} catch (e) {
console.error("Failed to load widgets, please check widgets.yaml for errors or remove example entries.");
if (e) console.error(e);
configuredWidgets = [];
}

return widgetsArray;
return configuredWidgets;
}

export async function servicesResponse() {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/config/config.js
Expand Up @@ -33,4 +33,4 @@ export function getSettings() {
const settingsYaml = join(process.cwd(), "config", "settings.yaml");
const fileContents = readFileSync(settingsYaml, "utf8");
return yaml.load(fileContents);
}
}
2 changes: 1 addition & 1 deletion src/utils/config/service-helpers.js
Expand Up @@ -165,4 +165,4 @@ export default async function getServiceWidget(group, service) {
}

return false;
}
}
73 changes: 73 additions & 0 deletions src/utils/config/widget-helpers.js
@@ -0,0 +1,73 @@
import { promises as fs } from "fs";
import path from "path";

import yaml from "js-yaml";

import checkAndCopyConfig from "utils/config/config";

export async function widgetsFromConfig() {
checkAndCopyConfig("widgets.yaml");

const widgetsYaml = path.join(process.cwd(), "config", "widgets.yaml");
const fileContents = await fs.readFile(widgetsYaml, "utf8");
const widgets = yaml.load(fileContents);

if (!widgets) return [];

// map easy to write YAML objects into easy to consume JS arrays
const widgetsArray = widgets.map((group, index) => ({
type: Object.keys(group)[0],
options: {
index,
...group[Object.keys(group)[0]]
},
}));
return widgetsArray;
}

export async function cleanWidgetGroups(widgets) {
return widgets.map((widget, index) => {
const sanitizedOptions = widget.options;
const optionKeys = Object.keys(sanitizedOptions);
["url", "username", "password", "key"].forEach((pO) => {
if (optionKeys.includes(pO)) {
delete sanitizedOptions[pO];
}
});

return {
type: widget.type,
options: {
index,
...sanitizedOptions
},
}
});
}

export async function getPrivateWidgetOptions(type, widgetIndex) {
const widgets = await widgetsFromConfig();

const privateOptions = widgets.map((widget) => {
const {
index,
url,
username,
password,
key
} = widget.options;

return {
type: widget.type,
options: {
index,
url,
username,
password,
key
},
}
});

return (type !== undefined && widgetIndex !== undefined) ? privateOptions.find(o => o.type === type && o.options.index === parseInt(widgetIndex, 10))?.options : privateOptions;
}
12 changes: 6 additions & 6 deletions src/widgets/unifi/proxy.js
Expand Up @@ -3,8 +3,8 @@ import cache from "memory-cache";
import { formatApiCall } from "utils/proxy/api-helpers";
import { httpProxy } from "utils/proxy/http";
import { addCookieToJar, setCookieHeader } from "utils/proxy/cookie-jar";
import { getSettings } from "utils/config/config";
import getServiceWidget from "utils/config/service-helpers";
import { getPrivateWidgetOptions } from "utils/config/widget-helpers";
import createLogger from "utils/logger";
import widgets from "widgets/widgets";

Expand All @@ -15,13 +15,13 @@ const logger = createLogger(proxyName);

async function getWidget(req) {
const { group, service, type } = req.query;

let widget = null;
if (type === "unifi_console") {
const settings = getSettings();
widget = settings.unifi_console;
if (type === "unifi_console") { // info widget
const index = req.query?.query ? JSON.parse(req.query.query).index : undefined;
widget = await getPrivateWidgetOptions(type, index);
if (!widget) {
logger.debug("There is no unifi_console section in settings.yaml");
logger.debug("Error retrieving settings for this Unifi widget");
return null;
}
widget.type = "unifi";
Expand Down

0 comments on commit 4ea2798

Please sign in to comment.