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 sensor_value
function that returns latest sensor reading
#34
Add sensor_value
function that returns latest sensor reading
#34
Conversation
Added function `sensor_value` to katportalclient that gets the latest value of a sensor via katmonitor. Added usage example script. JIRA: CB-3160
This PR depends on https://github.com/ska-sa/katportal/pull/333 EDIT: The above PR has been merged. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice, with good tests. Some minor things to change.
examples/get_latest_sensor_value.py
Outdated
sensor_names = yield portal_client.sensor_names(args.sensors) | ||
print "\nMatching sensor names for pattern {}: {}".format(args.sensors, sensor_names) | ||
|
||
# Fetch the details for the sensors found. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/details/readings
examples/get_latest_sensor_value.py
Outdated
try: | ||
sensor_value = yield portal_client.sensor_value(sensor_name, | ||
include_value_ts=True) | ||
except SensorNotFoundError, exc: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest the new-style syntax here:
except SensorNotFoundError as exc:
katportalclient/test/test_client.py
Outdated
|
||
self.mock_http_async_client().fetch.side_effect = fake_http_response(mon_response) | ||
result = yield self._portal_client.sensor_value("anc_tfr_m018_l_band_offset") | ||
expected_result = SensorSample(timestamp=1531302437.772357, value=43680.0, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This many digits in the timestamp is at the limit of what a double floating point variable can store, so it ends up slightly different to the string in the mon_response
. Suggest rounding off all the times to the nearest second to avoid this difference and make it easier to read.
katportalclient/test/test_client.py
Outdated
"""Test that we can handle single result""" | ||
mon_response = ('[{"status":"nominal",' | ||
'"name":"some_other_sample","component":"anc","value":43680.0,' | ||
'"value_ts":111.111,"time":111.111}]') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather use different values for value_ts
and time
. Then we can ensure the fields weren't swapped by accident.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea
|
||
|
||
def fake_http_response(response_string): | ||
"""Used as a mock side effect response for AsyncHTTPClient.fetch""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a nice solution.
The example script currently pops an error when being run. For example:
|
@ewanbarr |
Ok, I can't test out this change with the portal server I have access to. Is there a development portal that is up that includes ska-sa/katportal#333 (I don't have permission to see this repo)? Apart from that the interface looks fine. |
@ewanbarr I'll email you an address you can use. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to go 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine
sensor_value = yield portal_client.sensor_value(sensor_name, | ||
include_value_ts=True) | ||
except SensorNotFoundError as exc: | ||
print "\n", exc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure what the "exc" looks like by it may have been good to include the sensor_name in this print (although it probably is in the exception). No need to change just this, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the sensor name is in the string, see https://github.com/ska-sa/katportalclient/pull/34/files#diff-6355b287a79bb44c686cf1256475bf01R1256
@@ -1213,6 +1213,78 @@ def sensor_detail(self, sensor_name): | |||
else: | |||
raise tornado.gen.Return(results[0]) | |||
|
|||
@tornado.gen.coroutine | |||
def sensor_value(self, sensor_name, include_value_ts=False): | |||
"""Return the latest value of a sensor. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need to change. Not sure if "sensor_value" is a bit confusing as it actually returns the latest sensor sample - which include the value. But I think it is fine as the example shows how it is used. Could have been something like "sensor_reading" or "get_latest" ?
res = yield self._portal_client.sensor_value("anc_tfr_m018_l_band_offset_average", | ||
include_value_ts=True) | ||
assert res == expected_result | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good set of tests
sensor_value
tokatportalclient
that gets the latest value of a sensor (instance ofSensorSample
orSensorSampleValueTs
) viakatmonitor
.JIRA: CB-3160
Issue: #33