不到一个小时的时间,就有超过10,000次读取的两项云功能?

时间:2018-07-09 18:55:32

标签: firebase google-cloud-firestore google-cloud-functions

当前,我正在运行两个云功能。一个添加信息到帖子,另一个删除旧帖子,例如:

exports.reveal = functions.database.ref('/reveals/{postIDthatWasRevealed}/revealed').onUpdate((change, context) => {
const revealedValue = change.after.val()

if (revealedValue === true) {
   var updates = {}
   const postID = context.params.postIDthatWasRevealed
    console.log(postID)
   return admin.firestore().collection('posters').doc(postID).get().then(snapshot => {
        const value = snapshot.data()
        console.log(value)
       // console.log(value)
        const posterID = value.posterID
        const posterName = value.posterName
        const profileImage = value.profileImage
        const postKey = value.key
        return admin.database().ref('/convoID/' + postID).once('value', (snapshot) => {
        if (snapshot.exists()) {
           const convoIDCollection = snapshot.val()
           for (var child in convoIDCollection) {
               const convoID = child

               updates["/conversations/"+convoID+"/information/reciever/Name"] = posterName
               updates["/conversations/"+convoID+"/information/reciever/profileImage"] = profileImage
               updates["/conversations/"+convoID+"/key"] = postKey
           }
        }
           const currentTime = Date.now()
           const addedTime = currentTime + 172800000

           const batch = admin.firestore().batch()
           const postFireStoreRef = admin.firestore().collection('posts').doc(postID)

           batch.update(postFireStoreRef,{"revealedDate": currentTime})
           batch.update(postFireStoreRef,{"timeOfDeletion": addedTime})
           batch.update(postFireStoreRef,{"information": {"posterID":posterID,"posterName":posterName,"profileImage":profileImage} })
           batch.update(postFireStoreRef,{"key":postKey})

           return batch.commit(), admin.database().ref().update(updates)
        })
   })
}
else {
    return null
}
})

^当该帖子获得足够的点赞后,该信息就会添加到该帖子中。这些帖子存储在Firestore中,并且当与该帖子关联的Firebase节点获得足够的点赞时,将下载一些信息并附加到firestore帖子实体中。从理论上讲,它甚至不应读取一次,因为从Firestore实体获取的数据永远不会下载,而只会被修改。运行的第二个功能如下:

exports.hourly_job = functions.pubsub.topic('hourly-tick').onPublish((change,context) => {
    const currentTime = Date.now()
    const getPostsForDate = admin.firestore().collection('posts').where('timeOfDeletion', '<', currentTime)
    return getPostsForDate.get().then(snapshot => {
        const updates = {}
        const batch = admin.firestore().batch()
        snapshot.forEach((doc) => {
            var key = doc.id
            console.log(key)
            const convos =  database().ref('/convoID/' + key).once('value', (snapshot) => {
                 if (snapshot.exists){
                    for (var child in snapshot) {
                        const convoID = child
                        console.log(child+"shit")
                        updates["conversations/" + value] = null
                    }
                 }
             })
             updates["/convoID/"+ key] = null
             updates["/reveals/" + key] = null
             updates["/postDetails/" + key] = null
             const postFireStoreRef = admin.firestore().collection('posts').doc(key)
             const posterRef = admin.firestore().collection('posters').doc(key)
             batch.delete(postFireStoreRef)
             batch.delete(posterRef)
        })
        return admin.database().ref().update(updates), batch.commit()
})
})

每分钟,此查询器会查询消防站中的旧帖子。最多,它可能会返回两到三个帖子,从而导致一些阅读。但是,在对这些功能进行了一个小时的测试之后,Google App Engine配额显示了一万次阅读,而我预计将接近二十到五十。此外,功能一整天只部署了87次。这些功能是否没有优化?有没有办法监视读取操作的来源?

编辑:似乎每次我触发删除功能(实际上是更改帖子的时间戳,以便每当调用删除功能时将其删除),我的阅读次数都会增加数百。

0 个答案:

没有答案