Skip to content

Instantly share code, notes, and snippets.

@BK1031
Last active July 10, 2024 17:53
Show Gist options
  • Save BK1031/6999b209db9065551dc6fa545a2968a5 to your computer and use it in GitHub Desktop.
Save BK1031/6999b209db9065551dc6fa545a2968a5 to your computer and use it in GitHub Desktop.
racecar_analytics table average endpoints
func StartServer() {
r := gin.Default()
r.GET("/ecu", GetAllECUs)
r.GET("/battery", GetAllBatteries)
r.GET("/ecu/averages", GetECUAverages) // add this
r.GET("/battery/averages", GetBatteryAverages) // add this
r.Run(":" + Port)
}
// other functions here
func GetECUAverages(c *gin.Context) {
var result struct {
AvgMotorRPM float64 `json:"avg_motor_rpm"`
AvgSpeed float64 `json:"avg_speed"`
AvgThrottle float64 `json:"avg_throttle"`
AvgBrakePressure float64 `json:"avg_brake_pressure"`
}
DB.Model(&ECU{}).Select("AVG(motor_rpm) as avg_motor_rpm, AVG(speed) as avg_speed, AVG(throttle) as avg_throttle, AVG(brake_pressure) as avg_brake_pressure").Scan(&result)
c.JSON(http.StatusOK, result)
}
func GetBatteryAverages(c *gin.Context) {
var result struct {
AvgChargeLevel float64 `json:"avg_charge_level"`
AvgCellTemp1 float64 `json:"avg_cell_temp_1"`
AvgCellTemp2 float64 `json:"avg_cell_temp_2"`
AvgCellTemp3 float64 `json:"avg_cell_temp_3"`
AvgCellTemp4 float64 `json:"avg_cell_temp_4"`
AvgCellVoltage1 float64 `json:"avg_cell_voltage_1"`
AvgCellVoltage2 float64 `json:"avg_cell_voltage_2"`
AvgCellVoltage3 float64 `json:"avg_cell_voltage_3"`
AvgCellVoltage4 float64 `json:"avg_cell_voltage_4"`
}
DB.Model(&Battery{}).Select("AVG(charge_level) as avg_charge_level, AVG(cell_temp1) as avg_cell_temp1, AVG(cell_temp2) as avg_cell_temp2, AVG(cell_temp3) as avg_cell_temp3, AVG(cell_temp4) as avg_cell_temp4, AVG(cell_voltage1) as avg_cell_voltage1, AVG(cell_voltage2) as avg_cell_voltage2, AVG(cell_voltage3) as avg_cell_voltage3, AVG(cell_voltage4) as avg_cell_voltage4").Scan(&result)
c.JSON(http.StatusOK, result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment