Skip to content

Commit

Permalink
feat(tel-env): switch to using env package
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbeechey committed May 25, 2024
1 parent 74ffb27 commit 7b8ccb2
Show file tree
Hide file tree
Showing 28 changed files with 72 additions and 130 deletions.
6 changes: 5 additions & 1 deletion telemetry/packages/env/.env.docker → telemetry/.env.docker
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ PUBLIC_HTTP_DEBUG=true # default: true
PUBLIC_EXTENDED_DEBUGGING_TOOLS=false # default: false

# PUBLIC APP
PUBLIC_PUBLIC_APP_POD_ID=pod_2024
PUBLIC_PUBLIC_APP_POD_ID=pod_2024

# ENV
ENV=production
PUBLIC_IS_DOCKER=true
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ PUBLIC_HTTP_DEBUG=true # default: true
PUBLIC_EXTENDED_DEBUGGING_TOOLS=false # default: false

# PUBLIC APP
PUBLIC_PUBLIC_APP_POD_ID=pod_2024
PUBLIC_PUBLIC_APP_POD_ID=pod_2024

# ENV
ENV=development
PUBLIC_IS_DOCKER=false
7 changes: 6 additions & 1 deletion telemetry/packages/env/env.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { createEnv } from '@t3-oss/env-core';
import { z } from 'zod';
import 'dotenv/config';
import dotenv from 'dotenv';

// Load environment variables from the ".env" file in the root of the `/telemetry` directory.
dotenv.config({ path: '../../.env' });

/**
* HYPED Telemetry environment variables for the GUI.
Expand All @@ -12,6 +15,7 @@ export const env = createEnv({
INFLUX_ORG: z.string().min(1),
INFLUX_TELEMETRY_BUCKET: z.string().min(1),
INFLUX_FAULTS_BUCKET: z.string().min(1),
ENV: z.enum(['development', 'production']).default('development'),
},

/**
Expand All @@ -38,6 +42,7 @@ export const env = createEnv({
PUBLIC_EXTENDED_DEBUGGING_TOOLS: z.coerce.boolean().default(false),
PUBLIC_PUBLIC_APP_POD_ID: z.string().min(1),
PUBLIC_TELEMETRY_SERVER_URL: z.string().min(1),
PUBLIC_IS_DOCKER: z.coerce.boolean().default(false),
},

/**
Expand Down
4 changes: 3 additions & 1 deletion telemetry/packages/env/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
"name": "@hyped/env",
"version": "1.0.0",
"description": "Contains environment variable validation schemas for HYPED Telemetry",
"main": "dist/env.js",
"types": "dist/env.d.ts",
"type": "module",
"scripts": {
"build": "tsc",
"build": "tsc && node ./dist/env.js",
"dev": "tsc -w",
"validate": "tsc && node ./dist/env.js"
},
Expand Down
2 changes: 0 additions & 2 deletions telemetry/packages/public-app/.env.docker

This file was deleted.

2 changes: 0 additions & 2 deletions telemetry/packages/public-app/.env.example

This file was deleted.

3 changes: 2 additions & 1 deletion telemetry/packages/public-app/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import {
LaunchTimeResponse,
VelocityResponse,
} from '@hyped/telemetry-types/dist/server/responses';
import { env } from '@hyped/env';

/**
* The server endpoint for fetching public data.
*/
export const SERVER_ENDPOINT = `${process.env.NEXT_PUBLIC_TELEMETRY_SERVER}/pods/${process.env.NEXT_PUBLIC_POD_ID}/public-data`;
export const SERVER_ENDPOINT = `${env.PUBLIC_TELEMETRY_SERVER_URL}/pods/${env.PUBLIC_PUBLIC_APP_POD_ID}/public-data`;

const ONE_MINUTE = 60;

Expand Down
1 change: 1 addition & 0 deletions telemetry/packages/public-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"lint:fix": "eslint --fix --ext .ts,.tsx ."
},
"dependencies": {
"@hyped/env": "workspace:*",
"@preact/signals-react": "^1.3.6",
"@radix-ui/react-switch": "^1.0.3",
"@tremor/react": "^3.9.2",
Expand Down
6 changes: 0 additions & 6 deletions telemetry/packages/server/.env.docker

This file was deleted.

6 changes: 0 additions & 6 deletions telemetry/packages/server/.env.example

This file was deleted.

1 change: 1 addition & 0 deletions telemetry/packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"lint:fix": "eslint \"src/**/*.ts\" --fix"
},
"dependencies": {
"@hyped/env": "workspace:*",
"@hyped/telemetry-constants": "workspace:*",
"@influxdata/influxdb-client": "^1.33.1",
"@nestjs/common": "^9.2.1",
Expand Down
20 changes: 0 additions & 20 deletions telemetry/packages/server/src/modules/core/config.ts

This file was deleted.

23 changes: 10 additions & 13 deletions telemetry/packages/server/src/modules/influx/Influx.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { InfluxDB, QueryApi, WriteApi } from '@influxdata/influxdb-client';
import { Injectable, LoggerService, OnModuleInit } from '@nestjs/common';
import {
INFLUX_TELEMETRY_BUCKET,
INFLUX_ORG,
INFLUX_TOKEN,
INFLUX_URL,
INFLUX_FAULTS_BUCKET,
} from '@/modules/core/config';
import { Logger } from '@/modules/logger/Logger.decorator';
import { InfluxServiceError } from './errors/InfluxServiceError';
import { env } from '@hyped/env';

@Injectable()
export class InfluxService implements OnModuleInit {
Expand All @@ -20,24 +14,27 @@ export class InfluxService implements OnModuleInit {
private readonly logger: LoggerService;

async $connect() {
this.connection = new InfluxDB({ url: INFLUX_URL, token: INFLUX_TOKEN });
this.connection = new InfluxDB({
url: env.INFLUX_URL,
token: env.INFLUX_TOKEN,
});
this.telemetryWrite = this.connection.getWriteApi(
INFLUX_ORG,
INFLUX_TELEMETRY_BUCKET,
env.INFLUX_ORG,
env.INFLUX_TELEMETRY_BUCKET,
'ns',
{
batchSize: 1,
},
);
this.faultsWrite = this.connection.getWriteApi(
INFLUX_ORG,
INFLUX_FAULTS_BUCKET,
env.INFLUX_ORG,
env.INFLUX_FAULTS_BUCKET,
'ns',
{
batchSize: 1,
},
);
this.query = this.connection.getQueryApi(INFLUX_ORG);
this.query = this.connection.getQueryApi(env.INFLUX_ORG);

// Warn if InfluxDB isn't running
try {
Expand Down
6 changes: 3 additions & 3 deletions telemetry/packages/server/src/modules/logger/Logger.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Module } from '@nestjs/common';
import { utilities, WinstonModule, WinstonModuleOptions } from 'nest-winston';
import { format, transports } from 'winston';
import { ENV } from '@/modules/core/config';
import { LiveLogsTransport } from '../live-logs/LiveLogsTransport';
import { env } from '@hyped/env';

// In top-level 'telemetry' directory
const LOGGING_DIRECTORY = '../../logs';
Expand All @@ -26,7 +26,7 @@ const unhandledErrorFormat = format((info) => {
});

export const loggerOptions: WinstonModuleOptions = {
level: ENV === 'development' ? 'debug' : 'info',
level: env.ENV === 'development' ? 'debug' : 'info',
format: format.combine(
unhandledErrorFormat(),
format.timestamp({
Expand All @@ -44,7 +44,7 @@ export const loggerOptions: WinstonModuleOptions = {
level: 'error',
}),
new transports.File({ filename: 'all.log', dirname: LOGGING_DIRECTORY }),
...(ENV === 'development'
...(env.ENV === 'development'
? [
new transports.Console({
format: format.combine(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { env } from '@hyped/env';
import { Module } from '@nestjs/common';
import { MqttModule } from 'nest-mqtt';
import { MQTT_BROKER_HOST } from 'src/modules/core/config';

const mqttClient = MqttModule.forRoot({
host: MQTT_BROKER_HOST,
host: env.PUBLIC_MQTT_BROKER_HOST,
});

@Module({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { flux, fluxDateTime } from '@influxdata/influxdb-client';
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';
import { env } from '@hyped/env';

interface InfluxHistoricalRow extends InfluxRow {
measurementKey: string;
Expand Down Expand Up @@ -32,7 +32,7 @@ export class HistoricalTelemetryDataService {
);

const query = flux`
from(bucket: "${INFLUX_TELEMETRY_BUCKET}")
from(bucket: "${env.INFLUX_TELEMETRY_BUCKET}")
|> range(start: ${fluxStart}, stop: ${fluxEnd})
|> filter(fn: (r) => r["measurementKey"] == "${measurementKey}")
|> filter(fn: (r) => r["podId"] == "${podId}")`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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 { env } from '@hyped/env';
import { OpenMctFault } from '@hyped/telemetry-types';
import { HistoricalFaults } from '@hyped/telemetry-types/dist/openmct/openmct-fault.types';
import { fluxString } from '@influxdata/influxdb-client';
Expand Down Expand Up @@ -35,7 +35,7 @@ export class HistoricalFaultDataService {
): Promise<HistoricalFaults> {
const { podId, measurementKey, faultId } = props;

const query = `from(bucket: "${INFLUX_FAULTS_BUCKET}")
const query = `from(bucket: "${env.INFLUX_FAULTS_BUCKET}")
|> range(start: -24h)
${podId ? `|> filter(fn: (r) => r["podId"] == ${fluxString(podId) as unknown as string})` : ''}
${
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { HttpException, Injectable, LoggerService } from '@nestjs/common';
import { Logger } from '@/modules/logger/Logger.decorator';
import { INFLUX_TELEMETRY_BUCKET } from '../core/config';
import { flux } from '@influxdata/influxdb-client';
import { InfluxService } from '@/modules/influx/Influx.service';
import { ACTIVE_STATES } from '@hyped/telemetry-constants';
Expand All @@ -9,6 +8,7 @@ import {
LaunchTimeResponse,
StateResponse,
} from '@hyped/telemetry-types/dist/server/responses';
import { env } from '@hyped/env';

interface InfluxStateRow extends InfluxRow {
stateType: string;
Expand All @@ -30,7 +30,7 @@ export class PublicDataService {
public async getState(podId: string): Promise<StateResponse> {
// Get the last state reading from InfluxDB (measurement name should be 'state')
const query = flux`
from(bucket: "${INFLUX_TELEMETRY_BUCKET}")
from(bucket: "${env.INFLUX_TELEMETRY_BUCKET}")
|> range(start: -1d)
|> filter(fn: (r) => r["_measurement"] == "state")
|> filter(fn: (r) => r["podId"] == "${podId}")
Expand Down Expand Up @@ -93,7 +93,7 @@ export class PublicDataService {

// Get the last "ACCELERATING" state reading from InfluxDB
const query = flux`
from(bucket: "${INFLUX_TELEMETRY_BUCKET}")
from(bucket: "${env.INFLUX_TELEMETRY_BUCKET}")
|> range(start: -1d)
|> filter(fn: (r) => r["_measurement"] == "state")
|> filter(fn: (r) => r["podId"] == "${podId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import {
CardTitle,
} from '@/components/ui/card';
import { Server } from 'lucide-react';
import { config } from '@/config';
import { useLiveLogs } from '@/context/live-logs';
import { useEffect, useState } from 'react';
import { LatencyChart } from '@/components/shared/latency-chart';
import { PreviousLatenciesType } from '@/context/pods';
import { env } from '@hyped/env';

/**
* The number of previous latencies to show in the chart.
Expand Down Expand Up @@ -40,7 +40,7 @@ export const ServerConnectionStatus = () => {
// Check for server ping every second and set the latency
const interval = setInterval(() => {
const start = Date.now();
fetch(`${config.SERVER_ENDPOINT}/ping`)
fetch(`${env.PUBLIC_TELEMETRY_SERVER_URL}/ping`)
.then(() => {
// The latency is the time it took to get a response from the server (round trip)
const latency = Date.now() - start;
Expand Down Expand Up @@ -106,7 +106,7 @@ export const ServerConnectionStatus = () => {
</div>
</CardContent>
<CardFooter className="text-sm text-muted-foreground">
{config.SERVER_ENDPOINT}
{env.PUBLIC_TELEMETRY_SERVER_URL}
</CardFooter>
</Card>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@/components/ui/resizable';
import { useCurrentPod } from '@/context/pods';
import { PodStateUpdater } from './pod-state-updater';
import { config } from '@/config';
import { env } from '@hyped/env';

/**
* Debug view. Contains components for debugging.
Expand All @@ -21,7 +21,8 @@ import { config } from '@/config';
export const DebugView = () => {
const { currentPod: podId } = useCurrentPod();

const showExternalDebuggingTools = config.EXTENDED_DEBUGGING_TOOLS ?? false;
const showExternalDebuggingTools =
env.PUBLIC_EXTENDED_DEBUGGING_TOOLS ?? false;

return (
<ResizablePanelGroup direction="vertical">
Expand Down
4 changes: 2 additions & 2 deletions telemetry/packages/ui/app/context/errors.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { config } from '@/config';
import { log } from '@/lib/logger';
import { env } from '@hyped/env';
import { PodId } from '@hyped/telemetry-constants';
import { createContext, useContext, useState } from 'react';

Expand Down Expand Up @@ -51,7 +51,7 @@ export const ErrorProvider = ({ children }: ErrorProviderProps) => {
) => {
// If the error is a pod disconnect error and the config is set to disable it, don't raise the error
if (
config.DISABLE_POD_DISCONNECTED_ERROR &&
env.PUBLIC_UI_POD_DISCONNECTED_MESSAGES_DISABLED &&
id === ERROR_IDS.POD_DISCONNECT
) {
return;
Expand Down
4 changes: 2 additions & 2 deletions telemetry/packages/ui/app/context/live-logs.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { env } from '@hyped/env';
import { useState, useEffect, createContext, useContext } from 'react';
import { config } from '@/config';
import { io } from 'socket.io-client';

// Socket.io client for live logs from the telemetry server
const socket = io(config.SERVER_ENDPOINT, {
const socket = io(env.PUBLIC_TELEMETRY_SERVER_URL, {
path: '/live-logs',
});

Expand Down
Loading

0 comments on commit 7b8ccb2

Please sign in to comment.