From 49db71a98132b7a5613d71eb534136f2f6dc976b Mon Sep 17 00:00:00 2001 From: Pritimay Sarkar Date: Tue, 26 Sep 2023 04:36:51 +0530 Subject: [PATCH] add data regularity function --- cloud-functions/nodejs/functions/index.js | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/cloud-functions/nodejs/functions/index.js b/cloud-functions/nodejs/functions/index.js index 7eaa413..4e60d17 100644 --- a/cloud-functions/nodejs/functions/index.js +++ b/cloud-functions/nodejs/functions/index.js @@ -160,3 +160,43 @@ exports.kitAlert = async (event, context) => { } } }; + + +/** + * 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); + if (led1Buffer?.stringValue > 27000 || led2Buffer?.stringValue > 28000 || led1Sample?.stringValue ) { + 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") + }); + } +};