Skip to content

Commit

Permalink
fixed tests due to recursive dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mrinc committed Aug 20, 2024
1 parent 866f0e7 commit b9d3988
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 71 deletions.
68 changes: 29 additions & 39 deletions nodejs/src/base/BSBMetrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ export interface BSBMetricsConstructor<
>
extends BaseWithConfigConfig<
ReferencedConfig extends null
? null
: BSBReferencePluginConfigDefinition<ReferencedConfig>
? null
: BSBReferencePluginConfigDefinition<ReferencedConfig>
> {
}

Expand All @@ -23,8 +23,8 @@ export abstract class BSBMetrics<
>
extends BaseWithConfig<
ReferencedConfig extends null
? null
: BSBReferencePluginConfigDefinition<ReferencedConfig>
? null
: BSBReferencePluginConfigDefinition<ReferencedConfig>
> {
constructor(config: BSBMetricsConstructor<ReferencedConfig>) {
super(config);
Expand Down Expand Up @@ -59,63 +59,53 @@ export abstract class BSBMetrics<
*/
export class BSBMetricsRef
extends BSBMetrics {
public startTrace(pluginName: string, traceId: string): void | Promise<void> {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "startTrace");
dispose?(): void;

init?(): void;

run?(): void;

createCounter(pluginName: string, name: string, description: string, help: string): void | Promise<void> {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "createCounter");
}

public endTrace(pluginName: string, traceId: string, attributes?: Record<string, string>): void | Promise<void> {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "endTrace");
createGauge(pluginName: string, name: string, description: string, help: string): void | Promise<void> {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "createGauge");
}

public startSpan(pluginName: string, traceId: string, spanId: string, name: string, attributes?: Record<string, string>): void | Promise<void> {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "startSpan");
createHistogram(pluginName: string, name: string, description: string, help: string, boundaries: number[] | undefined): void | Promise<void> {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "createHistogram");
}

public endSpan(pluginName: string, traceId: string, spanId: string, attributes?: Record<string, string>): void | Promise<void> {
endSpan(pluginName: string, traceId: string, spanId: string, attributes: Record<string, string> | undefined): void | Promise<void> {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "endSpan");
}

public errorSpan(pluginName: string, traceId: string, spanId: string, error: BSBError<any> | Error, attributes?: Record<string, string>): void | Promise<void> {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "errorSpan");
endTrace(pluginName: string, traceId: string, attributes: Record<string, string> | undefined): void | Promise<void> {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "endTrace");
}

public updateMetric(type: "counter" | "gauge" | "histogram", pluginName: string, name: string, value: number, labels?: Record<string, string>): void {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "updateMetric");
errorSpan(pluginName: string, traceId: string, spanId: string, error: BSBError<any> | Error, attributes: Record<string, string> | undefined): void | Promise<void> {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "errorSpan");
}

createCounter(pluginName: string, name: string, description?: string): void {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "createCounter");
startSpan(pluginName: string, traceId: string, spanId: string, name: string, attributes: Record<string, string> | undefined): void | Promise<void> {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "startSpan");
}

updateCounter(event: "inc", pluginName: string, name: string, value: number, labels?: Record<string, string>) {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "counterEvent");
startTrace(pluginName: string, traceId: string): void | Promise<void> {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "startTrace");
}

createGauge(pluginName: string, name: string, description?: string): void {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "createGauge");
updateCounter(event: "inc", pluginName: string, name: string, value: number, labels: Record<string, string> | undefined): void | Promise<void> {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "counterEvent");
}

updateGauge(event: "set" | "inc" | "dec", pluginName: string, name: string, value: number, labels?: Record<string, string>) {
updateGauge(event: "set" | "inc" | "dec", pluginName: string, name: string, value: number, labels: Record<string, string> | undefined): void | Promise<void> {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "gaugeEvent");
}

createHistogram(pluginName: string, name: string, description: string, help: string, boundaries?: number[] | undefined): void {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "createHistogram");
}

updateHistogram(event: "record", pluginName: string, name: string, value: number, labels?: Record<string, string>) {
updateHistogram(event: "record", pluginName: string, name: string, value: number, labels: Record<string, string> | undefined): void | Promise<void> {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "histogramEvent");
}

createTrace(pluginName: string, name: string, description?: string): void {
throw BSB_ERROR_METHOD_NOT_IMPLEMENTED("BSBMetricsRef", "createTrace");
}

dispose?(): void;

init?(): void;

run?(): void;


}
7 changes: 7 additions & 0 deletions nodejs/src/base/BSBServiceClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import {BSBService, BSBServiceRef} from "./BSBService";
import {BSBError} from "./errorMessages";
import {PluginEvents} from "./PluginEvents";
import {Tools} from "./tools";

/**
* @hidden
Expand Down Expand Up @@ -106,6 +107,12 @@ export class ServiceClient<

constructor(service: ServiceT, context: BSBService) {
super(context);
if (!Tools.isObject(service.PLUGIN_CLIENT)) {
throw new BSBError("Plugin client is not defined in the service!");
}
if (!Tools.isString(service.PLUGIN_CLIENT.name)) {
throw new BSBError("Plugin client name is not defined in the service!");
}
this.pluginName = service.PLUGIN_CLIENT.name;
this.initBeforePlugins = service.PLUGIN_CLIENT.initBeforePlugins;
this.initAfterPlugins = service.PLUGIN_CLIENT.initAfterPlugins;
Expand Down
4 changes: 2 additions & 2 deletions nodejs/src/base/PluginMetrics.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {v7 as uuidv7} from "uuid";
import {CleanStringStrength, Counter, Gauge, Histogram, IPluginMetrics, Span, Timer, Trace} from "../interfaces";
import {MS_PER_NS, SBMetrics} from "../serviceBase";
import {SBMetrics} from "../serviceBase";
import {BSBError} from "./errorMessages";
import {Tools} from "./tools";
import {NS_PER_SEC} from "../serviceBase/serviceBase";
import {MS_PER_NS, NS_PER_SEC} from "./base";

export class PluginMetrics
implements IPluginMetrics {
Expand Down
9 changes: 9 additions & 0 deletions nodejs/src/base/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,12 @@ export abstract class BaseWithLoggingAndConfig<
);
}
}

/**
* @hidden
*/
export const NS_PER_SEC = 1e9;
/**
* @hidden
*/
export const MS_PER_NS = 1e-6;
4 changes: 4 additions & 0 deletions nodejs/src/plugins/service-default0/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {BSBPluginConfig, BSBService, BSBServiceConstructor, ServiceClient} from "../../base";
import {Plugin as Default1Plugin} from "../../plugins/service-default1/index";
import {z} from "zod";
import {BSBServiceClientDefinition} from "../../base";

export const secSchema = z.object({
testa: z.number(),
Expand Down Expand Up @@ -46,6 +47,9 @@ export interface Events {

export class Plugin
extends BSBService<Config, Events> {
public static PLUGIN_CLIENT: BSBServiceClientDefinition = {
name: "service-default0",
}
public initBeforePlugins?: string[] | undefined;
//public initAfterPlugins: string[] = ["service-default3"];
public initAfterPlugins?: string[] | undefined;
Expand Down
4 changes: 4 additions & 0 deletions nodejs/src/plugins/service-default1/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {BSBService, BSBPluginEvents} from "../../index";
import {BSBServiceClientDefinition} from "../../base";

export interface Events
extends BSBPluginEvents {
Expand All @@ -20,6 +21,9 @@ export interface Events

export class Plugin
extends BSBService<null, Events> {
public static PLUGIN_CLIENT: BSBServiceClientDefinition = {
name: "service-default1",
}
public initBeforePlugins?: string[] | undefined;
public initAfterPlugins?: string[] | undefined;
public runBeforePlugins?: string[] | undefined;
Expand Down
2 changes: 1 addition & 1 deletion nodejs/src/serviceBase/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {SBLogging} from "./logging";
import {SBMetrics} from "./metrics";
import {SBPlugins} from "./plugins";
import {Counter} from "../interfaces";
import {MS_PER_NS, NS_PER_SEC} from "./serviceBase";
import {MS_PER_NS, NS_PER_SEC} from "../base/base";

/**
* BSB Events Controller
Expand Down
46 changes: 17 additions & 29 deletions nodejs/src/serviceBase/serviceBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import {
BSBConfig,
BSBError,
BSBEvents, BSBLogging, BSBMetrics,
BSBService,
BSBService, MS_PER_NS, NS_PER_SEC,
PluginLogger,
SmartFunctionCallSync,
Tools,
} from "../base";
import {DEBUG_MODE, IPluginLogger, LogMeta, PluginTypeDefinitionRef} from "../interfaces";
import {DEBUG_MODE, IPluginLogger, LogMeta, PluginTypeDefinitionRef} from "../interfaces";
import {SBConfig} from "./config";
import {SBEvents} from "./events";
import {SBLogging} from "./logging";
Expand All @@ -36,14 +36,6 @@ export const BOOT_STAT_KEYS = {
*/
export type BootStatKeys = (typeof BOOT_STAT_KEYS)[keyof typeof BOOT_STAT_KEYS];

/**
* @hidden
*/
export const NS_PER_SEC = 1e9;
/**
* @hidden
*/
export const MS_PER_NS = 1e-6;
const TIMEKEEPLOG = "[TIMER] {timerName} took ({nsTime}ns) ({msTime}ms)";

export class ServiceBase {
Expand Down Expand Up @@ -87,8 +79,8 @@ export class ServiceBase {
this._keeps[stepName] || undefined
) as [number, number] | undefined);
this._keeps[stepName] = (
diff[0] * NS_PER_SEC + diff[1]
) * MS_PER_NS;
diff[0] * NS_PER_SEC + diff[1]
) * MS_PER_NS;
const logMeta: LogMeta<typeof TIMEKEEPLOG> = {
nsTime: diff[0] * NS_PER_SEC + diff[1],
msTime: this._keeps[stepName],
Expand All @@ -98,24 +90,22 @@ export class ServiceBase {
}

constructor(
debug: boolean = true, // Enable debug logging (true): disabled debug logging
live: boolean = false, // Disable development mode (true): changes the way plugins are imported
debug: boolean = true, // Enable debug logging (true): disabled debug logging
live: boolean = false, // Disable development mode (true): changes the way plugins are imported
cwd: string, // Current working directory: The current directory where you are running from
config: typeof SBConfig = SBConfig, // Config handler: Allows you to override default behavour,
plugins: typeof SBPlugins = SBPlugins, // Plugins handler: Allows you to override default behavour,
logging: typeof SBLogging = SBLogging, // Logging handler: Allows you to override default behavour,
metrics: typeof SBMetrics = SBMetrics, // Metrics handler: Allows you to override default behavour,
events: typeof SBEvents = SBEvents, // Events handler: Allows you to override default behavour,
config: typeof SBConfig = SBConfig, // Config handler: Allows you to override default behavour,
plugins: typeof SBPlugins = SBPlugins, // Plugins handler: Allows you to override default behavour,
logging: typeof SBLogging = SBLogging, // Logging handler: Allows you to override default behavour,
metrics: typeof SBMetrics = SBMetrics, // Metrics handler: Allows you to override default behavour,
events: typeof SBEvents = SBEvents, // Events handler: Allows you to override default behavour,
services: typeof SBServices = SBServices, // Services handler: Allows you to override default behavour
) {
this.cwd = cwd;
if (live === false) {
this.mode = "development";
}
else if (debug === true) {
} else if (debug === true) {
this.mode = "production-debug";
}
else {
} else {
this.mode = "production";
}

Expand Down Expand Up @@ -265,12 +255,11 @@ export class ServiceBase {
eCode,
reason,
extraMsg: Tools.isNullOrUndefined(extraData)
? ""
: extraData.toString(),
? ""
: extraData.toString(),
},
);
}
else {
} else {
this.log.error(
"Disposing service: {appId} code {eCode} ({reason})",
{
Expand All @@ -293,8 +282,7 @@ export class ServiceBase {
SmartFunctionCallSync(this.metrics, this.metrics.dispose);
this.log.warn("Disposing logger");
SmartFunctionCallSync(this.logging, this.logging.dispose);
}
catch (exc) {
} catch (exc) {
console.error(exc);
console.error("Disposing forcefully!");
}
Expand Down

0 comments on commit b9d3988

Please sign in to comment.