Skip to content

Commit

Permalink
feat: allow configuring the elastic index name
Browse files Browse the repository at this point in the history
  • Loading branch information
blacha committed Dec 16, 2024
1 parent a0ac5a3 commit 9059a86
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/_infra/src/analytics/edge.analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { RetentionDays } from 'aws-cdk-lib/aws-logs';
import { BlockPublicAccess, Bucket } from 'aws-cdk-lib/aws-s3';
import { Construct } from 'constructs';

import { getConfig } from '../config.js';

const CodePath = '../lambda-analytics/dist';
const CodePathV2 = '../lambda-analytics-cloudfront/dist';

Expand Down Expand Up @@ -68,6 +70,7 @@ export class EdgeAnalytics extends Stack {
[Env.Analytics.MaxRecords]: String(24 * 7 * 4),
[Env.Analytics.ElasticId]: Env.get(Env.Analytics.ElasticId) ?? '',
[Env.Analytics.ElasticApiKey]: Env.get(Env.Analytics.ElasticApiKey) ?? '',
[Env.Analytics.ElasticIndexName]: getConfig().ElasticHistoryIndexName,
AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
},
logRetention: RetentionDays.ONE_MONTH,
Expand Down
5 changes: 5 additions & 0 deletions packages/_infra/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export interface BaseMapsConfig {

/** AWS role config bucket */
AwsRoleConfigBucket: string;

/** Elastic Index to use for basemaps history data */
ElasticHistoryIndexName: string;
}

export const BaseMapsProdConfig: BaseMapsConfig = {
Expand All @@ -26,13 +29,15 @@ export const BaseMapsProdConfig: BaseMapsConfig = {
CloudFrontDns: ['basemaps.linz.govt.nz', 'tiles.basemaps.linz.govt.nz'],
PublicUrlBase: 'https://basemaps.linz.govt.nz',
AwsRoleConfigBucket: 'linz-bucket-config',
ElasticHistoryIndexName: 'basemaps-history',
};

export const BaseMapsDevConfig: BaseMapsConfig = {
CogBucket: ['basemaps-cog-test', ...BaseMapsProdConfig.CogBucket],
CloudFrontDns: ['dev.basemaps.linz.govt.nz', 'tiles.dev.basemaps.linz.govt.nz'],
PublicUrlBase: 'https://dev.basemaps.linz.govt.nz',
AwsRoleConfigBucket: 'linz-bucket-config',
ElasticHistoryIndexName: 'nonprod-basemaps-history',
};
/** Is this deployment intended for production */
export const IsProduction = process.env['NODE_ENV'] === 'production';
Expand Down
9 changes: 8 additions & 1 deletion packages/lambda-analytic-cloudfront/src/elastic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export class ElasticClient {
*/
minRequestCount: number = 1;

get indexName(): string {
const indexName = Env.get(Env.Analytics.ElasticIndexName);
if (indexName == null) throw new Error(`$${Env.Analytics.ElasticIndexName} is unset`);
return indexName;
}

get client(): Client {
if (this._client != null) return this._client;

Expand Down Expand Up @@ -46,6 +52,7 @@ export class ElasticClient {

const errors = this.errors;
const indexDelay = this.indexDelay;
const indexName = this.indexName;

async function doInsert(): Promise<void> {
inserts += operations.length / 2;
Expand All @@ -67,7 +74,7 @@ export class ElasticClient {
skipHits++;
continue;
}
operations.push({ index: { _index: 'basemaps-history-' + rec['@timestamp'].slice(0, 4), _id: rec.id } }, rec);
operations.push({ index: { _index: indexName + '-' + rec['@timestamp'].slice(0, 4), _id: rec.id } }, rec);
if (operations.length > 50_000) await doInsert();
}

Expand Down
2 changes: 2 additions & 0 deletions packages/shared/src/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ export const Env = {
ElasticId: 'ELASTIC_ID',
/** ElasticSearch's API key */
ElasticApiKey: 'ELASTIC_API_KEY',
/** Index to use for storing analytic data */
ElasticIndexName: 'ELASTIC_INDEX_NAME',
} as const,

/** Load a environment var defaulting to defaultOutput if it does not exist */
Expand Down

0 comments on commit 9059a86

Please sign in to comment.