2023-08-21 14:39:15 +05:30
|
|
|
const { onRequest } = require("firebase-functions/v2/https");
|
|
|
|
|
const logger = require("firebase-functions/logger");
|
|
|
|
|
const functions = require("firebase-functions");
|
|
|
|
|
const admin = require("firebase-admin");
|
|
|
|
|
const moment = require('moment');
|
2023-09-04 07:43:03 +05:30
|
|
|
const fs = require('fs');
|
|
|
|
|
const csv = require('csv-parser');
|
|
|
|
|
const html_to_pdf = require('html-pdf-node');
|
2023-08-21 14:39:15 +05:30
|
|
|
|
|
|
|
|
admin.initializeApp();
|
|
|
|
|
|
|
|
|
|
exports.registerUser = functions.region("asia-south1").https.onRequest(async (req, res) => {
|
2023-08-28 19:27:10 +05:30
|
|
|
res.set('Access-Control-Allow-Origin', "*")
|
|
|
|
|
res.set('Access-Control-Allow-Methods', 'GET, POST');
|
|
|
|
|
|
|
|
|
|
if (req.method === "OPTIONS") {
|
|
|
|
|
// stop preflight requests here
|
|
|
|
|
res.set("Access-Control-Allow-Headers", "Content-Type, x-client");
|
|
|
|
|
res.status(204).send('');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-21 14:39:15 +05:30
|
|
|
try {
|
|
|
|
|
const token = "e16a1bd15af2ee640f5a7a18c8d8333f5f01f7c66e003bbc21ee52870f1bce27";
|
|
|
|
|
logger.info(`User-Agent ${req.get("User-Agent")}`);
|
|
|
|
|
logger.info(`token ${req.get("x-client")}`);
|
|
|
|
|
if (req.get("x-client") !== token) {
|
|
|
|
|
res.json({ status: "403", message: "auth error", result: "true" });
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
const { body } = req;
|
|
|
|
|
const { _id } = body;
|
|
|
|
|
logger.info(`_id ${_id}`);
|
|
|
|
|
await admin
|
|
|
|
|
.firestore()
|
|
|
|
|
.collection("patientData")
|
|
|
|
|
.add(Object.assign(Object.assign({}, body), { createdAt: moment().utcOffset("+05:30").format("YYYY-MM-DD HH:mm:ss") }))
|
|
|
|
|
res.json({ status: "200" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (err) {
|
|
|
|
|
logger.info(`error ${err}`);
|
|
|
|
|
res.json({ status: "400", message: "some error.", result: "true" });
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-09-04 07:43:03 +05:30
|
|
|
|
|
|
|
|
exports.addBloodTest = functions.region("asia-south1").https.onRequest(async (req, res) => {
|
|
|
|
|
res.set('Access-Control-Allow-Origin', "*")
|
|
|
|
|
res.set('Access-Control-Allow-Methods', 'GET, POST');
|
|
|
|
|
|
|
|
|
|
if (req.method === "OPTIONS") {
|
|
|
|
|
// stop preflight requests here
|
|
|
|
|
res.set("Access-Control-Allow-Headers", "Content-Type, x-client");
|
|
|
|
|
res.status(204).send('');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const token = "e16a1bd15af2ee640f5a7a18c8d8333f5f01f7c66e003bbc21ee52870f1bce27";
|
|
|
|
|
logger.info(`User-Agent ${req.get("User-Agent")}`);
|
|
|
|
|
logger.info(`token ${req.get("x-client")}`);
|
|
|
|
|
if (req.get("x-client") !== token) {
|
|
|
|
|
res.json({ status: "403", message: "auth error", result: "true" });
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
const { body } = req;
|
|
|
|
|
const { _id } = body;
|
|
|
|
|
logger.info(`_id ${_id}`);
|
|
|
|
|
await admin
|
|
|
|
|
.firestore()
|
|
|
|
|
.collection("testData")
|
2023-09-19 22:50:33 +05:30
|
|
|
.add(Object.assign(Object.assign({}, body), { testTime: moment().utcOffset("+05:30").format("YYYY-MM-DD HH:mm:ss") }))
|
2023-09-04 07:43:03 +05:30
|
|
|
res.json({ status: "200" });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (err) {
|
|
|
|
|
logger.info(`error ${err}`);
|
|
|
|
|
res.json({ status: "400", message: "some error.", result: "true" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
exports.reportDownload = functions.region("asia-south1").https.onRequest(async (req, res) => {
|
|
|
|
|
res.set('Access-Control-Allow-Origin', "*")
|
|
|
|
|
res.set('Access-Control-Allow-Methods', 'GET, POST');
|
|
|
|
|
|
|
|
|
|
if (req.method === "OPTIONS") {
|
|
|
|
|
// stop preflight requests here
|
|
|
|
|
res.set("Access-Control-Allow-Headers", "Content-Type, x-client");
|
|
|
|
|
res.status(204).send('');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const token = "e16a1bd15af2ee640f5a7a18c8d8333f5f01f7c66e003bbc21ee52870f1bce27";
|
|
|
|
|
logger.info(`User-Agent ${req.get("User-Agent")}`);
|
|
|
|
|
logger.info(`token ${req.get("x-client")}`);
|
|
|
|
|
if (req.get("x-client") !== token) {
|
|
|
|
|
res.json({ status: "403", message: "auth error", result: "true" });
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
const { body } = req;
|
|
|
|
|
const { data } = body;
|
|
|
|
|
logger.info(`_id ${_id}`);
|
|
|
|
|
const fileName = `${data["_id"]}.pdf`;
|
|
|
|
|
// await makePdf();
|
|
|
|
|
res.sendFile(fileName, options, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
next(err);
|
|
|
|
|
} else {
|
|
|
|
|
console.log('Sent:', fileName);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (err) {
|
|
|
|
|
logger.info(`error ${err}`);
|
|
|
|
|
res.json({ status: "400", message: "some error.", result: "true" });
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2023-09-19 18:03:41 +05:30
|
|
|
/**
|
|
|
|
|
* Triggered by a change to a Firestore document.
|
|
|
|
|
*
|
|
|
|
|
* @param {!Object} event Event payload.
|
|
|
|
|
* @param {!Object} context Metadata for the event.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// const functions = require("firebase-functions");
|
|
|
|
|
// const admin = require("firebase-admin");
|
2023-09-23 13:42:18 +05:30
|
|
|
// const moment = require('moment');
|
2023-09-19 18:03:41 +05:30
|
|
|
|
|
|
|
|
// admin.initializeApp();
|
|
|
|
|
|
|
|
|
|
exports.kitAlert = async (event, context) => {
|
|
|
|
|
const resource = context.resource;
|
|
|
|
|
// log out the resource string that triggered the function
|
|
|
|
|
console.log('Function triggered by change to: ' + resource);
|
|
|
|
|
// now log the full event object
|
|
|
|
|
console.log(JSON.stringify(event));
|
2023-12-14 21:03:09 +05:30
|
|
|
const { kitSerial, classificationResult, _id, led1Average, led3Average, deviceRatio } = event.value.fields;
|
2023-09-19 18:03:41 +05:30
|
|
|
console.log('kitSerial', kitSerial);
|
|
|
|
|
console.log('classificationResult', classificationResult);
|
2023-11-11 15:55:45 +05:30
|
|
|
// if (classificationResult?.stringValue?.includes("Trait")) {
|
|
|
|
|
// const currentKitTests = await admin.firestore().collection("testData").where("kitSerial", "==", kitSerial.stringValue).get();
|
|
|
|
|
// let traitCount = 0;
|
|
|
|
|
// currentKitTests?.docs.forEach((x) => {
|
|
|
|
|
// if (x.data().classificationResult?.includes("Trait")) {
|
|
|
|
|
// traitCount++;
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
// console.log("traitCount", traitCount);
|
|
|
|
|
// if (traitCount > 2) {
|
|
|
|
|
// await admin.firestore().collection("alerts").add({
|
|
|
|
|
// name: 'Kit Alert',
|
|
|
|
|
// traitCount: traitCount,
|
|
|
|
|
// message: `${kitSerial.stringValue} has ${traitCount} traits`
|
|
|
|
|
// });
|
|
|
|
|
// }
|
|
|
|
|
// const sgMail = require('@sendgrid/mail');
|
|
|
|
|
// sgMail.setApiKey('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');
|
|
|
|
|
// const msg = {
|
|
|
|
|
// to: ['hpos.smi@gmail.com', 'pritimay@sminnovations.in'],
|
|
|
|
|
// from: 'pritimay@sminnovations.in',
|
|
|
|
|
// subject: `Kit Alert: ${kitSerial.stringValue}`,
|
|
|
|
|
// text: `${kitSerial.stringValue} has ${traitCount} traits`,
|
|
|
|
|
// html: `<p>${kitSerial.stringValue} has ${traitCount} traits</p>`,
|
|
|
|
|
// };
|
|
|
|
|
// sgMail.send(msg);
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
const led1Intercept = -0.85965;
|
|
|
|
|
const led3Slope = 0.184991;
|
|
|
|
|
const led3Intercept = 0.001704;
|
|
|
|
|
|
|
|
|
|
console.log('_id', _id?.stringValue);
|
|
|
|
|
console.log('led1Average', led1Average?.doubleValue);
|
|
|
|
|
console.log('led3Average', led3Average?.doubleValue);
|
|
|
|
|
const LED1Average = led1Average?.doubleValue;
|
|
|
|
|
const LED3Average = led3Average?.doubleValue;
|
|
|
|
|
|
|
|
|
|
const Abs427 = LED1Average * 1.2648 - 0.85965;
|
|
|
|
|
const Abs555 = LED3Average * 0.184991 + 0.001704;
|
|
|
|
|
const pdr = Abs555 / Abs427;
|
|
|
|
|
// const pdr = 0.239;
|
|
|
|
|
console.log(pdr);
|
|
|
|
|
let classification = "NA";
|
|
|
|
|
if (pdr > 0 && pdr <= 0.16) {
|
|
|
|
|
classification = "Normal";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pdr > 0.16 && pdr <= 0.165) {
|
|
|
|
|
classification = "Negative Borderline";
|
2023-09-19 18:03:41 +05:30
|
|
|
}
|
2023-11-11 15:55:45 +05:30
|
|
|
|
|
|
|
|
if (pdr > 0.165 && pdr <= 0.235) {
|
|
|
|
|
classification = "Sickle Cell Trait";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pdr > 0.235 && pdr <= 0.24) {
|
|
|
|
|
classification = "Positive Borderline";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (pdr > 0.24 && pdr <= 1) {
|
|
|
|
|
classification = "Sickle Cell Disease";
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-14 21:03:09 +05:30
|
|
|
// device ratio class
|
|
|
|
|
const devRatio = deviceRatio?.doubleValue;
|
|
|
|
|
let devRatioClass = "INVALID";
|
|
|
|
|
if (devRatio > 0 && devRatio <= 0.55) {
|
|
|
|
|
devRatioClass = "Normal";
|
|
|
|
|
}
|
|
|
|
|
if (devRatio > 0.55 && devRatio <= 0.575) {
|
|
|
|
|
devRatioClass = "Negative Borderline";
|
|
|
|
|
}
|
|
|
|
|
if (devRatio > 0.575 && devRatio <= 0.85) {
|
|
|
|
|
devRatioClass = "Sickle Cell Trait";
|
|
|
|
|
}
|
|
|
|
|
if (devRatio > 0.85 && devRatio <= 0.9) {
|
|
|
|
|
devRatioClass = "Positive Borderline";
|
|
|
|
|
}
|
|
|
|
|
if (devRatio > 0.9 && devRatio <= 1) {
|
|
|
|
|
devRatioClass = "Sickle Cell Disease";
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-11 15:55:45 +05:30
|
|
|
console.log(classification);
|
|
|
|
|
|
|
|
|
|
const docs = await admin.firestore().collection("testData").where("_id", "==", _id.stringValue).get();
|
|
|
|
|
docs?.forEach(async (doc) => {
|
2023-12-14 21:03:09 +05:30
|
|
|
await admin.firestore().collection("testData").doc(doc.id).update({
|
2023-12-10 13:46:29 +05:30
|
|
|
// abs427: Abs427,
|
|
|
|
|
// abs555: Abs555,
|
|
|
|
|
// predictedDenovixRatio: pdr,
|
|
|
|
|
// prdClassification: classification
|
2023-12-14 21:03:09 +05:30
|
|
|
// incubationTime: admin.firestore.FieldValue.delete()
|
|
|
|
|
deviceRatioClass: devRatioClass,
|
|
|
|
|
});
|
2023-11-11 15:55:45 +05:30
|
|
|
})
|
2023-09-19 18:03:41 +05:30
|
|
|
};
|
2023-09-26 04:36:51 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Triggered by a change to a Firestore document.
|
|
|
|
|
*
|
|
|
|
|
* @param {!Object} event Event payload.
|
|
|
|
|
* @param {!Object} context Metadata for the event.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// const functions = require("firebase-functions");
|
|
|
|
|
// const admin = require("firebase-admin");
|
|
|
|
|
// const moment = require('moment');
|
|
|
|
|
|
|
|
|
|
// admin.initializeApp();
|
|
|
|
|
|
|
|
|
|
exports.dataRegularity = async (event, context) => {
|
|
|
|
|
const resource = context.resource;
|
|
|
|
|
// log out the resource string that triggered the function
|
|
|
|
|
console.log('Function triggered by change to: ' + resource);
|
|
|
|
|
// now log the full event object
|
|
|
|
|
console.log(JSON.stringify(event));
|
|
|
|
|
const { kitSerial, classificationResult, _id, deviceId, led1Buffer, led2Buffer, led1Sample, led2Sample } = event.value.fields;
|
|
|
|
|
console.log('_id', _id?.stringValue);
|
2023-09-26 23:56:45 +05:30
|
|
|
if (led1Buffer?.stringValue > 27000 || led2Buffer?.stringValue > 28000 || led1Sample?.stringValue) {
|
2023-09-26 04:36:51 +05:30
|
|
|
const currentKitTests = await admin.firestore().collection("testData").where("kitSerial", "==", kitSerial.stringValue).get();
|
|
|
|
|
let traitCount = 0;
|
|
|
|
|
currentKitTests?.docs.forEach((x) => {
|
|
|
|
|
if (x.data().classificationResult?.includes("Trait")) {
|
|
|
|
|
traitCount++;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
await admin.firestore().collection("alerts").add({
|
|
|
|
|
name: 'Data Regularity',
|
|
|
|
|
kit: kitSerial.stringValue,
|
|
|
|
|
deviceId: deviceId,
|
|
|
|
|
message: `${_id.stringValue} has some irregularity in data`,
|
|
|
|
|
createdAt: moment().utcOffset("+05:30").format("YYYY-MM-DD HH:mm:ss")
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-09-26 23:56:45 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const axios = require('axios');
|
2023-10-23 12:58:38 +05:30
|
|
|
const admin = require("firebase-admin");
|
|
|
|
|
const moment = require('moment');
|
|
|
|
|
|
|
|
|
|
admin.initializeApp();
|
2023-09-26 23:56:45 +05:30
|
|
|
|
|
|
|
|
exports.migrateUsers = async (event, context) => {
|
|
|
|
|
const resource = context.resource;
|
|
|
|
|
// log out the resource string that triggered the function
|
|
|
|
|
console.log('Function triggered by change to: ' + resource);
|
|
|
|
|
// now log the full event object
|
|
|
|
|
console.log(JSON.stringify(event));
|
|
|
|
|
const { _id, name } = event.value.fields;
|
2023-10-23 12:58:38 +05:30
|
|
|
const { _idSearch, abhaIdSearch, aadharIdSearch } = event.value.fields;
|
|
|
|
|
if (!_idSearch?.stringValue) {
|
|
|
|
|
const docs = await admin.firestore().collection("patientData").where("_id", "==", _id.stringValue).get();
|
|
|
|
|
docs?.forEach(async (doc) => {
|
|
|
|
|
await admin.firestore().collection("patientData").doc(doc.id).update({
|
|
|
|
|
_idSearch: `${moment().format('YYYYMMDD')}${doc.data()._id}`,
|
|
|
|
|
abhaIdSearch: `${moment().format('YYYYMMDD')}${doc.data().abhaId}`,
|
|
|
|
|
aadharIdSearch: `${moment().format('YYYYMMDD')}${doc.data().aadharId}`,
|
|
|
|
|
});
|
|
|
|
|
console.log('search logic', _id.stringValue);
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-09-26 23:56:45 +05:30
|
|
|
const NO_OF_COPIES = 3;
|
|
|
|
|
for (let i = 0; i < NO_OF_COPIES; i++) {
|
2023-10-23 12:58:38 +05:30
|
|
|
await axios.post("https://asia-south1-hpos-preprod.cloudfunctions.net/registerUser",
|
|
|
|
|
{
|
|
|
|
|
"_id": `${_id.stringValue}${i}`,
|
|
|
|
|
"aadharId": "2808202300001",
|
|
|
|
|
"abhaId": "12434949797979",
|
|
|
|
|
"birthYear": "2000",
|
|
|
|
|
"bloodGroup": "",
|
|
|
|
|
"careOf": "",
|
|
|
|
|
"caste": "",
|
|
|
|
|
"city": "Bengaluru",
|
|
|
|
|
"district": "Bengaluru",
|
|
|
|
|
"gender": "Male",
|
|
|
|
|
"house": "42",
|
|
|
|
|
"maritalStatus": "Single",
|
|
|
|
|
"name": name.stringValue,
|
|
|
|
|
"phoneNumber": "9123456789",
|
|
|
|
|
"pinCode": "560060",
|
|
|
|
|
"registeredBy": "admin",
|
|
|
|
|
"registrationCenterName": "SMI",
|
|
|
|
|
"sickleCellHistory": null,
|
|
|
|
|
"state": "Karnataka",
|
|
|
|
|
"subCaste": "",
|
|
|
|
|
"testStatus": false,
|
|
|
|
|
"underMedication": false,
|
|
|
|
|
"underTransfusion": false,
|
|
|
|
|
"userImageURL": "https://hpos-prod.web.app/logo.png"
|
|
|
|
|
}, {
|
2023-09-26 23:56:45 +05:30
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
"x-client": "e16a1bd15af2ee640f5a7a18c8d8333f5f01f7c66e003bbc21ee52870f1bce27"
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-09-27 09:37:56 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Triggered by a change to a Firestore document.
|
|
|
|
|
*
|
|
|
|
|
* @param {!Object} event Event payload.
|
|
|
|
|
* @param {!Object} context Metadata for the event.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const admin = require("firebase-admin");
|
2023-11-06 20:28:00 +05:30
|
|
|
const moment = require('moment');
|
2023-09-27 09:37:56 +05:30
|
|
|
|
|
|
|
|
admin.initializeApp();
|
|
|
|
|
|
|
|
|
|
exports.retestMarking = async (event, context) => {
|
2023-09-30 14:01:44 +05:30
|
|
|
const resource = context.resource;
|
|
|
|
|
// log out the resource string that triggered the function
|
2023-10-23 12:58:38 +05:30
|
|
|
console.log('Function triggered by change to: ' + resource);
|
2023-09-30 14:01:44 +05:30
|
|
|
// now log the full event object
|
|
|
|
|
console.log(JSON.stringify(event));
|
|
|
|
|
const { _id, classificationResult } = event.value.fields;
|
|
|
|
|
console.log("classificationResult", classificationResult.stringValue);
|
2023-11-06 20:28:00 +05:30
|
|
|
if (classificationResult?.stringValue?.includes("Positive Borderline") || classificationResult?.stringValue?.includes("Inconclusive") || classificationResult?.stringValue?.includes("Trait")) {
|
2023-10-23 12:58:38 +05:30
|
|
|
const docs = await admin.firestore().collection("patientData").where("_id", "==", _id.stringValue).get();
|
|
|
|
|
docs?.forEach(async (doc) => {
|
|
|
|
|
await admin.firestore().collection("patientData").doc(doc.id).update({
|
|
|
|
|
testStatus: false,
|
|
|
|
|
retested: true
|
|
|
|
|
// incubationTime: admin.firestore.FieldValue.delete()
|
2023-09-30 14:01:44 +05:30
|
|
|
});
|
2023-10-23 12:58:38 +05:30
|
|
|
})
|
2023-10-23 03:23:50 +05:30
|
|
|
}
|
2023-11-06 20:28:00 +05:30
|
|
|
|
|
|
|
|
const { _idSearch, abhaIdSearch, aadharIdSearch } = event.value.fields;
|
|
|
|
|
if (!_idSearch?.stringValue) {
|
|
|
|
|
const docs = await admin.firestore().collection("patientData").where("_id", "==", _id.stringValue).get();
|
|
|
|
|
docs?.forEach(async (doc) => {
|
|
|
|
|
await admin.firestore().collection("patientData").doc(doc.id).update({
|
|
|
|
|
_idSearch: `${moment().format('YYYYMMDD')}${_id}`,
|
|
|
|
|
abhaIdSearch: `${moment().format('YYYYMMDD')}${abhaId}`,
|
|
|
|
|
aadharIdSearch: `${moment().format('YYYYMMDD')}${aadharId}`,
|
|
|
|
|
});
|
|
|
|
|
console.log('search logic', _id.stringValue);
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-10-23 12:58:38 +05:30
|
|
|
};
|
2023-11-06 20:28:00 +05:30
|
|
|
|
2023-12-10 13:46:29 +05:30
|
|
|
/**
|
|
|
|
|
* Triggered by a change to a Firestore document.
|
|
|
|
|
*
|
|
|
|
|
* @param {!Object} event Event payload.
|
|
|
|
|
* @param {!Object} context Metadata for the event.
|
|
|
|
|
*/
|
|
|
|
|
const axios = require('axios');
|
|
|
|
|
const admin = require("firebase-admin");
|
|
|
|
|
const moment = require('moment');
|
|
|
|
|
|
|
|
|
|
admin.initializeApp();
|
|
|
|
|
|
|
|
|
|
exports.sampleClassification = async (event, context) => {
|
|
|
|
|
const resource = context.resource;
|
|
|
|
|
// log out the resource string that triggered the function
|
|
|
|
|
console.log('Function triggered by change to: ' + resource);
|
|
|
|
|
// now log the full event object
|
|
|
|
|
console.log(JSON.stringify(event));
|
|
|
|
|
|
|
|
|
|
const { _id, calculatedRatio, deviceRatio, led1Buffer, led2Buffer, led1Sample, led2Sample} = event.value.fields;
|
|
|
|
|
const predictionResponse = await axios.post("https://asia-south1-hpos-af3cc.cloudfunctions.net/prediction",
|
|
|
|
|
{
|
|
|
|
|
"_id": `${_id.stringValue}`,
|
|
|
|
|
"calculatedRatio": calculatedRatio.stringValue || calculatedRatio.doubleValue,
|
|
|
|
|
"deviceRatio": deviceRatio.stringValue || deviceRatio.doubleValue || deviceRatio.integerValue,
|
|
|
|
|
"led1Buffer": led1Buffer.stringValue || led1Buffer.doubleValue || led1Buffer.integerValue,
|
|
|
|
|
"led2Buffer": led2Buffer.stringValue || led2Buffer.doubleValue || led2Buffer.integerValue,
|
|
|
|
|
"led1Sample": led1Sample.stringValue || led1Sample.doubleValue || led1Sample.integerValue,
|
|
|
|
|
"led2Sample": led2Sample.stringValue || led2Sample.doubleValue || led2Sample.integerValue
|
|
|
|
|
}, {
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
"x-client": "e16a1bd15af2ee640f5a7a18c8d8333f5f01f7c66e003bbc21ee52870f1bce27"
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
const { predictedClass } = predictionResponse.data;
|
|
|
|
|
console.log("predictedClass", predictedClass);
|
|
|
|
|
const docs = await admin.firestore().collection("testData").where("_id", "==", _id.stringValue).get();
|
|
|
|
|
docs?.forEach(async (doc) => {
|
|
|
|
|
await admin.firestore().collection("testData").doc(doc.id).update({
|
|
|
|
|
predictedClass: predictedClass,
|
|
|
|
|
modelId: "1",
|
|
|
|
|
modelVersion: "1.0.1"
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
};
|