Skip to content

Commit

Permalink
Refactored PrometheusMetric to Metric
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaakko Heusala committed Jan 22, 2024
1 parent 466f6cc commit 3c6c623
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 35 deletions.
14 changes: 7 additions & 7 deletions prometheus/PrometheusMetric.ts → metrics/Metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<PrometheusMetricDTO> {
export interface Metric extends Entity<MetricDTO> {

/**
* @inheritDoc
Expand All @@ -18,7 +18,7 @@ export interface PrometheusMetric extends Entity<PrometheusMetricDTO> {
/**
* @inheritDoc
*/
getDTO () : PrometheusMetricDTO;
getDTO () : MetricDTO;

/**
* @inheritDoc
Expand Down Expand Up @@ -73,14 +73,14 @@ export interface PrometheusMetric extends Entity<PrometheusMetricDTO> {
/**
* 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.
Expand All @@ -89,7 +89,7 @@ export interface PrometheusMetric extends Entity<PrometheusMetricDTO> {
*
* @param type
*/
type (type : PrometheusMetricType | undefined) : this;
type (type : MetricType | undefined) : this;


/**
Expand Down
61 changes: 61 additions & 0 deletions metrics/MetricCollection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright (c) 2024. Sendanor <info@sendanor.fi>. 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<MetricCollectionDTO> {

/**
* @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;

}
7 changes: 7 additions & 0 deletions metrics/MetricCollectionDTO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// Copyright (c) 2024. Sendanor <info@sendanor.fi>. All rights reserved.

import { MetricDTO } from "./MetricDTO";

export interface MetricCollectionDTO {
readonly payload: readonly MetricDTO[];
}
84 changes: 84 additions & 0 deletions metrics/MetricCollectionEntity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) 2024. Sendanor <info@sendanor.fi>. 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<MetricCollectionDTO, MetricCollection>('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;
}
6 changes: 3 additions & 3 deletions prometheus/PrometheusMetricDTO.ts → metrics/MetricDTO.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright (c) 2024. Sendanor <info@sendanor.fi>. 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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<PrometheusMetricDTO, PrometheusMetric>('PrometheusMetric')
EntityFactoryImpl.create<MetricDTO, Metric>('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) )
);
Expand All @@ -38,7 +38,7 @@ export const BasePrometheusMetricEntity = PrometheusMetricEntityFactory.createEn
*/
export class PrometheusMetricEntity
extends BasePrometheusMetricEntity
implements PrometheusMetric
implements Metric
{

/**
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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,
Expand All @@ -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)) {
Expand All @@ -106,7 +106,7 @@ export class PrometheusMetricEntity
* Construct an entity of PrometheusMetricEntity.
*/
public constructor (
dto ?: PrometheusMetricDTO | undefined,
dto ?: MetricDTO | undefined,
) {
super(dto);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 3c6c623

Please sign in to comment.