/* handleFlightLogEventsRequest(): returns all events associated with a given flight as JSON. Events are returned in ascending (oldest first) timestamp order. */ func handleFlightLogEventsRequest(args []string, w http.ResponseWriter, r *http.Request) { db, err := openDatabase() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } defer db.Close() if len(args) < 1 { http.Error(w, "/flightlog/events requires a flight id parameter", http.StatusBadRequest) return } flight, err := strconv.Atoi(args[0]) if err != nil { http.Error(w, "Invalid flight ID value", http.StatusBadRequest) return } var count int64 count = getCount(fmt.Sprintf("SELECT COUNT(*) FROM events WHERE startup_id = %d;", flight), db) sql := fmt.Sprintf("SELECT * FROM events WHERE startup_id = %d ORDER BY timestamp_id ASC LIMIT 1000;", flight) m, err := gosqljson.QueryDbToMapJSON(db, "any", sql) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } ret := fmt.Sprintf("{\"count\": %d, \"data\": %s}", count, m) setNoCache(w) setJSONHeaders(w) fmt.Fprintf(w, "%s\n", ret) }
/* handleFlightLogFlightsRequest(): returns a list of flights as JSON. Data is returned in descending (most recent first) timestamp order. If more than 100 flights are stored, the system will always return the 100 most recent unless an offset value is passed. */ func handleFlightLogFlightsRequest(args []string, w http.ResponseWriter, r *http.Request) { db, err := openDatabase() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } defer db.Close() var offset int if len(args) > 0 { offset, err = strconv.Atoi(args[0]) if err != nil { http.Error(w, "Invalid page value", http.StatusBadRequest) return } // page size is 10 records offset = (offset - 1) * 10 } var count int64 count = getCount("SELECT COUNT(*) FROM startup WHERE duration > 1 AND distance > 1 AND ((max_alt - start_alt) > 350);", db) sql := fmt.Sprintf("SELECT * FROM startup WHERE duration > 1 AND distance > 1 AND ((max_alt - start_alt) > 350) ORDER BY id DESC LIMIT 10 OFFSET %d;", offset) m, err := gosqljson.QueryDbToMapJSON(db, "any", sql) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } ret := fmt.Sprintf("{\"count\": %d, \"limit\": 10, \"offset\": %d, \"data\": %s}", count, offset, m) setNoCache(w) setJSONHeaders(w) fmt.Fprintf(w, "%s\n", ret) }