def _update_label_sensors(ctx, label, sensor, set_func): service = ctx.find_object(helium.Service) label = util.lookup_resource_id(service.get_labels, label) # Fetch the existing sensors sensors = service.get_label_sensors(label).get('data') sensor_ids = dpath.values(sensors, "*/id") # Look up full sensor ids for all given sensors sensor_list = [util.lookup_resource_id(service.get_sensors, sensor_id) for sensor_id in sensor] # And perform the set operation and ensure we have a valid list sensor_ids = set_func(set(sensor_ids), set(sensor_list)) if sensor_ids is None: sensor_ids = [] service.update_label_sensors(label, sensor_ids)
def stop(service, script, **kwargs): """Stop a given cloud-script. Stops the SCRIPT with the given id. """ script = util.lookup_resource_id(service.get_cloud_scripts, script) _tabulate([service.update_cloud_script(script, state="stopped", **kwargs).get('data')])
def timeseries(service, script, **kwargs): """List readings for a cloud-script. Lists one page of readings for a given SCRIPT. """ script = util.lookup_resource_id(service.get_cloud_scripts, script) data = service.get_cloud_script_timeseries(script, **kwargs).get('data') ts.tabulate(data, **kwargs)
def delete(service, script): """Delete a given cloud-script. Deletes the cloud-SCRIPT with the given id. """ script = util.lookup_resource_id(service.get_cloud_scripts, script) result = service.delete_cloud_script(script) click.echo("Deleted: " + script.encode('ascii') if result.status_code == 204 else result)
def delete(service, label): """Delete a label. Deletes the LABEL with the given id """ label_list = service.get_labels().get('data') label = [util.lookup_resource_id(label_list, label_id) for label_id in label] for entry in label: result = service.delete_label(entry) click.echo("Deleted: " + entry if result.status_code == 204 else result)
def dump(service, label, format, **kwargs): """Dumps timeseries data to files. Dumps the timeseries data for all sensors in a given LABEL. One file is generated for each sensor with the sensor id as filename and the file extension based on the requested dump format """ label = util.lookup_resource_id(service.get_labels, label) sensors = dpath.values(service.get_label_sensors(label), '/data/*/id') ts.dump(service, sensors, format, **kwargs)
def show(service, script, file): """Gets a script file from a given cloud-script. Fetches a FILE from a given cloud-SCRIPT. """ script = util.lookup_resource_id(service.get_cloud_scripts, script) json = service.get_cloud_script(script).get('data') file_urls = [f.encode('utf-8') for f in dpath.get(json, 'meta/scripts')] names = dict(zip(util.extract_script_filenames(file_urls), file_urls)) file_url = names[file] click.echo(requests.get(file_url).text)
def create(ctx, name, sensor): """Create a label. Creates a label with a given NAME and an (optional) list of SENSORs associated with that label. """ service = ctx.find_object(helium.Service) label = service.create_label(name).get('data') label_id = label['id'] if sensor: sensor_list = service.get_sensors().get('data') sensors = [util.lookup_resource_id(sensor_list, sensor_id) for sensor_id in sensor] service.update_label_sensors(label_id, sensors) _tabulate([service.get_label(label_id, include='sensor').get('data')])
def list(service, sensor, label, **kwargs): """List sensors. Lists information for a label of sensors or all sensors in the organization. """ if label: label = lookup_resource_id(service.get_labels, label) sensors = service.get_label(label, include="sensor").get('included') elif sensor: sensor = _find_sensor_id(service, sensor, **kwargs) sensors = [service.get_sensor(sensor).get('data')] else: sensors = service.get_sensors().get('data') _tabulate(sensors, **kwargs)
def status(service, script): """Displays current status information for a script. Display status information a given SCRIPT. If the script is in an error condition the error details are displayed. """ script = util.lookup_resource_id(service.get_cloud_scripts, script) data = service.get_cloud_script(script).get('data') click.echo('Status: ' + dpath.get(data, "attributes/state")) try: error = dpath.get(data, "attributes/error") except KeyError: error = None # Pull out the details if they're there if isinstance(error, dict): error = error.get('details') if error: click.echo('Error: ' + error)
def _find_sensor_id(service, sensor, **kwargs): return lookup_resource_id(service.get_sensors, sensor, **kwargs)