From 48c36aaf65a0d830bd2ec60739cc993e0941e51c Mon Sep 17 00:00:00 2001 From: Andrew Lee <1517745+andrewrlee@users.noreply.github.com> Date: Thu, 2 May 2024 09:45:22 +0100 Subject: [PATCH] HEAT-230 updating package and adding last updated date (#15) * HEAT-230 updating package and adding last updated date * HEAT-230 Handle when cloud role name differs from component name --- .../templates/update-dependency-info.yaml | 2 +- package.json | 20 ++++---- src/data/Components.test.ts | 47 +++++++++++++++++++ src/data/Components.ts | 11 ++++- src/data/appInsights/Client.ts | 1 - src/run.ts | 5 +- 6 files changed, 72 insertions(+), 14 deletions(-) diff --git a/helm_deploy/hmpps-component-dependencies/templates/update-dependency-info.yaml b/helm_deploy/hmpps-component-dependencies/templates/update-dependency-info.yaml index 05103f8..fae46f5 100644 --- a/helm_deploy/hmpps-component-dependencies/templates/update-dependency-info.yaml +++ b/helm_deploy/hmpps-component-dependencies/templates/update-dependency-info.yaml @@ -9,7 +9,7 @@ spec: concurrencyPolicy: Replace failedJobsHistoryLimit: 5 startingDeadlineSeconds: 43200 - successfulJobsHistoryLimit: 5 + successfulJobsHistoryLimit: 1 jobTemplate: spec: template: diff --git a/package.json b/package.json index fb655ae..e0a8e9a 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "dep-tree", + "name": "hmpps-component-dependencies", "version": "1.0.0", "description": "", "main": "index.js", @@ -69,12 +69,12 @@ ] }, "devDependencies": { - "@types/jest": "^27.4.1", + "@types/jest": "^29.5.12", "@types/superagent": "^8.1.6", - "@typescript-eslint/eslint-plugin": "^7.4.0", + "@typescript-eslint/eslint-plugin": "^7.8.0", "audit-ci": "^6.6.1", "dotenv": "^16.4.5", - "eslint": "^8.57.0", + "eslint": "^9.1.1", "eslint-config-airbnb-base": "^15.0.0", "eslint-config-prettier": "^9.1.0", "eslint-import-resolver-typescript": "^3.6.1", @@ -82,23 +82,23 @@ "eslint-plugin-no-only-tests": "^3.1.0", "eslint-plugin-prettier": "^5.1.3", "husky": "^9.0.11", - "jest": "^27.5.1", + "jest": "^29.7.0", "jest-html-reporter": "^3.10.2", "jest-junit": "^16.0.0", "lint-staged": "^15.2.2", "prettier": "^3.2.5", - "ts-jest": "^27.1.3", - "typescript": "^4.9.5" + "ts-jest": "^29.1.2", + "typescript": "^5.4.5" }, "dependencies": { "@types/bunyan": "^1.8.11", "@types/bunyan-format": "^0.2.9", - "applicationinsights": "^2.9.5", + "applicationinsights": "^3.0.1", "bunyan": "^1.8.15", "bunyan-format": "^0.2.1", "connect-redis": "^7.1.1", "redis": "^4.6.13", - "superagent": "^8.1.2", - "ts-node": "^10.9.1" + "superagent": "^9.0.2", + "ts-node": "^10.9.2" } } diff --git a/src/data/Components.test.ts b/src/data/Components.test.ts index c11df88..98b68aa 100644 --- a/src/data/Components.test.ts +++ b/src/data/Components.test.ts @@ -97,4 +97,51 @@ describe('Components', () => { expect(node.knownDependencies).toStrictEqual({}) } }) + + test('map with one way dependency between one known components and one unknown component by cloud role name', () => { + const component1: Component = { name: 'comp-1', cloudRoleName: 'comp1', environments: [{ name: 'dev', url: 'http://component1' }] } + + const components = new Components([component1]) + + const map = components.buildComponentMap([{ componentName: component1.cloudRoleName, type: 'http', dependencyHostname: 'http://some-unknown' }]) + + { + const node = map[component1.name] + + expect(node.component).toStrictEqual(component1) + expect(node.unknownDependencies).toStrictEqual([{ componentName: 'comp1', dependencyHostname: 'http://some-unknown', type: 'http' }]) + expect(node.reliedUponBy).toStrictEqual({}) + expect(node.dependencyCategories).toStrictEqual(['HTTP']) + expect(node.knownDependencies).toStrictEqual({}) + } + }) + + test('map with one way dependency between one known components when looking up based on cloud role name', () => { + const component1: Component = { name: 'comp-1', cloudRoleName: 'comp1', environments: [{ name: 'dev', url: 'http://component1' }] } + const component2: Component = { name: 'comp-2', cloudRoleName: 'comp2', environments: [{ name: 'dev', url: 'http://component2' }] } + + const components = new Components([component1, component2]) + + const map = components.buildComponentMap([{ componentName: component2.cloudRoleName, type: 'http', dependencyHostname: 'http://component1' }]) + + { + const node = map[component1.name] + + expect(node.component).toStrictEqual(component1) + expect(node.unknownDependencies).toStrictEqual([]) + expect(node.reliedUponBy[component2.name].component).toStrictEqual(component2) + expect(node.dependencyCategories).toStrictEqual([]) + expect(node.knownDependencies).toStrictEqual({}) + } + + { + const node = map[component2.name] + + expect(node.component).toStrictEqual(component2) + expect(node.unknownDependencies).toStrictEqual([]) + expect(node.reliedUponBy).toStrictEqual({}) + expect(node.dependencyCategories).toStrictEqual(['HTTP']) + expect(node.knownDependencies[component1.name].component).toStrictEqual(component1) + } + }) }) diff --git a/src/data/Components.ts b/src/data/Components.ts index 437fd34..38ced24 100644 --- a/src/data/Components.ts +++ b/src/data/Components.ts @@ -33,8 +33,17 @@ export class Components { return acc }, {} as ComponentMap) + const cloudRoleNameLookup = this.components.reduce((acc, component) => { + acc[component.cloudRoleName] = component.name + return acc + }, {} as Record) + + const lookUpComponentName = (name: string) => cloudRoleNameLookup[name] || name + dependencies.forEach(dependency => { - const { componentName, dependencyHostname } = dependency + const { componentName: cloudRoleName, dependencyHostname } = dependency + const componentName = lookUpComponentName(cloudRoleName) + const componentNode = componentMap[componentName] || new ComponentNode(componentName, undefined) const dependentComponent = this.getComponentForHostname(dependencyHostname) if (dependentComponent) { diff --git a/src/data/appInsights/Client.ts b/src/data/appInsights/Client.ts index 4e7a19b..3249664 100644 --- a/src/data/appInsights/Client.ts +++ b/src/data/appInsights/Client.ts @@ -1,7 +1,6 @@ import superagent from 'superagent' import { type AppInsightsCreds } from '../../config' - type QueryResult = { headers: string[] rows: string[][] diff --git a/src/run.ts b/src/run.ts index 53f8020..769953a 100644 --- a/src/run.ts +++ b/src/run.ts @@ -38,7 +38,10 @@ const run = async () => { const componentDependencies = await Promise.all(config.environments.map(environment => calculateDependencies(environment, components))) logger.info(`Starting to publish dependency info`) - await redisService.write(Object.fromEntries(componentDependencies)) + + const data = Object.fromEntries(componentDependencies) + await redisService.write({ lastUpdated: new Date().toISOString(), ...data }) + logger.info(`Finished publishing dependency info`) await redisClient.quit()