Reputation: 387
Currently, my database is set up such that there are posts, conversations, and postDetails.
"posts" : {
"-LG57gaS08hS8WsuU6j2" : {
"Revealed" : "true",
"datePosted" : "2018-06-28 11:24:02 +0000",
"key" : "GCrjH2E3pDuW7dEGl8",
"post" : "Lolllyooo",
"poster" : "CAD54A563CAB99107D9BBDB7F2234FA3",
"revealedDate" : 1530185042340,
"reveals" : 0,
"revealsRequired" : 3,
"timeOfDeletion" : 1530271442340,
"watchedBy" : {
"BmVot3XHEpYwMNtiucWSb8XPPM42" : "false",
"Ih5m9VUnJnewKvqiZCVgBFwCFrz1" : "false",
"NMo1gUPKWFcdhsrnCbKte7JfrcA2" : "false",
"dlwFYqlu2mgetB5zO6TNmFGBWcb2" : "false"
}
}
Poster information:
"posters" : {
"-LFzT4c6ylIcPne9F7QS" : {
"posterID" : "BmVot3XHEpYwMNtiucWSb8XPPM42",
"posterName" : "Jibran Khalil",
"profileImage" : "nil"
}
Conversations:
"conversations" : {
"-LFzccEzciNPSTFAZAhb" : {
"49C91D37EE1C4B3E07FE24FEBE9ED72B" : "true",
"CAD54A563CAB99107D9BBDB7F2234FA3" : "true",
"Date" : "2018-06-27 05:06:12 +0000",
"convoID" : "-LFzccEzciNPSTFAZAhb",
"created_at" : 1.5300759725991712E9,
"last_message" : "lesseee",
"last_message_time" : 1530077715525,
"postID" : "-LFzT4c6ylIcPne9F7QS",
"status" : "sent",
"timeOfDeletion" : 1530159609351
},
When a post's revealed status is changed from false to true, a cloud function is run that collects the post's information and appends it to the post, as such:
exports.checkIfRevealNumberIsEnough = functions.database.ref('/posts/{postIDthatWasRevealed}/Revealed').onUpdate((event) => {
const revealedValue = event.data.val()
if (revealedValue === "true") {
var updates = {}
const postID = event.params.postIDthatWasRevealed
const revealedConnection = admin.database().ref('/posts/'+postID).once('value', (snapshot) => {
const postDetails = snapshot.val()
const key = postDetails["key"]
const watchedBy = postDetails["watchedBy"]
const posterDetailsReference = admin.database().ref('/posters/'+ postID).once('value', (snapshot) => {
const posterDetails = snapshot.val()
const posterID = posterDetails.posterID
const posterName = posterDetails.posterName
var posterPic = ''
if (posterDetails.profileImage) {
posterPic = posterDetails.profileImage
}
else {
posterPic = "nil"
}
const currentTime= Date.now()
const addedTime = currentTime + 172800000
updates["/posts/"+postID+"revealedDate"] = currentTime
updates["/posts/"+postID+"timeOfDeletion"] = addedTime
updates["/posts/"+postID+"/information/posterID"] = posterID
updates["/posts/"+postID+"/information/posterName"] = posterName
updates["/posts/"+postID+"/information/profileImage"] = posterPic
for (var child in watchedBy) {
if (watchedBy[child] !== "false") {
const conversationID = watchedBy[child]
updates["/conversations/"+conversationID+"/information/reciever/Name"] = posterName
updates["/conversations/"+conversationID+"/information/reciever/profileImage"] = posterPic
updates["/conversations/"+conversationID+"key"] = key
}
}
});
});
return admin.database().ref().update(updates)
}
else {
return null
}
});
The data download usage should not be too massive, as I only download the post's information from the posters child and the post itself - maybe a few hundred bytes, or a small number of kilobytes, at most. Instead, the database profile run in my terminal, when the function runs, show that all the children in the database are being read and that 87 kb are downloaded, as such:
Downloaded Bytes
┌──────────────────────────────────────────┬──────────┬───────┬──────────┐ │ Path │ Total │ Count │ Average │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ / │ 87.99 kB │ 6 │ 14.67 kB │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /posts/-LG57kMo35XkrfCmVIGA │ 438 B │ 2 │ 219 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /posts/-LG57kMo35XkrfCmVIGA/Revealed │ 99 B │ 2 │ 49.5 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /posters/-LG57kMo35XkrfCmVIGA │ 93 B │ 1 │ 93 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /posts/-LG57gaS08hS8WsuU6j2/watchedBy │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /posts/-LG57kMo35XkrfCmVIGA/watchedBy │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /posts/-LG5Bxy5otbLgs-p43co/watchedBy │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /posters/-LG5Bxy5otbLgs-p43co │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /posts/-LG57gaS08hS8WsuU6j2 │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /posts/-LG5Bxy5otbLgs-p43co │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /reveals/-LFzJVQyQiDwTPMLTMlC │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /reveals/-LFzK2M1Tu5c2PJTfyxu │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /reveals/-LFzT4c6ylIcPne9F7QS │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /reveals/-LFzd-uN4kRfU18TBj0N │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /shortNames/NnB8iVzVOAg5gYtt5FAzLYuuaDO2 │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /users/$wildcard │ 0 B │ 26 │ 0 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /messages │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /posters │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /posts │ 0 B │ 2 │ 0 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /reveals │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /shortNames │ 0 B │ 1 │ 0 B │ ├──────────────────────────────────────────┼──────────┼───────┼──────────┤ │ /users │ 0 B │ 1 │ 0 B │ └──────────────────────────────────────────┴──────────┴───────┴──────────┘
Furthermore, there are no listed unindexed queries. I have tried to minimize the amount of data being download by minimizing references, and yet there is massive data downloaded and 84 operations recorded when the cloud function is run. What could be going on? Is the conversations update incorrect? How can I reduce the data download usage and for what reasons is it downloading almost 100 kilobytes on each invocation?
UPDATE: Additionally, there is a cron job run every minute. Could this be the possible culprit? There is only one reference and it uses once and not on value.
exports.hourly_job = functions.pubsub.topic('hourly-tick').onPublish((event) => {
const currentTime = Date.now()
const getPostsForDate = admin.database().ref('posts').orderByChild('timeOfDeletion').endAt(currentTime);
return getPostsForDate.once('value', (snapshot) => {
var updates = {};
var convoUpdates = {};
snapshot.forEach((childSnapshot) => {
const postDetails = childSnapshot.val();
const postIDtoDelete = childSnapshot.key
const peopleWatching = postDetails["watchedBy"]
console.log(peopleWatching)
updates[postIDtoDelete] = null
for (var childKey in peopleWatching) {
let value = peopleWatching[childKey]
if (value !== "false") {
convoUpdates[value] = null
}
}
})
admin.database().ref('posts').update(updates)
admin.database().ref('conversations').update(convoUpdates)
})
});
Upvotes: 0
Views: 67
Reputation: 38289
Add an index for posts
on timeOfDeletion
:
{
"rules": {
...
"posts": {
".indexOn": ["timeOfDeletion"]
}
...
}
}
Upvotes: 1