From 13620efcd5e3654d393988deeec552a27be0a221 Mon Sep 17 00:00:00 2001 From: Neshura Date: Sat, 10 Dec 2022 02:59:33 +0100 Subject: [PATCH 01/13] Changed next dev to port 4040 --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 94bd905..1dd3d52 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "dev:debug": "NODE_OPTIONS='--inspect' next dev -p 4040", - "dev": "next dev", + "dev": "next dev -p 4040", "build": "next build", "start": "next start", "lint": "next lint" @@ -17,10 +17,10 @@ "swr": "^1.3.0" }, "devDependencies": { - "eslint": "^8.23.1", - "eslint-config-next": "12.2.0", "@types/dockerode": "^3.3.14", "@types/react": "^18.0.14", + "eslint": "^8.23.1", + "eslint-config-next": "12.2.0", "typescript": "^4.7.4" } } From 39aaea05e54323418e5b97a6101c957044d46ab4 Mon Sep 17 00:00:00 2001 From: Neshura Date: Sat, 10 Dec 2022 03:00:25 +0100 Subject: [PATCH 02/13] Expanded Interfaces Services got expanded, now uses Enums where sensible --- interfaces/LinkTypes.ts | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/interfaces/LinkTypes.ts b/interfaces/LinkTypes.ts index 07932fb..9aabf4a 100644 --- a/interfaces/LinkTypes.ts +++ b/interfaces/LinkTypes.ts @@ -1,15 +1,42 @@ -export interface LinkList { - services: CustomLink[], +export interface EntryList { + services: Service[], games: CustomLink[] } export interface CustomLink { + name: string, + href: string, + desc: string, + ip: string, type: string, + location: string, + status: string, + docker_container_name: string +} + +export interface Service { name: string, href: string, desc: string, warn: string, - ip: string, - location: string, - status: string, - docker_container_name: string + type: ServiceType, + docker_container_name: string, + location: ServiceLocation, + status: ServiceStatus +} + +export enum ServiceStatus { + online = "Online", + offline = "Offline", + loading = "Loading", + error = "ERROR" +} + +export enum ServiceLocation { + brr7_4800u = "brr7-4800u", + other = "" +} + +export enum ServiceType { + docker = "docker", + app = "app" } \ No newline at end of file From 89a8278dbe53d708cc206611e80dcdbc02568821 Mon Sep 17 00:00:00 2001 From: Neshura Date: Sat, 10 Dec 2022 03:00:40 +0100 Subject: [PATCH 03/13] Changed naming from LinkList to EntryList --- pages/games.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pages/games.tsx b/pages/games.tsx index 17ce412..67577c9 100644 --- a/pages/games.tsx +++ b/pages/games.tsx @@ -3,9 +3,9 @@ import Link from 'next/link' import styles from '/styles/Home.module.css' import fsPromises from 'fs/promises' import path from 'path' -import type { CustomLink, LinkList } from '../interfaces/LinkTypes' +import type { CustomLink, EntryList } from '../interfaces/LinkTypes' -function Servers(props: LinkList) { +function Servers(props: EntryList) { const serverList = props.games return ( <> From 6af8cb3e1b98dd1305e79ebf5b8399e748da67c4 Mon Sep 17 00:00:00 2001 From: Neshura Date: Sat, 10 Dec 2022 03:02:03 +0100 Subject: [PATCH 04/13] Large Code Rewrite Added 2 API Endpoints for use with useSWR, removed the service and status fetching from getStaticProps, rewrote status fetching to use async (WIP, needs testing) --- pages/api/containers.tsx | 22 ++++ pages/api/services.tsx | 21 ++++ pages/services.tsx | 213 +++++++++++++++++++++------------------ 3 files changed, 157 insertions(+), 99 deletions(-) create mode 100644 pages/api/containers.tsx create mode 100644 pages/api/services.tsx diff --git a/pages/api/containers.tsx b/pages/api/containers.tsx new file mode 100644 index 0000000..44803a2 --- /dev/null +++ b/pages/api/containers.tsx @@ -0,0 +1,22 @@ +import fsPromises from 'fs/promises' +import path from 'path' +import { Service, ServiceStatus } from '../../interfaces/LinkTypes'; + +export default async function ServicesAPI(req: any, res: any) { + try { + var Docker = require('dockerode'); + + const options = { + socketPath: '/var/run/docker.sock', + path: '/v1.41/containers/json' + }; + var docker = new Docker({ socketPath: options.socketPath }); + const list = await docker.listContainers({ all: true }) + + res.status(200).json(list); + } + catch (error) { + console.log(error); + res.status(500).json({ error: 'Error reading data' }); + } +} \ No newline at end of file diff --git a/pages/api/services.tsx b/pages/api/services.tsx new file mode 100644 index 0000000..a416716 --- /dev/null +++ b/pages/api/services.tsx @@ -0,0 +1,21 @@ +import fsPromises from 'fs/promises' +import path from 'path' +import { Service, ServiceStatus } from '../../interfaces/LinkTypes'; + +export default async function ServicesAPI(req: any, res: any) { + try { + const filePath = path.join(process.cwd(), '/public/pages.json') + const data = await fsPromises.readFile(filePath) + .then((file) => JSON.parse(file.toString())); + data.services.forEach((service: Service) => { + service.status = ServiceStatus.loading; + }); + + res.status(200).json(data.services); + } + catch (error) { + console.log(error); + res.status(500).json({ error: 'Error reading data' }); + } +} + diff --git a/pages/services.tsx b/pages/services.tsx index 2ce661c..251be44 100644 --- a/pages/services.tsx +++ b/pages/services.tsx @@ -1,20 +1,50 @@ import Head from 'next/head' import Link from 'next/link' import styles from '/styles/Home.module.css' -import fsPromises from 'fs/promises' -import path, { resolve } from 'path' -import type { CustomLink, LinkList } from '../interfaces/LinkTypes' +import { Service, CustomLink, EntryList, ServiceStatus, ServiceType, ServiceLocation } from '../interfaces/LinkTypes' import Dockerode from 'dockerode'; +import { ReactElement, useEffect, useState } from 'react' +import useSWR, { KeyedMutator } from 'swr'; +const fetcher = (url: string) => fetch(url).then((res) => res.json()) -function Services(props: LinkList) { - const serviceList = props.services +//function Services(props: EntryList) { +const Services = () => { + const { serviceList, isLoading, isError } = useServices(); + + let content: ReactElement = <>; + + // TODO: look into asyncing this as well + useStatus(serviceList); + + if (isError) { + content =
Error Loading data
+ } + else if (isLoading) { + content =
Loading..
+ } + else if (serviceList) { + content = +
+ {serviceList.map((item: Service) => ( + + +

{item.name}

+
{item.status}
+

{item.desc}

+

{item.warn}

+
+ + ))} +
+ } return ( <> Neshura Servers +

@@ -24,116 +54,101 @@ function Services(props: LinkList) {

Lists all available Services, most likely up-to-date

-
- {serviceList.map((item: CustomLink) => ( - - -

{item.name}

-
{item.status}
-

{item.desc}

-

{item.warn}

-
- - ))} -
+ + {content} ) } -// Gets a List of all services specified in /public/pages.json -export async function getServerSideProps() { - const filePath = path.join(process.cwd(), '/public/pages.json') - // TODO: look into asyncing this API call - const jsonData = await fsPromises.readFile(filePath) - const list = JSON.parse(jsonData.toString()) - for (let index = 0; index < list.services.length; index++) { - // TODO: look into asyncing this as well - await status(list.services[index]); +function useStatus(serviceList: Service[] | undefined) { + const { data, error } = useSWR('/api/containers', fetcher); + + if (data && serviceList) { + serviceList.forEach((service: Service) => { + getStatus(data, service); + }) + } + else if (error) { + console.log(error); } - return { props: list } } -// reversing this to loop over given entries for every found container would probably improve latency -async function status(entry: CustomLink) { +function useServices(): { serviceList: Service[] | undefined, isLoading: boolean, isError: boolean } { + const { data, error } = useSWR('/api/services', fetcher); + + return { + serviceList: data, + isLoading: !error && !data, + isError: error + }; +} + +function getStatus(containers: Dockerode.ContainerInfo[], entry: Service) { // for now only the BRR7-4800U can be used with Docker, needs changing once more Servers are used // TODO: support multiple locations for docker - if (entry.location === "brr7-4800u") { + if (entry.location === ServiceLocation.brr7_4800u) { // app in this context means any non-docker page - if (entry.type === "app") { - - let data = new Response(); - try { - await fetch(entry.href).then((response: Response) => data = response); - } - catch (e) { - console.log(e) - return (entry.status = "Offline"); - } - - if (data.ok) { - if (data.status == 200 || data.status == 301 || data.status == 302) { - return (entry.status = "Online"); - } - else return (entry.status = "Offline"); - } - else return (entry.status = "Offline"); + if (entry.type === ServiceType.app) { + fetch(entry.href) + .then((data: Response) => { + if (data.ok) { + if (data.status == 200 || data.status == 301 || data.status == 302) { + entry.status = ServiceStatus.online; + } + else { + entry.status = ServiceStatus.offline; + } + } + else { + entry.status = ServiceStatus.offline; + } + }) + .catch(error => { + console.log(error); + entry.status = ServiceStatus.error; + }); } else if (entry.type === "docker") { - var Docker = require('dockerode'); - - // TODO: read these paths from some config instead of hardcoding them - const options = { - socketPath: '/var/run/docker.sock', - path: '/v1.41/containers/json' - }; - var docker = new Docker({ socketPath: options.socketPath }); - - // default is set as Offline, prevents uncaught cases without set status - entry.status = "Offline"; - // TODO: async possible? - await docker.listContainers({all: true}).then( - ((containers: Dockerode.ContainerInfo[]) => { - // Loop over every found container and compare to the entry provided - containers.forEach((element: Dockerode.ContainerInfo) => { - element.Names.forEach((containerName: string) => { - if (containerName.startsWith("/")) { - containerName = containerName.substring(1); - } - if (containerName === entry.docker_container_name) { - entry.status = "Online"; - } - }); - if (entry.docker_container_name == null) { - console.log("MISSING DOCKER CONTAINER NAME FOR " + entry.name); - entry.status = "ERROR"; - } - }); - }) - ); + entry.status = ServiceStatus.offline; + // Loop over every found container and compare to the entry provided + containers.forEach((element: Dockerode.ContainerInfo) => { + element.Names.forEach((containerName: string) => { + if (containerName.startsWith("/")) { + containerName = containerName.substring(1); + } + if (containerName === entry.docker_container_name) { + entry.status = ServiceStatus.online; + } + }); + if (entry.docker_container_name == null) { + console.log("MISSING DOCKER CONTAINER NAME FOR " + entry.name); + entry.status = ServiceStatus.error; + } + }); } - else { entry.status = "ERROR" } - return; + else { entry.status = ServiceStatus.error } } // for non-local locations pinging can be used to see if they're up - else if (entry.location != "") { - let data: Response; - try { - data = await fetch(entry.href); - } - catch (e) { - console.log(e) - return (entry.status = "Offline"); - } - - if (data.ok) { - if (data.status == 200 || data.status == 301 || data.status == 302) { - return (entry.status = "Online"); - } - else return (entry.status = "Offline"); - } - else return (entry.status = "Offline"); + else if (entry.location === ServiceLocation.other) { + fetch(entry.href) + .then((data) => { + if (data.ok) { + if (data.status == 200 || data.status == 301 || data.status == 302) { + return (entry.status = ServiceStatus.online); + } + else (entry.status = ServiceStatus.offline); + } + else { + entry.status = ServiceStatus.offline; + } + }) + .catch((error) => { + console.log(error); + entry.status = ServiceStatus.error; + }); } - else { return (entry.status = "ERROR") } + else { entry.status = ServiceStatus.error } + return; } export default Services \ No newline at end of file From 8426640271ed5839d284f1aacf33dc87f0defb67 Mon Sep 17 00:00:00 2001 From: Neshura Date: Sat, 10 Dec 2022 03:12:08 +0100 Subject: [PATCH 05/13] Potentially fixes #1 --- pages/services.tsx | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/pages/services.tsx b/pages/services.tsx index 251be44..b906318 100644 --- a/pages/services.tsx +++ b/pages/services.tsx @@ -111,15 +111,16 @@ function getStatus(containers: Dockerode.ContainerInfo[], entry: Service) { else if (entry.type === "docker") { entry.status = ServiceStatus.offline; // Loop over every found container and compare to the entry provided - containers.forEach((element: Dockerode.ContainerInfo) => { - element.Names.forEach((containerName: string) => { - if (containerName.startsWith("/")) { - containerName = containerName.substring(1); - } - if (containerName === entry.docker_container_name) { + containers.forEach((container: Dockerode.ContainerInfo) => { + console.log(container) + if (container.Names.includes("/" + entry.docker_container_name) || container.Names.includes(entry.docker_container_name)) { + if (container.State === "running") { entry.status = ServiceStatus.online; } - }); + else { + entry.status = ServiceStatus.offline; + } + } if (entry.docker_container_name == null) { console.log("MISSING DOCKER CONTAINER NAME FOR " + entry.name); entry.status = ServiceStatus.error; From fff04d019a40b68e8db4f7e71901b0b42c32cc5b Mon Sep 17 00:00:00 2001 From: Neshura Date: Sat, 10 Dec 2022 21:18:55 +0100 Subject: [PATCH 06/13] Small Syntax changes for containers API --- pages/api/containers.tsx | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/pages/api/containers.tsx b/pages/api/containers.tsx index 44803a2..6f75e6e 100644 --- a/pages/api/containers.tsx +++ b/pages/api/containers.tsx @@ -1,11 +1,7 @@ -import fsPromises from 'fs/promises' -import path from 'path' -import { Service, ServiceStatus } from '../../interfaces/LinkTypes'; +import Docker from 'dockerode' -export default async function ServicesAPI(req: any, res: any) { +export default async function ContainersAPI(req: any, res: any) { try { - var Docker = require('dockerode'); - const options = { socketPath: '/var/run/docker.sock', path: '/v1.41/containers/json' From 3303008ec38449e8f5eed300e619cfdadbf09d95 Mon Sep 17 00:00:00 2001 From: Neshura Date: Sun, 11 Dec 2022 16:30:11 +0100 Subject: [PATCH 07/13] incremental Data Fetching using useSWR not quite like I want but should be good enough --- pages/services.tsx | 214 +++++++++++++++++++++++++++------------------ 1 file changed, 127 insertions(+), 87 deletions(-) diff --git a/pages/services.tsx b/pages/services.tsx index b906318..383272e 100644 --- a/pages/services.tsx +++ b/pages/services.tsx @@ -1,32 +1,28 @@ import Head from 'next/head' import Link from 'next/link' import styles from '/styles/Home.module.css' -import { Service, CustomLink, EntryList, ServiceStatus, ServiceType, ServiceLocation } from '../interfaces/LinkTypes' +import { Service, ServiceStatus, ServiceType, ServiceLocation } from '../interfaces/LinkTypes'; import Dockerode from 'dockerode'; -import { ReactElement, useEffect, useState } from 'react' -import useSWR, { KeyedMutator } from 'swr'; +import { ReactElement } from 'react' +import useSWR from 'swr'; const fetcher = (url: string) => fetch(url).then((res) => res.json()) //function Services(props: EntryList) { -const Services = () => { - const { serviceList, isLoading, isError } = useServices(); +function Services() { + const { initialData, fullData, loadingInitial, loadingFull, error } = useServices(); + let content: ReactElement = <>; - // TODO: look into asyncing this as well - useStatus(serviceList); - - if (isError) { - content =
Error Loading data
+ if (error) { content =
Error loading data
} + else if (loadingInitial) { + content =
Loading
} - else if (isLoading) { - content =
Loading..
- } - else if (serviceList) { + else if (loadingFull) { content =
- {serviceList.map((item: Service) => ( + {initialData?.map((item: Service) => (

{item.name}

@@ -38,6 +34,25 @@ const Services = () => { ))}
} + else if (fullData) { + content = + + } + else { + content =
Error loading data
+ } + return ( <> @@ -60,96 +75,121 @@ const Services = () => { ) } -function useStatus(serviceList: Service[] | undefined) { - const { data, error } = useSWR('/api/containers', fetcher); +async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) { + // Currently the only location supporting different fetching depending on type is brr7-4800u + // Others to follow but low prio as this is currently the only location used - if (data && serviceList) { - serviceList.forEach((service: Service) => { - getStatus(data, service); - }) - } - else if (error) { - console.log(error); - } -} - -function useServices(): { serviceList: Service[] | undefined, isLoading: boolean, isError: boolean } { - const { data, error } = useSWR('/api/services', fetcher); - - return { - serviceList: data, - isLoading: !error && !data, - isError: error - }; -} - -function getStatus(containers: Dockerode.ContainerInfo[], entry: Service) { - // for now only the BRR7-4800U can be used with Docker, needs changing once more Servers are used - // TODO: support multiple locations for docker + // Location BRR7-4800U if (entry.location === ServiceLocation.brr7_4800u) { - // app in this context means any non-docker page + // Type APP if (entry.type === ServiceType.app) { - fetch(entry.href) - .then((data: Response) => { - if (data.ok) { - if (data.status == 200 || data.status == 301 || data.status == 302) { - entry.status = ServiceStatus.online; - } - else { - entry.status = ServiceStatus.offline; + await fetch(entry.href) + .then((response) => { + if (response.ok) { + switch(response.status) { + case 200: + case 301: + case 302: + entry.status = ServiceStatus.online; + break; + default: + entry.status = ServiceStatus.offline; } } else { entry.status = ServiceStatus.offline; } }) - .catch(error => { - console.log(error); + .catch((error) => { + console.error("Error pinging Website: ", error); entry.status = ServiceStatus.error; - }); + }) } - else if (entry.type === "docker") { - entry.status = ServiceStatus.offline; - // Loop over every found container and compare to the entry provided - containers.forEach((container: Dockerode.ContainerInfo) => { - console.log(container) - if (container.Names.includes("/" + entry.docker_container_name) || container.Names.includes(entry.docker_container_name)) { - if (container.State === "running") { - entry.status = ServiceStatus.online; + // Type Docker + else if (entry.type === ServiceType.docker) { + containers.forEach((container) => { + // Docker API returns container names with / prepended, not sure whether this always happens so both cases are checked + if (container.Names.includes( entry.docker_container_name || "/" + entry.docker_container_name)) { + // so far only "running" is properly implemented, mroe cases to follow as needed + switch (container.State) { + case "running": + entry.status = ServiceStatus.online; + break; + default: + console.log("Container Status " + container.State + " has no case implemented"); + entry.status = ServiceStatus.offline; + } + } + // If container name is not missing the container is set to offline + else if (entry.docker_container_name !== null) { + console.warn("Container for " + entry.name + " could not be found"); + entry.status = ServiceStatus.offline; + } + else { + console.error("Container Name not specified"); + entry.status = ServiceStatus.error; + } + }) + } + // If no Type matches + else { + console.warn("Service Type for Service " + entry.name + " not specified or invalid"); + entry.status = ServiceStatus.error; + } + } + // Location Other + // TODO: implement docker type for other locations + else if (entry.location === ServiceLocation.other) { + // Currently uses the same handling as app type for the other location + await fetch(entry.href) + .then((response) => { + if (response.ok) { + switch(response.status) { + case 200: + case 301: + case 302: + entry.status = ServiceStatus.online; + break; + default: + entry.status = ServiceStatus.offline; + } } else { entry.status = ServiceStatus.offline; } - } - if (entry.docker_container_name == null) { - console.log("MISSING DOCKER CONTAINER NAME FOR " + entry.name); + }) + .catch((error) => { + console.error("Error pinging Website: ", error); entry.status = ServiceStatus.error; - } - }); - } - else { entry.status = ServiceStatus.error } + }) } - // for non-local locations pinging can be used to see if they're up - else if (entry.location === ServiceLocation.other) { - fetch(entry.href) - .then((data) => { - if (data.ok) { - if (data.status == 200 || data.status == 301 || data.status == 302) { - return (entry.status = ServiceStatus.online); - } - else (entry.status = ServiceStatus.offline); - } - else { - entry.status = ServiceStatus.offline; - } - }) - .catch((error) => { - console.log(error); - entry.status = ServiceStatus.error; - }); + // If no Location matches + else { + console.warn("Service Location for Service " + entry.name + " not specified"); + entry.status = ServiceStatus.error; } - else { entry.status = ServiceStatus.error } - return; + return entry; +} + +const fetchFullDataArray = (containerData: Dockerode.ContainerInfo[], dataSet: Service[]) => { + const fetchStatus = (entry: Service) => getStatus(entry, containerData); + return Promise.all(dataSet.map(fetchStatus)); +} + +function useServices() { + const { data: containerData, error: containerError } = useSWR('/api/containers', fetcher); + const { data: initialData, error: initialError } = useSWR('/api/services', fetcher); + const loadingInitial = !initialData && !initialError + const { data: fullData, error: fullError } = useSWR((initialData && containerData) ? [containerData, initialData] : null, fetchFullDataArray) + const loadingFull = !fullData && !fullError + + return { + initialData, + fullData, + loadingInitial, + loadingFull, + error: initialError || fullError || containerError, + }; } export default Services \ No newline at end of file From c1d6f5d62ee97acb792fa2042a00728d630962d3 Mon Sep 17 00:00:00 2001 From: Neshura Date: Sun, 11 Dec 2022 16:37:59 +0100 Subject: [PATCH 08/13] Fixed docker container name checking --- pages/services.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/services.tsx b/pages/services.tsx index 383272e..60bdb97 100644 --- a/pages/services.tsx +++ b/pages/services.tsx @@ -109,7 +109,7 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) else if (entry.type === ServiceType.docker) { containers.forEach((container) => { // Docker API returns container names with / prepended, not sure whether this always happens so both cases are checked - if (container.Names.includes( entry.docker_container_name || "/" + entry.docker_container_name)) { + if ((container.Names.includes( entry.docker_container_name)) || (container.Names.includes( "/" + entry.docker_container_name))) { // so far only "running" is properly implemented, mroe cases to follow as needed switch (container.State) { case "running": From 3bd10e1f94c9e4d132551e20550b170c7acb8cdb Mon Sep 17 00:00:00 2001 From: Neshura Date: Sun, 11 Dec 2022 16:44:15 +0100 Subject: [PATCH 09/13] Debug Logging added --- pages/services.tsx | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pages/services.tsx b/pages/services.tsx index 60bdb97..1bcfe96 100644 --- a/pages/services.tsx +++ b/pages/services.tsx @@ -122,6 +122,10 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) } // If container name is not missing the container is set to offline else if (entry.docker_container_name !== null) { + // DEBUG + console.log(entry.docker_container_name); + console.log(container.Names); + // DEBUG console.warn("Container for " + entry.name + " could not be found"); entry.status = ServiceStatus.offline; } @@ -183,6 +187,8 @@ function useServices() { const { data: fullData, error: fullError } = useSWR((initialData && containerData) ? [containerData, initialData] : null, fetchFullDataArray) const loadingFull = !fullData && !fullError + console.log(fullError) // DEBUG + return { initialData, fullData, From a27f7457aa7d59e4e61bf03661843be62b826a40 Mon Sep 17 00:00:00 2001 From: Neshura Date: Sun, 11 Dec 2022 16:48:09 +0100 Subject: [PATCH 10/13] Fix attempt for Container name checking --- pages/services.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pages/services.tsx b/pages/services.tsx index 1bcfe96..7b62cb1 100644 --- a/pages/services.tsx +++ b/pages/services.tsx @@ -108,8 +108,8 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) // Type Docker else if (entry.type === ServiceType.docker) { containers.forEach((container) => { - // Docker API returns container names with / prepended, not sure whether this always happens so both cases are checked - if ((container.Names.includes( entry.docker_container_name)) || (container.Names.includes( "/" + entry.docker_container_name))) { + // Docker API returns container names with / prepended + if (container.Names.includes( "/" + entry.docker_container_name)) { // so far only "running" is properly implemented, mroe cases to follow as needed switch (container.State) { case "running": From 3a617f929116ff1f16f229c9f3f913237eb7124c Mon Sep 17 00:00:00 2001 From: Neshura Date: Sun, 11 Dec 2022 17:00:34 +0100 Subject: [PATCH 11/13] More logging --- pages/services.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pages/services.tsx b/pages/services.tsx index 7b62cb1..3111d67 100644 --- a/pages/services.tsx +++ b/pages/services.tsx @@ -109,7 +109,7 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) else if (entry.type === ServiceType.docker) { containers.forEach((container) => { // Docker API returns container names with / prepended - if (container.Names.includes( "/" + entry.docker_container_name)) { + if (container.Names.includes("/" + entry.docker_container_name)) { // so far only "running" is properly implemented, mroe cases to follow as needed switch (container.State) { case "running": @@ -123,8 +123,9 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) // If container name is not missing the container is set to offline else if (entry.docker_container_name !== null) { // DEBUG - console.log(entry.docker_container_name); - console.log(container.Names); + console.log(container.Names.includes("/" + entry.docker_container_name)) + console.log("/" + entry.docker_container_name); + console.log(container.Names[0]); // DEBUG console.warn("Container for " + entry.name + " could not be found"); entry.status = ServiceStatus.offline; From bff352a395c91f814c63382a46322fa847bd0880 Mon Sep 17 00:00:00 2001 From: Neshura Date: Sun, 11 Dec 2022 17:11:02 +0100 Subject: [PATCH 12/13] Replace foreach with for --- pages/services.tsx | 49 ++++++++++++++++++++++++---------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/pages/services.tsx b/pages/services.tsx index 3111d67..bdf57e9 100644 --- a/pages/services.tsx +++ b/pages/services.tsx @@ -86,7 +86,7 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) await fetch(entry.href) .then((response) => { if (response.ok) { - switch(response.status) { + switch (response.status) { case 200: case 301: case 302: @@ -107,9 +107,10 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) } // Type Docker else if (entry.type === ServiceType.docker) { - containers.forEach((container) => { + for (let i = 0; i < containers.length; i++) { + const container = containers[i]; // Docker API returns container names with / prepended - if (container.Names.includes("/" + entry.docker_container_name)) { + if (containers[i].Names.includes("/" + entry.docker_container_name)) { // so far only "running" is properly implemented, mroe cases to follow as needed switch (container.State) { case "running": @@ -119,6 +120,8 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) console.log("Container Status " + container.State + " has no case implemented"); entry.status = ServiceStatus.offline; } + // cancel the for + break; } // If container name is not missing the container is set to offline else if (entry.docker_container_name !== null) { @@ -134,7 +137,7 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) console.error("Container Name not specified"); entry.status = ServiceStatus.error; } - }) + } } // If no Type matches else { @@ -147,26 +150,26 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) else if (entry.location === ServiceLocation.other) { // Currently uses the same handling as app type for the other location await fetch(entry.href) - .then((response) => { - if (response.ok) { - switch(response.status) { - case 200: - case 301: - case 302: - entry.status = ServiceStatus.online; - break; - default: - entry.status = ServiceStatus.offline; - } + .then((response) => { + if (response.ok) { + switch (response.status) { + case 200: + case 301: + case 302: + entry.status = ServiceStatus.online; + break; + default: + entry.status = ServiceStatus.offline; } - else { - entry.status = ServiceStatus.offline; - } - }) - .catch((error) => { - console.error("Error pinging Website: ", error); - entry.status = ServiceStatus.error; - }) + } + else { + entry.status = ServiceStatus.offline; + } + }) + .catch((error) => { + console.error("Error pinging Website: ", error); + entry.status = ServiceStatus.error; + }) } // If no Location matches else { From 7daa8c9295f730f569b2802748a1a055e4bc740b Mon Sep 17 00:00:00 2001 From: Neshura Date: Sun, 11 Dec 2022 17:14:23 +0100 Subject: [PATCH 13/13] Removed Debug logging --- pages/services.tsx | 7 ------- 1 file changed, 7 deletions(-) diff --git a/pages/services.tsx b/pages/services.tsx index bdf57e9..1426db7 100644 --- a/pages/services.tsx +++ b/pages/services.tsx @@ -125,11 +125,6 @@ async function getStatus(entry: Service, containers: Dockerode.ContainerInfo[]) } // If container name is not missing the container is set to offline else if (entry.docker_container_name !== null) { - // DEBUG - console.log(container.Names.includes("/" + entry.docker_container_name)) - console.log("/" + entry.docker_container_name); - console.log(container.Names[0]); - // DEBUG console.warn("Container for " + entry.name + " could not be found"); entry.status = ServiceStatus.offline; } @@ -191,8 +186,6 @@ function useServices() { const { data: fullData, error: fullError } = useSWR((initialData && containerData) ? [containerData, initialData] : null, fetchFullDataArray) const loadingFull = !fullData && !fullError - console.log(fullError) // DEBUG - return { initialData, fullData,