Skip to content

Commit

Permalink
HEAT-230 Publishing data for all environments (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrlee authored Apr 16, 2024
1 parent 9cc9cdc commit 4d12e91
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 30 deletions.
42 changes: 26 additions & 16 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
export enum EnvType {
PROD = 'PROD',
PREPROD = 'PREPROD',
DEV = 'DEV',
}

export type AppInsightsCreds = { appId: string; appKey: string }

export type Environment = {
env: EnvType
appInsightsCreds: AppInsightsCreds
}

Expand Down Expand Up @@ -30,26 +37,29 @@ const config = {
tlsEnabled: get('REDIS_TLS_ENABLED', 'false'),
tlsVerification: get('REDIS_TLS_VERIFICATION', 'true'),
},
environments: {
dev: {
environments: [
{
env: EnvType.DEV,
appInsightsCreds: {
appId: get('DEV_APPINSIGHTS_ID'),
appKey: get('DEV_APPINSIGHTS_KEY'),
},
} as Environment,
// preprod: {
// appInsightsCreds: {
// appId: get('PREPROD_APPINSIGHTS_ID'),
// appKey: get('PREPROD_APPINSIGHTS_KEY'),
// },
// } as Environment,
// prod: {
// appInsightsCreds: {
// appId: get('PROD_APPINSIGHTS_ID'),
// appKey: get('PROD_APPINSIGHTS_KEY'),
// },
// } as Environment,
},
},
{
env: EnvType.PREPROD,
appInsightsCreds: {
appId: get('PREPROD_APPINSIGHTS_ID'),
appKey: get('PREPROD_APPINSIGHTS_KEY'),
},
},
{
env: EnvType.PROD,
appInsightsCreds: {
appId: get('PROD_APPINSIGHTS_ID'),
appKey: get('PROD_APPINSIGHTS_KEY'),
},
},
] as Environment[],
}

export default config
6 changes: 3 additions & 3 deletions src/data/appInsights/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { type Environment } from '../../config'
import { type AppInsightsCreds } from '../../config'
import { type Dependency } from '../Components'
import AppInsights from './Client'
import Queries from './queries'

const getDependencies = async (env: Environment): Promise<Dependency[]> => {
const appInsights = new AppInsights(env.appInsightsCreds)
const getDependencies = async (appInsightsCreds: AppInsightsCreds): Promise<Dependency[]> => {
const appInsights = new AppInsights(appInsightsCreds)
const results = await appInsights.query(Queries.DEPENDENCIES())

return results.rows.map(row => ({ componentName: row[0], dependencyHostname: row[1], type: row[2] }))
Expand Down
29 changes: 18 additions & 11 deletions src/run.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
import initialiseAppInsights from './utils/appInsights'
import applicationInfo from './utils/applicationInfo'

import config from './config'
import config, { type Environment } from './config'
import gatherDependencyInfo from './dependency-info-gatherer'
import getComponents from './data/serviceCatalogue'
import getDependencies from './data/appInsights'
import { createRedisClient } from './data/redis/redisClient'
import RedisService from './data/redis/redisService'
import logger from './utils/logger'
import { type Components } from './data/Components'

initialiseAppInsights(applicationInfo())

const calculateDependencies = async ({ env, appInsightsCreds }: Environment, components: Components) => {
const dependencies = await getDependencies(appInsightsCreds)
const componentMap = components.buildComponentMap(dependencies)
const { categoryToComponent, componentDependencyInfo, missingServices } = gatherDependencyInfo(componentMap)

logger.info(`${env}: Services missing from service catalogue: \n\t${missingServices.join('\n\t')}`)

const categoryCounts = Object.entries(categoryToComponent).map(([category, comps]) => `${category} => ${comps.length}`)
logger.info(`${env}: Category freqs: \n${categoryCounts.join('\n')}`)

return [env, { categoryToComponent, componentDependencyInfo, missingServices }]
}

const run = async () => {
const redisClient = createRedisClient()
await redisClient.connect()
Expand All @@ -20,20 +34,13 @@ const run = async () => {
logger.info(`Starting to gather dependency info`)

const components = await getComponents(config.serviceCatalogueUrl)
const dependencies = await getDependencies(config.environments.dev)
const componentMap = components.buildComponentMap(dependencies)

const { categoryToComponent, componentDependencyInfo, missingServices } = gatherDependencyInfo(componentMap)

logger.info(`Services missing from service catalogue: \n\t${missingServices.join('\n\t')}`)

const categoryCounts = Object.entries(categoryToComponent).map(([category, comps]) => `${category} => ${comps.length}`)
logger.info(`Category freqs: \n${categoryCounts.join('\n')}`)
const componentDependencies = await Promise.all(config.environments.map(environment => calculateDependencies(environment, components)))

logger.info(`Starting to publish dependency info`)
await redisService.write({ componentDependencyInfo, categoryToComponent })
await redisService.write(Object.fromEntries(componentDependencies))
logger.info(`Finished publishing dependency info`)

logger.info(`Finished publishing dependency info for ${Object.keys(componentMap).length} components`)
await redisClient.quit()
}

Expand Down

0 comments on commit 4d12e91

Please sign in to comment.