Skip to content

Commit 1de92d1

Browse files
committed
Various improvements for 0.4.0
1 parent 2d72068 commit 1de92d1

2 files changed

Lines changed: 37 additions & 18 deletions

File tree

README.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ It provides the following methods:
243243
- `set_device_parameters(secret_key, data_format, last_request_count, last_request_timestamp)`: Used to set the device data required for proper processing of the request in the handler if it was not set initially, which is often the case as the serial number is usually required to fetch that data. It will return `ValueError` if either of the parameters is invalid.
244244
- `is_auth_valid()`: Returns `true` if the authentication provided is valid or `false` if not. Note that it checks both that the signature is valid and that the `request_count` or `timestamp` are more recent than the one provided in the device parameters.
245245
- `get_simple_metrics()`: Returns the metrics provided in the simple expanded format. It will also convert relative timestamps into explicit timestamps for easier processing.
246+
- `get_data_timestamp()`: Returns the timestamp of the data, either the `data_collection_timestamp` if available or the timestamp `timestamp` or the time of the request as fallback.
247+
- `get_token_count()`: Returns the token count provided in the request (if any).
246248
- `expects_token_answer()`: Return `true` if the payload requested tokens in the answer. You can set the tokens to be returned by calling `add_tokens_to_answer(token_list)` with `token_list` being a list of token strings.
247249
- `expects_time_answer()`: Return `true` if the payload requested either relative time or absolute time in the answer. You can set the time to be returned by calling `add_time_to_answer(target_datetime)` with `target_datetime` being a datetime object. The function will automatically provide it in the correct format based on the request.
248250
- `add_settings_to_answer(settings_dict)`: Will add the provided settings dictionnary to the answer.
@@ -255,7 +257,7 @@ It provides the following methods:
255257

256258
```python
257259
from openpaygo import MetricsResponseHandler
258-
from my_db_service import get_device, get_data_format, store_metric
260+
from my_db_service import get_device, get_data_format, store_metric, get_pending_tokens
259261

260262

261263
@app.route('/dd')
@@ -272,23 +274,26 @@ def device_data():
272274
# We set the device parameters in the metrics handler
273275
metrics.set_device_parameters(
274276
secret_key=device.secret_key,
275-
data_format=data_format
277+
data_format=data_format,
278+
last_request_timestamp=device.last_request_timestamp
276279
)
277280
# We check the authentication
278281
if not metrics.is_auth_valid():
279282
return {'error': 'Invalid authentication'}, 403
280283
# We transform the condensed data received from the device in simple data
281284
simple_data = metrics.get_simple_metrics()
282285
# We store the metrics in our database
283-
for metric_data in simple_data.get('data'):
284-
store_metric(name=metric_data['name'], value=metric_data['value'])
285-
# Here the handler automatically computed the timestamp for each step
286+
for metric_name, metric_value in simple_data.get('data'):
287+
store_metric(name=metric_name, value=metric_value, time=metrics.get_data_timestamp())
288+
# We store the historical metrics as well
286289
for time_step in simple_data.get('historical_data'):
287-
for historical_metric_data in time_step:
288-
store_metric(name=metric_data['name'], value=metric_data['value'], time=time_step['timestamp'])
290+
# Here the handler automatically computed the timestamp for each step
291+
timestamp = timestep.pop('timestamp')
292+
for metric_name, metric_value in time_step:
293+
store_metric(name=metric_name, value=metric_value, time=timestamp)
289294
# We prepare the answer
290295
if metrics.expects_token_answer():
291-
metrics.add_tokens_to_answer(device.pending_token_list)
296+
metrics.add_tokens_to_answer(get_pending_tokens(device, metrics.get_token_count()))
292297
elif metrics.expects_time_answer():
293298
metrics.add_time_to_answer(device.expiration_datetime)
294299
# We can add extra data
@@ -300,6 +305,11 @@ def device_data():
300305

301306
## Changelog
302307

308+
### 2023-10-09 - v0.4.0
309+
- Added convenience functions for accessing token count and data timestamp
310+
- Added automatic verification of last request count or timestamp during auth
311+
- Fixed issues in documentation
312+
303313
### 2023-10-09 - v0.3.0
304314
- Fix token generation issue
305315
- Add support for OpenPAYGO Metrics Request Generation

openpaygo/metrics_response.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@ def get_data_format_id(self):
3232
def data_format_available(self):
3333
return self.data_format != None
3434

35-
def set_device_parameters(self, secret_key, data_format):
36-
self.secret_key = secret_key
37-
self.data_format = data_format
35+
def set_device_parameters(self, secret_key=None, data_format=None, last_request_count=None, last_request_timestamp=None):
36+
if secret_key:
37+
self.secret_key = secret_key
38+
if data_format:
39+
self.data_format = data_format
40+
if last_request_count:
41+
self.last_request_count = last_request_count
42+
if last_request_timestamp:
43+
self.last_request_timestamp = last_request_timestamp
3844

3945
def is_auth_valid(self):
4046
auth_string = self.request_dict.get('auth', None)
@@ -65,10 +71,16 @@ def get_simple_metrics(self):
6571
# We fill in the timestamps for each time step
6672
simple_dict['historical_data'] = self._fill_timestamp_in_historical_data(simple_dict['historical_data'])
6773
return simple_dict
68-
69-
def expects_token_answer(self):
74+
75+
def get_data_timestamp(self):
76+
return self.request_dict.get('data_collection_timestamp', self.request_dict.get('timestamp'))
77+
78+
def get_token_count(self):
7079
data = self._get_simple_data()
71-
return data.get('token_count') is not None
80+
return data.get('token_count')
81+
82+
def expects_token_answer(self):
83+
return self.get_token_count() is not None
7284

7385
def add_tokens_to_answer(self, token_list):
7486
self.response_dict['token_list'] = token_list
@@ -166,10 +178,7 @@ def _get_simple_historical_data(self):
166178
return clean_historical_data
167179

168180
def _fill_timestamp_in_historical_data(self, historical_data):
169-
if self.request_dict.get('data_collection_timestamp'):
170-
last_timestamp = datetime.fromtimestamp(self.request_dict.get('data_collection_timestamp'))
171-
else:
172-
last_timestamp = datetime.fromtimestamp(self.timestamp)
181+
last_timestamp = datetime.fromtimestamp(self.get_data_timestamp())
173182
for idx, time_step in enumerate(historical_data):
174183
if time_step.get('relative_time') is not None:
175184
last_timestamp = last_timestamp + timedelta(seconds=int(time_step.get('relative_time')))

0 commit comments

Comments
 (0)