func (uis *UIServer) schedulerHostUtilization(w http.ResponseWriter, r *http.Request) { // get granularity (in seconds) granularity, err := util.GetIntValue(r, "granularity", 0) if err != nil { uis.LoggedError(w, r, http.StatusInternalServerError, err) return } if granularity == 0 { uis.LoggedError(w, r, http.StatusBadRequest, fmt.Errorf("Invalid granularity")) return } // get number of days back daysBack, err := util.GetIntValue(r, "numberDays", 0) if err != nil { uis.LoggedError(w, r, http.StatusInternalServerError, err) return } if daysBack == 0 { uis.LoggedError(w, r, http.StatusBadRequest, fmt.Errorf("Invalid days back")) return } bucketData, err := model.CreateAllHostUtilizationBuckets(daysBack, granularity) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } uis.WriteJSON(w, http.StatusOK, bucketData) return }
func (restapi *restAPI) getHostUtilizationStats(w http.ResponseWriter, r *http.Request) { // get granularity (in seconds) granularity, err := util.GetIntValue(r, "granularity", 0) if err != nil { restapi.WriteJSON(w, http.StatusBadRequest, responseError{Message: err.Error()}) return } if granularity == 0 { restapi.WriteJSON(w, http.StatusBadRequest, responseError{Message: "invalid granularity"}) return } // get number of days back daysBack, err := util.GetIntValue(r, "numberDays", 0) if err != nil { restapi.WriteJSON(w, http.StatusBadRequest, responseError{Message: err.Error()}) return } if daysBack == 0 { restapi.WriteJSON(w, http.StatusBadRequest, responseError{Message: "invalid days back"}) return } isCSV, err := util.GetBoolValue(r, "csv", true) if err != nil { restapi.WriteJSON(w, http.StatusBadRequest, responseError{Message: err.Error()}) return } buckets, err := model.CreateAllHostUtilizationBuckets(daysBack, granularity) if err != nil { restapi.WriteJSON(w, http.StatusInternalServerError, responseError{Message: fmt.Sprintf("error getting buckets: %v", err.Error())}) return } restBuckets := []restHostUtilizationBucket{} // convert the time.Durations into integers for _, b := range buckets { r := restHostUtilizationBucket{ StaticHost: int(b.StaticHost), DynamicHost: int(b.DynamicHost), Task: int(b.Task), StartTime: b.StartTime, EndTime: b.EndTime, } restBuckets = append(restBuckets, r) } if isCSV { util.WriteCSVResponse(w, http.StatusOK, restBuckets) return } restapi.WriteJSON(w, http.StatusOK, buckets) }