Skip to content

Commit 5a6da9b

Browse files
committed
feat(core): add internalExtension activity logger and can be injected into cacheLayerLoader
1 parent d8f3a53 commit 5a6da9b

File tree

10 files changed

+92
-2
lines changed

10 files changed

+92
-2
lines changed

packages/core/src/containers/modules/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ExtensionLoader } from '../../lib/extension-loader';
33
import { ICoreOptions } from '../../models/coreOptions';
44
import templateEngineModules from '../../lib/template-engine/built-in-extensions';
55
import validatorModule from '../../lib/validators/built-in-validators';
6+
import LoggerModule from '../../lib/loggers';
67
import {
78
builtInCodeLoader,
89
builtInTemplateProvider,
@@ -23,6 +24,7 @@ export const extensionModule = (options: ICoreOptions) =>
2324
for (const templateEngineModule of templateEngineModules) {
2425
loader.loadInternalExtensionModule(templateEngineModule);
2526
}
27+
loader.loadInternalExtensionModule(LoggerModule);
2628
// Validator (single module)
2729
loader.loadInternalExtensionModule(validatorModule);
2830
// Template provider (single module)

packages/core/src/containers/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,6 @@ export const TYPES = {
5252
Extension_CompilerLoader: Symbol.for('Extension_CompilerLoader'),
5353
Extension_DataSource: Symbol.for('Extension_DataSource'),
5454
Extension_ProfileReader: Symbol.for('ProfileReader'),
55+
// Logger
56+
Extension_ActivityLogger: Symbol.for('Extension_ActivityLogger'),
5557
};

packages/core/src/lib/cache-layer/cacheLayerLoader.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as moment from 'moment';
44
import { inject, injectable, interfaces } from 'inversify';
55
import { TYPES } from '@vulcan-sql/core/types';
66
import {
7+
IActivityLogger,
78
CacheLayerInfo,
89
ICacheLayerOptions,
910
cacheProfileName,
@@ -22,16 +23,19 @@ export class CacheLayerLoader implements ICacheLayerLoader {
2223
private options: ICacheLayerOptions;
2324
private cacheStorage: DataSource;
2425
private logger = getLogger({ scopeName: 'CORE' });
25-
26+
private activityLoggers: IActivityLogger;
2627
constructor(
2728
@inject(TYPES.CacheLayerOptions) options: CacheLayerOptions,
2829
@inject(TYPES.Factory_DataSource)
29-
dataSourceFactory: interfaces.SimpleFactory<DataSource>
30+
dataSourceFactory: interfaces.SimpleFactory<DataSource>,
31+
@inject(TYPES.Extension_ActivityLogger)
32+
activityLogger: IActivityLogger
3033
) {
3134
this.dataSourceFactory = dataSourceFactory;
3235
this.options = options;
3336
// prepare cache data source
3437
this.cacheStorage = this.dataSourceFactory(cacheProfileName);
38+
this.activityLoggers = activityLogger;
3539
}
3640

3741
/**
@@ -46,6 +50,7 @@ export class CacheLayerLoader implements ICacheLayerLoader {
4650
const { cacheTableName, sql, profile, indexes, folderSubpath } = cache;
4751
const type = this.options.type!;
4852
const dataSource = this.dataSourceFactory(profile);
53+
await this.activityLoggers.log({ a: 1 });
4954

5055
// generate directory for cache file path to export
5156
// format => [folderPath]/[schema.templateSource]/[profileName]/[cacheTableName]]/[timestamp]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import {
2+
BaseActivityLogger,
3+
ActivityLoggerType,
4+
} from '../../models/extensions/logger';
5+
import {
6+
VulcanExtensionId,
7+
VulcanInternalExtension,
8+
} from '../../models/extensions';
9+
import axios from 'axios';
10+
11+
interface HttpLoggerConfig {
12+
connection?: HttpLoggerConnectionConfig;
13+
}
14+
15+
interface HttpLoggerConnectionConfig {
16+
protocol?: string | undefined;
17+
host?: string | undefined;
18+
port?: number | string;
19+
path?: string | undefined;
20+
headers?: NodeJS.Dict<string | string[]> | undefined;
21+
}
22+
23+
@VulcanInternalExtension('activity-log')
24+
@VulcanExtensionId(ActivityLoggerType.HTTP_LOGGER)
25+
export class HttpLogger extends BaseActivityLogger<HttpLoggerConfig> {
26+
public async log(payload: any): Promise<void> {
27+
const option = this.getOptions();
28+
if (!option) {
29+
throw new Error('Http logger option is not defined.');
30+
}
31+
// TODO-ac: should implement http logger
32+
try {
33+
// get connection info from option and use axios to send a post requet to the endpoint
34+
const { protocol, host, port, path, headers } = option.connection!;
35+
const url = `${protocol}://${host}:${port}${path}`;
36+
await axios.post(url, payload, { headers: headers as any });
37+
} catch (err) {
38+
console.error(err);
39+
}
40+
}
41+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { HttpLogger } from './httpLogger';
2+
3+
export default [HttpLogger];

packages/core/src/models/coreOptions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { IArtifactBuilderOptions } from './artifactBuilderOptions';
22
import { ICacheLayerOptions } from './cacheLayerOptions';
33
import { IDocumentOptions } from './documentOptions';
4+
import { IActivityLoggerOptions } from './loggerOptions';
45
import { IProfilesLookupOptions } from './profilesLookupOptions';
56
import { ITemplateEngineOptions } from './templateEngineOptions';
67

@@ -24,6 +25,7 @@ export interface ICoreOptions {
2425
extensions?: ExtensionAliases;
2526
document?: IDocumentOptions;
2627
profiles?: IProfilesLookupOptions;
28+
'activity-log'?: IActivityLoggerOptions;
2729
cache?: ICacheLayerOptions;
2830
[moduleAlias: string]: any;
2931
}

packages/core/src/models/extensions/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ export * from './persistentStore';
1212
export * from './codeLoader';
1313
export * from './dataSource';
1414
export * from './profileReader';
15+
export * from './logger';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { ExtensionBase } from './base';
2+
import { TYPES } from '@vulcan-sql/core/types';
3+
import { VulcanExtension } from './decorators';
4+
5+
export enum ActivityLoggerType {
6+
HTTP_LOGGER = 'http-logger',
7+
}
8+
9+
export interface IActivityLogger {
10+
log(content: any): Promise<void>;
11+
}
12+
13+
@VulcanExtension(TYPES.Extension_ActivityLogger, { enforcedId: true })
14+
export abstract class BaseActivityLogger<ActivityLoggerTypeOption>
15+
extends ExtensionBase
16+
implements IActivityLogger
17+
{
18+
public abstract log(context: any): Promise<void>;
19+
20+
protected getOptions(): ActivityLoggerTypeOption | undefined {
21+
if (!this.getConfig()) return undefined;
22+
if (!this.getConfig()['options']) return undefined;
23+
const option = this.getConfig()['options'][
24+
this.getExtensionId()!
25+
] as ActivityLoggerTypeOption;
26+
27+
return option;
28+
}
29+
}

packages/core/src/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export * from './documentOptions';
88
export * from './profilesLookupOptions';
99
export * from './cacheLayerOptions';
1010
export * from './profile';
11+
export * from './loggerOptions';
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface IActivityLoggerOptions {
2+
// different logger type settings
3+
[loggerType: string]: any;
4+
}

0 commit comments

Comments
 (0)