From 7b81d24e135c877cb98f4fa52867a0e7b7dea608 Mon Sep 17 00:00:00 2001 From: huanggze Date: Sun, 30 Jun 2019 19:46:50 +0800 Subject: [PATCH] remove buggy if statements Signed-off-by: huanggze --- pkg/apiserver/monitoring/monitoring.go | 52 ++++++++------------------ 1 file changed, 16 insertions(+), 36 deletions(-) diff --git a/pkg/apiserver/monitoring/monitoring.go b/pkg/apiserver/monitoring/monitoring.go index 3612fc40..4410e422 100644 --- a/pkg/apiserver/monitoring/monitoring.go +++ b/pkg/apiserver/monitoring/monitoring.go @@ -18,6 +18,7 @@ package monitoring import ( + "fmt" "github.com/emicklei/go-restful" "kubesphere.io/kubesphere/pkg/models/metrics" "kubesphere.io/kubesphere/pkg/simple/client/prometheus" @@ -42,26 +43,16 @@ func MonitorSpecificPodOnSpecificNode(request *restful.Request, response *restfu func MonitorPod(request *restful.Request, response *restful.Response) { requestParams := prometheus.ParseMonitoringRequestParams(request) podName := requestParams.PodName - metricName := requestParams.MetricsName if podName != "" { - // single pod single metric - queryType, params, nullRule := metrics.AssemblePodMetricRequestInfo(requestParams, metricName) - var res *metrics.FormatedMetric - if !nullRule { - metricsStr := prometheus.SendMonitoringRequest(prometheus.PrometheusEndpoint, queryType, params) - res = metrics.ReformatJson(metricsStr, metricName, map[string]string{metrics.MetricLevelPodName: ""}) - } - response.WriteAsJson(res) - - } else { - // multiple - rawMetrics := metrics.GetPodLevelMetrics(requestParams) - // sorting - sortedMetrics, maxMetricCount := metrics.Sort(requestParams.SortMetricName, requestParams.SortType, rawMetrics) - // paging - pagedMetrics := metrics.Page(requestParams.PageNum, requestParams.LimitNum, sortedMetrics, maxMetricCount) - response.WriteAsJson(pagedMetrics) + requestParams.ResourcesFilter = fmt.Sprintf("^%s$", requestParams.PodName) } + + rawMetrics := metrics.GetPodLevelMetrics(requestParams) + // sorting + sortedMetrics, maxMetricCount := metrics.Sort(requestParams.SortMetricName, requestParams.SortType, rawMetrics) + // paging + pagedMetrics := metrics.Page(requestParams.PageNum, requestParams.LimitNum, sortedMetrics, maxMetricCount) + response.WriteAsJson(pagedMetrics) } func MonitorAllContainersOnSpecificNode(request *restful.Request, response *restful.Response) { @@ -78,21 +69,13 @@ func MonitorSpecificContainerOfSpecificNamespace(request *restful.Request, respo func MonitorContainer(request *restful.Request, response *restful.Response) { requestParams := prometheus.ParseMonitoringRequestParams(request) - metricName := requestParams.MetricsName - if requestParams.MetricsFilter != "" { - rawMetrics := metrics.GetContainerLevelMetrics(requestParams) - // sorting - sortedMetrics, maxMetricCount := metrics.Sort(requestParams.SortMetricName, requestParams.SortType, rawMetrics) - // paging - pagedMetrics := metrics.Page(requestParams.PageNum, requestParams.LimitNum, sortedMetrics, maxMetricCount) - - response.WriteAsJson(pagedMetrics) - - } else { - res := metrics.MonitorContainer(requestParams, metricName) - response.WriteAsJson(res) - } + rawMetrics := metrics.GetContainerLevelMetrics(requestParams) + // sorting + sortedMetrics, maxMetricCount := metrics.Sort(requestParams.SortMetricName, requestParams.SortType, rawMetrics) + // paging + pagedMetrics := metrics.Page(requestParams.PageNum, requestParams.LimitNum, sortedMetrics, maxMetricCount) + response.WriteAsJson(pagedMetrics) } func MonitorSpecificWorkload(request *restful.Request, response *restful.Response) { @@ -132,7 +115,7 @@ func MonitorAllWorkspaces(request *restful.Request, response *restful.Response) res := metrics.GetAllWorkspacesStatistics() response.WriteAsJson(res) - } else if tp == "rank" { + } else { rawMetrics := metrics.MonitorAllWorkspaces(requestParams) // sorting @@ -142,9 +125,6 @@ func MonitorAllWorkspaces(request *restful.Request, response *restful.Response) pagedMetrics := metrics.Page(requestParams.PageNum, requestParams.LimitNum, sortedMetrics, maxMetricCount) response.WriteAsJson(pagedMetrics) - } else { - rawMetrics := metrics.MonitorAllWorkspaces(requestParams) - response.WriteAsJson(rawMetrics) } } -- GitLab