From a93bbce27120a7b516d598c532f45cefecd37b23 Mon Sep 17 00:00:00 2001 From: Ronald-pro <36841157+Ronald-pro@users.noreply.github.com> Date: Tue, 19 Nov 2024 14:19:16 +0300 Subject: [PATCH] blood pressure filter --- README.md | 4 +- routes/processes/nishauri_new.js | 119 ++++++++++++++++++++++++++++++- 2 files changed, 118 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0972519..cc5fd8f 100644 --- a/README.md +++ b/README.md @@ -66,13 +66,13 @@ create `.env` then edit it with your environment variable. use `.env.save` as a $ npm start ## docker option -* Create a image using +* Create a image using ```sh docker build -t ushauri_api:latest . ``` * Run the image creates ```sh - docker run -p 7002:5000 --name ushauri_api -d --restart always ushauri_api:latest + docker run -p 7002:5000 --name ushauri_api -d --restart always ushauri_api:latest ``` * Access the service via [localhost](http://127.0.0.1:7002) * stoping the service use diff --git a/routes/processes/nishauri_new.js b/routes/processes/nishauri_new.js index 3c3a815..f775a1a 100644 --- a/routes/processes/nishauri_new.js +++ b/routes/processes/nishauri_new.js @@ -5759,9 +5759,9 @@ router.get( }, attributes: [ [fn("DATE_FORMAT", col("created_at"), "%Y-%m-%d %H:00:00"), "hour"], - [fn("AVG", col("level")), "avg_level"], - [fn("MIN", col("level")), "min_level"], - [fn("MAX", col("level")), "max_level"] + [fn("AVG", col("level")), "avg_level"] + // [fn("MIN", col("level")), "min_level"], + // [fn("MAX", col("level")), "max_level"] ], group: [literal("hour")], order: [[col("created_at"), "ASC"]] @@ -5840,6 +5840,119 @@ router.get( } } ); + router.get( + "/get_blood_pressure_filter", + passport.authenticate("jwt", { session: false }), + async (req, res) => { + try { + let user_id = req.query.user_id; + let decoded_user_id = base64.decode(user_id); + + const today = new Date(); + const startOfWeek = new Date(today); + startOfWeek.setDate(today.getDate() - today.getDay()); + + // Retrieve hourly blood pressure data for today + const hourlyLogs = await NBloodPressure.findAll({ + where: { + user_id: decoded_user_id, + created_at: { + [Op.gte]: moment().startOf('day').toDate(), + [Op.lte]: moment().endOf('day').toDate(), + } + }, + attributes: [ + [fn("DATE_FORMAT", col("created_at"), "%Y-%m-%d %H:00:00"), "hour"], + [fn("AVG", col("systolic")), "avg_systolic"], + [fn("AVG", col("diastolic")), "avg_diastolic"], + [fn("AVG", col("pulse_rate")), "pulse_rate"] + ], + group: [literal("hour")], + order: [[col("created_at"), "ASC"]] + }); + + // Process hourly logs for today + const formattedHourlyLogs = hourlyLogs.map(log => { + return { + hour: log.dataValues.hour, + avg_systolic: log.dataValues.avg_systolic, + min_systolic: log.dataValues.min_systolic, + max_systolic: log.dataValues.max_systolic, + avg_diastolic: log.dataValues.avg_diastolic, + min_diastolic: log.dataValues.min_diastolic, + max_diastolic: log.dataValues.max_diastolic, + pulse_rate: log.dataValues.pulse_rate, + pulse_rate: log.dataValues.pulse_rate, + pulse_rate: log.dataValues.pulse_rate + }; + }); + + // Retrieve weekly blood pressure averages + const weeklyLogs = await NBloodPressure.findAll({ + where: { + user_id: decoded_user_id, + created_at: { + [Op.gte]: startOfWeek, + [Op.lte]: today + } + }, + attributes: [ + [fn("DAYNAME", col("created_at")), "dayName"], + [fn("DATE", col("created_at")), "date"], + "systolic", + "diastolic", + "pulse_rate" + ] + }); + + // Process weekly logs + const daysOfWeek = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; + const formattedWeeklyLogs = daysOfWeek.map(day => { + const log = weeklyLogs.find(entry => entry.dataValues.dayName === day); + return log ? log.dataValues : { dayName: day, date: null, systolic: null, diastolic: null, pulse_rate: null }; + }); + + // Retrieve average blood sugar level for the past six months + const sixMonthsAgo = new Date(today); + sixMonthsAgo.setMonth(today.getMonth() - 5); + + const monthlyAverages = await NBloodPressure.findAll({ + where: { + user_id: decoded_user_id, + created_at: { + [Op.gte]: sixMonthsAgo, + [Op.lte]: today + } + }, + attributes: [ + [fn("DATE_FORMAT", col("created_at"), "%M-%Y"), "month"], + [fn("AVG", col("systolic")), "avg_systolic"], + [fn("AVG", col("diastolic")), "avg_diastolic"], + [fn("AVG", col("pulse_rate")), "avg_pulse_rate"] + ], + group: [literal("month")], + order: [[col("created_at"), "DESC"]] + }); + + res.json({ + success: true, + message: "User blood pressure logs retrieved successfully", + data: { + hourly: formattedHourlyLogs, + weekly: formattedWeeklyLogs, + sixMonthly: monthlyAverages, + user_id: user_id + } + }); + } catch (error) { + console.error(error); + res.status(500).json({ + success: false, + message: "Error retrieving blood pressure logs" + }); + } + } + );