Skip to content

Commit 3d044ed

Browse files
Technical Analytics: Milestone 4 - Document Technical Analytics Changes (oppia#5353)
<!-- READ ME FIRST: Please fill in the explanation section below and check off every point from the Essential Checklist! --> ## Explanation <!-- - Explain what your PR does. If this PR fixes an existing bug, please include - "Fixes #bugnum:" in the explanation so that GitHub can auto-close the issue - when this PR is merged. --> When merged, this PR will; - Modify the Platform Parameter and Feature Flags documentation to reflect changes introduced by the Technical Analytics Milestones 1, 2 and 3 PRs. ## 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)). ## For UI-specific PRs only <!-- Delete these section if this PR does not include UI-related changes. --> If your PR includes UI-related changes, then: - Add screenshots for portrait/landscape for both a tablet & phone of the before & after UI changes - For the screenshots above, include both English and pseudo-localized (RTL) screenshots (see [RTL guide](https://github.com/oppia/oppia-android/wiki/RTL-Guidelines)) - Add a video showing the full UX flow with a screen reader enabled (see [accessibility guide](https://github.com/oppia/oppia-android/wiki/Accessibility-A11y-Guide)) - For PRs introducing new UI elements or color changes, both light and dark mode screenshots must be included - Add a screenshot demonstrating that you ran affected Espresso tests locally & that they're passing --------- Co-authored-by: Adhiambo Peres <[email protected]>
1 parent 4aa5337 commit 3d044ed

File tree

1 file changed

+132
-14
lines changed

1 file changed

+132
-14
lines changed

wiki/Platform-Parameters-&-Feature-Flags.md

Lines changed: 132 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
- [Introduction](#introduction)
44
- [How to create a Platform Parameter](#how-to-create-a-platform-parameter)
5-
- [How to consume a Platform Parameter](#how-to-consume-a-platform-parameter)
5+
- [How to create a Feature Flag](#how-to-create-a-feature-flag)
6+
- [How to consume a Platform Parameter or Feature Flag](#how-to-consume-a-platform-parameter-or-feature-flag)
7+
- [Ensuring your Feature Flags are logged on each app session](#ensuring-your-feature-flags-are-logged-on-each-app-session)
68
- [How to write tests related Platform Parameter](#how-to-write-tests-related-platform-parameter)
79
- [1. We actually don't test for platform parameter(s)](#1-we-actually-dont-test-for-platform-parameters)
810
- [2. We test for different values of platform parameter(s)](#2-we-test-for-different-values-of-platform-parameters)
@@ -14,13 +16,13 @@ In order to release these types of features in a smooth manner, we need to be ab
1416

1517
## How to create a Platform Parameter
1618
1. Create the Constants
17-
- If the Platform Parameter you intend to create is related to a particular feature, so first check that do there exist a file in the `utility\src\main\java\org\oppia\android\util\platformparameter` which contains other Platform Parameters related to the same feature. If there is no such then create a new Kotlin file along with its name corresponding to the feature.
18-
- After searching/making a "constants" file related to a feature, we need to define three things:
19+
- Platform parameters are typically stored inside `utility\src\main\java\org\oppia\android\util\platformparameter` in the `PlatformParameterConstants.kt`.
20+
- To create a new platform parameter, we need to define three things:
1921
1. Qualifier Annotation which will help us to distinguish our Platform Parameter from others.
2022

2123
<br>
2224

23-
```
25+
```kotlin
2426
/**
2527
* Qualifier for the platform parameter that defines the time period in hours, after which the
2628
* [PlatformParameterSyncUpWorker] will run again.
@@ -33,7 +35,7 @@ In order to release these types of features in a smooth manner, we need to be ab
3335

3436
<br>
3537

36-
```
38+
```kotlin
3739
/**
3840
* Name of the platform parameter that defines the time period in hours, after which the
3941
* [PlatformParameterSyncUpWorker] will run again.
@@ -45,7 +47,7 @@ In order to release these types of features in a smooth manner, we need to be ab
4547

4648
<br>
4749

48-
```
50+
```kotlin
4951
/**
5052
* Default value of the platform parameter that defines the time period in hours, after which the
5153
* [PlatformParameterSyncUpWorker] will run again.
@@ -59,7 +61,7 @@ In order to release these types of features in a smooth manner, we need to be ab
5961

6062
<br>
6163

62-
```
64+
```kotlin
6365
/* Dagger module that provides values for individual Platform Parameters. */
6466
@Module
6567
class PlatformParameterModule {
@@ -83,29 +85,145 @@ Note: If the Platform Parameter that you are creating will only be a Compile Tim
8385
- Note that permission will be required before accessing the Feature Gating console in the Oppia backend.
8486

8587

86-
## How to consume a Platform Parameter
87-
To consume a Platform Parameter in any file, we need to inject the specific `PlatformParameterValue\<T\>` instance along with the Qualifier Annotation defined for that Parameter. For eg - we are injecting the `SyncUpTimePeriodInHours` platform parameter in `PlatformParameterSyncUpWorkManagerInitializer`
88+
## How to create a Feature Flag
89+
1. Create the Constant
90+
- Feature flags, like platform parameters, are stored inside `utility\src\main\java\org\oppia\android\util\platformparameter` in the `FeatureFlagConstants.kt` file.
91+
- To add new feature flags, we need to define three things:
92+
1. Qualifier Annotation which will help us to distinguish our Platform Parameter from others. Each feature flag should be prepended with an `Enable` prefix to distinguish it from platform parameters.
93+
94+
<br>
8895

89-
```
96+
```kotlin
97+
/**
98+
* Qualifier for the [EnableAppAndOsDeprecation] feature flag that controls whether to enable
99+
* app and OS deprecation or not.
100+
*/
101+
@Qualifier
102+
annotation class EnableAppAndOsDeprecation
103+
```
104+
105+
2. The name of the Feature Flag in String format. This should also be prefixed with `android_enable_` to distinguish it from other feature flags on the Oppia-Web gating console.
106+
107+
<br>
108+
109+
```kotlin
110+
/** Name of the feature flag that controls whether to enable app and os deprecation. */
111+
const val APP_AND_OS_DEPRECATION = "android_enable_app_and_os_deprecation"
112+
```
113+
114+
3. The default value for the Feature Flag. For eg - here we define a `EnableAppAndOsDeprecation` feature flag and its default value as a constant.
115+
116+
<br>
117+
118+
```kotlin
119+
/**
120+
* Default value for the feature flag corresponding to [EnableAppAndOsDeprecation].
121+
*/
122+
const val ENABLE_APP_AND_OS_DEPRECATION_DEFAULT_VALUE = false
123+
```
124+
125+
2. Provide your Feature Flag
126+
- Feature Flags are still a special type of Platform Parameter and are provided in a similar manner. For providing your Feature Flag in the App, we need to first make a @Provides annotated method in the `PlatformParameterModule(domain\src\main\java\org\oppia\android\domain\platformparameter\PlatformParameterModule.kt)`
127+
- Since feature flags can only be booleans, The return type for this @Provides annotated method will be equal to `PlatformPrameterValue<Boolean>`. Any other type will cause the platform parameter sync to fail. For eg- here we provide `EnableAppAndOsDeprecation` feature flag.
128+
129+
<br>
130+
131+
```kotlin
132+
/* Dagger module that provides values for individual Platform Parameters. */
133+
@Module
134+
class PlatformParameterModule {
135+
...
136+
@Provides
137+
@EnableAppAndOsDeprecation
138+
fun provideEnableAppAndOsDeprecation(
139+
platformParameterSingleton: PlatformParameterSingleton
140+
): PlatformParameterValue<Boolean> {
141+
return platformParameterSingleton.getBooleanPlatformParameter(APP_AND_OS_DEPRECATION)
142+
?: PlatformParameterValue.createDefaultParameter(
143+
ENABLE_APP_AND_OS_DEPRECATION_DEFAULT_VALUE
144+
)
145+
}
146+
}
147+
```
148+
149+
3. Add Feature Flags to Feature Gating Console
150+
- All feature flags should be added to the feature flags console to allow them to be remotely enabled or disabled.
151+
- Add the name and the value of our Platform Parameter. This change will make our Compile-Time Platform Parameter to be a Run-Time Platform Parameter. This means that we can control its value from backend.
152+
- Note that permission will be required before accessing the Feature Gating console in the Oppia backend.
153+
154+
## How to consume a Platform Parameter or Feature Flag
155+
To consume a Platform Parameter in any file, we need to inject the specific `PlatformParameterValue<T>` instance along with the Qualifier Annotation defined for that Parameter. For eg - we are injecting the `SyncUpTimePeriodInHours` platform parameter and the `EnableAppAndOSDeprecation` feature flag in `PlatformParameterSyncUpWorkManagerInitializer`
156+
157+
```kotlin
90158
class PlatformParameterSyncUpWorkManagerInitializer @Inject constructor(
91159
private val context: Context,
92-
@SyncUpWorkerTimePeriodInHours private val syncUpWorkerTimePeriod : PlatformParameterValue<Int>
160+
@SyncUpWorkerTimePeriodInHours private val syncUpWorkerTimePeriod : PlatformParameterValue<Int>,
161+
@EnableAppAndOsDeprecation private val enableAppAndOsDeprecation: Provider<PlatformParameterValue<Boolean>>,
93162
) : ApplicationStartupListener {
94163
...
95164
fun exampleFunction(){
96165
val time: Int = syncUpWorkerTimePeriod.value
97-
// now we can use the value in the "time" variable, which will be an integer.
166+
// Now we can use the value in the "time" variable, which will be an integer.
167+
168+
val appAndOsDeprecationEnabled = enableAppAndOsDeprecation.value
169+
// This value can then be used to gate some features as desired.
98170
}
99171
}
100172
```
101173

102-
## How to write tests related Platform Parameter
174+
## Ensuring your Feature Flags are logged on each app session
175+
As a requirement, all feature flags should be logged at the beginning of each app session. This is done automatically via the `FeatureFlagsLogger.kt` inside `domain/src/main/java/org/oppia/android/domain/oppialogger/analytics/` but some configuration is required. To ensure any newly added feature flags are logged as part of this requirement, follow the steps below;
176+
177+
### 1. Import the feature flag to the FeatureFlagsLogger
178+
To get the value of the feature flag for logging, we need to consume the created feature flag as shown in the example below to ensure the value is present;
179+
180+
```kotlin
181+
/**
182+
* Convenience logger for feature flags.
183+
*
184+
* This logger is meant to be used for feature flag-related logging on every app launch. It is
185+
* primarily used within the ApplicationLifecycleObserver to log the status of feature flags in a
186+
* given app session.
187+
*/
188+
@Singleton
189+
class FeatureFlagsLogger @Inject constructor(
190+
...
191+
@EnableAppAndOsDeprecation
192+
private val enableAppAndOsDeprecation: PlatformParameterValue<Boolean>,
193+
) {
194+
...
195+
}
196+
```
197+
198+
### 2. Add an entry to the list of loggable feature flags
199+
The FeatureFlagsLogger contains a variable `featureFlagItemMap`, which is a map of feature flags to be logged and their names. Any newly added feature flags should also be added here to ensure that they are logged as well.
200+
201+
```kotlin
202+
/**
203+
* A variable containing a list of all the feature flags in the app.
204+
*
205+
* @return a list of key-value pairs of [String] and [PlatformParameterValue]
206+
*/
207+
private var featureFlagItemMap: Map<String, PlatformParameterValue<Boolean>> = mapOf(
208+
...
209+
APP_AND_OS_DEPRECATION to enableAppAndOsDeprecation
210+
)
211+
```
212+
213+
### 3. Update the Feature Flags Logger test
214+
Besides the feature-flag logger, the `FeatureFlagLoggerTest` located at `domain/src/test/java/org/oppia/android/domain/oppialogger/analytics/FeatureFlagsLoggerTest.kt` will also need to be updated to reflect the newly added feature flag(s). There are two tests that will need to be changed.
215+
216+
- The first test that should be updated is the `testLogFeatureFlags_correctNumberOfFeatureFlagsIsLogged` test. For this, only the constant `expectedFeatureFlagCount` will need to be updated. If a new feature flag was added, increment the count and if one was removed, decrement the count.
217+
218+
- The second test that will need to be updated is the `testLogFeatureFlags_allFeatureFlagNamesAreLogged`. This is a parameterized test that iterates through each currently existing feature flag to ensure each one of them is logged as expected. To update this test and ensure it passes after a feature flag change, modify the `RunParameterized()` section and either add the expected values for the new flag or remove the expected values for a removed feature flag.
219+
220+
## How to write tests related to Platform Parameters
103221
Before writing a test we must understand the purpose of the platform parameter in our class/classes (that needs to be tested). After verifying this we can divide testing procedures into following groups -
104222

105223
### 1. We actually don't test for platform parameter(s)
106224
We just need specific platform parameter(s) in the dagger graph because our class needs it, but our test cases are not actually verifying the behaviour of class based on different values of the platform parameter. These are the simplest cases to write tests for. We will only need to create a `TestModule` inside the Test class and then include this into the @Component for the `TestApplicationComponent`. For eg -
107225

108-
```
226+
```kotlin
109227
@Module
110228
class TestModule {
111229
@Provides

0 commit comments

Comments
 (0)