Skip to content
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

feat: fetch versions of external components #118

Merged
merged 2 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/controllers/serverVersions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ function compileVersions(component: any, prefix = ''): Versions {
Object.keys(compiledVersions).forEach((key) => (versions[key] = compiledVersions[key]))
})
}
if (component._extVersions) {
component._extVersions.forEach((child: PromiseSettledResult<any>) => {
if (child.status === 'rejected' || child.value.error) return
const compiledVersions = compileVersions(child.value, '~' + child.value.name + '.')
Object.keys(compiledVersions).forEach((key) => (versions[key] = compiledVersions[key]))
})
}
return versions
}

Expand Down
26 changes: 26 additions & 0 deletions src/lib/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fetch from 'node-fetch'
import { URL, URLSearchParams } from 'url'

const HEALTH_PATH = 'health'
const EXT_VERSIONS = ['external/sisyfos/health']

export async function getServerHealth(serverHost: string): Promise<any> {
if (serverHost !== '') {
Expand All @@ -26,6 +27,30 @@ export async function getServerHealth(serverHost: string): Promise<any> {
}
}

export async function getExternalVersions(serverHost: string): Promise<PromiseSettledResult<any>[]> {
const result: any[] = []
if (serverHost !== '') {
const serverLocation = /^https?:\/\//.test(serverHost) ? new URL(serverHost) : new URL(`http://${serverHost}`)

return Promise.allSettled(
EXT_VERSIONS.map(async (extUrl) => {
const url = `${serverLocation.href}${extUrl}`

try {
const response = await fetch(url, { method: 'GET' })
return await response.json()
} catch (error) {
return {
error: new Error(`Unable to fetch resource ${url}: ${error}`),
}
}
})
)
}

return result
}

/**
* Accepts a list of server hosts and returns the responses from a query to
* each server's /health service.
Expand All @@ -41,6 +66,7 @@ export async function getServerData(serverHosts: Array<string>): Promise<Array<a
return Promise.all(
serverHosts.map(async (host) => {
const serverHealth = await getServerHealth(host)
serverHealth._extVersions = await getExternalVersions(host)
serverHealth._host = host
return serverHealth
})
Expand Down
7 changes: 5 additions & 2 deletions test/serverStatus.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ describe('GET /serverStatus', () => {
expect(dom.window.document.characterSet).toEqual('UTF-8')
dom.window.document.getElementById('versions')

expect(fetch).toHaveBeenCalledTimes(1)
expect(fetch).toHaveBeenCalledWith('http://localhost:3000/health', {
expect(fetch).toHaveBeenCalledTimes(2)
expect(fetch).toHaveBeenNthCalledWith(1, 'http://localhost:3000/health', {
method: 'GET',
})
expect(fetch).toHaveBeenNthCalledWith(2, 'http://localhost:3000/external/sisyfos/health', {
method: 'GET',
})
})
Expand Down
7 changes: 5 additions & 2 deletions test/serverVersions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ describe('GET /serverVersions', () => {
expect(dom.window.document.characterSet).toEqual('UTF-8')
dom.window.document.getElementById('versions')

expect(fetch).toHaveBeenCalledTimes(1)
expect(fetch).toHaveBeenCalledWith('http://localhost:3000/health', {
expect(fetch).toHaveBeenCalledTimes(2)
expect(fetch).toHaveBeenNthCalledWith(1, 'http://localhost:3000/health', {
method: 'GET',
})
expect(fetch).toHaveBeenNthCalledWith(2, 'http://localhost:3000/external/sisyfos/health', {
method: 'GET',
})
})
Expand Down
Loading