Skip to content

Commit

Permalink
dump vitals from observation and image to s3 while creating automated…
Browse files Browse the repository at this point in the history
… daily round
  • Loading branch information
khavinshankar committed May 28, 2024
1 parent 6ebb4f1 commit 4431021
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
55 changes: 45 additions & 10 deletions src/cron/automatedDailyRounds.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios, { AxiosError, AxiosResponse } from "axios";
import { randomUUID } from "crypto";
import fs from "fs";
import path from "path";

Expand All @@ -9,17 +10,27 @@ import prisma from "@/lib/prisma";
import { AssetBed } from "@/types/asset";
import { CameraParams } from "@/types/camera";
import { CarePaginatedResponse } from "@/types/care";
import { DailyRoundObservation, Observation, ObservationType } from "@/types/observation";
import {
DailyRoundObservation,
Observation,
ObservationType,
} from "@/types/observation";
import { OCRV2Response } from "@/types/ocr";
import { CameraUtils } from "@/utils/CameraUtils";
import { isValid } from "@/utils/ObservationUtils";
import { generateHeaders } from "@/utils/assetUtils";
import { careApi, openaiApiKey, saveDailyRound } from "@/utils/configs";
import {
careApi,
hostname,
openaiApiKey,
s3SaveDailyRound,
saveDailyRound,
} from "@/utils/configs";
import { getPatientId } from "@/utils/dailyRoundUtils";
import { downloadImage } from "@/utils/downloadImageWithDigestRouter";
import { makeDataDumpToJson } from "@/utils/makeDataDump";
import { parseVitalsFromImage } from "@/utils/ocr";


const UPDATE_INTERVAL = 60 * 60 * 1000;

export async function getMonitorPreset(bedId: string, assetId: string) {
Expand Down Expand Up @@ -64,8 +75,8 @@ export async function getMonitorPreset(bedId: string, assetId: string) {
export async function saveImageLocally(
snapshotUrl: string,
camParams: CameraParams,
fileName = `image--${new Date().getTime()}.jpeg`,
) {
const fileName = `image--${new Date().getTime()}.jpeg`;
const imagePath = path.resolve("images", fileName);
await downloadImage(
snapshotUrl,
Expand All @@ -92,7 +103,7 @@ export async function getVitalsFromImage(imageUrl: string) {
// ? date.toISOString()
// : new Date().toISOString();
const isoDate = new Date().toISOString();

const payload = {
taken_at: isoDate,
spo2: data.spO2?.oxygen_saturation_percentage ?? null,
Expand Down Expand Up @@ -281,11 +292,16 @@ export async function automatedDailyRounds() {
return;
}

let vitals: DailyRoundObservation | null = await getVitalsFromObservations(
monitor.ipAddress,
);
const _id = randomUUID();
let vitals: DailyRoundObservation | null = s3SaveDailyRound
? null
: await getVitalsFromObservations(monitor.ipAddress);

console.log(`Vitals from observations: ${JSON.stringify(vitals)}`);
console.log(
saveDailyRound
? "Skipping vitals from observations as saving daily round is enabled"
: `Vitals from observations: ${JSON.stringify(vitals)}`,
);

if (!vitals && openaiApiKey) {
if (!asset_beds || asset_beds.length === 0) {
Expand Down Expand Up @@ -325,14 +341,33 @@ export async function automatedDailyRounds() {
const snapshotUrl = await CameraUtils.getSnapshotUri({
camParams: camera,
});
const imageUrl = await saveImageLocally(snapshotUrl.uri, camera);
const imageUrl = await saveImageLocally(snapshotUrl.uri, camera, _id);

CameraUtils.unlockCamera(camera.hostname);

vitals = await getVitalsFromImage(imageUrl);
console.log(`Vitals from image: ${JSON.stringify(vitals)}`);
}

if (s3SaveDailyRound) {
const vitalsFromObservation = await getVitalsFromObservations(
monitor.ipAddress,
);
console.log(
`Vitals from observations: ${JSON.stringify(vitalsFromObservation)}`,
);

makeDataDumpToJson(
{
vitalsFromObservation,
vitalsFromImage: vitals,
},
`${hostname}/daily-rounds/${_id}.json`,
);

vitals = vitalsFromObservation ?? vitals;
}

if (!vitals || !payloadHasData(vitals)) {
console.error(`No vitals found for the patient ${patient_id}`);
return;
Expand Down
6 changes: 6 additions & 0 deletions src/utils/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ export const s3BucketName = process.env.S3_BUCKET_NAME;
export const s3AccessKeyId = process.env.S3_ACCESS_KEY_ID;
export const s3SecretAccessKey = process.env.S3_SECRET_ACCESS_KEY;

export const s3SaveDailyRound =
Boolean(process.env.S3_SAVE_DAILY_ROUND) &&
s3BucketName &&
s3AccessKeyId &&
s3SecretAccessKey;

export const openaiApiKey = process.env.OPENAI_API_KEY ?? "";

0 comments on commit 4431021

Please sign in to comment.