Changed endpoints and db queries, chart now shows suggested mins and maxes
This commit is contained in:
24
Database.go
24
Database.go
@@ -26,17 +26,23 @@ func CloseDb() {
|
||||
}
|
||||
}
|
||||
|
||||
func writeSnapshotToDb(snapshotSub *SnapshotSubmission) error {
|
||||
query := fmt.Sprintf("INSERT INTO `snapshots` (`temp`, `humidity`, `co2`, `time`, `id`) VALUES (%v, %v, %v, '%v', NULL);",
|
||||
func writeSnapshotToDb(snapshotSub *SnapshotSubmission) (int64, error) {
|
||||
queryStr := "INSERT INTO `snapshots` (`temp`, `humidity`, `co2`, `time`, `id`) VALUES (%v, %v, %v, '%v', NULL);"
|
||||
query, err := ClimateDb.Prepare(queryStr)
|
||||
if err != nil {
|
||||
return -1, fmt.Errorf("couldn't prepare snapshot query: %w", err)
|
||||
}
|
||||
result, err := query.Exec(
|
||||
snapshotSub.Temp,
|
||||
snapshotSub.Humidity,
|
||||
snapshotSub.Co2,
|
||||
snapshotSub.Timestamp)
|
||||
_, err := ClimateDb.Query(query)
|
||||
snapshotSub.Timestamp,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
return -1, fmt.Errorf("couldn't execute query to write snapshot to db: %w", err)
|
||||
}
|
||||
return nil
|
||||
id, _ := result.LastInsertId()
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func getLastSnapshotRecordFromDb() (*SnapshotRecord, error) {
|
||||
@@ -55,7 +61,7 @@ func getLastSnapshotRecordFromDb() (*SnapshotRecord, error) {
|
||||
&snapshotRecord.Timestamp,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("couldn't read last snapshot from the database: %w", err)
|
||||
}
|
||||
return &snapshotRecord, nil
|
||||
}
|
||||
@@ -67,7 +73,7 @@ func getSnapshotRecordsFromDb(minuteAgo int) ([]*SnapshotRecord, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := 0; rows.Next(); i++ {
|
||||
for rows.Next() {
|
||||
var snapshotRecord SnapshotRecord
|
||||
err = rows.Scan(
|
||||
&snapshotRecord.Id,
|
||||
@@ -77,7 +83,7 @@ func getSnapshotRecordsFromDb(minuteAgo int) ([]*SnapshotRecord, error) {
|
||||
&snapshotRecord.Timestamp,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("couldn't read rows from the database: %w", err)
|
||||
}
|
||||
snapshots = append(snapshots, &snapshotRecord)
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ func getPatternHandler(pattern string) http.HandlerFunc {
|
||||
err = cb(w, r)
|
||||
}
|
||||
if err != nil {
|
||||
sendInternalError(err, w, r)
|
||||
internalErrorOnErr(err, w, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ func createSnapshotSubFromJsonBodyStream(jsonBodyStream io.ReadCloser) (*Snapsho
|
||||
func createJsonFromSnapshotRecords(records []*SnapshotRecord) ([]byte, error) {
|
||||
jsonResult, err := json.Marshal(SnapshotPayload{records})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("couldn't marshal json: %w", err)
|
||||
}
|
||||
return jsonResult, nil
|
||||
}
|
||||
@@ -66,54 +66,32 @@ 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) {
|
||||
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",
|
||||
@@ -123,4 +101,8 @@ func sendInternalError(err error, w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
fmt.Println(errorMessage)
|
||||
http.Error(w, errorMessage, 500)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user