diff --git a/src/config.ts b/src/config.ts index 299e25d..4ee3a54 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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 } @@ -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 diff --git a/src/data/appInsights/index.ts b/src/data/appInsights/index.ts index e873a28..0aec949 100644 --- a/src/data/appInsights/index.ts +++ b/src/data/appInsights/index.ts @@ -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 => { - const appInsights = new AppInsights(env.appInsightsCreds) +const getDependencies = async (appInsightsCreds: AppInsightsCreds): Promise => { + 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] })) diff --git a/src/run.ts b/src/run.ts index 2dc44bf..53f8020 100644 --- a/src/run.ts +++ b/src/run.ts @@ -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() @@ -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() }