Skip to content

Commit f789ee5

Browse files
Ran Isenbergrubenfonseca
Ran Isenberg
authored andcommitted
fixed exampe, added example
1 parent 8b0e14e commit f789ee5

File tree

4 files changed

+61
-15
lines changed

4 files changed

+61
-15
lines changed

docs/utilities/feature_flags.md

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,8 @@ Use cases:
459459
* Disable support/chat feature after working hours
460460
* Launch a new feature on a specific date and time
461461

462+
You can also have features enabled only at certain times of the day for premium tier customers
463+
462464
=== "app.py"
463465

464466
```python hl_lines="12"
@@ -473,7 +475,7 @@ Use cases:
473475

474476
=== "features.json"
475477

476-
```json hl_lines="15 19-27"
478+
```json hl_lines="9-11 14-21"
477479
--8<-- "examples/feature_flags/src/timebased_features.json"
478480
```
479481

@@ -491,6 +493,20 @@ You can also have features enabled only at certain times of the day.
491493
--8<-- "examples/feature_flags/src/timebased_happyhour_features.json"
492494
```
493495

496+
You can also have features enabled only at specific days, for example: enable christmas sale discount during specific dates.
497+
498+
=== "app.py"
499+
500+
```python hl_lines="10"
501+
--8<-- "examples/feature_flags/src/datetime_feature.py"
502+
```
503+
504+
=== "features.json"
505+
506+
```json hl_lines="9-14"
507+
--8<-- "examples/feature_flags/src/datetime_feature.json"
508+
```
509+
494510
???+ info "How should I use timezones?"
495511
You can use any [IANA time zone](https://www.iana.org/time-zones) (as originally specified
496512
in [PEP 615](https://peps.python.org/pep-0615/)) as part of your rules definition.
@@ -633,7 +649,7 @@ The `conditions` block is a list of conditions that contain `action`, `key`, and
633649
The `action` configuration can have the following values, where the expressions **`a`** is the `key` and **`b`** is the `value` above:
634650

635651
| Action | Equivalent expression |
636-
|-------------------------------------|----------------------------------------------------------|
652+
| ----------------------------------- | -------------------------------------------------------- |
637653
| **EQUALS** | `lambda a, b: a == b` |
638654
| **NOT_EQUALS** | `lambda a, b: a != b` |
639655
| **KEY_GREATER_THAN_VALUE** | `lambda a, b: a > b` |
@@ -657,11 +673,11 @@ The `action` configuration can have the following values, where the expressions
657673

658674
For time based keys, we provide a list of predefined keys. These will automatically get converted to the corresponding timestamp on each invocation of your Lambda function.
659675

660-
| Key | Meaning |
661-
|-------------------------|--------------------------------------------------------------------------|
662-
| CURRENT_TIME | The current time, 24 hour format (HH:mm) |
663-
| CURRENT_DATETIME | The current datetime ([ISO8601](https://en.wikipedia.org/wiki/ISO_8601)) |
664-
| CURRENT_DAY_OF_WEEK | The current day of the week (Monday-Sunday) |
676+
| Key | Meaning |
677+
| ------------------- | ------------------------------------------------------------------------ |
678+
| CURRENT_TIME | The current time, 24 hour format (HH:mm) |
679+
| CURRENT_DATETIME | The current datetime ([ISO8601](https://en.wikipedia.org/wiki/ISO_8601)) |
680+
| CURRENT_DAY_OF_WEEK | The current day of the week (Monday-Sunday) |
665681

666682
If not specified, the timezone used for calculations will be UTC.
667683

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"christmas_discount": {
3+
"default": false,
4+
"rules": {
5+
"enable discount during christmas": {
6+
"when_match": true,
7+
"conditions": [
8+
{
9+
"action": "SCHEDULE_BETWEEN_DATETIME_RANGE",
10+
"key": "CURRENT_DATETIME",
11+
"value": {
12+
"START": "2022-12-25T12:00:00",
13+
"END": "2022-12-31T23:59:59",
14+
"TIMEZONE": "America/New_York"
15+
}
16+
}
17+
]
18+
}
19+
}
20+
}
21+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from aws_lambda_powertools.utilities.feature_flags import AppConfigStore, FeatureFlags
2+
3+
app_config = AppConfigStore(environment="dev", application="product-catalogue", name="features")
4+
5+
feature_flags = FeatureFlags(store=app_config)
6+
7+
8+
def lambda_handler(event, context):
9+
# Get customer's tier from incoming request
10+
xmas_discount = feature_flags.evaluate(name="christmas_discount", default=False)
11+
12+
if xmas_discount:
13+
# Enable special discount on christmas:
14+
pass

examples/feature_flags/src/timebased_features.json

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,14 @@
22
"weekend_premium_discount": {
33
"default": false,
44
"rules": {
5-
"customer tier equals premium": {
5+
"customer tier equals premium and its time for a discount": {
66
"when_match": true,
77
"conditions": [
88
{
99
"action": "EQUALS",
1010
"key": "tier",
1111
"value": "premium"
12-
}
13-
]
14-
},
15-
"is weekend": {
16-
"when_match": true,
17-
"conditions": [
12+
},
1813
{
1914
"action": "SCHEDULE_BETWEEN_DAYS_OF_WEEK",
2015
"key": "CURRENT_DAY_OF_WEEK",
@@ -30,4 +25,4 @@
3025
}
3126
}
3227
}
33-
}
28+
}

0 commit comments

Comments
 (0)