Changed endpoints and db queries, chart now shows suggested mins and maxes
This commit is contained in:
@@ -66,61 +66,43 @@ func sendData(w http.ResponseWriter, r *http.Request) {
|
||||
count = newCount
|
||||
}
|
||||
records, err := getSnapshotRecordsFromDb(int(count))
|
||||
if err != nil {
|
||||
sendInternalError(fmt.Errorf("couldn't read rows from the database: %w", err), w, r)
|
||||
return
|
||||
}
|
||||
if internalErrorOnErr(fmt.Errorf("couldn't get snapshots from db: %w", err), w, r) { return }
|
||||
json, err := createJsonFromSnapshotRecords(records)
|
||||
if err != nil {
|
||||
sendInternalError(fmt.Errorf("couldn't create a json from the records: %w", err), w, r)
|
||||
return
|
||||
}
|
||||
if internalErrorOnErr(fmt.Errorf("couldn't create json from snapshots: %w", err), w, r) { return }
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, err = fmt.Fprintf(w, string(json))
|
||||
if err != nil {
|
||||
sendInternalError(err, w, r)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, string(json))
|
||||
}
|
||||
|
||||
func sendLastSnapshot(w http.ResponseWriter, r *http.Request) {
|
||||
record, err := getLastSnapshotRecordFromDb()
|
||||
if err != nil {
|
||||
sendInternalError(fmt.Errorf("couldn't read last snapshot from the database: %w", err), w, r)
|
||||
return
|
||||
}
|
||||
if internalErrorOnErr(fmt.Errorf("couldn't get last snapshot from db: %w", err), w, r) { return }
|
||||
json, err := createJsonFromSnapshotRecords([]*SnapshotRecord{record})
|
||||
if err != nil {
|
||||
sendInternalError(fmt.Errorf("couldn't create a json from the last record: %w", err), w, r)
|
||||
return
|
||||
}
|
||||
if internalErrorOnErr(fmt.Errorf("couldn't create json from last snapshots: %w", err), w, r) { return }
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, err = fmt.Fprintf(w, string(json))
|
||||
if err != nil {
|
||||
sendInternalError(err, w, r)
|
||||
return
|
||||
}
|
||||
fmt.Fprintf(w, string(json))
|
||||
}
|
||||
|
||||
func saveSnapshot(w http.ResponseWriter, r *http.Request) {
|
||||
snapshotSub, err := createSnapshotSubFromJsonBodyStream(r.Body)
|
||||
if err != nil {
|
||||
sendInternalError(fmt.Errorf("couldn't create snapshot from JSON: %w", err), w, r)
|
||||
}
|
||||
err = writeSnapshotToDb(snapshotSub)
|
||||
if err != nil {
|
||||
sendInternalError(fmt.Errorf("couldn't submit snapshot into the database: %w", err), w, r)
|
||||
}
|
||||
if internalErrorOnErr(fmt.Errorf("couldn't create snapshot from JSON: %w", err), w, r) { return }
|
||||
newId, err := writeSnapshotToDb(snapshotSub)
|
||||
if internalErrorOnErr(fmt.Errorf("couldn't submit snapshot into the database: %w", err), w, r) { return }
|
||||
fmt.Fprintf(w, "{id: %v}", newId)
|
||||
}
|
||||
|
||||
func sendInternalError(err error, w http.ResponseWriter, r *http.Request) {
|
||||
errorMessage := "Internal Server Error!"
|
||||
if DEBUG {
|
||||
errorMessage += fmt.Sprintf(" Happened during %s request for pattern '%s': %s",
|
||||
r.Method,
|
||||
r.URL,
|
||||
err.Error())
|
||||
func internalErrorOnErr(err error, w http.ResponseWriter, r *http.Request) bool {
|
||||
if err != nil {
|
||||
errorMessage := "Internal Server Error!"
|
||||
if DEBUG {
|
||||
errorMessage += fmt.Sprintf(" Happened during %s request for pattern '%s': %s",
|
||||
r.Method,
|
||||
r.URL,
|
||||
err.Error())
|
||||
}
|
||||
fmt.Println(errorMessage)
|
||||
http.Error(w, errorMessage, 500)
|
||||
return true
|
||||
}
|
||||
fmt.Println(errorMessage)
|
||||
http.Error(w, errorMessage, 500)
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user