Skip to content
Snippets Groups Projects
Commit 242cd2bc authored by Endrizzi, Marco (UG - Comp Sci & Elec Eng)'s avatar Endrizzi, Marco (UG - Comp Sci & Elec Eng)
Browse files

Added cloud functions to remove old signals/events

parent 80963082
No related branches found
No related tags found
No related merge requests found
// const functions = require("firebase-functions");
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
// functions.logger.info("Hello logs!", {structuredData: true});
// response.send("Hello from Firebase!");
// });
/* Delete signals older than 15m */
exports.deleteOldSignals = functions.pubsub
.schedule("* * * * *").onRun(() => {
const ref = admin.database().ref("signals"); // reference to the items
const now = Date.now();
const cutoff = now - 2 * 60 * 1000; // 2m
const oldItemsQuery = ref.orderByChild("timestamp").endAt(cutoff);
return oldItemsQuery.once("value", function(snapshot) {
// create a map with all children that need to be removed
const updates = {};
snapshot.forEach(function(child) {
updates[child.key] = null;
});
// execute all updates in one go and return the result
return ref.update(updates);
});
});
/* Delete events older than 5 hours */
exports.deleteOldEvents = functions.pubsub
.schedule("0 * * * *").onRun(() => {
const ref = admin.database().ref("events"); // reference to the items
const now = Date.now();
const cutoff = now - 5 * 60 * 60 * 1000;
const oldItemsQuery = ref.orderByChild("timestamp").endAt(cutoff);
return oldItemsQuery.once("value", function(snapshot) {
// create a map with all children that need to be removed
const updates = {};
snapshot.forEach(function(child) {
updates[child.key] = null;
});
// execute all updates in one go and return the result
return ref.update(updates);
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment