From 3c6c62399eae4d1a8ac6a34727ed22a2a9341496 Mon Sep 17 00:00:00 2001 From: Jaakko Heusala Date: Mon, 22 Jan 2024 13:07:14 +0200 Subject: [PATCH] Refactored PrometheusMetric to Metric --- .../PrometheusMetric.ts => metrics/Metric.ts | 14 ++-- metrics/MetricCollection.ts | 61 ++++++++++++++ metrics/MetricCollectionDTO.ts | 7 ++ metrics/MetricCollectionEntity.ts | 84 +++++++++++++++++++ .../MetricDTO.ts | 6 +- .../PrometheusMetricEntity.ts | 32 +++---- .../types/MetricType.ts | 18 ++-- 7 files changed, 187 insertions(+), 35 deletions(-) rename prometheus/PrometheusMetric.ts => metrics/Metric.ts (85%) create mode 100644 metrics/MetricCollection.ts create mode 100644 metrics/MetricCollectionDTO.ts create mode 100644 metrics/MetricCollectionEntity.ts rename prometheus/PrometheusMetricDTO.ts => metrics/MetricDTO.ts (63%) rename {prometheus => metrics}/PrometheusMetricEntity.ts (83%) rename prometheus/types/PrometheusMetricType.ts => metrics/types/MetricType.ts (58%) diff --git a/prometheus/PrometheusMetric.ts b/metrics/Metric.ts similarity index 85% rename from prometheus/PrometheusMetric.ts rename to metrics/Metric.ts index 1f5cc91..c23a106 100644 --- a/prometheus/PrometheusMetric.ts +++ b/metrics/Metric.ts @@ -2,13 +2,13 @@ import { Entity } from "../entities/types/Entity"; import { ReadonlyJsonObject } from "../Json"; -import { PrometheusMetricDTO } from "./PrometheusMetricDTO"; -import { PrometheusMetricType } from "./types/PrometheusMetricType"; +import { MetricDTO } from "./MetricDTO"; +import { MetricType } from "./types/MetricType"; /** * Presents an interface for SeoEntity. */ -export interface PrometheusMetric extends Entity { +export interface Metric extends Entity { /** * @inheritDoc @@ -18,7 +18,7 @@ export interface PrometheusMetric extends Entity { /** * @inheritDoc */ - getDTO () : PrometheusMetricDTO; + getDTO () : MetricDTO; /** * @inheritDoc @@ -73,14 +73,14 @@ export interface PrometheusMetric extends Entity { /** * Get a type. */ - getType () : PrometheusMetricType | undefined; + getType () : MetricType | undefined; /** * Set a type. * * @param type */ - setType (type : PrometheusMetricType | undefined) : this; + setType (type : MetricType | undefined) : this; /** * Set a type. @@ -89,7 +89,7 @@ export interface PrometheusMetric extends Entity { * * @param type */ - type (type : PrometheusMetricType | undefined) : this; + type (type : MetricType | undefined) : this; /** diff --git a/metrics/MetricCollection.ts b/metrics/MetricCollection.ts new file mode 100644 index 0000000..301ad6a --- /dev/null +++ b/metrics/MetricCollection.ts @@ -0,0 +1,61 @@ +// Copyright (c) 2024. Sendanor . All rights reserved. + +import { Entity } from "../entities/types/Entity"; +import { ReadonlyJsonObject } from "../Json"; +import { MetricDTO } from "./MetricDTO"; +import { PrometheusMetricEntity } from "./PrometheusMetricEntity"; +import { MetricCollectionDTO } from "./MetricCollectionDTO"; + +/** + * Presents an interface for SeoEntity. + */ +export interface MetricCollection extends Entity { + + /** + * @inheritDoc + */ + valueOf () : ReadonlyJsonObject; + + /** + * @inheritDoc + */ + getDTO () : MetricCollectionDTO; + + /** + * @inheritDoc + */ + toJSON () : ReadonlyJsonObject; + + /** + * Returns CSS styles. + */ + getCssStyles () : ReadonlyJsonObject; + + + /** + * Get a payload. + */ + getPayload () : readonly PrometheusMetricEntity[]; + + /** + * Get a payload as DTO. + */ + getPayloadDTO () : readonly MetricDTO[]; + + /** + * Set a payload. + * + * @param payload + */ + setPayload (payload : readonly ( MetricDTO | PrometheusMetricEntity )[]) : this; + + /** + * Set a payload. + * + * An alias for `.setPayload(payload)`. + * + * @param payload + */ + payload (payload : readonly (MetricDTO | PrometheusMetricEntity)[]) : this; + +} diff --git a/metrics/MetricCollectionDTO.ts b/metrics/MetricCollectionDTO.ts new file mode 100644 index 0000000..9ba2107 --- /dev/null +++ b/metrics/MetricCollectionDTO.ts @@ -0,0 +1,7 @@ +// Copyright (c) 2024. Sendanor . All rights reserved. + +import { MetricDTO } from "./MetricDTO"; + +export interface MetricCollectionDTO { + readonly payload: readonly MetricDTO[]; +} diff --git a/metrics/MetricCollectionEntity.ts b/metrics/MetricCollectionEntity.ts new file mode 100644 index 0000000..8e47597 --- /dev/null +++ b/metrics/MetricCollectionEntity.ts @@ -0,0 +1,84 @@ +// Copyright (c) 2024. Sendanor . All rights reserved. + +import { EntityFactoryImpl } from "../entities/types/EntityFactoryImpl"; +import { VariableType } from "../entities/types/VariableType"; +import { PrometheusMetricEntity } from "./PrometheusMetricEntity"; +import { MetricCollection } from "./MetricCollection"; +import { MetricCollectionDTO } from "./MetricCollectionDTO"; + +export const MetricCollectionEntityFactory = ( + EntityFactoryImpl.create('MetricCollection') + .add( EntityFactoryImpl.createArrayProperty("payload").setTypes(PrometheusMetricEntity) ) +); + +export const isMetricCollectionDTO = MetricCollectionEntityFactory.createTestFunctionOfDTO(); + +export const isMetricCollection = MetricCollectionEntityFactory.createTestFunctionOfInterface(); + +export const explainMetricCollectionDTO = MetricCollectionEntityFactory.createExplainFunctionOfDTO(); + +export const isMetricCollectionDTOOrUndefined = MetricCollectionEntityFactory.createTestFunctionOfDTOorOneOf(VariableType.UNDEFINED); + +export const explainMetricCollectionDTOOrUndefined = MetricCollectionEntityFactory.createExplainFunctionOfDTOorOneOf(VariableType.UNDEFINED); + +export const BaseMetricCollectionEntity = MetricCollectionEntityFactory.createEntityType(); + +/** + * Metric Collection entity. + */ +export class MetricCollectionEntity + extends BaseMetricCollectionEntity + implements MetricCollection +{ + + /** + * Creates a Metric Collection entity. + * + * @param value The optional DTO of Metric Collection + */ + public static create ( + value ?: MetricCollectionDTO, + ) : MetricCollectionEntity { + return new MetricCollectionEntity(value); + } + + /** + * Creates a Metric Collection entity from DTO. + * + * @param dto The optional DTO of Metric Collection + */ + public static createFromDTO ( + dto : MetricCollectionDTO, + ) : MetricCollectionEntity { + return new MetricCollectionEntity(dto); + } + + /** + * Normalizes the value as a DTO. + */ + public static toDTO ( + value: MetricCollectionDTO | MetricCollection | MetricCollectionEntity, + ) : MetricCollectionDTO { + if (isMetricCollectionEntity(value)) { + return value.getDTO(); + } else if (isMetricCollection(value)) { + return value.getDTO(); + } else { + return value; + } + } + + /** + * Construct an entity of MetricCollectionEntity. + */ + public constructor ( + dto ?: MetricCollectionDTO | undefined, + ) { + super(dto); + } + +} + +export function isMetricCollectionEntity ( value: unknown): value is MetricCollectionEntity { + return value instanceof MetricCollectionEntity; +} diff --git a/prometheus/PrometheusMetricDTO.ts b/metrics/MetricDTO.ts similarity index 63% rename from prometheus/PrometheusMetricDTO.ts rename to metrics/MetricDTO.ts index 770d2a4..0287e10 100644 --- a/prometheus/PrometheusMetricDTO.ts +++ b/metrics/MetricDTO.ts @@ -1,12 +1,12 @@ // Copyright (c) 2024. Sendanor . All rights reserved. import { ReadonlyJsonObject } from "../Json"; -import { PrometheusMetricType } from "./types/PrometheusMetricType"; +import { MetricType } from "./types/MetricType"; -export interface PrometheusMetricDTO { +export interface MetricDTO { readonly name ?: string; readonly help ?: string; - readonly type ?: PrometheusMetricType; + readonly type ?: MetricType; readonly labels ?: ReadonlyJsonObject; readonly value ?: string; } diff --git a/prometheus/PrometheusMetricEntity.ts b/metrics/PrometheusMetricEntity.ts similarity index 83% rename from prometheus/PrometheusMetricEntity.ts rename to metrics/PrometheusMetricEntity.ts index 700a6e2..4e2949c 100644 --- a/prometheus/PrometheusMetricEntity.ts +++ b/metrics/PrometheusMetricEntity.ts @@ -8,15 +8,15 @@ import { keys } from "../functions/keys"; import { map } from "../functions/map"; import { reduce } from "../functions/reduce"; import { trim } from "../functions/trim"; -import { PrometheusMetric } from "./PrometheusMetric"; -import { PrometheusMetricDTO } from "./PrometheusMetricDTO"; -import { PrometheusMetricType } from "./types/PrometheusMetricType"; +import { Metric } from "./Metric"; +import { MetricDTO } from "./MetricDTO"; +import { MetricType } from "./types/MetricType"; export const PrometheusMetricEntityFactory = ( - EntityFactoryImpl.create('PrometheusMetric') + EntityFactoryImpl.create('PrometheusMetric') .add( EntityFactoryImpl.createProperty("name").setTypes(VariableType.STRING, VariableType.UNDEFINED) ) .add( EntityFactoryImpl.createProperty("help").setTypes(VariableType.STRING, VariableType.UNDEFINED) ) - .add( EntityFactoryImpl.createProperty("type").setTypes(PrometheusMetricType, VariableType.UNDEFINED) ) + .add( EntityFactoryImpl.createProperty("type").setTypes(MetricType, VariableType.UNDEFINED) ) .add( EntityFactoryImpl.createProperty("labels").setTypes(VariableType.JSON, VariableType.UNDEFINED) ) .add( EntityFactoryImpl.createProperty("value").setTypes(VariableType.STRING, VariableType.UNDEFINED) ) ); @@ -38,7 +38,7 @@ export const BasePrometheusMetricEntity = PrometheusMetricEntityFactory.createEn */ export class PrometheusMetricEntity extends BasePrometheusMetricEntity - implements PrometheusMetric + implements Metric { /** @@ -47,7 +47,7 @@ export class PrometheusMetricEntity * @param value The optional DTO of PrometheusMetric */ public static create ( - value ?: PrometheusMetricDTO, + value ?: MetricDTO, ) : PrometheusMetricEntity { return new PrometheusMetricEntity(value); } @@ -58,7 +58,7 @@ export class PrometheusMetricEntity * @param dto The optional DTO of PrometheusMetric */ public static createFromDTO ( - dto : PrometheusMetricDTO, + dto : MetricDTO, ) : PrometheusMetricEntity { return new PrometheusMetricEntity(dto); } @@ -67,16 +67,16 @@ export class PrometheusMetricEntity * Merges multiple values as one entity. */ public static merge ( - ...values: readonly (PrometheusMetricDTO | PrometheusMetric | PrometheusMetricEntity)[] + ...values: readonly (MetricDTO | Metric | PrometheusMetricEntity)[] ) : PrometheusMetricEntity { return PrometheusMetricEntity.createFromDTO( reduce( values, ( - prev: PrometheusMetricDTO, - item: PrometheusMetricDTO | PrometheusMetric | PrometheusMetricEntity, - ) : PrometheusMetricDTO => { - const dto : PrometheusMetricDTO = this.toDTO(item); + prev: MetricDTO, + item: MetricDTO | Metric | PrometheusMetricEntity, + ) : MetricDTO => { + const dto : MetricDTO = this.toDTO(item); return { ...prev, ...dto, @@ -91,8 +91,8 @@ export class PrometheusMetricEntity * Normalizes the value as a DTO. */ public static toDTO ( - value: PrometheusMetricDTO | PrometheusMetric | PrometheusMetricEntity, - ) : PrometheusMetricDTO { + value: MetricDTO | Metric | PrometheusMetricEntity, + ) : MetricDTO { if (isPrometheusMetricEntity(value)) { return value.getDTO(); } else if (isPrometheusMetric(value)) { @@ -106,7 +106,7 @@ export class PrometheusMetricEntity * Construct an entity of PrometheusMetricEntity. */ public constructor ( - dto ?: PrometheusMetricDTO | undefined, + dto ?: MetricDTO | undefined, ) { super(dto); } diff --git a/prometheus/types/PrometheusMetricType.ts b/metrics/types/MetricType.ts similarity index 58% rename from prometheus/types/PrometheusMetricType.ts rename to metrics/types/MetricType.ts index d08e9e6..3acb999 100644 --- a/prometheus/types/PrometheusMetricType.ts +++ b/metrics/types/MetricType.ts @@ -13,27 +13,27 @@ import { explainOr, } from "../../types/explain"; -export enum PrometheusMetricType { +export enum MetricType { COUNTER = "counter", } -export function isPrometheusMetricType (value: unknown) : value is PrometheusMetricType { - return isEnum(PrometheusMetricType, value); +export function isPrometheusMetricType (value: unknown) : value is MetricType { + return isEnum(MetricType, value); } export function explainPrometheusMetricType (value : unknown) : string { - return explainEnum("PrometheusMetricType", PrometheusMetricType, isPrometheusMetricType, value); + return explainEnum("PrometheusMetricType", MetricType, isPrometheusMetricType, value); } -export function stringifyPrometheusMetricType (value : PrometheusMetricType) : string { - return stringifyEnum(PrometheusMetricType, value); +export function stringifyPrometheusMetricType (value : MetricType) : string { + return stringifyEnum(MetricType, value); } -export function parsePrometheusMetricType (value: any) : PrometheusMetricType | undefined { - return parseEnum(PrometheusMetricType, value) as PrometheusMetricType | undefined; +export function parsePrometheusMetricType (value: any) : MetricType | undefined { + return parseEnum(MetricType, value) as MetricType | undefined; } -export function isPrometheusMetricTypeOrUndefined (value: unknown): value is PrometheusMetricType | undefined { +export function isPrometheusMetricTypeOrUndefined (value: unknown): value is MetricType | undefined { return isUndefined(value) || isPrometheusMetricType(value); }