Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Cloud Firestore to the App #5001

Closed
6 of 7 tasks
adhiamboperes opened this issue May 29, 2023 · 1 comment · Fixed by #5098
Closed
6 of 7 tasks

Add Cloud Firestore to the App #5001

adhiamboperes opened this issue May 29, 2023 · 1 comment · Fixed by #5098
Assignees
Labels
enhancement End user-perceivable enhancements. Impact: High High perceived user impact (breaks a critical feature or blocks a release). Work: High It's not clear what the solution is.

Comments

@adhiamboperes
Copy link
Collaborator

adhiamboperes commented May 29, 2023

Add Firebase Firestore

This issue tracks milestone 7 of the NPS Survey.

Writing to Firestore

Collections and Documents in Firestore are created implicitly on first write. For our use case, we are creating collections at the root level of our database to organize unique data sets. This means we are going to have one collection with the name nps_survey_open_feedback for storing answers to the open-ended survey responses, and should we want to store different data in the future, we will create a new collection for it.

We will add the following data to the document:

  • survey_id: String. This is the id of the survey with which this free text answer is associated.
  • open_feedback_answer: String. The value of the answer provided by the user.
  • time_submitted: Long. We are storing a timestamp as a field in the documents to use for ordering in queries. We will also mark this field as a TTL field for automatic data deletion per the retention policy.

We are using the Firestore add() method as opposed to set() in order for Firestore to auto generate our document Ids, which need not be meaningful. This will also enforce uniqueness during write so that a user is unable to overwrite any other user’s document.

Authenticate with Firebase anonymously

We are implementing anonymous auth in the background to allow users to be authenticated with Firebase Security Rules.

Tasks

  • Configure Anonymous Auth
  • Configure Firestore upload
  • Test security rules
  • Test offline auth, storage and upload
@Gdakgdak22

This comment was marked as spam.

@adhiamboperes adhiamboperes added Work: High It's not clear what the solution is. and removed Work: Medium The means to find the solution is clear, but it isn't at good-first-issue level yet. labels Jan 26, 2024
adhiamboperes added a commit that referenced this issue Jan 26, 2024
## Explanation
Fixes #5001.
This is PR 5 of 6 Planned PRs.

We are adding a whole new infrastructure for uploading data to Firebase
Firestore.

### Key Changes
- Introducing a `FirestoreDataController` that handles Anonymous
Authentication and logging to Firestore, based on connectivity status.
- Adding the Firestore, Auth and AppCheck dependencies, and reppining
these maven dependencies.
- New event logger for converting event logs to documents(hashmaps) and
uploading them to firestore.
- Introduce AuthenticationController
This new controller provides functionality to retrieve the current
signed in user and also sign in a user anonymously, and can be called
from anywhere. It has a fake that is to be used in tests.
- Adding the newly created `utility` module files to `CODEOWNERS`
- Creating wrappers for FirebaseAuth, and Firestore in order to allow
switching out with fakes in tests and development versions using dagger.

The implementation relies on an AuthenticationWrapper to switch out the
real and fake authentication controllers for testing purposes. To do
this, I created a dagger module to provide the correct listener where
needed, hence the test file changes in the _app_ module tests.

### Technical Decisions
- I opted to create a new cache for events that will eventually be
uploaded to Firestore. This means they will not be accidentally uploaded
to Firebase.
- I opted to use EventLogs and convert them to Documents on the fly as
opposed to creating an entirely new model and logging infrastruture that
is Firestore-specific.
- I calculated the `FirestoreLogStorageCacheSize` based on the [average
length of a Google Review
](https://www.meetsoci.com/resources/insights/brands-franchises/state-of-google-reviews/#:~:text=Average%20review%20length%20ranges%20from,per%20review%20for%20department%20stores.&text=In%20comparison%20to%20other%20local%20platforms%2C%20Google%20reviews%20skew%20positively.)and
the [storage size computation
specs](https://firebase.google.com/docs/firestore/storage-size) defined
by Firebase. I computed it based on 10 profiles on a device over a 3
month period.
- I opted to use existing log upload infrastructure to upload to
firebase, as opposed to creating a whole new upload workflow so I can
reuse the existing test infrastructure as well.

### Dependency Updates
- I added dependencies for Firestore and Auth. The selected versions
were both the earliest compatible with the rest of our existing
dependencies and the latest that didn't cause proguarding issues. There
were incidental updates to transitive dependencies, both major and
minor.

### Proguard
Added new -dontwarns for the following:
`-dontwarn javax.naming.**`
[Reference](https://github.com/firebase/firebase-android-sdk/blob/00d4626/firebase-firestore/proguard.txt.)

## Essential Checklist
<!-- Please tick the relevant boxes by putting an "x" in them. -->
- [x] The PR title and explanation each start with "Fix #bugnum: " (If
this PR fixes part of an issue, prefix the title with "Fix part of
#bugnum: ...".)
- [x] Any changes to
[scripts/assets](https://github.com/oppia/oppia-android/tree/develop/scripts/assets)
files have their rationale included in the PR explanation.
- [x] The PR follows the [style
guide](https://github.com/oppia/oppia-android/wiki/Coding-style-guide).
- [x] The PR does not contain any unnecessary code changes from Android
Studio
([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#undo-unnecessary-changes)).
- [x] The PR is made from a branch that's **not** called "develop" and
is up-to-date with "develop".
- [x] The PR is **assigned** to the appropriate reviewers
([reference](https://github.com/oppia/oppia-android/wiki/Guidance-on-submitting-a-PR#clarification-regarding-assignees-and-reviewers-section)).

## Upload Screenshots
### Firestore Upload
<img width="1130" alt="Screenshot 2024-01-23 at 11 18 26"
src="https://github.com/oppia/oppia-android/assets/59600948/c555c283-06e5-471d-9084-b9a3b30f8592">

## Querying Synced Firestore Data on BigQuery
<img width="1071" alt="Screenshot 2024-01-23 at 11 26 26"
src="https://github.com/oppia/oppia-android/assets/59600948/3ba78102-5521-4cb7-bd69-6ede5d777e7d">

<img width="1071" alt="Screenshot 2024-01-23 at 11 26 44"
src="https://github.com/oppia/oppia-android/assets/59600948/46579afe-3c7b-4d5d-8bcf-a4e133a5f609">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement End user-perceivable enhancements. Impact: High High perceived user impact (breaks a critical feature or blocks a release). Work: High It's not clear what the solution is.
3 participants