modelData.ts

mail@pastecode.io avatar
unknown
typescript
a year ago
3.8 kB
2
Indexable
Never
// Utils
import {appendToJsonFile, createJsonFile} from '@Utils/fileHelper';

// Types
import {ModelEventType} from '@Types';
import {S3PathKey} from '@Types';

/**
 * It takes the railroad_id and returns the name of the file that will be used to store the model data
 * @param {string} railroad_id - The railroad's ID.
 * @returns A string with the name of the file that will be used to store the model data
 */
export const generateModelFileName = (
  railroad_id: string,
  simple_train_consist_id: string,
) => {
  const today = new Date().toISOString();
  const year = today.slice(0, 4);
  const month = today.slice(5, 7);
  const day = today.slice(8, 10);
  const epochTimeStamp = Date.now();
  return `${railroad_id}:${year}:${month}:${day}:${epochTimeStamp}:${simple_train_consist_id}`;
};

/**
 * It creates a new JSON file with the filename provided, and the JSON schema provided
 * @param {string} railroad_id - The ID of the railroad you're on.
 * @param {string} trip_id - This is the trip_id that you want to use.
 * @param {string} user_id - The user_id of the user who is creating the file.
 * @param {string} filename - the name of the file to be created
 * @param {any} location - the location object that will be used to generate the first Model log
 * @param {string} simple_train_consist_id - the simple train consist id
 * @param {any} loco_id - the locomotive id
 * @param {ModelEventType} event_type - the model event type
 */
export const createNewModelFile = async (
  railroad_id: string,
  trip_id: string,
  user_id: string,
  filename: string,
  location: any,
  simple_train_consist_id: string,
  loco_id: string,
  event_type: ModelEventType,
) => {
  const jsonSchema = {
    railroad_id: railroad_id,
    trip_id: trip_id,
    user_id: user_id,
    simple_train_consist_id: simple_train_consist_id,
    loco_id: loco_id,
    created_at_utc: new Date().toISOString(),
    data: [generateModelJsonLog(event_type, location, null)],
  };
  const stringify = JSON.stringify(jsonSchema);
  await createJsonFile(
    S3PathKey.APP_MODEL_LOGGING,
    filename,
    stringify.slice(0, stringify.length - 2),
  );
};

/**
 * It takes a filename and a location object, generates a model log object, stringifies it, and appends
 * it to the file
 * @param {string} filename - the name of the file to append to
 * @param {ModelEventType} event_type - the name of the file to append to
 * @param {any} location - the location object that will be used to generate the Model log
 * @param {any} recommendation - the generated recommendation
 */
export const appendToModelFile = async (
  filename: string,
  event_type: ModelEventType,
  location: any,
  recommendation: any,
) => {
  const modelLog = generateModelJsonLog(event_type, location, recommendation);
  const stringify = JSON.stringify(modelLog);
  await appendToJsonFile(
    S3PathKey.APP_MODEL_LOGGING,
    filename,
    `,${stringify}`,
  );
};

/**
 * It takes a location object and returns a Model log object
 * @param {any} location - the location object that will be used to generate the model log
 * @returns A JSON object
 */
export const generateModelJsonLog = (
  event_type: ModelEventType,
  location: any,
  recommendation: any,
) => {
  const modelLog = {
    event_type: event_type,
    timestamp_utc: new Date().toISOString(),
    recommendation: recommendation,
    position: {
      coordinate: {
        lat: location?.latitude,
        lng: location?.longitude,
        gps_error_radius_m: location?.accuracy,
      },
      altitude: {
        alt_m: location?.altitude,
        alt_error_m: location?.verticalAccuracy,
      },
    },
    velocity: {
      speed_mps: location?.speed,
      speed_error_mps: 0,
      course: location?.course,
      course_error: 0,
    },
  };
  return modelLog;
};