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
|
// Initialize and start REST Gateway
|
||||||
restUsername := getEnv("REST_USERNAME", "user")
|
restUsername := getEnv("REST_USERNAME", "user")
|
||||||
restPassword := getEnv("REST_PASSWORD", "password")
|
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")
|
port := getEnv("REST_PORT", "8080")
|
||||||
|
|
||||||
|
|||||||
@@ -78,3 +78,16 @@ func (c *MappingConfig) GetRoom(nodeID string) (string, bool) {
|
|||||||
room, ok := c.nodeToRoom[nodeID]
|
room, ok := c.nodeToRoom[nodeID]
|
||||||
return room, ok
|
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"
|
"gateway/influx"
|
||||||
"net/http"
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"slices"
|
||||||
|
|
||||||
_ "gateway/docs"
|
_ "gateway/docs"
|
||||||
|
|
||||||
@@ -15,17 +16,24 @@ import (
|
|||||||
ginSwagger "github.com/swaggo/gin-swagger"
|
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 {
|
type RestGateway struct {
|
||||||
influxGateway *influx.InfluxGateway
|
influxGateway *influx.InfluxGateway
|
||||||
|
mapping RoomLister
|
||||||
engine *gin.Engine
|
engine *gin.Engine
|
||||||
measurementName string
|
measurementName string
|
||||||
username string
|
username string
|
||||||
password 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{
|
g := &RestGateway{
|
||||||
influxGateway: influxGateway,
|
influxGateway: influxGateway,
|
||||||
|
mapping: mapping,
|
||||||
engine: gin.Default(),
|
engine: gin.Default(),
|
||||||
measurementName: measurementName,
|
measurementName: measurementName,
|
||||||
username: username,
|
username: username,
|
||||||
@@ -82,29 +90,8 @@ func (g *RestGateway) Run(addr string) error {
|
|||||||
// @Security BasicAuth
|
// @Security BasicAuth
|
||||||
// @Router /rooms [get]
|
// @Router /rooms [get]
|
||||||
func (g *RestGateway) getRooms(c *gin.Context) {
|
func (g *RestGateway) getRooms(c *gin.Context) {
|
||||||
// Query unique rooms from the measurement
|
rooms := g.mapping.Rooms()
|
||||||
query := fmt.Sprintf(`SELECT DISTINCT("room") FROM "%s"`, g.measurementName)
|
slices.Sort(rooms)
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
c.JSON(http.StatusOK, rooms)
|
c.JSON(http.StatusOK, rooms)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user