Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
G
group23_com2027
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Endrizzi, Marco (UG - Comp Sci & Elec Eng)
group23_com2027
Commits
242cd2bc
Commit
242cd2bc
authored
4 years ago
by
Endrizzi, Marco (UG - Comp Sci & Elec Eng)
Browse files
Options
Downloads
Patches
Plain Diff
Added cloud functions to remove old signals/events
parent
80963082
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
functions/index.js
+38
-8
38 additions, 8 deletions
functions/index.js
with
38 additions
and
8 deletions
functions/index.js
+
38
−
8
View file @
242cd2bc
// 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
);
});
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment