Skip to content

Commit

Permalink
fixed exampe, added example
Browse files Browse the repository at this point in the history
  • Loading branch information
Ran Isenberg committed Jan 13, 2023
1 parent 4310bcc commit bcb1dac
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
30 changes: 23 additions & 7 deletions docs/utilities/feature_flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,8 @@ Use cases:
* Disable support/chat feature after working hours
* Launch a new feature on a specific date and time

You can also have features enabled only at certain times of the day for premium tier customers

=== "app.py"

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

=== "features.json"

```json hl_lines="15 19-27"
```json hl_lines="9-11 14-21"
--8<-- "examples/feature_flags/src/timebased_features.json"
```

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

You can also have features enabled only at specific days, for example: enable christmas sale discount during specific dates.

=== "app.py"

```python hl_lines="10"
--8<-- "examples/feature_flags/src/datetime_feature.py"
```

=== "features.json"

```json hl_lines="9-14"
--8<-- "examples/feature_flags/src/datetime_feature.json"
```

???+ info "How should I use timezones?"
You can use any [IANA time zone](https://www.iana.org/time-zones) (as originally specified
in [PEP 615](https://peps.python.org/pep-0615/)) as part of your rules definition.
Expand Down Expand Up @@ -633,7 +649,7 @@ The `conditions` block is a list of conditions that contain `action`, `key`, and
The `action` configuration can have the following values, where the expressions **`a`** is the `key` and **`b`** is the `value` above:

| Action | Equivalent expression |
|-------------------------------------|----------------------------------------------------------|
| ----------------------------------- | -------------------------------------------------------- |
| **EQUALS** | `lambda a, b: a == b` |
| **NOT_EQUALS** | `lambda a, b: a != b` |
| **KEY_GREATER_THAN_VALUE** | `lambda a, b: a > b` |
Expand All @@ -657,11 +673,11 @@ The `action` configuration can have the following values, where the expressions

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.

| Key | Meaning |
|-------------------------|--------------------------------------------------------------------------|
| CURRENT_TIME | The current time, 24 hour format (HH:mm) |
| CURRENT_DATETIME | The current datetime ([ISO8601](https://en.wikipedia.org/wiki/ISO_8601)) |
| CURRENT_DAY_OF_WEEK | The current day of the week (Monday-Sunday) |
| Key | Meaning |
| ------------------- | ------------------------------------------------------------------------ |
| CURRENT_TIME | The current time, 24 hour format (HH:mm) |
| CURRENT_DATETIME | The current datetime ([ISO8601](https://en.wikipedia.org/wiki/ISO_8601)) |
| CURRENT_DAY_OF_WEEK | The current day of the week (Monday-Sunday) |

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

Expand Down
21 changes: 21 additions & 0 deletions examples/feature_flags/src/datetime_feature.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"christmas_discount": {
"default": false,
"rules": {
"enable discount during christmas": {
"when_match": true,
"conditions": [
{
"action": "SCHEDULE_BETWEEN_DATETIME_RANGE",
"key": "CURRENT_DATETIME",
"value": {
"START": "2022-12-25T12:00:00",
"END": "2022-12-31T23:59:59",
"TIMEZONE": "America/New_York"
}
}
]
}
}
}
}
14 changes: 14 additions & 0 deletions examples/feature_flags/src/datetime_feature.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from aws_lambda_powertools.utilities.feature_flags import AppConfigStore, FeatureFlags

app_config = AppConfigStore(environment="dev", application="product-catalogue", name="features")

feature_flags = FeatureFlags(store=app_config)


def lambda_handler(event, context):
# Get customer's tier from incoming request
xmas_discount = feature_flags.evaluate(name="christmas_discount", default=False)

if xmas_discount:
# Enable special discount on christmas:
pass
11 changes: 3 additions & 8 deletions examples/feature_flags/src/timebased_features.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,14 @@
"weekend_premium_discount": {
"default": false,
"rules": {
"customer tier equals premium": {
"customer tier equals premium and its time for a discount": {
"when_match": true,
"conditions": [
{
"action": "EQUALS",
"key": "tier",
"value": "premium"
}
]
},
"is weekend": {
"when_match": true,
"conditions": [
},
{
"action": "SCHEDULE_BETWEEN_DAYS_OF_WEEK",
"key": "CURRENT_DAY_OF_WEEK",
Expand All @@ -30,4 +25,4 @@
}
}
}
}
}

0 comments on commit bcb1dac

Please sign in to comment.