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'] response = views.list_resources(resource_type) if request.url_rule.rule == '/json': json_dict = {'total': response['total'], 'resources': []} for r in response['resources']: ghc_url = '%s/resource/%s' % (GHC_SITE_URL, r.identifier) 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, 'last_check': r.last_run.checked_datetime.strftime('%Y-%m-%dT%H:%M:%SZ'), 'status': r.last_run.success, 'response_time': round(r.average_response_time, 2), 'reliability': round(r.reliability, 2) }) 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', 'last_check', 'status', 'response_time', 'reliability' ] writer.writerow(header) for r in response['resources']: ghc_url = '%s%s' % (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, r.last_run.checked_datetime.strftime('%Y-%m-%dT%H:%M:%SZ'), r.last_run.success, round(r.average_response_time, 2), round(r.reliability, 2) ]) return output.getvalue()
def home(): """homepage""" resource_type = None if request.args.get('resource_type') in RESOURCE_TYPES.keys(): resource_type = request.args['resource_type'] response = views.list_resources(resource_type) return render_template('home.html', response=response)
def resources(): """lists resources with optional filter""" resource_type = None if request.args.get('resource_type') in RESOURCE_TYPES.keys(): resource_type = request.args['resource_type'] tag = request.args.get('tag') query = request.args.get('q') response = views.list_resources(resource_type, query, tag) return render_template('resources.html', response=response)
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'] response = views.list_resources(resource_type) json_dict = {'resources': []} for r in response['resources']: json_dict['resources'].append({ 'resource_type': r.resource_type, 'title': r.title, 'url': r.url }) return jsonify(json_dict)
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']: ghc_url = '%s/resource/%s' % (GHC_SITE_URL, r.identifier) 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': r.first_run.checked_datetime.strftime( '%Y-%m-%dT%H:%M:%SZ'), 'last_run': r.last_run.checked_datetime.strftime( '%Y-%m-%dT%H:%M:%SZ'), 'status': r.last_run.success, '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) }) 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']: ghc_url = '%s%s' % (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, r.first_run.checked_datetime.strftime( '%Y-%m-%dT%H:%M:%SZ'), r.last_run.checked_datetime.strftime( '%Y-%m-%dT%H:%M:%SZ'), r.last_run.success, round(r.average_response_time, 2), round(r.reliability, 2) ]) 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']: ghc_url = '%s/resource/%s' % (CONFIG['GHC_SITE_URL'], r.identifier) 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': r.first_run.checked_datetime.strftime('%Y-%m-%dT%H:%M:%SZ'), 'last_run': r.last_run.checked_datetime.strftime('%Y-%m-%dT%H:%M:%SZ'), 'status': r.last_run.success, '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': r.last_run.report, 'recipients': r.dump_recipients() }) 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', 'recipients' ] writer.writerow(header) for r in response['resources']: ghc_url = '%s%s' % (CONFIG['GHC_SITE_URL'], url_for('get_resource_by_id', identifier=r.identifier)) # serialize recipients into a string recipients = r.dump_recipients() recipients_str = json.dumps(recipients) writer.writerow([ r.resource_type, r.title, r.url, ghc_url, '%s/json' % ghc_url, '%s/csv' % ghc_url, r.first_run.checked_datetime.strftime('%Y-%m-%dT%H:%M:%SZ'), r.last_run.checked_datetime.strftime('%Y-%m-%dT%H:%M:%SZ'), r.last_run.success, round(r.average_response_time, 2), round(r.reliability, 2), recipients_str ]) 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'}