From c65b510f1795f72d98c601ba69f3ccbbd5e487ca Mon Sep 17 00:00:00 2001 From: Filip Barl Date: Tue, 20 Jun 2017 11:45:04 +0200 Subject: [PATCH] Addressed the comments. --- app/api_topologies.go | 23 +++++++++++++---------- app/api_topology.go | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/app/api_topologies.go b/app/api_topologies.go index 334ecdd596..bb16836fe9 100644 --- a/app/api_topologies.go +++ b/app/api_topologies.go @@ -415,14 +415,17 @@ type topologyStats struct { FilteredNodes int `json:"filtered_nodes"` } -// deserializedTimestamp converts the ISO8601 query param into a proper timestamp. -func deserializedTimestamp(timestampStr string) time.Time { - // If no timestamp is given, assume the current time. - timestamp := time.Now() - if timestampStr != "" { - timestamp, _ = time.Parse(time.RFC3339, timestampStr) - } - return timestamp +// deserializeTimestamp converts the ISO8601 query param into a proper timestamp. +func deserializeTimestamp(timestamp string) time.Time { + if timestamp != "" { + result, err := time.Parse(time.RFC3339, timestamp) + if err != nil { + log.Errorf("Error parsing timestamp '%s' - make sure the time format is correct", timestamp) + } + return result + } + // Default to current time if no timestamp is provided. + return time.Now() } // AddContainerFilters adds to the default Registry (topologyRegistry)'s containerFilters @@ -487,7 +490,7 @@ func (r *Registry) walk(f func(APITopologyDesc)) { // makeTopologyList returns a handler that yields an APITopologyList. func (r *Registry) makeTopologyList(rep Reporter) CtxHandlerFunc { return func(ctx context.Context, w http.ResponseWriter, req *http.Request) { - timestamp := deserializedTimestamp(req.URL.Query().Get("timestamp")) + timestamp := deserializeTimestamp(req.URL.Query().Get("timestamp")) report, err := rep.Report(ctx, timestamp) if err != nil { respondWith(w, http.StatusInternalServerError, err) @@ -577,7 +580,7 @@ func (r *Registry) captureRenderer(rep Reporter, f rendererHandler) CtxHandlerFu return func(ctx context.Context, w http.ResponseWriter, req *http.Request) { var ( topologyID = mux.Vars(req)["topology"] - timestamp = deserializedTimestamp(req.URL.Query().Get("timestamp")) + timestamp = deserializeTimestamp(req.URL.Query().Get("timestamp")) ) if _, ok := r.get(topologyID); !ok { http.NotFound(w, req) diff --git a/app/api_topology.go b/app/api_topology.go index 5315ce22e7..173e275b99 100644 --- a/app/api_topology.go +++ b/app/api_topology.go @@ -97,7 +97,7 @@ func handleWebsocket( tick = time.Tick(loop) wait = make(chan struct{}, 1) topologyID = mux.Vars(r)["topology"] - startReportingAt = deserializedTimestamp(r.Form.Get("timestamp")) + startReportingAt = deserializeTimestamp(r.Form.Get("timestamp")) channelOpenedAt = time.Now() )