fix(db): get rooms from mapping file
Signed-off-by: Klagarge <remi@heredero.ch>
This commit is contained in:
@@ -167,7 +167,7 @@ func main() {
|
||||
// Initialize and start REST Gateway
|
||||
restUsername := getEnv("REST_USERNAME", "user")
|
||||
restPassword := getEnv("REST_PASSWORD", "password")
|
||||
restGateway := rest.NewRestGateway(influxGateway, measurementName, restUsername, restPassword)
|
||||
restGateway := rest.NewRestGateway(influxGateway, mapping, measurementName, restUsername, restPassword)
|
||||
|
||||
port := getEnv("REST_PORT", "8080")
|
||||
|
||||
|
||||
@@ -78,3 +78,16 @@ func (c *MappingConfig) GetRoom(nodeID string) (string, bool) {
|
||||
room, ok := c.nodeToRoom[nodeID]
|
||||
return room, ok
|
||||
}
|
||||
|
||||
// Rooms returns the list of all room names defined in the mapping.
|
||||
func (c *MappingConfig) Rooms() []string {
|
||||
seen := make(map[string]struct{})
|
||||
for _, room := range c.nodeToRoom {
|
||||
seen[room] = struct{}{}
|
||||
}
|
||||
rooms := make([]string, 0, len(seen))
|
||||
for room := range seen {
|
||||
rooms = append(rooms, room)
|
||||
}
|
||||
return rooms
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"gateway/influx"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"slices"
|
||||
|
||||
_ "gateway/docs"
|
||||
|
||||
@@ -15,17 +16,24 @@ import (
|
||||
ginSwagger "github.com/swaggo/gin-swagger"
|
||||
)
|
||||
|
||||
// RoomLister is an interface for getting the list of rooms from the mapping.
|
||||
type RoomLister interface {
|
||||
Rooms() []string
|
||||
}
|
||||
|
||||
type RestGateway struct {
|
||||
influxGateway *influx.InfluxGateway
|
||||
mapping RoomLister
|
||||
engine *gin.Engine
|
||||
measurementName string
|
||||
username string
|
||||
password string
|
||||
}
|
||||
|
||||
func NewRestGateway(influxGateway *influx.InfluxGateway, measurementName string, username, password string) *RestGateway {
|
||||
func NewRestGateway(influxGateway *influx.InfluxGateway, mapping RoomLister, measurementName string, username, password string) *RestGateway {
|
||||
g := &RestGateway{
|
||||
influxGateway: influxGateway,
|
||||
mapping: mapping,
|
||||
engine: gin.Default(),
|
||||
measurementName: measurementName,
|
||||
username: username,
|
||||
@@ -82,29 +90,8 @@ func (g *RestGateway) Run(addr string) error {
|
||||
// @Security BasicAuth
|
||||
// @Router /rooms [get]
|
||||
func (g *RestGateway) getRooms(c *gin.Context) {
|
||||
// Query unique rooms from the measurement
|
||||
query := fmt.Sprintf(`SELECT DISTINCT("room") FROM "%s"`, g.measurementName)
|
||||
|
||||
// Using context.Background() as seen in working snippet
|
||||
it, err := g.influxGateway.Query(context.Background(), query)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var rooms []string
|
||||
for it.Next() {
|
||||
val := it.Value()
|
||||
if room, ok := val["room"].(string); ok {
|
||||
rooms = append(rooms, room)
|
||||
}
|
||||
}
|
||||
|
||||
if err := it.Err(); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
rooms := g.mapping.Rooms()
|
||||
slices.Sort(rooms)
|
||||
c.JSON(http.StatusOK, rooms)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user