def export_resource(identifier): """export resource as JSON or CSV""" resource = views.get_resource_by_id(identifier) history_csv = '%s/resource/%s/history/csv' % (CONFIG['GHC_SITE_URL'], resource.identifier) history_json = '%s/resource/%s/history/json' % (CONFIG['GHC_SITE_URL'], resource.identifier) if 'json' in request.url_rule.rule: last_run_report = '-' if resource.last_run: last_run_report = resource.last_run.report json_dict = { 'identifier': resource.identifier, 'title': resource.title, 'url': resource.url, 'resource_type': resource.resource_type, 'owner': resource.owner.username, 'min_response_time': resource.min_response_time, 'average_response_time': resource.average_response_time, 'max_response_time': resource.max_response_time, 'reliability': resource.reliability, 'status': format_run_status(resource.last_run), 'first_run': format_checked_datetime(resource.first_run), 'last_run': format_checked_datetime(resource.last_run), 'history_csv': history_csv, 'history_json': history_json, 'last_report': format_obj_value(last_run_report) } return jsonify(json_dict) elif 'csv' in request.url_rule.rule: output = StringIO() writer = csv.writer(output) header = [ 'identifier', 'title', 'url', 'resource_type', 'owner', 'min_response_time', 'average_response_time', 'max_response_time', 'reliability', 'status', 'first_run', 'last_run', 'history_csv', 'history_json' ] writer.writerow(header) writer.writerow([ resource.identifier, resource.title, resource.url, resource.resource_type, resource.owner.username, resource.min_response_time, resource.average_response_time, resource.max_response_time, resource.reliability, format_run_status(resource.last_run), format_checked_datetime(resource.first_run), format_checked_datetime(resource.last_run), history_csv, history_json ]) return output.getvalue(), 200, {'Content-type': 'text/csv'}
def export_resource(identifier): """export resource as JSON or CSV""" resource = views.get_resource_by_id(identifier) history_csv = '%s/resource/%s/history/csv' % (CONFIG['GHC_SITE_URL'], resource.identifier) history_json = '%s/resource/%s/history/json' % (CONFIG['GHC_SITE_URL'], resource.identifier) if 'json' in request.url_rule.rule: last_run_report = '-' if resource.last_run: last_run_report = resource.last_run.report json_dict = { 'identifier': resource.identifier, 'title': resource.title.encode('utf-8'), 'url': resource.url, 'resource_type': resource.resource_type, 'owner': resource.owner.username, 'min_response_time': resource.min_response_time, 'average_response_time': resource.average_response_time, 'max_response_time': resource.max_response_time, 'reliability': resource.reliability, 'status': format_run_status(resource.last_run), 'first_run': format_checked_datetime(resource.first_run), 'last_run': format_checked_datetime(resource.last_run), 'history_csv': history_csv, 'history_json': history_json, 'last_report': format_obj_value(last_run_report) } return jsonify(json_dict) elif 'csv' in request.url_rule.rule: output = StringIO() writer = csv.writer(output) header = [ 'identifier', 'title', 'url', 'resource_type', 'owner', 'min_response_time', 'average_response_time', 'max_response_time', 'reliability', 'status', 'first_run', 'last_run', 'history_csv', 'history_json' ] writer.writerow(header) writer.writerow([ resource.identifier, resource.title.encode('utf-8'), resource.url, resource.resource_type, resource.owner.username, resource.min_response_time, resource.average_response_time, resource.max_response_time, resource.reliability, format_run_status(resource.last_run), format_checked_datetime(resource.first_run), format_checked_datetime(resource.last_run), history_csv, history_json ]) return output.getvalue(), 200, {'Content-type': 'text/csv'}
def export(): """export resource list as JSON""" resource_type = None if request.args.get('resource_type') in RESOURCE_TYPES.keys(): resource_type = request.args['resource_type'] query = request.args.get('q') response = views.list_resources(resource_type, query) if request.url_rule.rule == '/json': json_dict = {'total': response['total'], 'resources': []} for r in response['resources']: try: ghc_url = '%s/resource/%s' % \ (CONFIG['GHC_SITE_URL'], r.identifier) last_run_report = '-' if r.last_run: last_run_report = r.last_run.report json_dict['resources'].append({ 'resource_type': r.resource_type, 'title': r.title, 'url': r.url, 'ghc_url': ghc_url, 'ghc_json': '%s/json' % ghc_url, 'ghc_csv': '%s/csv' % ghc_url, 'first_run': format_checked_datetime(r.first_run), 'last_run': format_checked_datetime(r.last_run), 'status': format_run_status(r.last_run), 'min_response_time': round(r.min_response_time, 2), 'average_response_time': round(r.average_response_time, 2), 'max_response_time': round(r.max_response_time, 2), 'reliability': round(r.reliability, 2), 'last_report': format_obj_value(last_run_report) }) except Exception as e: LOGGER.warning( 'JSON error resource id=%d: %s' % (r.identifier, str(e))) return jsonify(json_dict) elif request.url_rule.rule == '/csv': output = StringIO() writer = csv.writer(output) header = [ 'resource_type', 'title', 'url', 'ghc_url', 'ghc_json', 'ghc_csv', 'first_run', 'last_run', 'status', 'min_response_time', 'average_response_time', 'max_response_time', 'reliability' ] writer.writerow(header) for r in response['resources']: try: ghc_url = '%s%s' % (CONFIG['GHC_SITE_URL'], url_for('get_resource_by_id', identifier=r.identifier)) writer.writerow([ r.resource_type, r.title, r.url, ghc_url, '%s/json' % ghc_url, '%s/csv' % ghc_url, format_checked_datetime(r.first_run), format_checked_datetime(r.last_run), format_run_status(r.last_run), round(r.min_response_time, 2), round(r.average_response_time, 2), round(r.max_response_time, 2), round(r.reliability, 2) ]) except Exception as e: LOGGER.warning( 'CSV error resource id=%d: %s' % (r.identifier, str(e))) return output.getvalue(), 200, {'Content-type': 'text/csv'}
def export(): """export resource list as JSON""" resource_type = None if request.args.get('resource_type') in RESOURCE_TYPES.keys(): resource_type = request.args['resource_type'] query = request.args.get('q') response = views.list_resources(resource_type, query) if request.url_rule.rule == '/json': json_dict = {'total': response['total'], 'resources': []} for r in response['resources']: try: ghc_url = '%s/resource/%s' % \ (CONFIG['GHC_SITE_URL'], r.identifier) last_run_report = '-' if r.last_run: last_run_report = r.last_run.report json_dict['resources'].append({ 'resource_type': r.resource_type, 'title': r.title.encode('utf-8'), 'url': r.url, 'ghc_url': ghc_url, 'ghc_json': '%s/json' % ghc_url, 'ghc_csv': '%s/csv' % ghc_url, 'first_run': format_checked_datetime(r.first_run), 'last_run': format_checked_datetime(r.last_run), 'status': format_run_status(r.last_run), 'min_response_time': round(r.min_response_time, 2), 'average_response_time': round(r.average_response_time, 2), 'max_response_time': round(r.max_response_time, 2), 'reliability': round(r.reliability, 2), 'last_report': format_obj_value(last_run_report) }) except Exception as e: LOGGER.warning( 'JSON error resource id=%d: %s' % (r.identifier, str(e))) return jsonify(json_dict) elif request.url_rule.rule == '/csv': output = StringIO() writer = csv.writer(output) header = [ 'resource_type', 'title', 'url', 'ghc_url', 'ghc_json', 'ghc_csv', 'first_run', 'last_run', 'status', 'min_response_time', 'average_response_time', 'max_response_time', 'reliability' ] writer.writerow(header) for r in response['resources']: try: ghc_url = '%s%s' % (CONFIG['GHC_SITE_URL'], url_for('get_resource_by_id', identifier=r.identifier)) writer.writerow([ r.resource_type, r.title.encode('utf-8'), r.url, ghc_url, '%s/json' % ghc_url, '%s/csv' % ghc_url, format_checked_datetime(r.first_run), format_checked_datetime(r.last_run), format_run_status(r.last_run), round(r.min_response_time, 2), round(r.average_response_time, 2), round(r.max_response_time, 2), round(r.reliability, 2) ]) except Exception as e: LOGGER.warning( 'CSV error resource id=%d: %s' % (r.identifier, str(e))) return output.getvalue(), 200, {'Content-type': 'text/csv'}