commit
1656f02418
@ -0,0 +1,20 @@
|
||||
name: 'Reaction Comments'
|
||||
|
||||
on:
|
||||
issue_comment:
|
||||
types: [created, edited]
|
||||
pull_request_review_comment:
|
||||
types: [created, edited]
|
||||
schedule:
|
||||
- cron: '0 0 * * *'
|
||||
|
||||
permissions:
|
||||
actions: write
|
||||
issues: write
|
||||
pull-requests: write
|
||||
|
||||
jobs:
|
||||
action:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: dessant/reaction-comments@v4
|
@ -0,0 +1,33 @@
|
||||
---
|
||||
title: ArgoCD
|
||||
description: ArgoCD Widget Configuration
|
||||
---
|
||||
|
||||
Learn more about [ArgoCD](https://argo-cd.readthedocs.io/en/stable/).
|
||||
|
||||
Allowed fields (limited to a max of 4): `["apps", "synced", "outOfSync", "healthy", "progressing", "degraded", "suspended", "missing"]`
|
||||
|
||||
```yaml
|
||||
widget:
|
||||
type: argocd
|
||||
url: http://argocd.host.or.ip:port
|
||||
key: argocdapikey
|
||||
```
|
||||
|
||||
You can generate an API key either by creating a bearer token for an existing account, see [Authorization](https://argo-cd.readthedocs.io/en/latest/developer-guide/api-docs/#authorization) (not recommended) or create a new local user account with limited privileges and generate an authentication token for this account. To do this the steps are:
|
||||
|
||||
- [Create a new local user](https://argo-cd.readthedocs.io/en/stable/operator-manual/user-management/#create-new-user) and give it the `apiKey` capability
|
||||
- Setup [RBAC configuration](https://argo-cd.readthedocs.io/en/stable/operator-manual/rbac/#rbac-configuration) for your the user and give it readonly access to your ArgoCD resources, e.g. by giving it the `role:readonly` role.
|
||||
- In your ArgoCD project under _Settings / Accounts_ open the newly created account and in the _Tokens_ section click on _Generate New_ to generate an access token, optionally specifying an expiry date.
|
||||
|
||||
If you installed ArgoCD via the official Helm chart, the account creation and rbac config can be achived by overriding these helm values:
|
||||
|
||||
```yaml
|
||||
configs:
|
||||
cm:
|
||||
accounts.readonly: apiKey
|
||||
rbac:
|
||||
policy.csv: "g, readonly, role:readonly"
|
||||
```
|
||||
|
||||
This creates a new account called `readonly` and attaches the `role:readonly` role to it.
|
@ -0,0 +1,22 @@
|
||||
---
|
||||
title: Beszel
|
||||
description: Beszel Widget Configuration
|
||||
---
|
||||
|
||||
Learn more about [Beszel](https://github.com/henrygd/beszel)
|
||||
|
||||
The widget has two modes, a single system with detailed info if `systemId` is provided, or an overview of all systems if `systemId` is not provided.
|
||||
|
||||
The `systemID` in the `id` field on the collections page of Beszel.
|
||||
|
||||
Allowed fields for 'overview' mode: `["systems", "up"]`
|
||||
Allowed fields for a single system: `["name", "status", "updated", "cpu", "memory", "disk", "network"]`
|
||||
|
||||
```yaml
|
||||
widget:
|
||||
type: beszel
|
||||
url: http://beszel.host.or.ip
|
||||
username: username # email
|
||||
password: password
|
||||
systemId: systemId # optional
|
||||
```
|
@ -0,0 +1,20 @@
|
||||
---
|
||||
title: Gitlab
|
||||
description: Gitlab Widget Configuration
|
||||
---
|
||||
|
||||
Learn more about [Gitlab](https://gitlab.com).
|
||||
|
||||
API requires a personal access token with either `read_api` or `api` permission. See the [gitlab documentation](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html#create-a-personal-access-token) for details on generating one.
|
||||
|
||||
Your Gitlab user ID can be found on [your profile page](https://support.circleci.com/hc/en-us/articles/20761157174043-How-to-find-your-GitLab-User-ID).
|
||||
|
||||
Allowed fields: `["events", "issues", "merges", "projects"]`.
|
||||
|
||||
```yaml
|
||||
widget:
|
||||
type: gitlab
|
||||
url: http://gitlab.host.or.ip:port
|
||||
key: personal-access-token
|
||||
user_id: 123456
|
||||
```
|
@ -0,0 +1,19 @@
|
||||
---
|
||||
title: Headscale
|
||||
description: Headscale Widget Configuration
|
||||
---
|
||||
|
||||
Learn more about [Headscale](https://headscale.net/).
|
||||
|
||||
You will need to generate an API access token from the [command line](https://headscale.net/ref/remote-cli/#create-an-api-key) using `headscale apikeys create` command.
|
||||
|
||||
To find your node ID, you can use `headscale nodes list` command.
|
||||
|
||||
Allowed fields: `["name", "address", "last_seen", "status"]`.
|
||||
|
||||
```yaml
|
||||
widget:
|
||||
type: headscale
|
||||
nodeId: nodeid
|
||||
key: headscaleapiaccesstoken
|
||||
```
|
@ -0,0 +1,67 @@
|
||||
---
|
||||
title: Prometheus Metric
|
||||
description: Prometheus Metric Widget Configuration
|
||||
---
|
||||
|
||||
Learn more about [Querying Prometheus](https://prometheus.io/docs/prometheus/latest/querying/basics/).
|
||||
|
||||
This widget can show metrics for your service defined by PromQL queries which are requested from a running Prometheus instance.
|
||||
|
||||
Quries can be defined in the `metrics` array of the widget along with a label to be used to present the metric value. You can optionally specify a global `refreshInterval` in milliseconds and/or define the `refreshInterval` per metric. Inside the optional `format` object of a metric various formatting styles and transformations can be applied (see below).
|
||||
|
||||
```yaml
|
||||
widget:
|
||||
type: prometheusmetric
|
||||
url: https://prometheus.host.or.ip
|
||||
refreshInterval: 10000 # optional - in milliseconds, defaults to 10s
|
||||
metrics:
|
||||
- label: Metric 1
|
||||
query: alertmanager_alerts{state="active"}
|
||||
- label: Metric 2
|
||||
query: apiserver_storage_size_bytes{node="mynode"}
|
||||
format:
|
||||
type: bytes
|
||||
- label: Metric 3
|
||||
query: avg(prometheus_notifications_latency_seconds)
|
||||
format:
|
||||
type: number
|
||||
suffix: s
|
||||
options:
|
||||
maximumFractionDigits: 4
|
||||
- label: Metric 4
|
||||
query: time()
|
||||
refreshInterval: 1000 # will override global refreshInterval
|
||||
format:
|
||||
type: date
|
||||
scale: 1000
|
||||
options:
|
||||
timeStyle: medium
|
||||
```
|
||||
|
||||
## Formatting
|
||||
|
||||
Supported values for `format.type` are `text`, `number`, `percent`, `bytes`, `bits`, `bbytes`, `bbits`, `byterate`, `bibyterate`, `bitrate`, `bibitrate`, `date`, `duration`, `relativeDate`, and `text` which is the default.
|
||||
|
||||
The `dateStyle` and `timeStyle` options of the `date` format are passed directly to [Intl.DateTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat/DateTimeFormat) and the `style` and `numeric` options of `relativeDate` are passed to [Intl.RelativeTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat). For the `number` format, options of [Intl.NumberFormat](https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat) can be used, e.g. `maximumFractionDigits` or `minimumFractionDigits`.
|
||||
|
||||
### Data Transformation
|
||||
|
||||
You can manipulate your metric value with the following tools: `scale`, `prefix` and `suffix`, for example:
|
||||
|
||||
```yaml
|
||||
- query: my_custom_metric{}
|
||||
label: Metric 1
|
||||
format:
|
||||
type: number
|
||||
scale: 1000 # multiplies value by a number or fraction string e.g. 1/16
|
||||
- query: my_custom_metric{}
|
||||
label: Metric 2
|
||||
format:
|
||||
type: number
|
||||
prefix: "$" # prefixes value with given string
|
||||
- query: my_custom_metric{}
|
||||
label: Metric 3
|
||||
format:
|
||||
type: number
|
||||
suffix: "€" # suffixes value with given string
|
||||
```
|
@ -0,0 +1,15 @@
|
||||
---
|
||||
title: Spoolman
|
||||
description: Spoolman Widget Configuration
|
||||
---
|
||||
|
||||
Learn more about [Spoolman](https://github.com/Donkie/Spoolman).
|
||||
|
||||
4 spools are displayed by default. If more than 4 spools are configured in spoolman you can use the spoolIds configuration option to control which are displayed.
|
||||
|
||||
```yaml
|
||||
widget:
|
||||
type: spoolman
|
||||
url: http://spoolman.host.or.ip
|
||||
spoolIds: [1, 2, 3, 4] # optional
|
||||
```
|
@ -0,0 +1,20 @@
|
||||
---
|
||||
title: Suwayomi
|
||||
description: Suwayomi Widget Configuration
|
||||
---
|
||||
|
||||
Learn more about [Suwayomi](https://github.com/Suwayomi/Suwayomi-Server).
|
||||
|
||||
Allowed fields: ["download", "nondownload", "read", "unread", "downloadedread", "downloadedunread", "nondownloadedread", "nondownloadedunread"]
|
||||
|
||||
The widget defaults to the first four above. If more than four fields are provided, only the first 4 are displayed.
|
||||
Category IDs can be obtained from the url when navigating to it, `?tab={categoryID}`.
|
||||
|
||||
```yaml
|
||||
widget:
|
||||
type: suwayomi
|
||||
url: http://suwayomi.host.or.ip
|
||||
username: username #optional
|
||||
password: password #optional
|
||||
category: 0 #optional, defaults to all categories
|
||||
```
|
@ -0,0 +1,52 @@
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { widget } = service;
|
||||
|
||||
if (!widget.fields) {
|
||||
widget.fields = ["apps", "synced", "outOfSync", "healthy"];
|
||||
}
|
||||
|
||||
const MAX_ALLOWED_FIELDS = 4;
|
||||
if (widget.fields.length > MAX_ALLOWED_FIELDS) {
|
||||
widget.fields = widget.fields.slice(0, MAX_ALLOWED_FIELDS);
|
||||
}
|
||||
|
||||
const { data: appsData, error: appsError } = useWidgetAPI(widget, "applications");
|
||||
|
||||
const appCounts = widget.fields.map((status) => {
|
||||
if (status === "apps") {
|
||||
return { status, count: appsData?.items?.length };
|
||||
}
|
||||
const count = appsData?.items?.filter(
|
||||
(item) =>
|
||||
item.status?.sync?.status.toLowerCase() === status.toLowerCase() ||
|
||||
item.status?.health?.status.toLowerCase() === status.toLowerCase(),
|
||||
).length;
|
||||
return { status, count };
|
||||
});
|
||||
|
||||
if (appsError) {
|
||||
return <Container service={service} error={appsError} />;
|
||||
}
|
||||
|
||||
if (!appsData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
{appCounts.map((a) => (
|
||||
<Block label={`argocd.${a.status}`} key={a.status} />
|
||||
))}
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
{appCounts.map((a) => (
|
||||
<Block label={`argocd.${a.status}`} key={a.status} value={a.count} />
|
||||
))}
|
||||
</Container>
|
||||
);
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
import credentialedProxyHandler from "utils/proxy/handlers/credentialed";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/api/v1/{endpoint}",
|
||||
proxyHandler: credentialedProxyHandler,
|
||||
|
||||
mappings: {
|
||||
applications: {
|
||||
endpoint: "applications",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default widget;
|
@ -0,0 +1,60 @@
|
||||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
const { systemId } = widget;
|
||||
|
||||
const { data: systems, error: systemsError } = useWidgetAPI(widget, "systems");
|
||||
|
||||
const MAX_ALLOWED_FIELDS = 4;
|
||||
if (!widget.fields?.length > 0) {
|
||||
widget.fields = systemId ? ["name", "status", "cpu", "memory"] : ["systems", "up"];
|
||||
}
|
||||
if (widget.fields?.length > MAX_ALLOWED_FIELDS) {
|
||||
widget.fields = widget.fields.slice(0, MAX_ALLOWED_FIELDS);
|
||||
}
|
||||
|
||||
if (systemsError) {
|
||||
return <Container service={service} error={systemsError} />;
|
||||
}
|
||||
|
||||
if (!systems) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="beszel.systems" />
|
||||
<Block label="beszel.up" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
if (systemId) {
|
||||
const system = systems.items.find((item) => item.id === systemId);
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="beszel.name" value={system.name} />
|
||||
<Block label="beszel.status" value={t(`beszel.${system.status}`)} />
|
||||
<Block label="beszel.updated" value={t("common.relativeDate", { value: system.updated })} />
|
||||
<Block label="beszel.cpu" value={t("common.percent", { value: system.info.cpu, maximumFractionDigits: 2 })} />
|
||||
<Block label="beszel.memory" value={t("common.percent", { value: system.info.mp, maximumFractionDigits: 2 })} />
|
||||
<Block label="beszel.disk" value={t("common.percent", { value: system.info.dp, maximumFractionDigits: 2 })} />
|
||||
<Block label="beszel.network" value={t("common.percent", { value: system.info.b, maximumFractionDigits: 2 })} />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
const upTotal = systems.items.filter((item) => item.status === "up").length;
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="beszel.systems" value={systems.totalItems} />
|
||||
<Block label="beszel.up" value={`${upTotal} / ${systems.totalItems}`} />
|
||||
</Container>
|
||||
);
|
||||
}
|
@ -0,0 +1,99 @@
|
||||
import cache from "memory-cache";
|
||||
|
||||
import getServiceWidget from "utils/config/service-helpers";
|
||||
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||
import { httpProxy } from "utils/proxy/http";
|
||||
import widgets from "widgets/widgets";
|
||||
import createLogger from "utils/logger";
|
||||
|
||||
const proxyName = "beszelProxyHandler";
|
||||
const tokenCacheKey = `${proxyName}__token`;
|
||||
const logger = createLogger(proxyName);
|
||||
|
||||
async function login(loginUrl, username, password, service) {
|
||||
const authResponse = await httpProxy(loginUrl, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ identity: username, password }),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
});
|
||||
|
||||
const status = authResponse[0];
|
||||
let data = authResponse[2];
|
||||
try {
|
||||
data = JSON.parse(Buffer.from(authResponse[2]).toString());
|
||||
|
||||
if (status === 200) {
|
||||
cache.put(`${tokenCacheKey}.${service}`, data.token);
|
||||
}
|
||||
} catch (e) {
|
||||
logger.error(`Error ${status} logging into beszel`, JSON.stringify(authResponse[2]));
|
||||
}
|
||||
return [status, data.token ?? data];
|
||||
}
|
||||
|
||||
export default async function beszelProxyHandler(req, res) {
|
||||
const { group, service, endpoint } = req.query;
|
||||
|
||||
if (group && service) {
|
||||
const widget = await getServiceWidget(group, service);
|
||||
|
||||
if (!widgets?.[widget.type]?.api) {
|
||||
return res.status(403).json({ error: "Service does not support API calls" });
|
||||
}
|
||||
|
||||
if (widget) {
|
||||
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
|
||||
const loginUrl = formatApiCall(widgets[widget.type].api, { endpoint: "admins/auth-with-password", ...widget });
|
||||
|
||||
let status;
|
||||
let data;
|
||||
|
||||
let token = cache.get(`${tokenCacheKey}.${service}`);
|
||||
if (!token) {
|
||||
[status, token] = await login(loginUrl, widget.username, widget.password, service);
|
||||
if (status !== 200) {
|
||||
logger.debug(`HTTP ${status} logging into npm api: ${token}`);
|
||||
return res.status(status).send(token);
|
||||
}
|
||||
}
|
||||
|
||||
[status, , data] = await httpProxy(url, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (status === 403) {
|
||||
logger.debug(`HTTP ${status} retrieving data from npm api, logging in and trying again.`);
|
||||
cache.del(`${tokenCacheKey}.${service}`);
|
||||
[status, token] = await login(loginUrl, widget.username, widget.password, service);
|
||||
|
||||
if (status !== 200) {
|
||||
logger.debug(`HTTP ${status} logging into npm api: ${data}`);
|
||||
return res.status(status).send(data);
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
[status, , data] = await httpProxy(url, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (status !== 200) {
|
||||
return res.status(status).send(data);
|
||||
}
|
||||
|
||||
return res.send(data);
|
||||
}
|
||||
}
|
||||
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
import beszelProxyHandler from "./proxy";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/api/{endpoint}",
|
||||
proxyHandler: beszelProxyHandler,
|
||||
|
||||
mappings: {
|
||||
systems: {
|
||||
endpoint: "collections/systems/records?page=1&perPage=500&sort=%2Bcreated",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default widget;
|
@ -0,0 +1,36 @@
|
||||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
const { widget } = service;
|
||||
|
||||
const { data: gitlabCounts, error: gitlabCountsError } = useWidgetAPI(widget, "counts");
|
||||
|
||||
if (gitlabCountsError) {
|
||||
return <Container service={service} error={gitlabCountsError} />;
|
||||
}
|
||||
|
||||
if (!gitlabCounts) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="gitlab.groups" />
|
||||
<Block label="gitlab.issues" />
|
||||
<Block label="gitlab.merges" />
|
||||
<Block label="gitlab.projects" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="gitlab.groups" value={t("common.number", { value: gitlabCounts.groups_count })} />
|
||||
<Block label="gitlab.issues" value={t("common.number", { value: gitlabCounts.issues_count })} />
|
||||
<Block label="gitlab.merges" value={t("common.number", { value: gitlabCounts.merge_requests_count })} />
|
||||
<Block label="gitlab.projects" value={t("common.number", { value: gitlabCounts.projects_count })} />
|
||||
</Container>
|
||||
);
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
import credentialedProxyHandler from "utils/proxy/handlers/credentialed";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/api/v4/{endpoint}",
|
||||
proxyHandler: credentialedProxyHandler,
|
||||
mappings: {
|
||||
counts: {
|
||||
endpoint: "users/{user_id}/associations_count",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default widget;
|
@ -0,0 +1,43 @@
|
||||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
const { widget } = service;
|
||||
|
||||
const { data: nodeData, error: nodeError } = useWidgetAPI(widget, "node");
|
||||
|
||||
if (nodeError) {
|
||||
return <Container service={service} error={nodeError} />;
|
||||
}
|
||||
|
||||
if (!nodeData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="headscale.name" />
|
||||
<Block label="headscale.address" />
|
||||
<Block label="headscale.last_seen" />
|
||||
<Block label="headscale.status" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
const {
|
||||
givenName,
|
||||
ipAddresses: [address],
|
||||
lastSeen,
|
||||
online,
|
||||
} = nodeData.node;
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="headscale.name" value={givenName} />
|
||||
<Block label="headscale.address" value={address} />
|
||||
<Block label="headscale.last_seen" value={t("common.relativeDate", { value: lastSeen })} />
|
||||
<Block label="headscale.status" value={t(online ? "headscale.online" : "headscale.offline")} />
|
||||
</Container>
|
||||
);
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
import credentialedProxyHandler from "utils/proxy/handlers/credentialed";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/api/v1/{endpoint}/{nodeId}",
|
||||
proxyHandler: credentialedProxyHandler,
|
||||
|
||||
mappings: {
|
||||
node: {
|
||||
endpoint: "node",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default widget;
|
@ -0,0 +1,116 @@
|
||||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
function formatValue(t, metric, rawValue) {
|
||||
if (!metric?.format) return rawValue;
|
||||
if (!rawValue) return "-";
|
||||
|
||||
let value = rawValue;
|
||||
|
||||
// Scale the value. Accepts either a number to multiply by or a string
|
||||
// like "12/345".
|
||||
const scale = metric?.format?.scale;
|
||||
if (typeof scale === "number") {
|
||||
value *= scale;
|
||||
} else if (typeof scale === "string" && scale.includes("/")) {
|
||||
const parts = scale.split("/");
|
||||
const numerator = parts[0] ? parseFloat(parts[0]) : 1;
|
||||
const denominator = parts[1] ? parseFloat(parts[1]) : 1;
|
||||
value = (value * numerator) / denominator;
|
||||
} else {
|
||||
value = parseFloat(value);
|
||||
}
|
||||
|
||||
// Format the value using a known type and optional options.
|
||||
switch (metric?.format?.type) {
|
||||
case "text":
|
||||
break;
|
||||
default:
|
||||
value = t(`common.${metric.format.type}`, { value, ...metric.format?.options });
|
||||
}
|
||||
|
||||
// Apply fixed prefix.
|
||||
const prefix = metric?.format?.prefix;
|
||||
if (prefix) {
|
||||
value = `${prefix}${value}`;
|
||||
}
|
||||
|
||||
// Apply fixed suffix.
|
||||
const suffix = metric?.format?.suffix;
|
||||
if (suffix) {
|
||||
value = `${value}${suffix}`;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
|
||||
const { metrics = [], refreshInterval = 10000 } = widget;
|
||||
|
||||
let prometheusmetricError;
|
||||
|
||||
const prometheusmetricData = new Map(
|
||||
metrics.slice(0, 4).map((metric) => {
|
||||
// disable the rule that hooks should not be called from a callback,
|
||||
// because we don't need a strong guarantee of hook execution order here.
|
||||
// eslint-disable-next-line react-hooks/rules-of-hooks
|
||||
const { data: resultData, error: resultError } = useWidgetAPI(widget, "query", {
|
||||
query: metric.query,
|
||||
refreshInterval: Math.max(1000, metric.refreshInterval ?? refreshInterval),
|
||||
});
|
||||
if (resultError) {
|
||||
prometheusmetricError = resultError;
|
||||
}
|
||||
return [metric.key ?? metric.label, resultData];
|
||||
}),
|
||||
);
|
||||
|
||||
if (prometheusmetricError) {
|
||||
return <Container service={service} error={prometheusmetricError} />;
|
||||
}
|
||||
|
||||
if (!prometheusmetricData) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
{metrics.slice(0, 4).map((item) => (
|
||||
<Block label={item.label} key={item.label} />
|
||||
))}
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
function getResultValue(data) {
|
||||
// Fetches the first metric result from the Prometheus query result data.
|
||||
// The first element in the result value is the timestamp which is ignored here.
|
||||
const resultType = data?.data?.resultType;
|
||||
const result = data?.data?.result;
|
||||
|
||||
switch (resultType) {
|
||||
case "vector":
|
||||
return result?.[0]?.value?.[1];
|
||||
case "scalar":
|
||||
return result?.[1];
|
||||
default:
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
{metrics.map((metric) => (
|
||||
<Block
|
||||
label={metric.label}
|
||||
key={metric.key ?? metric.label}
|
||||
value={formatValue(t, metric, getResultValue(prometheusmetricData.get(metric.key ?? metric.label)))}
|
||||
/>
|
||||
))}
|
||||
</Container>
|
||||
);
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
import genericProxyHandler from "utils/proxy/handlers/generic";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/api/v1/{endpoint}",
|
||||
proxyHandler: genericProxyHandler,
|
||||
|
||||
mappings: {
|
||||
query: {
|
||||
method: "GET",
|
||||
endpoint: "query",
|
||||
params: ["query"],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default widget;
|
@ -0,0 +1,63 @@
|
||||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
const { widget } = service;
|
||||
|
||||
// eslint-disable-next-line prefer-const
|
||||
let { data: spoolData, error: spoolError } = useWidgetAPI(widget, "spools");
|
||||
|
||||
if (spoolError) {
|
||||
return <Container service={service} error={spoolError} />;
|
||||
}
|
||||
|
||||
if (!spoolData) {
|
||||
const nBlocksGuess = widget.spoolIds?.length ?? 4;
|
||||
return (
|
||||
<Container service={service}>
|
||||
{[...Array(nBlocksGuess)].map((_, i) => (
|
||||
// eslint-disable-next-line react/no-array-index-key
|
||||
<Block key={i} label="spoolman.loading" />
|
||||
))}
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
if (spoolData.error || spoolData.message) {
|
||||
return <Container service={service} error={spoolData?.error ?? spoolData} />;
|
||||
}
|
||||
|
||||
if (spoolData.length === 0) {
|
||||
return (
|
||||
<Container service={service}>
|
||||
<Block label="spoolman.noSpools" />
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
if (widget.spoolIds?.length) {
|
||||
spoolData = spoolData.filter((spool) => widget.spoolIds.includes(spool.id));
|
||||
}
|
||||
|
||||
if (spoolData.length > 4) {
|
||||
spoolData = spoolData.slice(0, 4);
|
||||
}
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
{spoolData.map((spool) => (
|
||||
<Block
|
||||
key={spool.id}
|
||||
label={spool.filament.name}
|
||||
value={t("common.percent", {
|
||||
value: (spool.remaining_weight / spool.initial_weight) * 100,
|
||||
})}
|
||||
/>
|
||||
))}
|
||||
</Container>
|
||||
);
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
import credentialedProxyHandler from "utils/proxy/handlers/credentialed";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/api/v1/{endpoint}",
|
||||
proxyHandler: credentialedProxyHandler,
|
||||
|
||||
mappings: {
|
||||
spools: {
|
||||
endpoint: "spool",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export default widget;
|
@ -0,0 +1,40 @@
|
||||
import { useTranslation } from "next-i18next";
|
||||
|
||||
import Container from "components/services/widget/container";
|
||||
import Block from "components/services/widget/block";
|
||||
import useWidgetAPI from "utils/proxy/use-widget-api";
|
||||
|
||||
export default function Component({ service }) {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const { widget } = service;
|
||||
|
||||
const { data: suwayomiData, error: suwayomiError } = useWidgetAPI(widget);
|
||||
|
||||
if (suwayomiError) {
|
||||
return <Container service={service} error={suwayomiError} />;
|
||||
}
|
||||
|
||||
if (!suwayomiData) {
|
||||
if (!widget.fields || widget.fields.length === 0) {
|
||||
widget.fields = ["download", "nondownload", "read", "unread"];
|
||||
} else if (widget.fields.length > 4) {
|
||||
widget.fields = widget.fields.slice(0, 4);
|
||||
}
|
||||
return (
|
||||
<Container service={service}>
|
||||
{widget.fields.map((field) => (
|
||||
<Block key={field} label={`suwayomi.${field}`} />
|
||||
))}
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Container service={service}>
|
||||
{suwayomiData.map((data) => (
|
||||
<Block key={data.label} label={data.label} value={t("common.number", { value: data.count })} />
|
||||
))}
|
||||
</Container>
|
||||
);
|
||||
}
|
@ -0,0 +1,175 @@
|
||||
import { httpProxy } from "utils/proxy/http";
|
||||
import { formatApiCall } from "utils/proxy/api-helpers";
|
||||
import getServiceWidget from "utils/config/service-helpers";
|
||||
import createLogger from "utils/logger";
|
||||
import widgets from "widgets/widgets";
|
||||
|
||||
const proxyName = "suwayomiProxyHandler";
|
||||
const logger = createLogger(proxyName);
|
||||
|
||||
const countsToExtract = {
|
||||
download: {
|
||||
condition: (c) => c.isDownloaded,
|
||||
gqlCondition: "isDownloaded: true",
|
||||
},
|
||||
nondownload: {
|
||||
condition: (c) => !c.isDownloaded,
|
||||
gqlCondition: "isDownloaded: false",
|
||||
},
|
||||
read: {
|
||||
condition: (c) => c.isRead,
|
||||
gqlCondition: "isRead: true",
|
||||
},
|
||||
unread: {
|
||||
condition: (c) => !c.isRead,
|
||||
gqlCondition: "isRead: false",
|
||||
},
|
||||
downloadedread: {
|
||||
condition: (c) => c.isDownloaded && c.isRead,
|
||||
gqlCondition: "isDownloaded: true, isRead: true",
|
||||
},
|
||||
downloadedunread: {
|
||||
condition: (c) => c.isDownloaded && !c.isRead,
|
||||
gqlCondition: "isDownloaded: true, isRead: false",
|
||||
},
|
||||
nondownloadedread: {
|
||||
condition: (c) => !c.isDownloaded && c.isRead,
|
||||
gqlCondition: "isDownloaded: false, isRead: true",
|
||||
},
|
||||
nondownloadedunread: {
|
||||
condition: (c) => !c.isDownloaded && !c.isRead,
|
||||
gqlCondition: "isDownloaded: false, isRead: false",
|
||||
},
|
||||
};
|
||||
|
||||
function makeBody(fields, category = "all") {
|
||||
if (Number.isNaN(Number(category))) {
|
||||
let query = "";
|
||||
fields.forEach((field) => {
|
||||
query += `
|
||||
${field}: chapters(
|
||||
condition: {${countsToExtract[field].gqlCondition}}
|
||||
filter: {inLibrary: {equalTo: true}}
|
||||
) {
|
||||
totalCount
|
||||
}`;
|
||||
});
|
||||
return JSON.stringify({
|
||||
operationName: "Counts",
|
||||
query: `
|
||||
query Counts {
|
||||
${query}
|
||||
}`,
|
||||
});
|
||||
}
|
||||
|
||||
return JSON.stringify({
|
||||
operationName: "category",
|
||||
query: `
|
||||
query category($id: Int!) {
|
||||
category(id: $id) {
|
||||
# name
|
||||
mangas {
|
||||
nodes {
|
||||
chapters {
|
||||
nodes {
|
||||
isRead
|
||||
isDownloaded
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
variables: {
|
||||
id: Number(category),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function extractCounts(responseJSON, fields) {
|
||||
if (!("category" in responseJSON.data)) {
|
||||
return fields.map((field) => ({
|
||||
count: responseJSON.data[field].totalCount,
|
||||
label: `suwayomi.${field}`,
|
||||
}));
|
||||
}
|
||||
const tmp = responseJSON.data.category.mangas.nodes.reduce(
|
||||
(accumulator, manga) => {
|
||||
manga.chapters.nodes.forEach((chapter) => {
|
||||
fields.forEach((field, i) => {
|
||||
if (countsToExtract[field].condition(chapter)) {
|
||||
accumulator[i] += 1;
|
||||
}
|
||||
});
|
||||
});
|
||||
return accumulator;
|
||||
},
|
||||
[0, 0, 0, 0],
|
||||
);
|
||||
return fields.map((field, i) => ({
|
||||
count: tmp[i],
|
||||
label: `suwayomi.${field}`,
|
||||
}));
|
||||
}
|
||||
|
||||
export default async function suwayomiProxyHandler(req, res) {
|
||||
const { group, service, endpoint } = req.query;
|
||||
|
||||
if (!group || !service) {
|
||||
logger.debug("Invalid or missing service '%s' or group '%s'", service, group);
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
||||
|
||||
const widget = await getServiceWidget(group, service);
|
||||
|
||||
if (!widget) {
|
||||
logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group);
|
||||
return res.status(400).json({ error: "Invalid proxy service type" });
|
||||
}
|
||||
|
||||
if (!widget.fields || widget.fields.length === 0) {
|
||||
widget.fields = ["download", "nondownload", "read", "unread"];
|
||||
} else if (widget.fields.length > 4) {
|
||||
widget.fields = widget.fields.slice(0, 4);
|
||||
}
|
||||
|
||||
const url = new URL(formatApiCall(widgets[widget.type].api, { endpoint, ...widget }));
|
||||
|
||||
const body = makeBody(widget.fields, widget.category);
|
||||
|
||||
const headers = {
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
|
||||
if (widget.username && widget.password) {
|
||||
headers.Authorization = `Basic ${Buffer.from(`${widget.username}:${widget.password}`).toString("base64")}`;
|
||||
}
|
||||
|
||||
const [status, contentType, data] = await httpProxy(url, {
|
||||
method: "POST",
|
||||
body,
|
||||
headers,
|
||||
});
|
||||
|
||||
if (status === 401) {
|
||||
logger.error("Invalid or missing username or password for service '%s' in group '%s'", service, group);
|
||||
return res.status(status).send({ error: { message: "401: unauthorized, username or password is incorrect." } });
|
||||
}
|
||||
|
||||
if (status !== 200) {
|
||||
logger.error(
|
||||
"Error getting data from Suwayomi for service '%s' in group '%s': %d. Data: %s",
|
||||
service,
|
||||
group,
|
||||
status,
|
||||
data,
|
||||
);
|
||||
return res.status(status).send({ error: { message: "Error getting data. body: %s, data: %s", body, data } });
|
||||
}
|
||||
|
||||
const returnData = extractCounts(JSON.parse(data), widget.fields);
|
||||
|
||||
if (contentType) res.setHeader("Content-Type", contentType);
|
||||
return res.status(status).send(returnData);
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
import suwayomiProxyHandler from "./proxy";
|
||||
|
||||
const widget = {
|
||||
api: "{url}/api/graphql",
|
||||
proxyHandler: suwayomiProxyHandler,
|
||||
};
|
||||
|
||||
export default widget;
|
Loading…
Reference in new issue