Firestore creating a day total for data entries

Viewed 179

Hi I am new to Firestore and working with a Swift project. My aim is to have an insert of data, which the user will insert.

However I want to be able to achieve a day total for each day I enter a new value and then update this value every time I insert a new entry. Would this be able to be done through firebase functions or will I require another route which is less expensive to achieve this. Sorry if this doesn't make sense I will try to provide some samples of what I am trying to achieve:

So for instance I will be making an entry which will follow this data format in JSON.

{
   [userId] : [{ 
        trackingData:[
             { [random id]: { value: 15 }, timestamp: 22 Feb 2022 },
             { [random id]: { value: 10 }, timestamp: 22 Feb 2022  },
        ]
   }]
}

My question is basically is there a way to have calculate the total day value of the tracking data per day. Using Firestore.

Edit...

The structure is quite simple as shown above when a user enters a record its saved on to its user id as reference. Then placed in its own trackingData collection. Where the results are then saved in a form of an array, where each entry is a single object.

2 Answers

A simple solution that would avoid using cloud functions is to just create a collection called "DailyTotals" whose documents would be the daily totals each day.

Then all you have to do is any time a user inserts a new value, just fetch the daily total for that day, add the latest value, then update Firebase. It would cost you an extra read and write, but it would save you from having to mess with cloud functions.

What is understood from the question is that you have a simple JSON for each user. You are tracking user activity in this JSON under the field trackingData. This trackingData Array is appended one element of each user activity event, and this event has some event data and timestamp attached to it. Now, you want to count user activities/day. IMO, The question is missing a key detail about your use case that is "When do you want to update this count (aka SLA)?"

  1. As soon as the event is registered? A cloud function will be the most suitable option here, Just listen for updated documents and trigger the function to upsert this particular day's count by one. This is expensive as you are going to read and update the same document every single time a user event occurs.
  2. End of the day? Run a cron job on or after 23:59:59 and calculate the last day's count for each user. You are going to read and update the aggregate document only once a day.
  3. Some other time? Same as the end of day one but the cron schedule will be different. You will further optimize reads/updates to your SLA.

DO NOT RELY on the client to do this calculation, because:

  1. If you are sending an event and count update in the same request, it is an unnecessary load and it will fail if one of those two actions fails.

  2. If you are sending two independent requests there will be chances of inconsistency if one of those two will fail.

Cheers!

Related