func queryCalls(w http.ResponseWriter, r *http.Request) { calls, err := appmon.QueryCalls("") if err != nil { log.Printf("QueryCalls: %s", err) w.WriteHeader(http.StatusInternalServerError) return } if calls == nil { calls = []*appmon.Call{} } json.NewEncoder(w).Encode(calls) }
func uiCalls(w http.ResponseWriter, r *http.Request) { q := r.URL.Query() lastNHoursStr := q.Get("lastNHours") var lastNHours int if lastNHoursStr == "" { lastNHours = 7 } else { var err error lastNHours, err = strconv.Atoi(lastNHoursStr) if err != nil { http.Error(w, "bad 'lastNHours' parameter: "+err.Error(), http.StatusBadRequest) return } } failedOnlyStr := q.Get("failedOnly") var failedOnly bool if failedOnlyStr != "" { var err error failedOnly, err = strconv.ParseBool(failedOnlyStr) if err != nil { http.Error(w, "bad 'failedOnly' parameter: "+err.Error(), http.StatusBadRequest) return } } sort := q.Get("sort") if sort == "" { sort = "date" } sorts := map[string]string{"date": "start", "duration": `"end" - "start"`} if _, ok := sorts[sort]; !ok { http.Error(w, "bad 'sort' parameter", http.StatusBadRequest) return } callRoutes, err := getCallRoutes(lastNHours, failedOnly) if err != nil { http.Error(w, "getCallRoutes failed: "+err.Error(), http.StatusInternalServerError) return } var calls []*appmon.Call selectedRoute := q.Get("route") selectedApp := q.Get("app") if selectedRoute != "" && selectedApp != "" { calls, err = appmon.QueryCalls(`WHERE app = $1 AND route = $2 AND (current_timestamp - "start" < ($3::int * interval '1 hour')) AND ((NOT $4) OR (http_status_code < 200 OR http_status_code >= 400)) ORDER BY `+sorts[sort]+` DESC LIMIT 100`, selectedApp, selectedRoute, lastNHours, failedOnly) if err != nil { http.Error(w, "QueryCalls failed: "+err.Error(), http.StatusInternalServerError) return } } tmpl(appmonUICalls, uiCallsHTML)(w, struct { common LastNHours int FailedOnly bool Sort string CallRoutes []*callRoute SelectedApp string SelectedRoute string Calls []*appmon.Call }{ common: newCommon("Calls"), LastNHours: lastNHours, FailedOnly: failedOnly, Sort: sort, CallRoutes: callRoutes, SelectedApp: selectedApp, SelectedRoute: selectedRoute, Calls: calls, }) }