[homeconnect] Show response code in request detail modal and JS code cleanup (#10695) (#10914)

Signed-off-by: Jonas Brüstel <jonas@bruestel.net>
This commit is contained in:
bruestel 2021-07-06 19:49:56 +02:00 committed by GitHub
parent 58f20c5028
commit c0a2a4b104
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 71 deletions

View File

@ -163,9 +163,7 @@ public class HomeConnectServlet extends HttpServlet {
String path = request.getPathInfo(); String path = request.getPathInfo();
if (path == null || path.isEmpty() || path.equals(ROOT_PATH)) { if (path == null || path.isEmpty() || path.equals(ROOT_PATH)) {
@Nullable
String code = request.getParameter(PARAM_CODE); String code = request.getParameter(PARAM_CODE);
@Nullable
String state = request.getParameter(PARAM_STATE); String state = request.getParameter(PARAM_STATE);
if (code != null && state != null && !code.trim().isEmpty() && !state.trim().isEmpty()) { if (code != null && state != null && !code.trim().isEmpty() && !state.trim().isEmpty()) {
getBridgeAuthenticationPage(request, response, code, state); getBridgeAuthenticationPage(request, response, code, state);
@ -173,9 +171,7 @@ public class HomeConnectServlet extends HttpServlet {
getBridgesPage(request, response); getBridgesPage(request, response);
} }
} else if (pathMatches(path, APPLIANCES_PATH)) { } else if (pathMatches(path, APPLIANCES_PATH)) {
@Nullable
String action = request.getParameter(PARAM_ACTION); String action = request.getParameter(PARAM_ACTION);
@Nullable
String thingId = request.getParameter(PARAM_THING_ID); String thingId = request.getParameter(PARAM_THING_ID);
if (action != null && thingId != null && !action.trim().isEmpty() && !thingId.trim().isEmpty()) { if (action != null && thingId != null && !action.trim().isEmpty() && !thingId.trim().isEmpty()) {
processApplianceActions(response, action, thingId); processApplianceActions(response, action, thingId);
@ -183,9 +179,7 @@ public class HomeConnectServlet extends HttpServlet {
getAppliancesPage(request, response); getAppliancesPage(request, response);
} }
} else if (pathMatches(path, REQUEST_LOG_PATH)) { } else if (pathMatches(path, REQUEST_LOG_PATH)) {
@Nullable
String export = request.getParameter(PARAM_EXPORT); String export = request.getParameter(PARAM_EXPORT);
@Nullable
String bridgeId = request.getParameter(PARAM_BRIDGE_ID); String bridgeId = request.getParameter(PARAM_BRIDGE_ID);
if (export != null && bridgeId != null && !export.trim().isEmpty() && !bridgeId.trim().isEmpty()) { if (export != null && bridgeId != null && !export.trim().isEmpty() && !bridgeId.trim().isEmpty()) {
getRequestLogExport(response, bridgeId); getRequestLogExport(response, bridgeId);
@ -193,9 +187,7 @@ public class HomeConnectServlet extends HttpServlet {
getRequestLogPage(request, response); getRequestLogPage(request, response);
} }
} else if (pathMatches(path, EVENT_LOG_PATH)) { } else if (pathMatches(path, EVENT_LOG_PATH)) {
@Nullable
String export = request.getParameter(PARAM_EXPORT); String export = request.getParameter(PARAM_EXPORT);
@Nullable
String bridgeId = request.getParameter(PARAM_BRIDGE_ID); String bridgeId = request.getParameter(PARAM_BRIDGE_ID);
if (export != null && bridgeId != null && !export.trim().isEmpty() && !bridgeId.trim().isEmpty()) { if (export != null && bridgeId != null && !export.trim().isEmpty() && !bridgeId.trim().isEmpty()) {
getEventLogExport(response, bridgeId); getEventLogExport(response, bridgeId);
@ -225,11 +217,8 @@ public class HomeConnectServlet extends HttpServlet {
} }
} else if (pathMatches(path, APPLIANCES_PATH)) { } else if (pathMatches(path, APPLIANCES_PATH)) {
String requestPayload = request.getReader().lines().collect(Collectors.joining(System.lineSeparator())); String requestPayload = request.getReader().lines().collect(Collectors.joining(System.lineSeparator()));
@Nullable
String action = request.getParameter(PARAM_ACTION); String action = request.getParameter(PARAM_ACTION);
@Nullable
String thingId = request.getParameter(PARAM_THING_ID); String thingId = request.getParameter(PARAM_THING_ID);
@Nullable
String targetPath = request.getParameter(PARAM_PATH); String targetPath = request.getParameter(PARAM_PATH);
if ((ACTION_PUT_RAW.equals(action) || ACTION_GET_RAW.equals(action)) && thingId != null if ((ACTION_PUT_RAW.equals(action) || ACTION_GET_RAW.equals(action)) && thingId != null
@ -366,7 +355,6 @@ public class HomeConnectServlet extends HttpServlet {
String actionResponse = bridgeHandler.get().getApiClient().putRaw(haId, path, body); String actionResponse = bridgeHandler.get().getApiClient().putRaw(haId, path, body);
response.getWriter().write(actionResponse); response.getWriter().write(actionResponse);
} else if (ACTION_GET_RAW.equals(action)) { } else if (ACTION_GET_RAW.equals(action)) {
@Nullable
String actionResponse = bridgeHandler.get().getApiClient().getRaw(haId, path, true); String actionResponse = bridgeHandler.get().getApiClient().getRaw(haId, path, true);
if (actionResponse == null) { if (actionResponse == null) {
response.getWriter().write("{\"status\": \"No response\"}"); response.getWriter().write("{\"status\": \"No response\"}");
@ -393,9 +381,7 @@ public class HomeConnectServlet extends HttpServlet {
} }
private void postBridgesPage(HttpServletRequest request, HttpServletResponse response) throws IOException { private void postBridgesPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
@Nullable
String action = request.getParameter(PARAM_ACTION); String action = request.getParameter(PARAM_ACTION);
@Nullable
String bridgeId = request.getParameter(PARAM_BRIDGE_ID); String bridgeId = request.getParameter(PARAM_BRIDGE_ID);
Optional<HomeConnectBridgeHandler> bridgeHandlerOptional = bridgeHandlers.stream().filter( Optional<HomeConnectBridgeHandler> bridgeHandlerOptional = bridgeHandlers.stream().filter(
homeConnectBridgeHandler -> homeConnectBridgeHandler.getThing().getUID().toString().equals(bridgeId)) homeConnectBridgeHandler -> homeConnectBridgeHandler.getThing().getUID().toString().equals(bridgeId))

View File

@ -1,4 +1,4 @@
/* globals Chart:false, feather:false, Plotly:false, requests:false */ /* globals Chart:false, feather:false, requests:false */
(function () { (function () {
'use strict' 'use strict'
@ -112,6 +112,7 @@
var modal = $(this); var modal = $(this);
var requestBodyElement = modal.find('.modal-request-body'); var requestBodyElement = modal.find('.modal-request-body');
var title = modal.find('.modal-title'); var title = modal.find('.modal-title');
var titleBadgeElement = modal.find('.modal-title-badge');
var responseBodyElement = modal.find('.modal-response-body'); var responseBodyElement = modal.find('.modal-response-body');
var requestHeaderElement = modal.find('.modal-request-header'); var requestHeaderElement = modal.find('.modal-request-header');
var responseHeaderElement = modal.find('.modal-response-header'); var responseHeaderElement = modal.find('.modal-response-header');
@ -134,6 +135,23 @@
responseBodyElement.addClass('text-muted') responseBodyElement.addClass('text-muted')
} }
titleBadgeElement.empty();
if (request.homeConnectResponse) {
var statusCode = request.homeConnectResponse.code;
titleBadgeElement.text(statusCode);
titleBadgeElement.removeClass('badge-success');
titleBadgeElement.removeClass('badge-danger');
titleBadgeElement.removeClass('badge-warning');
if (statusCode >= 300 && statusCode != 404) {
titleBadgeElement.addClass('badge-danger');
} else if (statusCode >= 200 && statusCode < 300) {
titleBadgeElement.addClass('badge-success');
} else {
titleBadgeElement.addClass('badge-warning');
}
}
responseHeaderElement.empty(); responseHeaderElement.empty();
if (request.homeConnectResponse && request.homeConnectResponse.header) { if (request.homeConnectResponse && request.homeConnectResponse.header) {
var responseHeader = request.homeConnectResponse.header; var responseHeader = request.homeConnectResponse.header;
@ -159,60 +177,4 @@
$('.reload-page').click(function () { $('.reload-page').click(function () {
location.reload(); location.reload();
}); });
$('.request-chart').each(function (index, element) {
var bridgeId = $(this).data('bridge-id');
var chartElement = element;
function makeplot (bridgeId, chartElement) {
Plotly.d3.csv('requests?bridgeId=' + bridgeId + '&action=request-csv', function (data) {
processData(data, chartElement)
});
}
function processData (allRows, chartElement) {
console.log(allRows);
var x = [], y = [], standardDeviation = [];
for (var i = 0; i < allRows.length; i++) {
var row = allRows[i];
x.push(row['time']);
y.push(row['requests']);
}
console.log('X', x, 'Y', y, 'SD', standardDeviation);
makePlotly(x, y, standardDeviation, chartElement);
}
function makePlotly (x, y, standard_deviation, chartElement){
var traces = [{
x: x,
y: y,
type: 'histogram',
histfunc: 'sum',
xbins: {
size: 1000
}
}];
Plotly.newPlot(chartElement, traces,
{
xaxis: {
rangemode: 'nonnegative',
autorange: true,
title: '',
type: 'date'
},
yaxis: {
title: 'requests',
rangemode: 'nonnegative'
}
},
{
displayModeBar: false,
responsive: true
});
}
makeplot(bridgeId, chartElement);
});
}()) }())

View File

@ -60,6 +60,7 @@
<div class="modal-dialog modal-lg" role="document"> <div class="modal-dialog modal-lg" role="document">
<div class="modal-content"> <div class="modal-content">
<div class="modal-header"> <div class="modal-header">
<span class="modal-title-badge badge mt-2 mr-1">200</span>
<h5 class="modal-title .text-truncate" id="requestDetailModalLabel">Request Details</h5> <h5 class="modal-title .text-truncate" id="requestDetailModalLabel">Request Details</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close"> <button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span> <span aria-hidden="true">&times;</span>