Skip to content

Commit dd62833

Browse files
committed
Switch analytics to dual-key setup
1 parent 5f4ffc4 commit dd62833

18 files changed

Lines changed: 238 additions & 113 deletions

File tree

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Module } from '@nestjs/common';
22
import { ClientsModule, Transport } from '@nestjs/microservices';
3-
import { AnalyticsConfigProto, AnalyticsConfigProtoFile, AnalyticsProto, AnalyticsProtoFile } from 'juno-proto';
3+
import { AnalyticsConfigProto, AnalyticsConfigProtoFile } from 'juno-proto';
44
import { AnalyticsController } from './analytics.controller';
55
import { AnalyticsService } from './analytics.service';
66
import { AnalyticsConfigModule } from '../analytics_config/analytics_config.module';
@@ -13,16 +13,15 @@ import { AnalyticsConfigModule } from '../analytics_config/analytics_config.modu
1313
transport: Transport.GRPC,
1414
options: {
1515
url: process.env.DB_SERVICE_ADDR,
16-
package: AnalyticsConfigProto.JUNO_ANALYTICS_SERVICE_ANALYTICS_CONFIG_PACKAGE_NAME,
16+
package:
17+
AnalyticsConfigProto.JUNO_ANALYTICS_SERVICE_ANALYTICS_CONFIG_PACKAGE_NAME,
1718
protoPath: AnalyticsConfigProtoFile,
1819
},
1920
},
2021
]),
2122
AnalyticsConfigModule,
2223
],
2324
controllers: [AnalyticsController],
24-
providers: [
25-
AnalyticsService,
26-
],
25+
providers: [AnalyticsService],
2726
})
2827
export class AnalyticsModule {}

‎packages/analytics-service/src/modules/analytics/analytics.service.ts‎

Lines changed: 104 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,29 @@ import { AnalyticsConfigProto, AnalyticsProto } from 'juno-proto';
55
import { lastValueFrom } from 'rxjs';
66
import { ANALYTICS_CONFIG_DB_SERVICE_NAME } from 'juno-proto/dist/gen/analytics_config';
77

8-
type EventEnvironment = any;
9-
108
@Injectable()
119
export class AnalyticsService implements OnModuleInit {
1210
private analyticsService: AnalyticsConfigProto.AnalyticsConfigDbServiceClient;
1311

14-
private AnalyticsLogger: any;
12+
private AnalyticsLogger: any;
1513
private AnalyticsViewer: any;
1614
private EventEnvironment: any;
1715

18-
constructor(@Inject(ANALYTICS_CONFIG_DB_SERVICE_NAME) private analyticsClient: ClientGrpc
16+
constructor(
17+
@Inject(ANALYTICS_CONFIG_DB_SERVICE_NAME)
18+
private analyticsClient: ClientGrpc,
1919
) {}
2020

2121
async onModuleInit() {
22-
this.analyticsService = this.analyticsClient.getService<AnalyticsConfigProto.AnalyticsConfigDbServiceClient>(
23-
ANALYTICS_CONFIG_DB_SERVICE_NAME
24-
);
22+
this.analyticsService =
23+
this.analyticsClient.getService<AnalyticsConfigProto.AnalyticsConfigDbServiceClient>(
24+
ANALYTICS_CONFIG_DB_SERVICE_NAME,
25+
);
2526

2627
// bog-analytics is absolutely cursed and outdated, have to do some shenanigans
2728
// to get around ESM import
2829
const loadModule = eval('(specifier) => import(specifier)');
29-
const bogAnalytics = await loadModule('bog-analytics');
30+
const bogAnalytics = await loadModule('bog-analytics');
3031

3132
this.AnalyticsLogger = bogAnalytics.AnalyticsLogger;
3233
this.AnalyticsViewer = bogAnalytics.AnalyticsViewer;
@@ -35,7 +36,11 @@ export class AnalyticsService implements OnModuleInit {
3536

3637
// bog-analytics currently doesn't allow stateless requests with passing in a key,
3738
// so we have to compromise by re-instantiating the object each time
38-
async getAnalyticsViewer(analyticsEnvironment: any, configId: number, junoEnvironment: string) {
39+
async getAnalyticsViewer(
40+
analyticsEnvironment: any,
41+
configId: number,
42+
junoEnvironment: string,
43+
) {
3944
const config = await lastValueFrom(
4045
this.analyticsService.readAnalyticsConfig({
4146
id: configId,
@@ -49,7 +54,7 @@ export class AnalyticsService implements OnModuleInit {
4954
});
5055

5156
try {
52-
viewer.authenticate(config.analyticsKey);
57+
viewer.authenticate(config.serverAnalyticsKey);
5358
} catch (e) {
5459
throw new RpcException({
5560
code: status.UNAUTHENTICATED,
@@ -60,7 +65,11 @@ export class AnalyticsService implements OnModuleInit {
6065
return viewer;
6166
}
6267

63-
async getAnalyticsLogger(analyticsEnvironment: any, configId: number, junoEnvironment: string) {
68+
async getAnalyticsLogger(
69+
analyticsEnvironment: any,
70+
configId: number,
71+
junoEnvironment: string,
72+
) {
6473
const config = await lastValueFrom(
6574
this.analyticsService.readAnalyticsConfig({
6675
id: configId,
@@ -70,11 +79,11 @@ export class AnalyticsService implements OnModuleInit {
7079

7180
const viewer = new this.AnalyticsLogger({
7281
apiBaseUrl: process.env.BOG_ANALYTICS_BASE_URL,
73-
environment: analyticsEnvironment
82+
environment: analyticsEnvironment,
7483
});
7584

7685
try {
77-
viewer.authenticate(config.analyticsKey);
86+
viewer.authenticate(config.clientAnalyticsKey);
7887
} catch (e) {
7988
throw new RpcException({
8089
code: status.UNAUTHENTICATED,
@@ -88,7 +97,11 @@ export class AnalyticsService implements OnModuleInit {
8897
async logClickEvent(
8998
event: AnalyticsProto.ClickEventRequest,
9099
): Promise<AnalyticsProto.ClickEventResponse> {
91-
const logger = await this.getAnalyticsLogger(this.EventEnvironment.DEVELOPMENT, event.configId, event.configEnvironment);
100+
const logger = await this.getAnalyticsLogger(
101+
this.EventEnvironment.DEVELOPMENT,
102+
event.configId,
103+
event.configEnvironment,
104+
);
92105

93106
if (
94107
!event ||
@@ -128,8 +141,12 @@ export class AnalyticsService implements OnModuleInit {
128141
async logInputEvent(
129142
event: AnalyticsProto.InputEventRequest,
130143
): Promise<AnalyticsProto.InputEventResponse> {
131-
// TODO: we need to allow bog-analytics specific environments
132-
const logger = await this.getAnalyticsLogger(this.EventEnvironment.DEVELOPMENT, event.configId, event.environment);
144+
// TODO: we need to allow bog-analytics specific environments
145+
const logger = await this.getAnalyticsLogger(
146+
this.EventEnvironment.DEVELOPMENT,
147+
event.configId,
148+
event.environment,
149+
);
133150

134151
if (
135152
!event ||
@@ -173,8 +190,12 @@ export class AnalyticsService implements OnModuleInit {
173190
async logVisitEvent(
174191
event: AnalyticsProto.VisitEventRequest,
175192
): Promise<AnalyticsProto.VisitEventResponse> {
176-
// TODO: we need to allow environments
177-
const logger = await this.getAnalyticsLogger(this.EventEnvironment.DEVELOPMENT, event.configId, event.environment);
193+
// TODO: we need to allow environments
194+
const logger = await this.getAnalyticsLogger(
195+
this.EventEnvironment.DEVELOPMENT,
196+
event.configId,
197+
event.environment,
198+
);
178199

179200
if (
180201
!event ||
@@ -214,8 +235,12 @@ export class AnalyticsService implements OnModuleInit {
214235
async logCustomEvent(
215236
event: AnalyticsProto.CustomEventRequest,
216237
): Promise<AnalyticsProto.CustomEventResponse> {
217-
// TODO: we need to allow environments
218-
const logger = await this.getAnalyticsLogger(this.EventEnvironment.DEVELOPMENT, event.configId, event.environment);
238+
// TODO: we need to allow environments
239+
const logger = await this.getAnalyticsLogger(
240+
this.EventEnvironment.DEVELOPMENT,
241+
event.configId,
242+
event.environment,
243+
);
219244

220245
if (
221246
!event ||
@@ -261,7 +286,11 @@ export class AnalyticsService implements OnModuleInit {
261286
request: AnalyticsProto.CustomEventTypeRequest,
262287
): Promise<AnalyticsProto.CustomEventTypeResponse> {
263288
// TODO: environments
264-
const viewer = await this.getAnalyticsViewer(this.EventEnvironment.DEVELOPMENT, request.configId, request.configEnvironment);
289+
const viewer = await this.getAnalyticsViewer(
290+
this.EventEnvironment.DEVELOPMENT,
291+
request.configId,
292+
request.configEnvironment,
293+
);
265294

266295
if (!request.projectName || request.projectName.length === 0) {
267296
throw new RpcException({
@@ -270,9 +299,7 @@ export class AnalyticsService implements OnModuleInit {
270299
});
271300
}
272301

273-
const response = await viewer.getCustomEventTypes(
274-
request.projectName,
275-
);
302+
const response = await viewer.getCustomEventTypes(request.projectName);
276303

277304
if (!response || response.length === 0) {
278305
return {
@@ -299,7 +326,11 @@ export class AnalyticsService implements OnModuleInit {
299326
request: AnalyticsProto.CustomGraphTypeRequest,
300327
): Promise<AnalyticsProto.CustomGraphTypeResponse> {
301328
// TODO: environments
302-
const viewer = await this.getAnalyticsViewer(this.EventEnvironment.DEVELOPMENT, request.configId, request.configEnvironment);
329+
const viewer = await this.getAnalyticsViewer(
330+
this.EventEnvironment.DEVELOPMENT,
331+
request.configId,
332+
request.configEnvironment,
333+
);
303334

304335
if (!request.projectName || request.projectName.length === 0) {
305336
throw new RpcException({
@@ -341,7 +372,11 @@ export class AnalyticsService implements OnModuleInit {
341372
async getClickEventsPaginated(
342373
request: AnalyticsProto.GetClickEventsRequest,
343374
): Promise<AnalyticsProto.GetClickEventsResponse> {
344-
const viewer = await this.getAnalyticsViewer(this.EventEnvironment.DEVELOPMENT, request.configId, request.environment);
375+
const viewer = await this.getAnalyticsViewer(
376+
this.EventEnvironment.DEVELOPMENT,
377+
request.configId,
378+
request.environment,
379+
);
345380

346381
if (!request.projectName || request.projectName.length === 0) {
347382
throw new RpcException({
@@ -358,8 +393,7 @@ export class AnalyticsService implements OnModuleInit {
358393
afterTime: request.afterTime || undefined,
359394
};
360395

361-
const response =
362-
await viewer.getClickEventsPaginated(queryParams);
396+
const response = await viewer.getClickEventsPaginated(queryParams);
363397

364398
if (!response) {
365399
return { events: [], afterId: '' };
@@ -392,7 +426,12 @@ export class AnalyticsService implements OnModuleInit {
392426
async getAllClickEvents(
393427
request: AnalyticsProto.GetAllClickEventsRequest,
394428
): Promise<AnalyticsProto.GetAllClickEventsResponse> {
395-
const viewer = await this.getAnalyticsViewer(this.EventEnvironment.DEVELOPMENT, request.configId, request.configEnvironment);
429+
console.log('requesting new viewer ');
430+
const viewer = await this.getAnalyticsViewer(
431+
this.EventEnvironment.DEVELOPMENT,
432+
request.configId,
433+
request.configEnvironment,
434+
);
396435

397436
if (!request.projectName || request.projectName.length === 0) {
398437
throw new RpcException({
@@ -412,7 +451,10 @@ export class AnalyticsService implements OnModuleInit {
412451
limit,
413452
);
414453

454+
console.log(response);
455+
415456
if (!response) {
457+
console.log('null,');
416458
return { events: [] };
417459
}
418460

@@ -442,7 +484,11 @@ export class AnalyticsService implements OnModuleInit {
442484
async getVisitEventsPaginated(
443485
request: AnalyticsProto.GetVisitEventsRequest,
444486
): Promise<AnalyticsProto.GetVisitEventsResponse> {
445-
const viewer = await this.getAnalyticsViewer(this.EventEnvironment.DEVELOPMENT, request.configId, request.configEnvironment);
487+
const viewer = await this.getAnalyticsViewer(
488+
this.EventEnvironment.DEVELOPMENT,
489+
request.configId,
490+
request.configEnvironment,
491+
);
446492

447493
if (!request.projectName || request.projectName.length === 0) {
448494
throw new RpcException({
@@ -459,8 +505,7 @@ export class AnalyticsService implements OnModuleInit {
459505
afterTime: request.afterTime || undefined,
460506
};
461507

462-
const response =
463-
await viewer.getVisitEventsPaginated(queryParams);
508+
const response = await viewer.getVisitEventsPaginated(queryParams);
464509

465510
if (!response) {
466511
return { events: [], afterId: '' };
@@ -493,7 +538,11 @@ export class AnalyticsService implements OnModuleInit {
493538
async getAllVisitEvents(
494539
request: AnalyticsProto.GetAllVisitEventsRequest,
495540
): Promise<AnalyticsProto.GetAllVisitEventsResponse> {
496-
const viewer = await this.getAnalyticsViewer(this.EventEnvironment.DEVELOPMENT, request.configId, request.configEnvironment);
541+
const viewer = await this.getAnalyticsViewer(
542+
this.EventEnvironment.DEVELOPMENT,
543+
request.configId,
544+
request.configEnvironment,
545+
);
497546

498547
if (!request.projectName || request.projectName.length === 0) {
499548
throw new RpcException({
@@ -543,7 +592,11 @@ export class AnalyticsService implements OnModuleInit {
543592
async getInputEventsPaginated(
544593
request: AnalyticsProto.GetInputEventsRequest,
545594
): Promise<AnalyticsProto.GetInputEventsResponse> {
546-
const viewer = await this.getAnalyticsViewer(this.EventEnvironment.DEVELOPMENT, request.configId, request.configEnvironment);
595+
const viewer = await this.getAnalyticsViewer(
596+
this.EventEnvironment.DEVELOPMENT,
597+
request.configId,
598+
request.configEnvironment,
599+
);
547600

548601
if (!request.projectName || request.projectName.length === 0) {
549602
throw new RpcException({
@@ -560,8 +613,7 @@ export class AnalyticsService implements OnModuleInit {
560613
afterTime: request.afterTime || undefined,
561614
};
562615

563-
const response =
564-
await viewer.getInputEventsPaginated(queryParams);
616+
const response = await viewer.getInputEventsPaginated(queryParams);
565617

566618
if (!response) {
567619
return { events: [], afterId: '' };
@@ -595,7 +647,11 @@ export class AnalyticsService implements OnModuleInit {
595647
async getAllInputEvents(
596648
request: AnalyticsProto.GetAllInputEventsRequest,
597649
): Promise<AnalyticsProto.GetAllInputEventsResponse> {
598-
const viewer = await this.getAnalyticsViewer(this.EventEnvironment.DEVELOPMENT, request.configId, request.configEnvironment);
650+
const viewer = await this.getAnalyticsViewer(
651+
this.EventEnvironment.DEVELOPMENT,
652+
request.configId,
653+
request.configEnvironment,
654+
);
599655

600656
if (!request.projectName || request.projectName.length === 0) {
601657
throw new RpcException({
@@ -646,7 +702,11 @@ export class AnalyticsService implements OnModuleInit {
646702
async getCustomEventsPaginated(
647703
request: AnalyticsProto.GetCustomEventsRequest,
648704
): Promise<AnalyticsProto.GetCustomEventsResponse> {
649-
const viewer = await this.getAnalyticsViewer(this.EventEnvironment.DEVELOPMENT, request.configId, request.configEnvironment);
705+
const viewer = await this.getAnalyticsViewer(
706+
this.EventEnvironment.DEVELOPMENT,
707+
request.configId,
708+
request.configEnvironment,
709+
);
650710

651711
if (!request.projectName || request.projectName.length === 0) {
652712
throw new RpcException({
@@ -679,8 +739,7 @@ export class AnalyticsService implements OnModuleInit {
679739
subcategory: request.subcategory,
680740
};
681741

682-
const response =
683-
await viewer.getCustomEventsPaginated(queryParams);
742+
const response = await viewer.getCustomEventsPaginated(queryParams);
684743

685744
if (!response) {
686745
return { events: [], afterId: '' };
@@ -711,7 +770,11 @@ export class AnalyticsService implements OnModuleInit {
711770
async getAllCustomEvents(
712771
request: AnalyticsProto.GetAllCustomEventsRequest,
713772
): Promise<AnalyticsProto.GetAllCustomEventsResponse> {
714-
const viewer = await this.getAnalyticsViewer(this.EventEnvironment.DEVELOPMENT, request.configId, request.configEnvironment);
773+
const viewer = await this.getAnalyticsViewer(
774+
this.EventEnvironment.DEVELOPMENT,
775+
request.configId,
776+
request.configEnvironment,
777+
);
715778

716779
if (!request.projectName || request.projectName.length === 0) {
717780
throw new RpcException({

‎packages/analytics-service/src/modules/analytics_config/analytics_config.service.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@ export class AnalyticsConfigService implements OnModuleInit {
2121
);
2222
}
2323

24-
async getAnalyticsKey(
24+
// TODO: This method isn't used by anything; should either be used in replacement of other code
25+
// or removed entirely
26+
async getAnalyticsKeys(
2527
projectId: number,
2628
environment: string,
27-
): Promise<string> {
29+
): Promise<[string, string]> {
2830
const config = await lastValueFrom(
2931
this.analyticsConfigDbService.readAnalyticsConfig({
3032
id: projectId,
3133
environment,
3234
}),
3335
);
34-
return config.analyticsKey;
36+
return [config.serverAnalyticsKey, config.clientAnalyticsKey];
3537
}
3638
}

0 commit comments

Comments
 (0)