From d196ae7868da64c34b2c83363d69203a333cbd70 Mon Sep 17 00:00:00 2001 From: Hongwei Date: Mon, 4 Dec 2023 14:00:52 +0100 Subject: [PATCH 1/3] feature/added the lastEndpoint info on metric page --- .../static/metrics/js/lastEndpointMetric.js | 17 +++++++++++++++++ apimanager/metrics/templates/metrics/api.html | 6 ++++-- apimanager/metrics/urls.py | 6 +++++- apimanager/metrics/views.py | 19 +++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 apimanager/metrics/static/metrics/js/lastEndpointMetric.js diff --git a/apimanager/metrics/static/metrics/js/lastEndpointMetric.js b/apimanager/metrics/static/metrics/js/lastEndpointMetric.js new file mode 100644 index 00000000..e22ceaac --- /dev/null +++ b/apimanager/metrics/static/metrics/js/lastEndpointMetric.js @@ -0,0 +1,17 @@ +$(document).ready(function($) { + getMetricLastEndpoint(); +}); + +function getMetricLastEndpoint(){ + $.ajax({url: "/metrics/api/last-endpoint", success: function(result){ + var content = "Last call: " + +result['app_name']+" " + +result['verb']+" " + + result['implemented_by_partial_function'] + +" costed " + +result['duration'] + +" ms."; + $("#last_endpoint").text(content); + setTimeout(function(){getMetricLastEndpoint();}, 5000); // will call function to update time every 5 seconds + }}); +} diff --git a/apimanager/metrics/templates/metrics/api.html b/apimanager/metrics/templates/metrics/api.html index 24d13c52..e8b174c1 100644 --- a/apimanager/metrics/templates/metrics/api.html +++ b/apimanager/metrics/templates/metrics/api.html @@ -3,7 +3,9 @@ {% load i18n %} {% block page_title %}{{ block.super }} / API Metrics{% endblock page_title %} - +{% block extrajs %} + +{% endblock extrajs %} {% load bootstrap3 %} {% block content %}
@@ -12,7 +14,7 @@ {{ form.media }} {# Form required JS and CSS #} {% endblock %}

{% trans "API Metrics" %}

- +
Last call: API Manager getMetrics costed 294 ms

{% trans "Filter" %}

diff --git a/apimanager/metrics/urls.py b/apimanager/metrics/urls.py index 3803ef73..d94d6b8b 100644 --- a/apimanager/metrics/urls.py +++ b/apimanager/metrics/urls.py @@ -15,13 +15,17 @@ WeeklySummaryView, DailySummaryView, HourlySummaryView, - CustomSummaryView + CustomSummaryView, + get_metric_last_endpoint ) urlpatterns = [ url(r'^api/$', APIMetricsView.as_view(), name='api-metrics'), + url(r'^api/last-endpoint/$', + get_metric_last_endpoint, + name='api-metrics-last-endpoint'), url(r'^api/summary-partial-function$', APISummaryPartialFunctionView.as_view(), name='api-metrics-summary-partial-function'), diff --git a/apimanager/metrics/views.py b/apimanager/metrics/views.py index 587e3b14..a40c33d0 100644 --- a/apimanager/metrics/views.py +++ b/apimanager/metrics/views.py @@ -10,6 +10,7 @@ from enum import Enum from django.conf import settings +from django.http import JsonResponse from apimanager import local_settings from apimanager.settings import API_HOST, EXCLUDE_APPS, EXCLUDE_FUNCTIONS, EXCLUDE_URL_PATTERN, API_EXPLORER_APP_NAME, API_DATE_FORMAT_WITH_MILLISECONDS, API_DATE_FORMAT_WITH_SECONDS , DEBUG from django.contrib import messages @@ -201,6 +202,24 @@ def get_context_data(self, **kwargs): }) return context +def get_metric_last_endpoint(request): + urlpath = "/management/metrics?limit=1" + api = API(request.session.get('obp')) + last_endpoint_metric={} + try: + metric = api.get(urlpath)['metrics'][0] + last_endpoint_metric={ + 'app_name':metric['app_name'], + 'verb': metric['verb'], + 'implemented_by_partial_function': metric['implemented_by_partial_function'], + 'duration': metric['duration'] + } + except Exception as err: + LOGGER.exception('error_once_only - Error Message: {}'.format(err)) + + return JsonResponse(last_endpoint_metric) + + class APISummaryPartialFunctionView(APIMetricsView): template_name = 'metrics/api_summary_partial_function.html' From e31d48dbe7ae8c87be9eff028e613c065f40cd80 Mon Sep 17 00:00:00 2001 From: Hongwei Date: Wed, 6 Dec 2023 12:02:11 +0100 Subject: [PATCH 2/3] feature/added the lastEndpoint info on metric page - tweaked the page content --- .../metrics/static/metrics/js/lastEndpointMetric.js | 11 ++++++----- apimanager/metrics/templates/metrics/api.html | 2 +- apimanager/metrics/views.py | 9 +++++---- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/apimanager/metrics/static/metrics/js/lastEndpointMetric.js b/apimanager/metrics/static/metrics/js/lastEndpointMetric.js index e22ceaac..82c11f88 100644 --- a/apimanager/metrics/static/metrics/js/lastEndpointMetric.js +++ b/apimanager/metrics/static/metrics/js/lastEndpointMetric.js @@ -4,12 +4,13 @@ $(document).ready(function($) { function getMetricLastEndpoint(){ $.ajax({url: "/metrics/api/last-endpoint", success: function(result){ - var content = "Last call: " - +result['app_name']+" " + var content = "" + +result['implemented_by_partial_function']+" took " + +result['duration']+"ms at " + +result['date']+" " +result['verb']+" " - + result['implemented_by_partial_function'] - +" costed " - +result['duration'] + + result['url'] + +" ms."; $("#last_endpoint").text(content); setTimeout(function(){getMetricLastEndpoint();}, 5000); // will call function to update time every 5 seconds diff --git a/apimanager/metrics/templates/metrics/api.html b/apimanager/metrics/templates/metrics/api.html index e8b174c1..99184780 100644 --- a/apimanager/metrics/templates/metrics/api.html +++ b/apimanager/metrics/templates/metrics/api.html @@ -14,7 +14,7 @@ {{ form.media }} {# Form required JS and CSS #} {% endblock %}

{% trans "API Metrics" %}

-
Last call: API Manager getMetrics costed 294 ms
+
getBanks took 43ms at 2023-12-06T11:00:49Z GET /obp/v4.0.0/banks ms.

{% trans "Filter" %}

diff --git a/apimanager/metrics/views.py b/apimanager/metrics/views.py index a40c33d0..cdacee80 100644 --- a/apimanager/metrics/views.py +++ b/apimanager/metrics/views.py @@ -209,10 +209,11 @@ def get_metric_last_endpoint(request): try: metric = api.get(urlpath)['metrics'][0] last_endpoint_metric={ - 'app_name':metric['app_name'], - 'verb': metric['verb'], - 'implemented_by_partial_function': metric['implemented_by_partial_function'], - 'duration': metric['duration'] + 'implemented_by_partial_function':metric['implemented_by_partial_function'], + 'duration': metric['duration'], + 'date': metric['date'], + 'verb': metric['verb'], + 'url': metric['url'] } except Exception as err: LOGGER.exception('error_once_only - Error Message: {}'.format(err)) From 5e3b073cde7e02436730178a7f0e4f60175e1b43 Mon Sep 17 00:00:00 2001 From: Hongwei Date: Wed, 6 Dec 2023 12:15:37 +0100 Subject: [PATCH 3/3] feature/added the lastEndpoint info on metric page - added the to_date to get_metric_last_endpoint --- apimanager/metrics/static/metrics/js/lastEndpointMetric.js | 6 ++---- apimanager/metrics/templates/metrics/api.html | 2 +- apimanager/metrics/views.py | 3 ++- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/apimanager/metrics/static/metrics/js/lastEndpointMetric.js b/apimanager/metrics/static/metrics/js/lastEndpointMetric.js index 82c11f88..7e623329 100644 --- a/apimanager/metrics/static/metrics/js/lastEndpointMetric.js +++ b/apimanager/metrics/static/metrics/js/lastEndpointMetric.js @@ -6,12 +6,10 @@ function getMetricLastEndpoint(){ $.ajax({url: "/metrics/api/last-endpoint", success: function(result){ var content = "" +result['implemented_by_partial_function']+" took " - +result['duration']+"ms at " + +result['duration']+" ms at " +result['date']+" " +result['verb']+" " - + result['url'] - - +" ms."; + + result['url']; $("#last_endpoint").text(content); setTimeout(function(){getMetricLastEndpoint();}, 5000); // will call function to update time every 5 seconds }}); diff --git a/apimanager/metrics/templates/metrics/api.html b/apimanager/metrics/templates/metrics/api.html index 99184780..b335207c 100644 --- a/apimanager/metrics/templates/metrics/api.html +++ b/apimanager/metrics/templates/metrics/api.html @@ -14,7 +14,7 @@ {{ form.media }} {# Form required JS and CSS #} {% endblock %}

{% trans "API Metrics" %}

-
getBanks took 43ms at 2023-12-06T11:00:49Z GET /obp/v4.0.0/banks ms.
+

getBanks took 43ms at 2023-12-06T11:00:49Z GET /obp/v4.0.0/banks ms.

{% trans "Filter" %}

diff --git a/apimanager/metrics/views.py b/apimanager/metrics/views.py index cdacee80..fc4fa29f 100644 --- a/apimanager/metrics/views.py +++ b/apimanager/metrics/views.py @@ -203,7 +203,8 @@ def get_context_data(self, **kwargs): return context def get_metric_last_endpoint(request): - urlpath = "/management/metrics?limit=1" + to_date = datetime.datetime.now().strftime(settings.API_DATE_FORMAT_WITH_MILLISECONDS) + urlpath = "/management/metrics?limit=1&to_date="+to_date api = API(request.session.get('obp')) last_endpoint_metric={} try: