Skip to content

Commit

Permalink
refactor(tel-server): Implement Arjun's suggestions
Browse files Browse the repository at this point in the history
:)
  • Loading branch information
davidbeechey committed Oct 16, 2023
1 parent 84db5c9 commit b0be1eb
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 40 deletions.
16 changes: 5 additions & 11 deletions telemetry/packages/constants/src/pods/states.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,9 @@ export const ALL_POD_STATE_TYPES = [
export const getStateType = (
state: string,
): (typeof ALL_POD_STATE_TYPES)[number] => {
if (FAILURE_STATES[state as keyof typeof FAILURE_STATES]) {
return 'FAILURE';
} else if (PASSIVE_STATES[state as keyof typeof PASSIVE_STATES]) {
return 'PASSIVE';
} else if (ACTIVE_STATES[state as keyof typeof ACTIVE_STATES]) {
return 'ACTIVE';
} else if (NULL_STATES[state as keyof typeof NULL_STATES]) {
return 'NULL';
} else {
throw new Error(`Unknown state: ${state}`);
}
if (FAILURE_STATES[state as keyof typeof FAILURE_STATES]) return 'FAILURE';
if (PASSIVE_STATES[state as keyof typeof PASSIVE_STATES]) return 'PASSIVE';
if (ACTIVE_STATES[state as keyof typeof ACTIVE_STATES]) return 'ACTIVE';
if (NULL_STATES[state as keyof typeof NULL_STATES]) return 'NULL';
throw new Error(`Unknown state: ${state}`);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface InfluxRow {
_time: string;
_value: string;
podId: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class MeasurementService {
if (breachLevel) {
this.logger.debug(
`Measurement breached limits {${props.podId}/${props.measurementKey}}: ${breachLevel} with value ${props.value}`,
MeasurementService.name,
);
await this.faultService.addLimitBreachFault({
level: breachLevel,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { pods } from '@hyped/telemetry-constants';
import { zodEnumFromObjKeys } from 'src/utils/zodEnumFromObjKeys';
import { zodEnumFromObjKeys } from '@/modules/common/utils/zodEnumFromObjKeys';
import { z } from 'zod';

export const MeasurementReadingSchema = z
Expand All @@ -18,6 +18,7 @@ export const MeasurementReadingSchema = z
return false;
}

// Validate enum values
if (measurement.format === 'enum') {
const enumValue = measurement.enumerations.find(
(e) => e.value === value,
Expand All @@ -28,6 +29,14 @@ export const MeasurementReadingSchema = z
}
}

// Validate integers and floats
if (
(measurement.format === 'float' && isNaN(value)) ||
(measurement.format === 'integer' && !Number.isInteger(value))
) {
return false;
}

return true;
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ import { HttpException, Injectable, LoggerService } from '@nestjs/common';
import { INFLUX_TELEMETRY_BUCKET } from '@/core/config';
import { InfluxService } from '@/modules/influx/Influx.service';
import { Logger } from '@/modules/logger/Logger.decorator';
import { InfluxRow } from '@/modules/common/types/InfluxRow';

type InfluxRow = {
_time: string;
_value: string;
interface InfluxHistoricalRow extends InfluxRow {
measurementKey: string;
podId: string;
format: string;
};
}

@Injectable()
export class HistoricalTelemetryDataService {
Expand Down Expand Up @@ -40,7 +38,8 @@ export class HistoricalTelemetryDataService {
|> filter(fn: (r) => r["podId"] == "${podId}")`;

try {
const data = await this.influxService.query.collectRows<InfluxRow>(query);
const data =
await this.influxService.query.collectRows<InfluxHistoricalRow>(query);

return data.map((row) => ({
id: row['measurementKey'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import { INFLUX_FAULTS_BUCKET } from '@/core/config';
import { InfluxRow } from '@/modules/common/types/InfluxRow';
import { InfluxService } from '@/modules/influx/Influx.service';
import { Logger } from '@/modules/logger/Logger.decorator';
import { OpenMctFault } from '@hyped/telemetry-types';
import {
flux,
fluxBool,
fluxExpression,
fluxString,
} from '@influxdata/influxdb-client';
import { fluxExpression, fluxString } from '@influxdata/influxdb-client';
import { Injectable, LoggerService } from '@nestjs/common';

type InfluxRow = {
_time: string;
_value: string;
podId: string;
interface InfluxFaultRow extends InfluxRow {
fault: string;
};
}

type GetHistoricalFaultsInput = {
podId: string;
Expand Down Expand Up @@ -67,7 +60,9 @@ export class HistoricalFaultDataService {
|> last()`;

try {
const data = await this.influxService.query.collectRows<InfluxRow>(query);
const data = await this.influxService.query.collectRows<InfluxFaultRow>(
query,
);
return data.map((row) => ({
timestamp: new Date(row['_time']).getTime(),
fault: JSON.parse(row['_value']) as OpenMctFault,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { HttpException, Injectable, LoggerService } from '@nestjs/common';
import { Logger } from '../logger/Logger.decorator';
import { Logger } from '@/modules/logger/Logger.decorator';
import { INFLUX_TELEMETRY_BUCKET } from '../core/config';
import { flux } from '@influxdata/influxdb-client';
import { InfluxService } from '../influx/Influx.service';
import { InfluxService } from '@/modules/influx/Influx.service';
import { ACTIVE_STATES } from '@hyped/telemetry-constants';
import { InfluxRow } from '@/modules/common/types/InfluxRow';

type InfluxRow = {
_time: string;
_value: string;
podId: string;
interface InfluxStateRow extends InfluxRow {
stateType: string;
};
}

@Injectable()
export class PublicDataService {
Expand Down Expand Up @@ -38,7 +36,9 @@ export class PublicDataService {
`;

try {
const data = await this.influxService.query.collectRows<InfluxRow>(query);
const data = await this.influxService.query.collectRows<InfluxStateRow>(
query,
);

return {
currentState: data[0]
Expand Down Expand Up @@ -101,7 +101,9 @@ export class PublicDataService {
`;

try {
const data = await this.influxService.query.collectRows<InfluxRow>(query);
const data = await this.influxService.query.collectRows<InfluxStateRow>(
query,
);
const launchTime = new Date(data[0]['_time']).getTime();

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ALL_POD_STATES, pods } from '@hyped/telemetry-constants';
import { zodEnumFromObjKeys } from 'src/utils/zodEnumFromObjKeys';
import { zodEnumFromObjKeys } from '@/modules/common/utils/zodEnumFromObjKeys';
import { z } from 'zod';

export const StateUpdateSchema = z.object({
Expand Down

0 comments on commit b0be1eb

Please sign in to comment.