def delete(self, name, device_id): abort_when_node_not_found(name) result = database.delete_stats_interval_info(name, device_id) if not result: log.info('No such device %s:%s', name, device_id) abort(404) log.info('Successfully removed statistics gathering interval \ for device %s:%s statistics gathering interval info: %s', name, device_id, result) return result, 200
def delete(self, name, device_id): abort_when_node_not_found(name) result = database.delete_stats_interval_info(name, device_id) if not result: log.info('No such device %s:%s', name, device_id) abort(404) log.info( 'Successfully removed statistics gathering interval \ for device %s:%s statistics gathering interval info: %s', name, device_id, result) return result, 200
def put(self, name, device_id, date_time): power_usage = request.args.get('power_usage') abort_when_not_int(power_usage) power_usage = int(power_usage) try: datetime.datetime.strptime(date_time, '%Y-%m-%dT%H:%M') except ValueError: abort(400) computation_node = abort_when_node_not_found(name) abort_when_device_not_found(device_id, computation_node) if database.check_stats_present(name, device_id): database.update_stats_data(name, device_id, {date_time: power_usage}) log.debug('Stats updated for device %s:%s, date: %s, power usage: %s', name, device_id, date_time, power_usage) else: database.replace_stats_data(name, device_id, {'name': name, 'device_id': device_id, date_time: power_usage}) log.debug('Stats set for device %s:%s, date: %s, power usage: %s', name, device_id, date_time, power_usage) interval_info = database.get_stats_interval_info(name, device_id) interval = datetime.timedelta(minutes=interval_info['interval']) new_next_measurement = (datetime.datetime.utcnow() + interval) new_next_measurement_str = new_next_measurement.strftime('%Y-%m-%dT%H:%M') # pylint: disable=no-member interval_info['next_measurement'] = new_next_measurement_str database.replace_stats_interval_info(name, device_id, interval_info) return 'Statistics data successfully set', 201
def put(self, name, device_id): soft_limit = request.args.get('soft_limit') abort_when_not_int(soft_limit) computation_node = abort_when_node_not_found(name) if int(soft_limit) < 0: log.error(str.format('Number is not positive: {}', soft_limit)) abort(400) if not any(d['id'] == device_id for d in computation_node['backend_info']['devices']): log.error('There is no such device: %s', device_id) abort(404) limit_info = { 'name': name, 'device_id': device_id, 'soft_limit': soft_limit } upsert_result = database.replace_soft_limit_for_device( name, device_id, limit_info) if upsert_result.modified_count: log.info( 'Power limit for device %s:%s was already set in a database to %s', name, device_id, soft_limit) log.info('Stored power limit info %s', limit_info) else: log.info('Stored power limit info %s on id %s', limit_info, upsert_result.upserted_id) return 'Soft limit successfully set', 201
def delete(self, name, device_id): node_info = abort_when_node_not_found(name) address = node_info['address'] port = node_info['port'] [device_type] = [ d['Type'] for d in node_info['backend_info']['devices'] if d['id'] == device_id ] result = database.delete_power_limit_info(name, device_id) if not result: log.info('No such device %s:%s', name, device_id) abort(404) try: response = delete_power_limit(address, port, device_id, device_type) log.info('Power limit for device %s deletion info: %s', device_id, response) except requests.exceptions.ConnectionError: log.error('Connection could not be established to %s:%s', address, port) return 'Warning: power limit was deleted from database but could not be deleted from device', 406 log.info( 'Successfully removed power limit for device %s:%s power limit info: %s', name, device_id, result) return result, 200
def delete(self, name, device_id): computation_node = abort_when_node_not_found(name) address = computation_node['address'] port = computation_node['port'] if not any(d['id'] == device_id for d in computation_node['backend_info']['devices']): log.error('There is no such device: %s', device_id) abort(404) [device_type] = [ d['Type'] for d in computation_node['backend_info']['devices'] if d['id'] == device_id ] result = database.delete_rule(name, device_id) try: resp = delete_power_limit(address, port, device_id, device_type) log.info('Power limit for device %s deletion info: %s', device_id, resp) except requests.exceptions.ConnectionError: log.error('Connection could not be established to node %s:%s', address, port) return 'Warning: power limit was deleted from database but could not be deleted from device!', 406 return result, 200
def put(self, name, device_id): power_limit = request.args.get('power_limit') abort_when_not_int(power_limit) computation_node = abort_when_node_not_found(name) if not any(d['id'] == device_id for d in computation_node['backend_info']['devices']): log.error('There is no such device: %s', device_id) abort(404) limit_info = { 'name': name, 'device_id': device_id, 'power_limit': power_limit } [device_type] = [d['Type'] for d in computation_node['backend_info']['devices'] if d['id'] == device_id] previous_limit_data = get_power_limit(computation_node['address'], computation_node['port'], device_id, device_type) previous_limit_info = json.loads(previous_limit_data.content.decode('ascii')) previous_limit = previous_limit_info[0]['PowerLimit'] upsert_result = database.replace_power_limit_for_device(name, device_id, limit_info) if upsert_result.modified_count: log.info('Power limit for device %s:%s was already set in a database to %s', name, device_id, power_limit) log.info('Stored power limit info %s', limit_info) else: log.info('Stored power limit info %s on id %s', limit_info, upsert_result.upserted_id) try: response = put_power_limit(computation_node['address'], computation_node['port'], device_id, device_type, power_limit) log.info(response.text) response_object = json.loads(response.content.decode('ascii')) if not response_object[0]['Success']: log.info('Could not set power limit on device %s:%s, restoring previous value: %s', name, device_id, previous_limit) try: limit_info['power_limit'] = int(previous_limit) upsert_result = database.replace_power_limit_for_device(name, device_id, limit_info) if upsert_result.modified_count: log.info('Power limit for device %s:%s was already set in a database to %s', name, device_id, power_limit) log.info('Restored previous power limit %s', limit_info) else: log.info('Restored previous power limit info %s:%s', limit_info, upsert_result.upserted_id) except ValueError: pass return response_object[0]['ErrorMessage'], 406 except requests.exceptions.ConnectionError: log.error('Connection could not be established to %s:%s', computation_node['address'], computation_node['port']) return 'Failed to set power limit on device, but added to database', 201 return 'Power limit successfully set', 201
def put(self, name, device_id): statistics_interval = request.args.get('statistics_interval') abort_when_not_int(statistics_interval) statistics_interval = int(statistics_interval) computation_node = abort_when_node_not_found(name) if not any(d['id'] == device_id for d in computation_node['backend_info']['devices']): log.error('There is no such device: %s', device_id) abort(404) set_statistics_interval(name, device_id, statistics_interval) return 'Statistics gathering interval successfully set', 201
def statistics_data_plot(name, device_id): computation_node = abort_when_node_not_found(name) abort_when_device_not_found(device_id, computation_node) result = database.get_stats_data(name, device_id) if not result: log.info('No stats for device %s:%s', name, device_id) abort(404) res_list = sorted([(datetime.datetime.strptime(k, '%Y-%m-%dT%H:%M'), v) for k, v in result.items()]) list_to_return = [{k.strftime('%Y-%m-%dT%H:%M'): v} for k, v in res_list] log.info('Successfully get device %s:%s stats data info: %s', name, device_id, list_to_return) values = [] labels = [] for element in list_to_return: [val] = element.values() [lab] = element.keys() values.append(val) labels.append(lab) ind = np.arange(len(list_to_return)) # the x locations for the groups width = 0.2 # the width of the bars fig, usagePlot = plt.subplots() rects1 = usagePlot.bar((ind + width) / 2, values, width, color='r') # add some text for labels, title and axes ticks usagePlot.set_ylabel('Power usage [W]') usagePlot.set_title('Power usage for device') usagePlot.set_xticks((ind + width) / 2) usagePlot.set_xticklabels(labels) # ax.legend(rects1[0], 'Device 1') def autolabel(rects): # attach some text labels for rect in rects: height = rect.get_height() usagePlot.text(rect.get_x() + rect.get_width() / 2., 1.02 * height, '%d' % int(height), ha='center', va='bottom') autolabel(rects1) # plt.show() fig.savefig('/tmp/plot.png') # save the figure to file plt.close(fig) # close the figure return send_file('/tmp/plot.png')
def get(self, name, device_id): beginning = request.args.get('date_time_begin') end = request.args.get('date_time_end') if beginning: try: beginning = datetime.datetime.strptime(beginning, '%Y-%m-%dT%H:%M') except ValueError: abort(400) if end: try: end = datetime.datetime.strptime(end, '%Y-%m-%dT%H:%M') except ValueError: abort(400) if beginning and end and beginning > end: abort(400) computation_node = abort_when_node_not_found(name) abort_when_device_not_found(device_id, computation_node) result = database.get_stats_data(name, device_id) if not result: log.info('No statistics for device %s:%s', name, device_id) abort(404) res_list = sorted([(datetime.datetime.strptime(k, '%Y-%m-%dT%H:%M'), v) for k, v in result.items()]) if beginning: _, beg_index = self._find(lambda t: t[0] >= beginning, res_list) else: beg_index = 0 if beg_index is None: return 404 if end: _, end_index = self._find(lambda t: t[0] > end, res_list) else: end_index = None list_to_return = res_list[beg_index:end_index] list_to_return = [{ k.strftime('%Y-%m-%dT%H:%M'): v } for k, v in list_to_return] log.info('Successfully get device %s:%s statistics data info: %s', name, device_id, list_to_return) return list_to_return, 200
def get(self, name, device_id): computation_node = abort_when_node_not_found(name) abort_when_device_not_found(device_id, computation_node) db_records = database.get_last_power_usage(name, device_id) if not db_records or len(db_records) <= 0: log.info('No last power usage for device %s:%s', name, device_id) abort(404) sorted_result = sorted(zip(db_records.keys(), db_records.values()), key=lambda usage: usage[0], reverse=True) sorted_result = [u[1] for u in sorted_result if isinstance(u[1], int)] result = {'last_power_usage': sorted_result[0]} log.info('Successfully get device %s:%s last power usage info: %s', name, device_id, result) return result, 200
def delete(self, name, device_id, date_time): try: datetime.datetime.strptime(date_time, '%Y-%m-%dT%H:%M') except ValueError: abort(400) computation_node = abort_when_node_not_found(name) abort_when_device_not_found(device_id, computation_node) result = database.delete_stats_for_time(name, device_id, {date_time: ""}) if not result: log.info('No such statistic for device %s:%s', name, device_id) abort(404) if not result.matched_count: log.info('Did not manage to delete statistic for device %s:%s', name, device_id) abort(404) log.info('Successfully deleted device %s:%s statistics data info', name, device_id) return 204
def get(self, name, device_id): beginning = request.args.get('date_time_begin') end = request.args.get('date_time_end') if beginning: try: beginning = datetime.datetime.strptime(beginning, '%Y-%m-%dT%H:%M') except ValueError: abort(400) if end: try: end = datetime.datetime.strptime(end, '%Y-%m-%dT%H:%M') except ValueError: abort(400) if beginning and end and beginning > end: abort(400) computation_node = abort_when_node_not_found(name) abort_when_device_not_found(device_id, computation_node) result = database.get_stats_data(name, device_id) if not result: log.info('No statistics for device %s:%s', name, device_id) abort(404) res_list = sorted([(datetime.datetime.strptime(k, '%Y-%m-%dT%H:%M'), v) for k, v in result.items()]) if beginning: _, beg_index = self._find(lambda t: t[0] >= beginning, res_list) else: beg_index = 0 if beg_index is None: return 404 if end: _, end_index = self._find(lambda t: t[0] > end, res_list) else: end_index = None list_to_return = res_list[beg_index:end_index] list_to_return = [{k.strftime('%Y-%m-%dT%H:%M'): v} for k, v in list_to_return] log.info('Successfully get device %s:%s statistics data info: %s', name, device_id, list_to_return) return list_to_return, 200
def get(self, name, device_id, date_time): try: datetime.datetime.strptime(date_time, '%Y-%m-%dT%H:%M') except ValueError: abort(400) computation_node = abort_when_node_not_found(name) abort_when_device_not_found(device_id, computation_node) result = database.get_stats_data_for_time(name, device_id, date_time) if not result: log.info('No such statistic for device %s:%s', name, device_id) abort(404) if not result.get(date_time): log.info('No statistics for %s', date_time) abort(404) log.info('Successfully get device %s:%s statistics data info: %s', name, device_id, result) return result, 200
def delete(self, name, device_id): node_info = abort_when_node_not_found(name) address = node_info['address'] port = node_info['port'] [device_type] = [d['Type'] for d in node_info['backend_info']['devices'] if d['id'] == device_id] result = database.delete_power_limit_info(name, device_id) if not result: log.info('No such device %s:%s', name, device_id) abort(404) try: response = delete_power_limit(address, port, device_id, device_type) log.info('Power limit for device %s deletion info: %s', device_id, response) except requests.exceptions.ConnectionError: log.error('Connection could not be established to %s:%s', address, port) return 'Warning: power limit was deleted from database but could not be deleted from device', 406 log.info('Successfully removed power limit for device %s:%s power limit info: %s', name, device_id, result) return result, 200
def put(self, name: str, device_id: str): rule_type = request.args.get('rule_type') rule_params = request.json if rule_type not in AVAILABLE_RULE_TYPES: abort(400) computation_node = abort_when_node_not_found(name) if not any(d['id'] == device_id for d in computation_node['backend_info']['devices']): log.error('There is no such device: %s', device_id) abort(404) limit = rule_params.get("limit") if isinstance(limit, float): computation_node, dev_type = get_com_node_and_dev_type( name, device_id) constraints = get_constraints( computation_node['address'], computation_node['port'], device_id, dev_type).json()[0]["PowerLimitConstraints"] lower = constraints["lower"] upper = constraints["upper"] rule_params["limit"] = int((upper - lower) * limit + lower) if rule_type == "Withdrawable": previous_rule = database.get_rule_for_device(name, device_id) if previous_rule: if previous_rule["rule_type"] == "Withdrawable": previous_rule = previous_rule["rule_params"].get( "previous_rule", None) rule_params["previous_rule"] = previous_rule database.replace_rule_for_device( name, device_id, { 'name': name, 'device_id': device_id, 'rule_type': rule_type, 'rule_params': rule_params }) return 'Rule successfully set', 201
def put(self, name, device_id): statistics_interval = request.args.get('statistics_interval') abort_when_not_int(statistics_interval) statistics_interval = int(statistics_interval) computation_node = abort_when_node_not_found(name) if not any(d['id'] == device_id for d in computation_node['backend_info']['devices']): log.error('There is no such device: %s', device_id) abort(404) current_interval_info = database.get_stats_interval_info(name, device_id) if current_interval_info: old_interval = datetime.timedelta(minutes=current_interval_info['interval']) old_next_measurement = datetime.datetime.strptime(current_interval_info['next_measurement'], '%Y-%m-%dT%H:%M') new_interval = datetime.timedelta(minutes=statistics_interval) new_next_measurement = (old_next_measurement - old_interval + new_interval) interval_info = current_interval_info interval_info['interval'] = statistics_interval new_next_measurement_str = new_next_measurement.strftime('%Y-%m-%dT%H:%M') # pylint: disable=no-member interval_info['next_measurement'] = new_next_measurement_str else: interval_info = { 'name': name, 'device_id': device_id, 'interval': statistics_interval, 'next_measurement': (datetime.datetime.utcnow() + datetime.timedelta( minutes=statistics_interval)).strftime('%Y-%m-%dT%H:%M') # pylint: disable=no-member } upsert_result = database.replace_stats_interval_info(name, device_id, interval_info) if upsert_result.modified_count: log.info('Statistics gathering interval for device %s:%s was already set in a database', name, device_id) log.info('Stored statistics gathering interval info %s', interval_info) else: log.info('Stored statistics gathering interval info %s on id %s', interval_info, upsert_result.upserted_id) return 'Statistics gathering interval successfully set', 201
def put(self, name, device_id): power_limit = request.args.get('power_limit') abort_when_not_int(power_limit) computation_node = abort_when_node_not_found(name) if not any(d['id'] == device_id for d in computation_node['backend_info']['devices']): log.error('There is no such device: %s', device_id) abort(404) limit_info = { 'name': name, 'device_id': device_id, 'power_limit': power_limit } [device_type] = [ d['Type'] for d in computation_node['backend_info']['devices'] if d['id'] == device_id ] previous_limit_data = get_power_limit(computation_node['address'], computation_node['port'], device_id, device_type) previous_limit_info = json.loads( previous_limit_data.content.decode('ascii')) previous_limit = previous_limit_info[0]['PowerLimit'] upsert_result = database.replace_power_limit_for_device( name, device_id, limit_info) if upsert_result.modified_count: log.info( 'Power limit for device %s:%s was already set in a database to %s', name, device_id, power_limit) log.info('Stored power limit info %s', limit_info) else: log.info('Stored power limit info %s on id %s', limit_info, upsert_result.upserted_id) try: response = put_power_limit(computation_node['address'], computation_node['port'], device_id, device_type, power_limit) log.info(response.text) response_object = json.loads(response.content.decode('ascii')) if not response_object[0]['Success']: log.info( 'Could not set power limit on device %s:%s, restoring previous value: %s', name, device_id, previous_limit) try: limit_info['power_limit'] = int(previous_limit) upsert_result = database.replace_power_limit_for_device( name, device_id, limit_info) if upsert_result.modified_count: log.info( 'Power limit for device %s:%s was already set in a database to %s', name, device_id, power_limit) log.info('Restored previous power limit %s', limit_info) else: log.info('Restored previous power limit info %s:%s', limit_info, upsert_result.upserted_id) except ValueError: pass return response_object[0]['ErrorMessage'], 406 except requests.exceptions.ConnectionError: log.error('Connection could not be established to %s:%s', computation_node['address'], computation_node['port']) return 'Failed to set power limit on device, but added to database', 201 return 'Power limit successfully set', 201