Skip to content
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

Merged
merged 4 commits into from Jul 12, 2018

Conversation

SKAJohanVenter
Copy link
Contributor

@SKAJohanVenter SKAJohanVenter commented Jul 11, 2018

  • Added function sensor_value to katportalclient that gets the latest value of a sensor (instance of SensorSample or SensorSampleValueTs ) via katmonitor.
  • Added tests
  • Added usage example script

JIRA: CB-3160
Issue: #33

Added function `sensor_value` to katportalclient that gets the latest value of a sensor
via katmonitor.

Added usage example script.

JIRA: CB-3160
@SKAJohanVenter
Copy link
Contributor Author

SKAJohanVenter commented Jul 11, 2018

This PR depends on https://github.com/ska-sa/katportal/pull/333

EDIT: The above PR has been merged.

Copy link
Contributor

@tockards tockards left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Copy link
Contributor

@ajoubertza ajoubertza left a 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.

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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/details/readings

try:
sensor_value = yield portal_client.sensor_value(sensor_name,
include_value_ts=True)
except SensorNotFoundError, exc:
Copy link
Contributor

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:


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,
Copy link
Contributor

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.

"""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}]')
Copy link
Contributor

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.

Copy link
Contributor Author

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"""
Copy link
Contributor

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.

@ewanbarr
Copy link

The example script currently pops an error when being run. For example:

$ python examples/get_latest_sensor_value.py --host portal.mkat.karoo.kat.ac.za m009_observer
Matching sensor names for pattern ['m009_observer']: [u'm009_observer']
Traceback (most recent call last):
  File "examples/get_latest_sensor_value.py", line 74, in <module>
    io_loop.run_sync(main)
  File "/Users/ebarr/anaconda/envs/py27/lib/python2.7/site-packages/tornado/ioloop.py", line 458, in run_sync
    return future_cell[0].result()
  File "/Users/ebarr/anaconda/envs/py27/lib/python2.7/site-packages/tornado/concurrent.py", line 238, in result
    raise_exc_info(self._exc_info)
  File "/Users/ebarr/anaconda/envs/py27/lib/python2.7/site-packages/tornado/gen.py", line 1063, in run
    yielded = self.gen.throw(*exc_info)
  File "examples/get_latest_sensor_value.py", line 40, in main
    include_value_ts=True)
  File "/Users/ebarr/anaconda/envs/py27/lib/python2.7/site-packages/tornado/gen.py", line 1055, in run
    value = future.result()
  File "/Users/ebarr/anaconda/envs/py27/lib/python2.7/site-packages/tornado/concurrent.py", line 238, in result
    raise_exc_info(self._exc_info)
  File "/Users/ebarr/anaconda/envs/py27/lib/python2.7/site-packages/tornado/gen.py", line 307, in wrapper
    yielded = next(result)
  File "/Users/ebarr/Soft/katportalclient/katportalclient/client.py", line 1245, in sensor_value
    url = self.sitemap['monitor'] + '/list-sensors/all'
KeyError: 'monitor'

@SKAJohanVenter
Copy link
Contributor Author

@ewanbarr
This is expected, this change depends on a change in katportal (it also needs to be deployed), see my earlier comment:
#34 (comment)

@ewanbarr
Copy link

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.

@SKAJohanVenter
Copy link
Contributor Author

@ewanbarr I'll email you an address you can use.

Copy link
Contributor

@ajoubertza ajoubertza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to go 👍

Copy link
Contributor

@lvdheever lvdheever left a 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
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -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.
Copy link
Contributor

@lvdheever lvdheever Jul 12, 2018

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good set of tests

@SKAJohanVenter SKAJohanVenter merged commit 6a437d7 into master Jul 12, 2018
@SKAJohanVenter SKAJohanVenter deleted the user/johan/CB-3160/get_latest_sensor_value branch July 12, 2018 13:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants