Merge pull request #99 from hongwei1/develop

added the cache and fixed the save issues
This commit is contained in:
Simon Redfern 2020-01-09 16:07:39 +01:00 committed by GitHub
commit f930141c79
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 301 additions and 141 deletions

View File

@ -249,6 +249,15 @@ EXCLUDE_URL_PATTERN = []
# App Name to aggregate metrics
API_EXPLORER_APP_NAME = 'xxx'
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake',
}
}
CACHE_DATEFORMAT = '%Y%m%d%H%M%S'
CACHE_TIME = 3600
CACHE_TIME_SHORT = 300
# Local settings can override anything in here
try:

View File

@ -8,7 +8,8 @@ $(document).ready(function($) {
bank_id_pattern = $(runner).find('textarea[name="bank_id_pattern"]').val();
is_bank_id_exact_match = $(runner).find('.is_bank_id_exact_match').val();
parameters = $(runner).find('textarea[name="parameters"]').val();
$('.runner button.forSave').attr("disabled","disabled");
$('.runner button.forDelete').attr("disabled","disabled");
$.post('methodrouting/save/method', {
'method_routing_id': method_routing_id,
'method_name': method_name,
@ -17,18 +18,22 @@ $(document).ready(function($) {
'is_bank_id_exact_match': is_bank_id_exact_match,
'parameters': parameters
}, function (response) {
t.next().show().fadeOut(1000);
location.reload();
});
return false;
});
$('.runner button.forDelete').click(function() {
var t = $(this);
var runner = $(this).parent().parent().parent();
method_routing_id = $(runner).find('.method_routing_id').text();
$('.runner button.forSave').attr("disabled","disabled");
$('.runner button.forDelete').attr("disabled","disabled");
$.post('methodrouting/delete/method', {
'method_routing_id': method_routing_id
}, function (response) {
t.parent().parent().parent().remove();
location.reload();
});
return false;
});
});

View File

@ -33,8 +33,6 @@ class IndexView(LoginRequiredMixin, FormView):
if 'code' in response and response['code'] >= 400:
error_once_only(self.request, response['message'])
else:
msg = 'Submit successfully!'
messages.success(self.request, msg)
method_routings=response['method_routings']
except APIError as err:
error_once_only(self.request, Exception("OBP-API server is not running or do not response properly. "
@ -75,7 +73,6 @@ def methodrouting_save(request):
else: # if method_routing_id not empty. we will update the current method routing ..
urlpath = '/management/method_routings/{}'.format(method_routing_id)
result = api.put(urlpath, payload=payload)
return result

View File

@ -222,7 +222,7 @@ class CustomSummaryForm(forms.Form):
# ),
widget=DateTimePickerInput(format='%Y-%m-%d %H:%M:%S'),
required=True,
initial=str(datetime.now().strftime('%Y-%m-%d %H:%M:%S')),
initial=str(datetime.now().strftime('%Y-%m-%d %H:00:00')),
)
from_date_custom = forms.DateTimeField(
@ -236,7 +236,7 @@ class CustomSummaryForm(forms.Form):
# ),
widget=DateTimePickerInput(format='%Y-%m-%d %H:%M:%S'),
required=True,
initial=(datetime.now() - timedelta(6)).strftime('%Y-%m-%d %H:%M:%S'),
initial=(datetime.now() - timedelta(6)).strftime('%Y-%m-%d %H:00:00'),
)
include_obp_apps = forms.BooleanField(required=False)
@ -256,7 +256,7 @@ class MetricsSummaryForm(forms.Form):
# 'class': 'form-control',
# }
# ),
widget=DateTimePickerInput(format='%Y-%m-%d %H:00:00'),
widget=DateTimePickerInput(format='%Y-%m-%d %H:%M:%S'),
required=True,
# initial=str(datetime.now().strftime('%Y-%m-%dT%H:%M:%S.%fZ')),
initial=str(datetime.now().strftime('%Y-%m-%d %H:00:00')),

View File

@ -119,6 +119,10 @@
<td>Top 10 APIs:</td>
<td><img src="data:image/png;base64, {{ top_apis_bar_chart }}" alt="somealt" /></td>
</tr>
<tr>
<td>Top 10 Consumers:</td>
<td><img src="data:image/png;base64, {{ top_consumers_bar_chart }}" alt="somealt" /></td>
</tr>
<tr>
<td>Top Warehouse APIs:</td>
<td>{% if top_warehouse_apis %}{%for element in top_warehouse_apis%} <p>{{ element.Implemented_by_partial_function }} : {{ element.count }}</p> {% endfor %}{% else %} <p>N/A</p>{% endif %}</td>

View File

@ -113,6 +113,10 @@
<td>Top 10 APIs:</td>
<td><img src="data:image/png;base64, {{ top_apis_bar_chart }}" alt="somealt" /></td>
</tr>
<tr>
<td>Top 10 Consumers:</td>
<td><img src="data:image/png;base64, {{ top_consumers_bar_chart }}" alt="somealt" /></td>
</tr>
<tr>
<td>Top Warehouse APIs:</td>
<td>{% if top_warehouse_apis %}{%for element in top_warehouse_apis%} <p>{{ element.Implemented_by_partial_function }} : {{ element.count }}</p> {% endfor %}{% else %} <p>N/A</p>{% endif %}</td>

View File

@ -116,6 +116,10 @@
<td>Top 10 APIs:</td>
<td><img src="data:image/png;base64, {{ top_apis_bar_chart }}" alt="somealt" /></td>
</tr>
<tr>
<td>Top 10 Consumers:</td>
<td><img src="data:image/png;base64, {{ top_consumers_bar_chart }}" alt="somealt" /></td>
</tr>
<tr>
<td>Top Warehouse APIs:</td>
<td>{% if top_warehouse_apis %}{%for element in top_warehouse_apis%} <p>{{ element.Implemented_by_partial_function }} : {{ element.count }}</p> {% endfor %}{% else %} <p>N/A</p>{% endif %}</td>

View File

@ -118,6 +118,10 @@
<td>Top 10 APIs:</td>
<td><img src="data:image/png;base64, {{ top_apis_bar_chart }}" alt="somealt" /></td>
</tr>
<tr>
<td>Top 10 Consumers:</td>
<td><img src="data:image/png;base64, {{ top_consumers_bar_chart }}" alt="somealt" /></td>
</tr>
<tr>
<td>Top Warehouse APIs:</td>
<td>{% if top_warehouse_apis %}{%for element in top_warehouse_apis%} <p>{{ element.Implemented_by_partial_function }} : {{ element.count }}</p> {% endfor %}{% else %} <p>N/A</p>{% endif %}</td>

View File

@ -114,6 +114,10 @@
<td>Top 10 APIs:</td>
<td><img src="data:image/png;base64, {{ top_apis_bar_chart }}" alt="somealt" /></td>
</tr>
<tr>
<td>Top 10 Consumers:</td>
<td><img src="data:image/png;base64, {{ top_consumers_bar_chart }}" alt="somealt" /></td>
</tr>
<tr>
<td>Top Warehouse APIs:</td>
<td>{% if top_warehouse_apis %}{%for element in top_warehouse_apis%} <p>{{ element.Implemented_by_partial_function }} : {{ element.count }}</p> {% endfor %}{% else %} <p>N/A</p>{% endif %}</td>

View File

@ -114,6 +114,10 @@
<td>Top 10 APIs:</td>
<td><img src="data:image/png;base64, {{ top_apis_bar_chart }}" alt="somealt" /></td>
</tr>
<tr>
<td>Top 10 Consumers:</td>
<td><img src="data:image/png;base64, {{ top_consumers_bar_chart }}" alt="somealt" /></td>
</tr>
<tr>
<td>Top Warehouse APIs:</td>
<td>{% if top_warehouse_apis %}{%for element in top_warehouse_apis%} <p>{{ element.Implemented_by_partial_function }} : {{ element.count }}</p> {% endfor %}{% else %} <p>N/A</p>{% endif %}</td>

View File

@ -9,13 +9,15 @@ import operator
from datetime import datetime, timedelta
from django.conf import settings
from apimanager.settings import API_HOST, EXCLUDE_APPS, EXCLUDE_FUNCTIONS, EXCLUDE_URL_PATTERN, API_EXPLORER_APP_NAME, API_DATEFORMAT
from apimanager.settings import API_HOST, EXCLUDE_APPS, EXCLUDE_FUNCTIONS, EXCLUDE_URL_PATTERN, API_EXPLORER_APP_NAME, API_DATEFORMAT,CACHE_DATEFORMAT,CACHE_TIME,CACHE_TIME_SHORT
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import TemplateView
from obp.api import API, APIError
from .forms import APIMetricsForm, ConnectorMetricsForm, MetricsSummaryForm, CustomSummaryForm
from pylab import *
from django.core.cache import cache
import traceback
try:
# Python 2
import cStringIO
@ -368,16 +370,26 @@ class MetricsSummaryView(LoginRequiredMixin, TemplateView):
to_date = datetime.datetime.strptime(to_date, API_DATEFORMAT)
urlpath='/management/consumers'
api = API(self.request.session.get('obp'))
apicaches=None
try:
apps = api.get(urlpath)
if apps is not None and 'code' in apps and apps['code'] == 403:
error_once_only(self.request, apps['message'])
else:
apps_list = apps["list"]
except APIError as err:
error_once_only(self.request, err)
apicaches=cache.get('consumers,{}'.format(self.request.session.get('obp')['authenticator_kwargs']['token']))
except Exception as err:
error_once_only(self.request, 'Unknown Error. {}'.format(type(err).__name__))
apicaches=None
if not apicaches is None:
apps_list=apicaches
else:
try:
apps = api.get(urlpath)
if apps is not None and 'code' in apps and apps['code'] == 403:
error_once_only(self.request, apps['message'])
else:
apps_list = apps["consumers"]
cache.set('consumers,{}'.format(self.request.session.get('obp')['authenticator_kwargs']['token']),apps_list)
except APIError as err:
error_once_only(self.request, err)
except Exception as err:
error_once_only(self.request, 'Unknown Error. {}'.format(type(err).__name__))
for app in apps_list:
app_created_date = datetime.datetime.strptime(app["created"], '%Y-%m-%dT%H:%M:%SZ')
@ -438,20 +450,35 @@ class MetricsSummaryView(LoginRequiredMixin, TemplateView):
while date_to <= to_date:
urlpath = '/management/aggregate-metrics?from_date={}&to_date={}'.format(
date_from.strftime(API_DATEFORMAT), date_to.strftime(API_DATEFORMAT))
api = API(self.request.session.get('obp'))
apicaches=None
try:
metrics = api.get(urlpath)
if metrics is not None and 'code' in metrics and metrics['code'] == 403:
error_once_only(self.request, metrics['message'])
else:
result = metrics[0]["count"]
result_list_pure.append(result)
result_list.append('{} - {} # {}'.format(date_from, date_to, result))
sum += result
except APIError as err:
error_once_only(self.request, err)
apicaches=cache.get('aggregate-metrics,{},{},{}'.format(self.request.session.get('obp')['authenticator_kwargs']['token'],date_from.strftime(CACHE_DATEFORMAT), date_to.strftime(CACHE_DATEFORMAT)))
except Exception as err:
error_once_only(self.request, 'Unknown Error. {}'.format(type(err).__name__))
apicaches=None
if not apicaches is None:
result = apicaches
result_list_pure.append(result)
result_list.append('{} - {} # {}'.format(date_from, date_to, result))
sum += result
else:
api = API(self.request.session.get('obp'))
try:
metrics = api.get(urlpath)
if metrics is not None and 'code' in metrics and metrics['code'] == 403:
error_once_only(self.request, metrics['message'])
if(metrics['message'].startswith('OBP-20006')):
break
else:
result = metrics[0]["count"]
result_list_pure.append(result)
result_list.append('{} - {} # {}'.format(date_from, date_to, result))
sum += result
cache.set('aggregate-metrics,{},{},{}'.format(self.request.session.get('obp')['authenticator_kwargs']['token'],date_from.strftime(CACHE_DATEFORMAT), date_to.strftime(CACHE_DATEFORMAT)),result,CACHE_TIME if date_from.minute==0 and date_from.second==0 and date_to.minute==0 and date_to.second==0 else CACHE_TIME_SHORT)
except APIError as err:
error_once_only(self.request, err)
except Exception as err:
error_once_only(self.request, 'Unknown Error. {}'.format(type(err).__name__))
date_from = date_to
date_list.append(date_from)
@ -460,20 +487,35 @@ class MetricsSummaryView(LoginRequiredMixin, TemplateView):
while date_to <= to_date:
urlpath = '/management/aggregate-metrics?from_date={}&to_date={}&exclude_app_names={}'.format(
date_from.strftime(API_DATEFORMAT), date_to.strftime(API_DATEFORMAT), ",".join(EXCLUDE_APPS))
api = API(self.request.session.get('obp'))
apicaches=None
try:
metrics = api.get(urlpath)
if metrics is not None and 'code' in metrics and metrics['code'] == 403:
error_once_only(self.request, metrics['message'])
else:
result = metrics[0]["count"]
result_list_pure.append(result)
result_list.append('{} - {} # {}'.format(date_from, date_to, result))
sum += result
except APIError as err:
error_once_only(self.request, err)
apicaches=cache.get('aggregate-metrics,{},{},{},{}'.format(self.request.session.get('obp')['authenticator_kwargs']['token'],date_from.strftime(CACHE_DATEFORMAT), date_to.strftime(CACHE_DATEFORMAT),",".join(EXCLUDE_APPS)))
except Exception as err:
error_once_only(self.request, 'Unknown Error. {}'.format(type(err).__name__))
apicaches=None
if not apicaches is None:
result = apicaches
result_list_pure.append(result)
result_list.append('{} - {} # {}'.format(date_from, date_to, result))
sum += result
else:
api = API(self.request.session.get('obp'))
try:
metrics = api.get(urlpath)
if metrics is not None and 'code' in metrics and metrics['code'] == 403:
error_once_only(self.request, metrics['message'])
if(metrics['message'].startswith('OBP-20006')):
break
else:
result = metrics[0]["count"]
result_list_pure.append(result)
result_list.append('{} - {} # {}'.format(date_from, date_to, result))
sum += result
cache.set('aggregate-metrics,{},{},{},{}'.format(self.request.session.get('obp')['authenticator_kwargs']['token'],date_from.strftime(CACHE_DATEFORMAT), date_to.strftime(CACHE_DATEFORMAT),",".join(EXCLUDE_APPS)),result,CACHE_TIME if date_from.minute==0 and date_from.second==0 and date_to.minute==0 and date_to.second==0 else CACHE_TIME_SHORT)
except APIError as err:
error_once_only(self.request, err)
except Exception as err:
error_once_only(self.request, 'Unknown Error. {}'.format(type(err).__name__))
date_from = date_to
date_list.append(date_from)
@ -603,6 +645,29 @@ class MetricsSummaryView(LoginRequiredMixin, TemplateView):
# Clear the previous plot.
plt.gcf().clear()
return image_base64
def plot_topconsumer_bar_chart(self, data):
x = []
y = []
for item in data:
y.append(item['count'])
x.append(item['app_name'])
plt.barh(x, y)
plt.title("Top consumers", fontsize=10)
plt.xlabel("Number of API Calls", fontsize=8)
plt.xticks([])
plt.ylabel("Consumers", fontsize=8)
plt.tick_params(axis='y', labelsize=8)
for i, j in zip(y, x):
plt.text(i, j, str(i), clip_on=True, ha='center',va='center', fontsize=8)
plt.tight_layout()
buf = BytesIO()
plt.savefig(buf, format='png')
image_base64 = base64.b64encode(buf.getvalue()).decode('utf-8').replace('\n', '')
buf.close()
# Clear the previous plot.
plt.gcf().clear()
return image_base64
def get_users_cansearchwarehouse(self):
users = []
@ -681,6 +746,47 @@ class MetricsSummaryView(LoginRequiredMixin, TemplateView):
top_apis = reversed(top_apis)
return top_apis
def get_top_consumers(self, cleaned_data, from_date, to_date):
top_consumers = []
form = self.get_form()
if cleaned_data.get('include_obp_apps'):
urlpath = '/management/metrics/top-consumers?from_date={}&to_date={}'.format(from_date, to_date)
api = API(self.request.session.get('obp'))
try:
top_consumers = api.get(urlpath)
if top_consumers is not None and 'code' in top_consumers and top_consumers['code']==403:
error_once_only(self.request, top_consumers['message'])
top_consumers=[]
else:
top_consumers = top_consumers['top_consumers']
except APIError as err:
error_once_only(self.request, err)
except Exception as err:
error_once_only(self.request, 'Unknown Error. {}'.format(type(err).__name__))
else:
urlpath = '/management/metrics/top-consumers?from_date={}&to_date={}&exclude_app_names={}&exclude_implemented_by_partial_functions={}&exclude_url_pattern={}'.format(
from_date, to_date, ",".join(EXCLUDE_APPS), ",".join(EXCLUDE_FUNCTIONS), ",".join(EXCLUDE_URL_PATTERN))
api = API(self.request.session.get('obp'))
try:
top_consumers = api.get(urlpath)
if top_consumers is not None and 'code' in top_consumers and top_consumers['code']==403:
error_once_only(self.request, top_consumers['message'])
top_consumers=[]
else:
top_consumers = top_consumers['top_consumers']
except APIError as err:
error_once_only(self.request, err)
except Exception as err:
error_once_only(self.request, 'Unknown Error. {}'.format(type(err).__name__))
for consumer in top_consumers:
if consumer['app_name'] == "":
top_consumers.remove(consumer)
top_consumers = top_consumers[:10]
top_consumers = reversed(top_consumers)
return top_consumers
def get_top_warehouse_calls(self, cleaned_data, from_date, to_date):
try:
@ -717,23 +823,34 @@ class MetricsSummaryView(LoginRequiredMixin, TemplateView):
return top_apps_using_warehouse
def median_time_to_first_api_call(self, from_date, to_date):
if 2>1:
return 0
form = self.get_form()
new_apps_list = []
apps = []
apps_list = []
urlpath_consumers = '/management/consumers'
api = API(self.request.session.get('obp'))
apicaches=None
try:
apps = api.get(urlpath_consumers)
if apps is not None and 'code' in apps and apps['code']==403:
error_once_only(self.request, apps['message'])
else:
apps_list = apps["list"]
except APIError as err:
error_once_only(self.request, err)
apicaches=cache.get('consumers,{}'.format(self.request.session.get('obp')['authenticator_kwargs']['token']))
except Exception as err:
error_once_only(self.request, 'Unknown Error. {}'.format(type(err).__name__))
apicaches=None
if not apicaches is None:
apps_list=apicaches
else:
api = API(self.request.session.get('obp'))
try:
apps = api.get(urlpath_consumers)
if apps is not None and 'code' in apps and apps['code']==403:
error_once_only(self.request, apps['message'])
else:
apps_list = apps["consumers"]
cache.set('consumers,{}'.format(self.request.session.get('obp')['authenticator_kwargs']['token']),apps_list)
except APIError as err:
error_once_only(self.request, err)
except Exception as err:
error_once_only(self.request, 'Unknown Error. {}'.format(type(err).__name__))
for app in apps_list:
created_date = datetime.datetime.strptime(app['created'], '%Y-%m-%dT%H:%M:%SZ')
@ -744,17 +861,32 @@ class MetricsSummaryView(LoginRequiredMixin, TemplateView):
times_to_first_call = []
strfrom_date=datetime.datetime.strptime(from_date, API_DATEFORMAT)
strto_date=datetime.datetime.strptime(to_date, API_DATEFORMAT)
for app in new_apps_list:
urlpath_metrics = '/management/metrics?from_date={}&to_date={}&consumer_id={}&sort_by={}&direction={}&limit={}'.format(
from_date, to_date, app['consumer_id'], 'date', 'asc', '1')
api = API(self.request.session.get('obp'))
try:
metrics = api.get(urlpath_metrics)
if metrics is not None and 'code' in metrics and metrics['code'] == 403:
error_once_only(self.request, metrics['message'])
metrics = []
apicaches=None
try:
apicaches=cache.get('metrics,{},{},{},{}'.format(self.request.session.get('obp')['authenticator_kwargs']['token'],app['consumer_id'],strfrom_date.strftime(CACHE_DATEFORMAT), strto_date.strftime(CACHE_DATEFORMAT)))
except Exception as err:
apicaches=None
metrics=[]
if not apicaches is None:
metrics=apicaches
else:
metrics = list(metrics['metrics'])
metrics = api.get(urlpath_metrics)
if metrics is not None and 'code' in metrics and metrics['code'] == 403:
error_once_only(self.request, metrics['message'])
if(metrics['message'].startswith('OBP-20006')):
break
metrics = []
else:
metrics = list(metrics['metrics'])
cache.set('metrics,{},{},{},{}'.format(self.request.session.get('obp')['authenticator_kwargs']['token'],app['consumer_id'],strfrom_date.strftime(CACHE_DATEFORMAT), strto_date.strftime(CACHE_DATEFORMAT)),metrics,CACHE_TIME if strfrom_date.minute==0 and strfrom_date.second==0 and strto_date.minute==0 and strto_date.second==0 else CACHE_TIME_SHORT)
if metrics:
time_difference = datetime.datetime.strptime(metrics[0]['date'], '%Y-%m-%dT%H:%M:%S.%fZ') - datetime.datetime.strptime(app['created'], '%Y-%m-%dT%H:%M:%SZ')
times_to_first_call.append(time_difference.total_seconds())
@ -780,19 +912,21 @@ class MetricsSummaryView(LoginRequiredMixin, TemplateView):
to_date = to_date.strftime(API_DATEFORMAT)
from_date = (datetime.datetime.strptime(to_date, API_DATEFORMAT) - timedelta(30)).strftime(API_DATEFORMAT)
context = super(MetricsSummaryView, self).get_context_data(**kwargs)
api_host_name = API_HOST
top_apps_using_warehouse = self.get_top_apps_using_warehouse(from_date, to_date)
user_email_cansearchwarehouse, number_of_users_with_cansearchwarehouse = self.get_users_cansearchwarehouse()
median_time_to_first_api_call = self.median_time_to_first_api_call(from_date, to_date)
if form.is_valid():
top_apis = self.get_top_apis(form.cleaned_data, from_date, to_date)
top_apis_bar_chart = self.plot_bar_chart(top_apis)
top_consumers=self.get_top_consumers(form.cleaned_data, from_date, to_date)
top_consumers_bar_chart = self.plot_topconsumer_bar_chart(top_consumers)
top_warehouse_calls = self.get_top_warehouse_calls(form.cleaned_data, from_date, to_date)
api_calls, average_response_time, average_calls_per_day = self.get_aggregate_metrics(form.cleaned_data, from_date, to_date)
calls_by_api_explorer, average_response_time_api_explorer, average_calls_per_day_api_explorer = self.get_aggregate_metrics_api_explorer(from_date, to_date)
calls_per_month_list, calls_per_month, date_list = self.calls_per_month(form.cleaned_data, from_date, to_date)
#calls_per_month_list, calls_per_month, date_list = self.calls_per_month(form.cleaned_data, from_date, to_date)
calls_per_day_list, calls_per_day, date_list = self.calls_per_day(form.cleaned_data, from_date, to_date)
per_day_chart = self.plot_line_chart(calls_per_day, date_list, "day")
unique_app_names, number_of_apps_with_unique_app_name, number_of_apps_with_unique_developer_email = self.get_total_number_of_apps(form.cleaned_data, from_date, to_date)
@ -801,7 +935,7 @@ class MetricsSummaryView(LoginRequiredMixin, TemplateView):
context.update({
'api_calls': api_calls,
'calls_by_api_explorer': calls_by_api_explorer,
'calls_per_month_list': calls_per_month_list,
#'calls_per_month_list': calls_per_month_list,
'per_day_chart': per_day_chart,
'number_of_apps_with_unique_app_name': number_of_apps_with_unique_app_name,
'number_of_apps_with_unique_developer_email': number_of_apps_with_unique_developer_email,
@ -816,6 +950,7 @@ class MetricsSummaryView(LoginRequiredMixin, TemplateView):
'from_date': (datetime.datetime.strptime(from_date, API_DATEFORMAT)).strftime('%Y-%m-%d'),
'to_date': (datetime.datetime.strptime(to_date, API_DATEFORMAT)).strftime('%Y-%m-%d'),
'top_apis_bar_chart': top_apis_bar_chart,
'top_consumers_bar_chart':top_consumers_bar_chart,
'median_time_to_first_api_call': median_time_to_first_api_call,
'form': form,
'excluded_apps':EXCLUDE_APPS,
@ -833,7 +968,7 @@ class YearlySummaryView(MetricsSummaryView):
to_date = to_date.strftime(API_DATEFORMAT)
from_date = (datetime.datetime.strptime(to_date, API_DATEFORMAT) - timedelta(365)).strftime(API_DATEFORMAT)
context = super(YearlySummaryView, self).get_context_data(**kwargs)
context = super(MetricsSummaryView, self).get_context_data(**kwargs)
api_host_name = API_HOST
top_apps_using_warehouse = self.get_top_apps_using_warehouse(from_date, to_date)
user_email_cansearchwarehouse, number_of_users_with_cansearchwarehouse = self.get_users_cansearchwarehouse()
@ -848,6 +983,8 @@ class YearlySummaryView(MetricsSummaryView):
active_apps_names = self.get_active_apps(form.cleaned_data, from_date, to_date)
top_apis = self.get_top_apis(form.cleaned_data, from_date, to_date)
top_apis_bar_chart = self.plot_bar_chart(top_apis)
top_consumers=self.get_top_consumers(form.cleaned_data, from_date, to_date)
top_consumers_bar_chart = self.plot_topconsumer_bar_chart(top_consumers)
top_warehouse_calls = self.get_top_warehouse_calls(form.cleaned_data, from_date, to_date)
context.update({
@ -869,6 +1006,7 @@ class YearlySummaryView(MetricsSummaryView):
'from_date': (datetime.datetime.strptime(from_date, API_DATEFORMAT)).strftime('%Y-%m-%d'),
'to_date': (datetime.datetime.strptime(to_date, API_DATEFORMAT)).strftime('%Y-%m-%d'),
'top_apis_bar_chart': top_apis_bar_chart,
'top_consumers_bar_chart':top_consumers_bar_chart,
'median_time_to_first_api_call': median_time_to_first_api_call,
'form': form,
'excluded_apps':EXCLUDE_APPS,
@ -886,7 +1024,7 @@ class QuarterlySummaryView(MetricsSummaryView):
to_date = to_date.strftime(API_DATEFORMAT)
from_date = (datetime.datetime.strptime(to_date, API_DATEFORMAT) - timedelta(90)).strftime(API_DATEFORMAT)
context = super(QuarterlySummaryView, self).get_context_data(**kwargs)
context = super(MetricsSummaryView, self).get_context_data(**kwargs)
api_host_name = API_HOST
top_apps_using_warehouse = self.get_top_apps_using_warehouse(from_date, to_date)
user_email_cansearchwarehouse, number_of_users_with_cansearchwarehouse = self.get_users_cansearchwarehouse()
@ -903,6 +1041,8 @@ class QuarterlySummaryView(MetricsSummaryView):
active_apps_names = self.get_active_apps(form.cleaned_data, from_date, to_date)
top_apis = self.get_top_apis(form.cleaned_data, from_date, to_date)
top_apis_bar_chart = self.plot_bar_chart(top_apis)
top_consumers=self.get_top_consumers(form.cleaned_data, from_date, to_date)
top_consumers_bar_chart = self.plot_topconsumer_bar_chart(top_consumers)
top_warehouse_calls = self.get_top_warehouse_calls(form.cleaned_data, from_date, to_date)
context.update({
@ -926,6 +1066,7 @@ class QuarterlySummaryView(MetricsSummaryView):
'from_date': (datetime.datetime.strptime(from_date, API_DATEFORMAT)).strftime('%Y-%m-%d'),
'to_date': (datetime.datetime.strptime(to_date, API_DATEFORMAT)).strftime('%Y-%m-%d'),
'top_apis_bar_chart': top_apis_bar_chart,
'top_consumers_bar_chart':top_consumers_bar_chart,
'median_time_to_first_api_call': median_time_to_first_api_call,
'form': form,
'excluded_apps':EXCLUDE_APPS,
@ -943,7 +1084,7 @@ class WeeklySummaryView(MetricsSummaryView):
to_date = to_date.strftime(API_DATEFORMAT)
from_date = (datetime.datetime.strptime(to_date, API_DATEFORMAT) - timedelta(7)).strftime(API_DATEFORMAT)
context = super(WeeklySummaryView, self).get_context_data(**kwargs)
context = super(MetricsSummaryView, self).get_context_data(**kwargs)
api_host_name = API_HOST
top_apps_using_warehouse = self.get_top_apps_using_warehouse(from_date, to_date)
user_email_cansearchwarehouse, number_of_users_with_cansearchwarehouse = self.get_users_cansearchwarehouse()
@ -959,6 +1100,8 @@ class WeeklySummaryView(MetricsSummaryView):
active_apps_names = self.get_active_apps(form.cleaned_data, from_date, to_date)
top_apis = self.get_top_apis(form.cleaned_data, from_date, to_date)
top_apis_bar_chart = self.plot_bar_chart(top_apis)
top_consumers=self.get_top_consumers(form.cleaned_data, from_date, to_date)
top_consumers_bar_chart = self.plot_topconsumer_bar_chart(top_consumers)
top_warehouse_calls = self.get_top_warehouse_calls(form.cleaned_data, from_date, to_date)
context.update({
@ -979,6 +1122,7 @@ class WeeklySummaryView(MetricsSummaryView):
'from_date': (datetime.datetime.strptime(from_date, API_DATEFORMAT)).strftime('%Y-%m-%d'),
'to_date': (datetime.datetime.strptime(to_date, API_DATEFORMAT)).strftime('%Y-%m-%d'),
'top_apis_bar_chart': top_apis_bar_chart,
'top_consumers_bar_chart':top_consumers_bar_chart,
# ##'calls_per_half_day': calls_per_half_day,
'median_time_to_first_api_call': median_time_to_first_api_call,
'form': form,
@ -1015,6 +1159,8 @@ class DailySummaryView(MetricsSummaryView):
active_apps_names = self.get_active_apps(form.cleaned_data, from_date, to_date)
top_apis = self.get_top_apis(form.cleaned_data, from_date, to_date)
top_apis_bar_chart = self.plot_bar_chart(top_apis)
top_consumers=self.get_top_consumers(form.cleaned_data, from_date, to_date)
top_consumers_bar_chart = self.plot_topconsumer_bar_chart(top_consumers)
top_warehouse_calls = self.get_top_warehouse_calls(form.cleaned_data, from_date, to_date)
context.update({
@ -1036,6 +1182,7 @@ class DailySummaryView(MetricsSummaryView):
'from_date': (datetime.datetime.strptime(from_date, API_DATEFORMAT)).strftime('%Y-%m-%d'),
'to_date': (datetime.datetime.strptime(to_date, API_DATEFORMAT)).strftime('%Y-%m-%d'),
'top_apis_bar_chart': top_apis_bar_chart,
'top_consumers_bar_chart':top_consumers_bar_chart,
'median_time_to_first_api_call': median_time_to_first_api_call,
'form': form,
'excluded_apps':EXCLUDE_APPS,
@ -1059,7 +1206,7 @@ class CustomSummaryView(MetricsSummaryView):
from_date = datetime.datetime.strptime(form.data['from_date_custom'], '%Y-%m-%d %H:%M:%S')
from_date = from_date.strftime(API_DATEFORMAT)
context = super(CustomSummaryView, self).get_context_data(**kwargs)
context = super(MetricsSummaryView, self).get_context_data(**kwargs)
api_host_name = API_HOST
top_apps_using_warehouse = self.get_top_apps_using_warehouse(from_date, to_date)
user_email_cansearchwarehouse, number_of_users_with_cansearchwarehouse = self.get_users_cansearchwarehouse()
@ -1076,6 +1223,8 @@ class CustomSummaryView(MetricsSummaryView):
active_apps_names = self.get_active_apps(form.cleaned_data, from_date, to_date)
top_apis = self.get_top_apis(form.cleaned_data, from_date, to_date)
top_apis_bar_chart = self.plot_bar_chart(top_apis)
top_consumers=self.get_top_consumers(form.cleaned_data, from_date, to_date)
top_consumers_bar_chart = self.plot_topconsumer_bar_chart(top_consumers)
top_warehouse_calls = self.get_top_warehouse_calls(form.cleaned_data, from_date, to_date)
context.update({
@ -1096,6 +1245,7 @@ class CustomSummaryView(MetricsSummaryView):
'from_date': (datetime.datetime.strptime(from_date, API_DATEFORMAT)).strftime('%Y-%m-%d'),
'to_date': (datetime.datetime.strptime(to_date, API_DATEFORMAT)).strftime('%Y-%m-%d'),
'top_apis_bar_chart': top_apis_bar_chart,
'top_consumers_bar_chart':top_consumers_bar_chart,
'median_time_to_first_api_call': median_time_to_first_api_call,
# ##'calls_per_day': calls_per_day,
# ##'calls_per_half_day': calls_per_half_day,

View File

@ -136,7 +136,7 @@ class API(object):
return response.text
else:
data = response.json()
if 'error' in data:
if isinstance(data,dict) and 'error' in data:
self.handle_response_error(prefix, data['error'])
return data

View File

@ -2,6 +2,7 @@ from django.contrib import messages
import functools
from obp.api import API, APIError
from django.http import JsonResponse
import traceback
def error_once_only(request, err):
"""
@ -19,7 +20,7 @@ def exception_handle(fn):
def wrapper(request, *args, **kwargs):
try:
result = fn(request, *args, **kwargs)
if 'code' in result and result['code'] >= 400:
if isinstance(result,dict) and 'code' in result and result['code'] >= 400:
error_once_only(request, result['message'])
else:
msg = 'Submit successfully!'

View File

@ -5,9 +5,7 @@ $(document).ready(function($) {
var runner = t.parent().parent().parent();
var web_ui_props_name = $(runner).find('.web_ui_props_name').text();
var web_ui_props_value = $(runner).find('.web_ui_props_value').val();
$('.dynamic-message').each(function(i, d_msg){
$(d_msg).remove();
});
var webui = $('#webui');
if(web_ui_props_value.trim() === '') {
$('<div class="alert alert-dismissible alert-danger dynamic-message" role="alert">\n' +
@ -17,6 +15,7 @@ $(document).ready(function($) {
).insertBefore(webui);
return;
}
/*
t.attr("disabled","disabled").toggleClass("disabled");
t.next().attr("disabled","disabled").toggleClass("disabled");
$.ajax({
@ -48,22 +47,30 @@ $(document).ready(function($) {
t.next().removeAttr("disabled").toggleClass("disabled")
}
});
*/
$('.runner button.forSave').attr("disabled","disabled");
$('.runner button.forDelete').attr("disabled","disabled");
$.post('/webui/save/method', {
'web_ui_props_name': web_ui_props_name,
'web_ui_props_value': web_ui_props_value,
}, function (response) {
location.reload();
});
});
$('.runner button.forDelete').click(function() {
$('.runner button.forDelete').click(function(e) {
e.preventDefault();
var t = $(this);
var runner = t.parent().parent().parent();
var web_ui_props_name = $(runner).find('.web_ui_props_name').text();
var textArea = runner.find('.web_ui_props_value');
var props_id = $(runner).find('.web_ui_props_id');
var web_ui_props_id = props_id.val();
$('.dynamic-message').each(function(i, d_msg){
$(d_msg).remove();
});
var webui = $('#webui');
/*
t.attr("disabled","disabled").toggleClass("disabled");
t.next().attr("disabled","disabled").toggleClass("disabled");
$.ajax({
type: 'POST',
url: '/webui/delete/method',
@ -93,5 +100,13 @@ $(document).ready(function($) {
t.next().removeAttr("disabled").toggleClass("disabled")
}
});
*/
$('.runner button.forSave').attr("disabled","disabled");
$('.runner button.forDelete').attr("disabled","disabled");
$.post('/webui/delete/method', {
'web_ui_props_id': web_ui_props_id
}, function (response) {
location.reload();
});
});
});

View File

@ -8,13 +8,16 @@
<div id="webui_list">
<h1>WEBUI</h1>
<div class="row">
<div class="col-xs-12 col-sm-4">
<div class="col-xs-12 col-sm-3">
<label class="form-group">Web UI Props Name:</label> <br>
</div>
<div class="col-xs-12 col-sm-4">
<div class="col-xs-12 col-sm-3">
<label class="form-group">Web UI Props Value:</label> <br>
</div>
<div class="col-sm-12 col-sm-4">
<div class="col-xs-12 col-sm-3">
<label class="form-group">Web UI Props Id:</label> <br>
</div>
<div class="col-sm-12 col-sm-3">
<label class="form-group">Save Value:</label> <br>
</div>
</div>
@ -24,18 +27,22 @@
<div class="runner">
<div class="row">
<input type="hidden" class="web_ui_props_id" value="{{ webui_prop.web_ui_props_id }}"/>
<div class="col-xs-12 col-sm-4">
<div class="col-xs-12 col-sm-3">
<div class="form-group" cols="20" rows="1">
<div class="web_ui_props_name">{{ webui_prop.name }}</div>
</div>
</div>
<div class="col-xs-12 col-sm-4">
<div class="col-xs-12 col-sm-3">
<div class="form-group">
<textarea class="form-control required web_ui_props_value" cols="40"
rows="1">{{ webui_prop.value }}</textarea>
</div>
</div>
<div class="col-sm-12 col-sm-4">
<div class="col-xs-12 col-sm-3">
<div class="form-group" cols="1" rows="1"><div
class="">{{ webui_prop.web_ui_props_id }}</div></div>
</div>
<div class="col-sm-12 col-sm-3">
<div class="form-group">
<button class="btn btn-primary btn-green forSave">Save</button>
<button class="btn btn-primary btn-red forDelete">Delete</button>
@ -54,7 +61,6 @@
{% endblock extrajs %}
{% block extracss %}
<link href="{% static 'webui/css/webui.css' %}" rel="stylesheet">
{% endblock extracss %}

View File

@ -13,19 +13,7 @@ from django.http import JsonResponse
from .forms import WebuiForm
from django.urls import reverse_lazy
from django.views.decorators.csrf import csrf_exempt
def error_once_only(request, err):
"""
Just add the error once
:param request:
:param err:
:return:
"""
storage = messages.get_messages(request)
if str(err) not in [str(m.message) for m in storage]:
messages.error(request, err)
from utils.ErrorHandler import exception_handle, error_once_only
class IndexView(LoginRequiredMixin, FormView):
"""Index view for config"""
@ -58,6 +46,7 @@ class IndexView(LoginRequiredMixin, FormView):
# }
# ]
# }
#print(response)
context.update(response)
except APIError as err:
messages.error(self.request, Exception("The OBP-API server is not running or does not respond properly."
@ -71,7 +60,7 @@ class IndexView(LoginRequiredMixin, FormView):
form = super(IndexView, self).get_form(*args, **kwargs)
return form
@exception_handle
@csrf_exempt
def webui_save(request):
web_ui_props_name = request.POST.get('web_ui_props_name')
@ -81,58 +70,22 @@ def webui_save(request):
'name': web_ui_props_name,
'value': web_ui_props_value
}
response = __send_request(request, '/management/webui_props', 'post', payload)
status_code = response['code']
errors = [str(m.message) for m in messages.get_messages(request)]
response = JsonResponse(
{'code': status_code, 'errors': errors, 'web_ui_props_id': response['result'].get('web_ui_props_id')})
response.status_code = status_code
api = API(request.session.get('obp'))
urlpath = '/management/webui_props'
response = api.post(urlpath, payload=payload)
return response
@exception_handle
@csrf_exempt
def webui_delete(request):
web_ui_props_id = request.POST.get('web_ui_props_id')
web_ui_props_name = request.POST.get('web_ui_props_name')
if web_ui_props_id == 'default' or web_ui_props_id == '' or web_ui_props_id is None:
status_code = 200
return {'code':403,'message':'Cann\'t delete web_ui_props_id default'}
else:
status_code = __send_request(request, '/management/webui_props/' + web_ui_props_id, 'delete')['code']
default_value = ''
if 200 <= status_code <= 299:
all_webui = __send_request(request, '/management/webui_props?active=true', 'get')
status_code = all_webui['code']
for v in all_webui['result']['webui_props']:
if v['name'] == web_ui_props_name:
default_value = v['value']
break
errors = [str(m.message) for m in messages.get_messages(request)]
response = JsonResponse({'code': status_code, 'errors': errors, 'default_value': default_value})
response.status_code = status_code
return response
def __send_request(request, url, method_name, payload=None):
api = API(request.session.get('obp'))
code = 200
try:
if payload:
result = getattr(api, method_name)(url, payload=payload)
else:
result = getattr(api, method_name)(url)
except APIError as err:
code = 500
error_once_only(request, APIError(Exception("The OBP-API server is not running or does not respond properly."
"Please check OBP-API server. Details: " + str(err))))
except Exception as err:
code = 500
error_once_only(request, "Unknown Error. Details: " + str(err))
if 'code' in result and result['code'] >= 400:
code = int(result['code'])
error_once_only(request, result['message'])
return {'code': code, 'result': result}
api = API(request.session.get('obp'))
urlpath = '/management/webui_props/{}'.format(web_ui_props_id)
result = api.delete(urlpath)
print(result)
return result