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 {
|
func writeSnapshotToDb(snapshotSub *SnapshotSubmission) (int64, error) {
|
||||||
query := fmt.Sprintf("INSERT INTO `snapshots` (`temp`, `humidity`, `co2`, `time`, `id`) VALUES (%v, %v, %v, '%v', NULL);",
|
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.Temp,
|
||||||
snapshotSub.Humidity,
|
snapshotSub.Humidity,
|
||||||
snapshotSub.Co2,
|
snapshotSub.Co2,
|
||||||
snapshotSub.Timestamp)
|
snapshotSub.Timestamp,
|
||||||
_, err := ClimateDb.Query(query)
|
)
|
||||||
if err != nil {
|
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) {
|
func getLastSnapshotRecordFromDb() (*SnapshotRecord, error) {
|
||||||
@@ -55,7 +61,7 @@ func getLastSnapshotRecordFromDb() (*SnapshotRecord, error) {
|
|||||||
&snapshotRecord.Timestamp,
|
&snapshotRecord.Timestamp,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("couldn't read last snapshot from the database: %w", err)
|
||||||
}
|
}
|
||||||
return &snapshotRecord, nil
|
return &snapshotRecord, nil
|
||||||
}
|
}
|
||||||
@@ -67,7 +73,7 @@ func getSnapshotRecordsFromDb(minuteAgo int) ([]*SnapshotRecord, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
for i := 0; rows.Next(); i++ {
|
for rows.Next() {
|
||||||
var snapshotRecord SnapshotRecord
|
var snapshotRecord SnapshotRecord
|
||||||
err = rows.Scan(
|
err = rows.Scan(
|
||||||
&snapshotRecord.Id,
|
&snapshotRecord.Id,
|
||||||
@@ -77,7 +83,7 @@ func getSnapshotRecordsFromDb(minuteAgo int) ([]*SnapshotRecord, error) {
|
|||||||
&snapshotRecord.Timestamp,
|
&snapshotRecord.Timestamp,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("couldn't read rows from the database: %w", err)
|
||||||
}
|
}
|
||||||
snapshots = append(snapshots, &snapshotRecord)
|
snapshots = append(snapshots, &snapshotRecord)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ func getPatternHandler(pattern string) http.HandlerFunc {
|
|||||||
err = cb(w, r)
|
err = cb(w, r)
|
||||||
}
|
}
|
||||||
if err != nil {
|
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) {
|
func createJsonFromSnapshotRecords(records []*SnapshotRecord) ([]byte, error) {
|
||||||
jsonResult, err := json.Marshal(SnapshotPayload{records})
|
jsonResult, err := json.Marshal(SnapshotPayload{records})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("couldn't marshal json: %w", err)
|
||||||
}
|
}
|
||||||
return jsonResult, nil
|
return jsonResult, nil
|
||||||
}
|
}
|
||||||
@@ -66,61 +66,43 @@ func sendData(w http.ResponseWriter, r *http.Request) {
|
|||||||
count = newCount
|
count = newCount
|
||||||
}
|
}
|
||||||
records, err := getSnapshotRecordsFromDb(int(count))
|
records, err := getSnapshotRecordsFromDb(int(count))
|
||||||
if err != nil {
|
if internalErrorOnErr(fmt.Errorf("couldn't get snapshots from db: %w", err), w, r) { return }
|
||||||
sendInternalError(fmt.Errorf("couldn't read rows from the database: %w", err), w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
json, err := createJsonFromSnapshotRecords(records)
|
json, err := createJsonFromSnapshotRecords(records)
|
||||||
if err != nil {
|
if internalErrorOnErr(fmt.Errorf("couldn't create json from snapshots: %w", err), w, r) { return }
|
||||||
sendInternalError(fmt.Errorf("couldn't create a json from the records: %w", err), w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
_, err = fmt.Fprintf(w, string(json))
|
fmt.Fprintf(w, string(json))
|
||||||
if err != nil {
|
|
||||||
sendInternalError(err, w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendLastSnapshot(w http.ResponseWriter, r *http.Request) {
|
func sendLastSnapshot(w http.ResponseWriter, r *http.Request) {
|
||||||
record, err := getLastSnapshotRecordFromDb()
|
record, err := getLastSnapshotRecordFromDb()
|
||||||
if err != nil {
|
if internalErrorOnErr(fmt.Errorf("couldn't get last snapshot from db: %w", err), w, r) { return }
|
||||||
sendInternalError(fmt.Errorf("couldn't read last snapshot from the database: %w", err), w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
json, err := createJsonFromSnapshotRecords([]*SnapshotRecord{record})
|
json, err := createJsonFromSnapshotRecords([]*SnapshotRecord{record})
|
||||||
if err != nil {
|
if internalErrorOnErr(fmt.Errorf("couldn't create json from last snapshots: %w", err), w, r) { return }
|
||||||
sendInternalError(fmt.Errorf("couldn't create a json from the last record: %w", err), w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
_, err = fmt.Fprintf(w, string(json))
|
fmt.Fprintf(w, string(json))
|
||||||
if err != nil {
|
|
||||||
sendInternalError(err, w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveSnapshot(w http.ResponseWriter, r *http.Request) {
|
func saveSnapshot(w http.ResponseWriter, r *http.Request) {
|
||||||
snapshotSub, err := createSnapshotSubFromJsonBodyStream(r.Body)
|
snapshotSub, err := createSnapshotSubFromJsonBodyStream(r.Body)
|
||||||
if err != nil {
|
if internalErrorOnErr(fmt.Errorf("couldn't create snapshot from JSON: %w", err), w, r) { return }
|
||||||
sendInternalError(fmt.Errorf("couldn't create snapshot from JSON: %w", err), w, r)
|
newId, err := writeSnapshotToDb(snapshotSub)
|
||||||
}
|
if internalErrorOnErr(fmt.Errorf("couldn't submit snapshot into the database: %w", err), w, r) { return }
|
||||||
err = writeSnapshotToDb(snapshotSub)
|
fmt.Fprintf(w, "{id: %v}", newId)
|
||||||
if err != nil {
|
|
||||||
sendInternalError(fmt.Errorf("couldn't submit snapshot into the database: %w", err), w, r)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendInternalError(err error, w http.ResponseWriter, r *http.Request) {
|
func internalErrorOnErr(err error, w http.ResponseWriter, r *http.Request) bool {
|
||||||
errorMessage := "Internal Server Error!"
|
if err != nil {
|
||||||
if DEBUG {
|
errorMessage := "Internal Server Error!"
|
||||||
errorMessage += fmt.Sprintf(" Happened during %s request for pattern '%s': %s",
|
if DEBUG {
|
||||||
r.Method,
|
errorMessage += fmt.Sprintf(" Happened during %s request for pattern '%s': %s",
|
||||||
r.URL,
|
r.Method,
|
||||||
err.Error())
|
r.URL,
|
||||||
|
err.Error())
|
||||||
|
}
|
||||||
|
fmt.Println(errorMessage)
|
||||||
|
http.Error(w, errorMessage, 500)
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
fmt.Println(errorMessage)
|
return false
|
||||||
http.Error(w, errorMessage, 500)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user