Implementing a cloud function that can update the uniqueViewCount
field in the recording document given passed in parameters of viewerId
and recordingId
with the following requirements:
- Needs to be able to handle significant amounts of views within a day (if recording goes viral)
- Only counts unique views (one view per user on a recording)
- Must update data in such a way that a same viewer opening a same recording on different devices would only increment the
uniqueViewCount
once
- ViewerIds and RecordingIds are unique
- The
trackRecordingView
cloud function is called by the client every time any user opens any recording in the mobile app
- Added a collection of
UniqueView
documents intypes.ts
for all the unique views of all recordings- This allows us to handle significant amounts of writes to the
UniqueView
documents of recordings (given the 1MB limit of data per document)
- This allows us to handle significant amounts of writes to the
- Used a transaction to handle read/writes to
User
,Recording
andUniqueView
documents- This ensures consistency between concurrent writes to a document (if a recording goes viral) as it ensures that each subsequent write sees the contents of the document before it is written
- This ensures that if two requests for the same recording are made by the same viewer simultaneously they will not be double counted
- However, transactions fail when the client is offline though that should not be an issue in this particular case
- Bonus from
track-recording-views.ts
: For the purpose of readability and maintenance, I factored out the interfaces and enum fromtrack-recording-views.ts
and placed them all intypes.ts
. - Bonus from
index.ts
: Validated thatviewerId
andrecordingId
are strings (not null or of another type) and are non-empty.
- You will need to have Node.js and npm installed. If you do not, visit Download Node.js and install the latest version of npm with
npm install npm@latest -g
. Ensure that both are correctly installed usingnode -v
andnpm -v
. - Clone the repository with
git clone https://github.com/JuliaGrandury/Views-Tracking-Challenge-Siro
- install the firebase CLI with
npm install -g firebase-tools
- navigate to the functions directory and install dependencies with
cd functions && npm i
- initiate and run the emulator with
npm run serve
(from the functions directory)
Could account for the fact that users might briefly open the recording but not view the majority of it and therefore should not be counted as viewers in the uniqueViewCount or uniqueRecordingViewCount.