Skip to content

Commit

Permalink
Merge pull request #741 from ps2alerts/refactor/config_and_prometheus…
Browse files Browse the repository at this point in the history
…_metrics

Refactor/config and prometheus metrics
  • Loading branch information
Maelstromeous committed Apr 27, 2023
2 parents e72a0f5 + 0ddec83 commit 9298c0a
Show file tree
Hide file tree
Showing 97 changed files with 1,726 additions and 1,145 deletions.
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,22 @@
"dependencies": {
"@nestjs/axios": "^2.0.0",
"@nestjs/common": "^9.4.0",
"@nestjs/config": "^2.3.1",
"@nestjs/core": "^9.4.0",
"@nestjs/microservices": "^9.4.0",
"@nestjs/platform-fastify": "^9.4.0",
"@nestjs/terminus": "^9.1.1",
"@willsoto/nestjs-prometheus": "^5.1.0",
"amqp-connection-manager": "^4.1.12",
"amqplib": "^0.10.0",
"axios": "^1.3.5",
"date-fns": "^2.29.3",
"dotenv": "^16.0.3",
"ioredis": "^5.3.2",
"lodash": "^4.17.21",
"prom-client": "^14.2.0",
"ps2census": "^4.4.1",
"reflect-metadata": "^0.1.13"
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.0"
},
"devDependencies": {
"@nestjs/cli": "^9.0.0",
Expand Down
45 changes: 37 additions & 8 deletions provisioning/dev/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,75 @@ version: "3.9"
services:
aggregator-pc:
image: ps2alerts/aggregator:dev
container_name: ps2alerts-aggregator-pc
restart: "no"
volumes:
- "../../:/app:rw"
ports:
- "1337"
- "9229:9229"
- "1337:1337"
- "9229:9229" # Debug port
networks:
- "ps2alerts"
environment:
CENSUS_ENVIRONMENT: "ps2"
APP_PORT: "1337"
labels:
traefik.enable: "false"

traefik.enable: "true"
traefik.http.routers.ps2alerts_aggregator_pc_http.rule: "Host(`dev.aggregator.ps2alerts.com`)"
traefik.http.routers.ps2alerts_aggregator_pc_http.entrypoints: "web"
traefik.http.routers.ps2alerts_aggregator_pc.middlewares: "redirect-to-https"
traefik.http.middlewares.redirect-to-https.redirectscheme.scheme: "https"
traefik.http.routers.ps2alerts_aggregator_pc.entrypoints: "websecure"
traefik.http.routers.ps2alerts_aggregator_pc.rule: "Host(`dev.aggregator.ps2alerts.com`)"
traefik.http.routers.ps2alerts_aggregator_pc.tls: "true"
traefik.http.routers.ps2alerts_aggregator_pc.tls.domains[0].main: "dev.aggregator.ps2alerts.com"
aggregator-ps4eu:
image: ps2alerts/aggregator:dev
container_name: ps2alerts-aggregator-ps4eu
restart: "no"
volumes:
- "../../:/app:rw"
ports:
- "1338"
- "1338:1338"
networks:
- "ps2alerts"
environment:
CENSUS_ENVIRONMENT: "ps2ps4eu"
APP_PORT: "1338"
labels:
traefik.enable: "false"
traefik.enable: "true"
traefik.http.routers.ps2alerts_aggregator_ps4eu_http.rule: "Host(`dev.aggregator-ps4eu.ps2alerts.com`)"
traefik.http.routers.ps2alerts_aggregator_ps4eu_http.entrypoints: "web"
traefik.http.routers.ps2alerts_aggregator_ps4eu.middlewares: "redirect-to-https"
traefik.http.middlewares.redirect-to-https.redirectscheme.scheme: "https"
traefik.http.routers.ps2alerts_aggregator_ps4eu.entrypoints: "websecure"
traefik.http.routers.ps2alerts_aggregator_ps4eu.rule: "Host(`dev.aggregator-ps4eu.ps2alerts.com`)"
traefik.http.routers.ps2alerts_aggregator_ps4eu.tls: "true"
traefik.http.routers.ps2alerts_aggregator_ps4eu.tls.domains[0].main: "dev.aggregator-ps4eu.ps2alerts.com"

aggregator-ps4us:
image: ps2alerts/aggregator:dev
container_name: ps2alerts-aggregator-ps4us
restart: "no"
volumes:
- "../../:/app:rw"
ports:
- "1339"
- "1339:1339"
networks:
- "ps2alerts"
environment:
CENSUS_ENVIRONMENT: "ps2ps4us"
APP_PORT: "1339"
labels:
traefik.enable: "false"
traefik.enable: "true"
traefik.http.routers.ps2alerts_aggregator_ps4us_http.rule: "Host(`dev.aggregator-ps4us.ps2alerts.com`)"
traefik.http.routers.ps2alerts_aggregator_ps4us_http.entrypoints: "web"
traefik.http.routers.ps2alerts_aggregator_ps4us.middlewares: "redirect-to-https"
traefik.http.middlewares.redirect-to-https.redirectscheme.scheme: "https"
traefik.http.routers.ps2alerts_aggregator_ps4us.entrypoints: "websecure"
traefik.http.routers.ps2alerts_aggregator_ps4us.rule: "Host(`dev.aggregator-ps4us.ps2alerts.com`)"
traefik.http.routers.ps2alerts_aggregator_ps4us.tls: "true"
traefik.http.routers.ps2alerts_aggregator_ps4us.tls.domains[0].main: "dev.aggregator-ps4us.ps2alerts.com"
networks:
ps2alerts:
external:
Expand Down
12 changes: 10 additions & 2 deletions provisioning/dev/start.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,18 @@
volumes:
- "{{ root_dir }}:/app:rw"
ports:
- "1337"
- "1337:1337"
- "9229:9229"
networks:
- name: "ps2alerts"
tty: true
labels:
traefik.enable: "false"
traefik.enable: "true"
traefik.http.routers.ps2alerts_aggregator_pc_http.rule: "Host(`dev.aggregator.ps2alerts.com`)"
traefik.http.routers.ps2alerts_aggregator_pc_http.entrypoints: "web"
traefik.http.routers.ps2alerts_aggregator_pc.middlewares: "redirect-to-https"
traefik.http.middlewares.redirect-to-https.redirectscheme.scheme: "https"
traefik.http.routers.ps2alerts_aggregator_pc.entrypoints: "websecure"
traefik.http.routers.ps2alerts_aggregator_pc.rule: "Host(`dev.aggregator.ps2alerts.com`)"
traefik.http.routers.ps2alerts_aggregator_pc.tls: "true"
traefik.http.routers.ps2alerts_aggregator_pc.tls.domains[0].main: "dev.aggregator.ps2alerts.com"
31 changes: 24 additions & 7 deletions src/AppModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import InstanceAuthority from './authorities/InstanceAuthority';
import OverdueInstanceAuthority from './authorities/OverdueInstanceAuthority';
import PopulationAuthority from './authorities/PopulationAuthority';
import QueueAuthority from './authorities/QueueAuthority';
import TimingStatisticsAuthority from './authorities/TimingStatisticsAuthority';
import MetricsAuthority from './authorities/MetricsAuthority';
import CharacterBroker from './brokers/CharacterBroker';
import FacilityDataBroker from './brokers/FacilityDataBroker';
import ItemBroker from './brokers/ItemBroker';
Expand Down Expand Up @@ -39,11 +39,11 @@ import CharacterPresenceHandler from './handlers/CharacterPresenceHandler';
import OutfitParticipantCacheHandler from './handlers/OutfitParticipantCacheHandler';
import PopulationHandler from './handlers/PopulationHandler';
import ZoneDataParser from './parsers/ZoneDataParser';
import AuthorityService from './services/authorities/AuthorityService';
import RabbitMQModule from './services/rabbitmq/RabbitMQModule';
import RedisModule from './services/redis/RedisModule';
import CensusModule from './services/census/CensusModule';
import PS2AlertsApiModule from './services/ps2alerts-api/PS2AlertsApiModule';
import AuthorityService from './modules/authorities/AuthorityService';
import RabbitMQModule from './modules/rabbitmq/RabbitMQModule';
import RedisModule from './modules/redis/RedisModule';
import CensusModule from './modules/census/CensusModule';
import PS2AlertsApiModule from './modules/ps2alerts-api/PS2AlertsApiModule';
import {TYPES} from './constants/types';
import MessageQueueHandlerInterface from './interfaces/MessageQueueHandlerInterface';
import PopulationData from './data/PopulationData';
Expand All @@ -56,14 +56,26 @@ import AdminAggregatorSubscriber from './subscribers/AdminAggregatorSubscriber';
import MetagameSubscriber from './subscribers/MetagameSubscriber';
import {HealthModule} from './health/HealthModule';
import {Death} from 'ps2census';
import {ConfigModule} from '@nestjs/config';
import {config} from './config';
import MetricsModule from './modules/metrics/MetricsModule';
import {PS2AlertsApiDriver} from './drivers/PS2AlertsApiDriver';
import {FalconRequestDriver} from './drivers/FalconRequestDriver';
import {CensusRequestDriver} from './drivers/CensusRequestDriver';

@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [config],
expandVariables: true,
}),
HealthModule,
RedisModule,
RabbitMQModule,
CensusModule,
PS2AlertsApiModule,
MetricsModule,
],
providers: [
// TODO: Split this monstrosity into separate modules with some sort of hierarchy
Expand All @@ -72,7 +84,7 @@ import {Death} from 'ps2census';
OverdueInstanceAuthority,
PopulationAuthority,
QueueAuthority,
TimingStatisticsAuthority,
MetricsAuthority,

// Brokers
CharacterBroker,
Expand Down Expand Up @@ -128,6 +140,11 @@ import {Death} from 'ps2census';
AdminAggregatorSubscriber,
MetagameSubscriber,

// Drivers
CensusRequestDriver,
PS2AlertsApiDriver,
FalconRequestDriver,

// Multi injects
{
provide: TYPES.populationAggregates,
Expand Down
10 changes: 2 additions & 8 deletions src/actions/MetagameInstanceTerritoryFacilityControlAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ import {ActionInterface} from '../interfaces/ActionInterface';
import FacilityControlEvent from '../handlers/ps2census/events/FacilityControlEvent';
import TerritoryResultInterface from '../ps2alerts-constants/interfaces/TerritoryResultInterface';
import {ps2AlertsApiEndpoints} from '../ps2alerts-constants/ps2AlertsApiEndpoints';
import {AxiosInstance} from 'axios';
import {Logger} from '@nestjs/common';
import StatisticsHandler, {MetricTypes} from '../handlers/StatisticsHandler';
import {PS2AlertsApiDriver} from '../drivers/PS2AlertsApiDriver';

export default class MetagameInstanceTerritoryFacilityControlAction implements ActionInterface<boolean> {
private static readonly logger = new Logger('MetagameInstanceTerritoryFacilityControlAction');

constructor(
private readonly event: FacilityControlEvent,
private readonly territoryResultAction: ActionInterface<TerritoryResultInterface>,
private readonly ps2AlertsApiClient: AxiosInstance,
private readonly statisticsHandler: StatisticsHandler,
private readonly ps2AlertsApiClient: PS2AlertsApiDriver,
) {}

public async execute(): Promise<boolean> {
Expand All @@ -27,8 +25,6 @@ export default class MetagameInstanceTerritoryFacilityControlAction implements A
// Update the result for the instance
await this.territoryResultAction.execute();

const started = new Date();

// Update the mapControl record for the facility itself, since we now know the result values.
// This is used to display the snapshot in times for each facility capture, so we can render the map correctly.
// Note: This will update the LATEST record, it is assumed it is created first.
Expand All @@ -41,8 +37,6 @@ export default class MetagameInstanceTerritoryFacilityControlAction implements A
MetagameInstanceTerritoryFacilityControlAction.logger.error(`[${this.event.instance.instanceId}] Unable to update the facility control record via API! Err: ${err.message}`);
});

await this.statisticsHandler.logTime(started, MetricTypes.PS2ALERTS_API);

return true;
}
}
10 changes: 2 additions & 8 deletions src/actions/MetagameInstanceTerritoryResultAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ import TerritoryResultInterface from '../ps2alerts-constants/interfaces/Territor
import ApplicationException from '../exceptions/ApplicationException';
import MetagameTerritoryInstance from '../instances/MetagameTerritoryInstance';
import {ps2AlertsApiEndpoints} from '../ps2alerts-constants/ps2AlertsApiEndpoints';
import {AxiosInstance} from 'axios';
import {
MetagameTerritoryControlResultInterface,
} from '../ps2alerts-constants/interfaces/MetagameTerritoryControlResultInterface';
import MetagameTerritoryCalculator from '../calculators/MetagameTerritoryCalculator';
import {Logger} from '@nestjs/common';
import StatisticsHandler, {MetricTypes} from '../handlers/StatisticsHandler';
import {PS2AlertsApiDriver} from '../drivers/PS2AlertsApiDriver';

// This class takes care of calculating the result of an instance and updating it via both the API and in memory
export default class MetagameInstanceTerritoryResultAction implements ActionInterface<TerritoryResultInterface> {
Expand All @@ -18,8 +17,7 @@ export default class MetagameInstanceTerritoryResultAction implements ActionInte
constructor(
private readonly instance: MetagameTerritoryInstance,
private readonly territoryCalculator: MetagameTerritoryCalculator,
private readonly ps2alertsApiClient: AxiosInstance,
private readonly statisticsHandler: StatisticsHandler,
private readonly ps2alertsApiClient: PS2AlertsApiDriver,
) {}

public async execute(): Promise<MetagameTerritoryControlResultInterface> {
Expand All @@ -30,8 +28,6 @@ export default class MetagameInstanceTerritoryResultAction implements ActionInte
this.instance.result = result;
}

const started = new Date();

// Call API to patch the instance record
await this.ps2alertsApiClient.patch(
ps2AlertsApiEndpoints.instancesInstance
Expand All @@ -41,8 +37,6 @@ export default class MetagameInstanceTerritoryResultAction implements ActionInte
throw new ApplicationException(`[${this.instance.instanceId}] Unable to update instance result data! Err: ${err.message} - Data: ${JSON.stringify({result})}`, 'MetagameInstanceTerritoryResultAction');
});

await this.statisticsHandler.logTime(started, MetricTypes.PS2ALERTS_API);

return result;
}

Expand Down
30 changes: 14 additions & 16 deletions src/actions/MetagameInstanceTerritoryStartAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,44 @@ import CensusMapRegionQueryParser from '../parsers/CensusMapRegionQueryParser';
import MapDataInterface from '../interfaces/MapDataInterface';
import {censusOldFacilities} from '../ps2alerts-constants/censusOldFacilities';
import {Rest} from 'ps2census';
import {AxiosInstance} from 'axios';
import {ps2AlertsApiEndpoints} from '../ps2alerts-constants/ps2AlertsApiEndpoints';
import Redis from 'ioredis';
import ZoneDataParser from '../parsers/ZoneDataParser';
import {Logger} from '@nestjs/common';
import StatisticsHandler, {MetricTypes} from '../handlers/StatisticsHandler';
import MetricsHandler from '../handlers/MetricsHandler';
import {PS2AlertsApiDriver} from '../drivers/PS2AlertsApiDriver';
import {CensusRequestDriver} from '../drivers/CensusRequestDriver';

export default class MetagameInstanceTerritoryStartAction implements ActionInterface<boolean> {
private static readonly logger = new Logger('MetagameInstanceTerritoryStartAction');

constructor(
private readonly instance: MetagameTerritoryInstance,
private readonly ps2alertsApiClient: AxiosInstance,
private readonly ps2alertsApiClient: PS2AlertsApiDriver,
private readonly restClient: Rest.Client,
private readonly cacheClient: Redis,
private readonly zoneDataParser: ZoneDataParser,
private readonly statisticsHandler: StatisticsHandler,
private readonly metricsHandler: MetricsHandler,
private readonly censusRequestDriver: CensusRequestDriver,
) {}

public async execute(): Promise<boolean> {
MetagameInstanceTerritoryStartAction.logger.log(`[${this.instance.instanceId}] Running startActions()`);
MetagameInstanceTerritoryStartAction.logger.log(`[${this.instance.instanceId}] Trying to get initial map state`);
MetagameInstanceTerritoryStartAction.logger.debug(`[${this.instance.instanceId}] Running startActions()`);
MetagameInstanceTerritoryStartAction.logger.debug(`[${this.instance.instanceId}] Trying to get initial map state`);

const docs = await this.getInitialMap();

if (!docs) {
throw new ApplicationException(`[${this.instance.instanceId}] Map state was empty!`, 'MetagameInstanceTerritoryStartAction');
}

const started = new Date();

await this.ps2alertsApiClient.post(
ps2AlertsApiEndpoints.instanceEntriesFacilityBatch,
docs,
).catch((err: Error) => {
throw new ApplicationException(`[${this.instance.instanceId}] Unable to update bracket! E: ${err.message}`, 'MetagameInstanceTerritoryStartAction');
throw new ApplicationException(`[${this.instance.instanceId}] Unable to insert initial map state! E: ${err.message}`, 'MetagameInstanceTerritoryStartAction');
});

await this.statisticsHandler.logTime(started, MetricTypes.PS2ALERTS_API);

MetagameInstanceTerritoryStartAction.logger.log(`[${this.instance.instanceId}] Inserted initial map state`);

return true;
Expand All @@ -61,17 +59,17 @@ export default class MetagameInstanceTerritoryStartAction implements ActionInter
this.instance,
this.cacheClient,
this.zoneDataParser,
this.statisticsHandler,
this.metricsHandler,
this.censusRequestDriver,
).getMapData();
await this.statisticsHandler.logTime(date, MetricTypes.CENSUS_MAP_REGION);

if (mapData.length === 0) {
throw new ApplicationException('Unable to properly get map data from census!');
if (!mapData.Regions?.Row || mapData.Regions.Row.length === 0) {
throw new ApplicationException(`[${this.instance.instanceId}] Unable to properly get map data from census!`, 'MetagameInstanceTerritoryStartAction');
}

const docs: MapDataInterface[] = [];

mapData[0].Regions.Row.forEach((row) => {
mapData.Regions.Row.forEach((row) => {
// Check if we have a facility type, if we don't chuck it as it's an old facility
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const facilityId = parseInt(row.RowData.map_region.facility_id, 10);
Expand Down
Loading

0 comments on commit 9298c0a

Please sign in to comment.