Skip to content

Commit

Permalink
Merge pull request gethomepage#1917 from benphelps/non-graphs
Browse files Browse the repository at this point in the history
mini-non-chart charts
  • Loading branch information
benphelps authored Sep 6, 2023
2 parents c2058f3 + 17b0f63 commit 99f60ea
Show file tree
Hide file tree
Showing 12 changed files with 434 additions and 242 deletions.
1 change: 1 addition & 0 deletions public/locales/en/common.json
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@
"load": "Load",
"wait": "Please wait",
"temp": "TEMP",
"_temp": "Temp",
"warn": "Warn",
"uptime": "UP",
"total": "Total",
Expand Down
150 changes: 91 additions & 59 deletions src/utils/config/service-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import getKubeConfig from "utils/config/kubernetes";

const logger = createLogger("service-helpers");


export async function servicesFromConfig() {
checkAndCopyConfig("services.yaml");

Expand All @@ -32,14 +31,14 @@ export async function servicesFromConfig() {
services: servicesGroup[Object.keys(servicesGroup)[0]].map((entries) => ({
name: Object.keys(entries)[0],
...entries[Object.keys(entries)[0]],
type: 'service'
type: "service",
})),
}));

// add default weight to services based on their position in the configuration
servicesArray.forEach((group, groupIndex) => {
group.services.forEach((service, serviceIndex) => {
if(!service.weight) {
if (!service.weight) {
servicesArray[groupIndex].services[serviceIndex].weight = (serviceIndex + 1) * 100;
}
});
Expand All @@ -66,7 +65,9 @@ export async function servicesFromDocker() {
const isSwarm = !!servers[serverName].swarm;
const docker = new Docker(getDockerArguments(serverName).conn);
const listProperties = { all: true };
const containers = await ((isSwarm) ? docker.listServices(listProperties) : docker.listContainers(listProperties));
const containers = await (isSwarm
? docker.listServices(listProperties)
: docker.listContainers(listProperties));

// bad docker connections can result in a <Buffer ...> object?
// in any case, this ensures the result is the expected array
Expand All @@ -76,19 +77,23 @@ export async function servicesFromDocker() {

const discovered = containers.map((container) => {
let constructedService = null;
const containerLabels = isSwarm ? shvl.get(container, 'Spec.Labels') : container.Labels;
const containerName = isSwarm ? shvl.get(container, 'Spec.Name') : container.Names[0];
const containerLabels = isSwarm ? shvl.get(container, "Spec.Labels") : container.Labels;
const containerName = isSwarm ? shvl.get(container, "Spec.Name") : container.Names[0];

Object.keys(containerLabels).forEach((label) => {
if (label.startsWith("homepage.")) {
if (!constructedService) {
constructedService = {
container: containerName.replace(/^\//, ""),
server: serverName,
type: 'service'
type: "service",
};
}
shvl.set(constructedService, label.replace("homepage.", ""), substituteEnvironmentVars(containerLabels[label]));
shvl.set(
constructedService,
label.replace("homepage.", ""),
substituteEnvironmentVars(containerLabels[label])
);
}
});

Expand Down Expand Up @@ -132,12 +137,12 @@ export async function servicesFromDocker() {
function getUrlFromIngress(ingress) {
const urlHost = ingress.spec.rules[0].host;
const urlPath = ingress.spec.rules[0].http.paths[0].path;
const urlSchema = ingress.spec.tls ? 'https' : 'http';
const urlSchema = ingress.spec.tls ? "https" : "http";
return `${urlSchema}://${urlHost}${urlPath}`;
}

export async function servicesFromKubernetes() {
const ANNOTATION_BASE = 'gethomepage.dev';
const ANNOTATION_BASE = "gethomepage.dev";
const ANNOTATION_WIDGET_BASE = `${ANNOTATION_BASE}/widget.`;
const ANNOTATION_POD_SELECTOR = `${ANNOTATION_BASE}/pod-selector`;

Expand All @@ -151,83 +156,104 @@ export async function servicesFromKubernetes() {
const networking = kc.makeApiClient(NetworkingV1Api);
const crd = kc.makeApiClient(CustomObjectsApi);

const ingressList = await networking.listIngressForAllNamespaces(null, null, null, null)
const ingressList = await networking
.listIngressForAllNamespaces(null, null, null, null)
.then((response) => response.body)
.catch((error) => {
logger.error("Error getting ingresses: %d %s %s", error.statusCode, error.body, error.response);
return null;
});

const traefikIngressListContaino = await crd.listClusterCustomObject("traefik.containo.us", "v1alpha1", "ingressroutes")
const traefikIngressListContaino = await crd
.listClusterCustomObject("traefik.containo.us", "v1alpha1", "ingressroutes")
.then((response) => response.body)
.catch(async (error) => {
if (error.statusCode !== 404) {
logger.error("Error getting traefik ingresses from traefik.containo.us: %d %s %s", error.statusCode, error.body, error.response);
logger.error(
"Error getting traefik ingresses from traefik.containo.us: %d %s %s",
error.statusCode,
error.body,
error.response
);
}

return [];
});

const traefikIngressListIo = await crd.listClusterCustomObject("traefik.io", "v1alpha1", "ingressroutes")
const traefikIngressListIo = await crd
.listClusterCustomObject("traefik.io", "v1alpha1", "ingressroutes")
.then((response) => response.body)
.catch(async (error) => {
if (error.statusCode !== 404) {
logger.error("Error getting traefik ingresses from traefik.io: %d %s %s", error.statusCode, error.body, error.response);
}

logger.error(
"Error getting traefik ingresses from traefik.io: %d %s %s",
error.statusCode,
error.body,
error.response
);
}

return [];
});



const traefikIngressList = [...traefikIngressListContaino, ...traefikIngressListIo];

if (traefikIngressList && traefikIngressList.items.length > 0) {
const traefikServices = traefikIngressList.items
.filter((ingress) => ingress.metadata.annotations && ingress.metadata.annotations[`${ANNOTATION_BASE}/href`])
const traefikServices = traefikIngressList.items.filter(
(ingress) => ingress.metadata.annotations && ingress.metadata.annotations[`${ANNOTATION_BASE}/href`]
);
ingressList.items.push(...traefikServices);
}

if (!ingressList) {
return [];
}
const services = ingressList.items
.filter((ingress) => ingress.metadata.annotations && ingress.metadata.annotations[`${ANNOTATION_BASE}/enabled`] === 'true')
.filter(
(ingress) =>
ingress.metadata.annotations && ingress.metadata.annotations[`${ANNOTATION_BASE}/enabled`] === "true"
)
.map((ingress) => {
let constructedService = {
app: ingress.metadata.name,
namespace: ingress.metadata.namespace,
href: ingress.metadata.annotations[`${ANNOTATION_BASE}/href`] || getUrlFromIngress(ingress),
name: ingress.metadata.annotations[`${ANNOTATION_BASE}/name`] || ingress.metadata.name,
group: ingress.metadata.annotations[`${ANNOTATION_BASE}/group`] || "Kubernetes",
weight: ingress.metadata.annotations[`${ANNOTATION_BASE}/weight`] || '0',
icon: ingress.metadata.annotations[`${ANNOTATION_BASE}/icon`] || '',
description: ingress.metadata.annotations[`${ANNOTATION_BASE}/description`] || '',
external: false,
type: 'service'
};
if (ingress.metadata.annotations[`${ANNOTATION_BASE}/external`]) {
constructedService.external = String(ingress.metadata.annotations[`${ANNOTATION_BASE}/external`]).toLowerCase() === "true"
}
if (ingress.metadata.annotations[ANNOTATION_POD_SELECTOR]) {
constructedService.podSelector = ingress.metadata.annotations[ANNOTATION_POD_SELECTOR];
}
if (ingress.metadata.annotations[`${ANNOTATION_BASE}/ping`]) {
constructedService.ping = ingress.metadata.annotations[`${ANNOTATION_BASE}/ping`];
}
Object.keys(ingress.metadata.annotations).forEach((annotation) => {
if (annotation.startsWith(ANNOTATION_WIDGET_BASE)) {
shvl.set(constructedService, annotation.replace(`${ANNOTATION_BASE}/`, ""), ingress.metadata.annotations[annotation]);
let constructedService = {
app: ingress.metadata.name,
namespace: ingress.metadata.namespace,
href: ingress.metadata.annotations[`${ANNOTATION_BASE}/href`] || getUrlFromIngress(ingress),
name: ingress.metadata.annotations[`${ANNOTATION_BASE}/name`] || ingress.metadata.name,
group: ingress.metadata.annotations[`${ANNOTATION_BASE}/group`] || "Kubernetes",
weight: ingress.metadata.annotations[`${ANNOTATION_BASE}/weight`] || "0",
icon: ingress.metadata.annotations[`${ANNOTATION_BASE}/icon`] || "",
description: ingress.metadata.annotations[`${ANNOTATION_BASE}/description`] || "",
external: false,
type: "service",
};
if (ingress.metadata.annotations[`${ANNOTATION_BASE}/external`]) {
constructedService.external =
String(ingress.metadata.annotations[`${ANNOTATION_BASE}/external`]).toLowerCase() === "true";
}
});
if (ingress.metadata.annotations[ANNOTATION_POD_SELECTOR]) {
constructedService.podSelector = ingress.metadata.annotations[ANNOTATION_POD_SELECTOR];
}
if (ingress.metadata.annotations[`${ANNOTATION_BASE}/ping`]) {
constructedService.ping = ingress.metadata.annotations[`${ANNOTATION_BASE}/ping`];
}
Object.keys(ingress.metadata.annotations).forEach((annotation) => {
if (annotation.startsWith(ANNOTATION_WIDGET_BASE)) {
shvl.set(
constructedService,
annotation.replace(`${ANNOTATION_BASE}/`, ""),
ingress.metadata.annotations[annotation]
);
}
});

try {
constructedService = JSON.parse(substituteEnvironmentVars(JSON.stringify(constructedService)));
} catch (e) {
logger.error("Error attempting k8s environment variable substitution.");
}
try {
constructedService = JSON.parse(substituteEnvironmentVars(JSON.stringify(constructedService)));
} catch (e) {
logger.error("Error attempting k8s environment variable substitution.");
}

return constructedService;
});
return constructedService;
});

const mappedServiceGroups = [];

Expand All @@ -251,7 +277,6 @@ export async function servicesFromKubernetes() {
});

return mappedServiceGroups;

} catch (e) {
logger.error(e);
throw e;
Expand All @@ -264,7 +289,7 @@ export function cleanServiceGroups(groups) {
services: serviceGroup.services.map((service) => {
const cleanedService = { ...service };
if (cleanedService.showStats !== undefined) cleanedService.showStats = JSON.parse(cleanedService.showStats);
if (typeof service.weight === 'string') {
if (typeof service.weight === "string") {
const weight = parseInt(service.weight, 10);
if (Number.isNaN(weight)) {
cleanedService.weight = 0;
Expand Down Expand Up @@ -303,6 +328,7 @@ export function cleanServiceGroups(groups) {
userEmail, // azuredevops
repositoryId,
metric, // glances
chart, // glances
stream, // mjpeg
fit,
method, // openmediavault widget
Expand All @@ -311,9 +337,10 @@ export function cleanServiceGroups(groups) {
} = cleanedService.widget;

let fieldsList = fields;
if (typeof fields === 'string') {
try { JSON.parse(fields) }
catch (e) {
if (typeof fields === "string") {
try {
JSON.parse(fields);
} catch (e) {
logger.error("Invalid fields list detected in config for service '%s'", service.name);
fieldsList = null;
}
Expand Down Expand Up @@ -373,6 +400,11 @@ export function cleanServiceGroups(groups) {
}
if (type === "glances") {
if (metric) cleanedService.widget.metric = metric;
if (chart !== undefined) {
cleanedService.widget.chart = chart;
} else {
cleanedService.widget.chart = true;
}
}
if (type === "mjpeg") {
if (stream) cleanedService.widget.stream = stream;
Expand Down
5 changes: 3 additions & 2 deletions src/widgets/glances/components/container.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
export default function Container({ children, className = "" }) {
export default function Container({ children, chart = true, className = "" }) {
return (
<div>
{children}
<div className={`absolute top-0 right-0 bottom-0 left-0 overflow-clip pointer-events-none ${className}`} />
<div className="h-[68px] overflow-clip" />
{ chart && <div className="h-[68px] overflow-clip" /> }
{ !chart && <div className="h-[16px] overflow-clip" /> }
</div>
);
}
43 changes: 29 additions & 14 deletions src/widgets/glances/metrics/cpu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const pointsLimit = 15;

export default function Component({ service }) {
const { t } = useTranslation();
const { widget } = service;
const { chart } = widget;

const [dataPoints, setDataPoints] = useState(new Array(pointsLimit).fill({ value: 0 }, 0, pointsLimit));

Expand Down Expand Up @@ -44,32 +46,45 @@ export default function Component({ service }) {
}

return (
<Container>
<Chart
dataPoints={dataPoints}
label={[t("resources.used")]}
formatter={(value) => t("common.number", {
value,
style: "unit",
unit: "percent",
maximumFractionDigits: 0,
})}
/>
<Container chart={chart}>
{ chart && (
<Chart
dataPoints={dataPoints}
label={[t("resources.used")]}
formatter={(value) => t("common.number", {
value,
style: "unit",
unit: "percent",
maximumFractionDigits: 0,
})}
/>
)}

{ !chart && (
<Block position="top-3 right-3">
<div className="text-xs opacity-50">
{systemData.linux_distro && `${systemData.linux_distro} - ` }
{systemData.os_version && systemData.os_version }
</div>
</Block>
)}

{systemData && !systemError && (
<Block position="bottom-3 left-3">
{systemData.linux_distro && (
{systemData.linux_distro && chart && (
<div className="text-xs opacity-50">
{systemData.linux_distro}
</div>
)}
{systemData.os_version && (

{systemData.os_version && chart && (
<div className="text-xs opacity-50">
{systemData.os_version}
</div>
)}

{systemData.hostname && (
<div className="text-xs opacity-75">
<div className="text-xs opacity-50">
{systemData.hostname}
</div>
)}
Expand Down
Loading

0 comments on commit 99f60ea

Please sign in to comment.