| @@ -4,7 +4,9 @@ import ( | |||||
| "context" | "context" | ||||
| "encoding/json" | "encoding/json" | ||||
| "errors" | "errors" | ||||
| "log/slog" | |||||
| "net/http" | "net/http" | ||||
| "strconv" | |||||
| "github.com/AFASystems/presence/internal/pkg/api/response" | "github.com/AFASystems/presence/internal/pkg/api/response" | ||||
| "github.com/AFASystems/presence/internal/pkg/service" | "github.com/AFASystems/presence/internal/pkg/service" | ||||
| @@ -15,7 +17,31 @@ import ( | |||||
| func AlertsListController(db *gorm.DB, ctx context.Context) http.HandlerFunc { | func AlertsListController(db *gorm.DB, ctx context.Context) http.HandlerFunc { | ||||
| return func(w http.ResponseWriter, r *http.Request) { | return func(w http.ResponseWriter, r *http.Request) { | ||||
| alerts, err := service.GetAllAlerts(db, ctx) | |||||
| query := r.URL.Query() | |||||
| lStr := query.Get("limit") | |||||
| if lStr == "" { | |||||
| lStr = "100" | |||||
| } | |||||
| limit, err := strconv.Atoi(lStr) | |||||
| if err != nil { | |||||
| slog.Error("invalid limit parameter", "value", lStr) | |||||
| response.BadRequest(w, "invalid limit parameter") | |||||
| return | |||||
| } | |||||
| oStr := query.Get("offset") | |||||
| if oStr == "" { | |||||
| oStr = "0" | |||||
| } | |||||
| offset, err := strconv.Atoi(oStr) | |||||
| if err != nil { | |||||
| slog.Error("invalid offset parameter", "value", oStr) | |||||
| response.BadRequest(w, "invalid offset parameter") | |||||
| return | |||||
| } | |||||
| alerts, err := service.GetAllAlerts(db, ctx, limit, offset) | |||||
| if err != nil { | if err != nil { | ||||
| response.InternalError(w, "failed to list alerts", err) | response.InternalError(w, "failed to list alerts", err) | ||||
| return | return | ||||
| @@ -3,7 +3,9 @@ package controller | |||||
| import ( | import ( | ||||
| "context" | "context" | ||||
| "encoding/json" | "encoding/json" | ||||
| "log/slog" | |||||
| "net/http" | "net/http" | ||||
| "strconv" | |||||
| "github.com/AFASystems/presence/internal/pkg/api/response" | "github.com/AFASystems/presence/internal/pkg/api/response" | ||||
| "github.com/AFASystems/presence/internal/pkg/model" | "github.com/AFASystems/presence/internal/pkg/model" | ||||
| @@ -34,12 +36,32 @@ func FloorAddController(db *gorm.DB, context context.Context) http.HandlerFunc { | |||||
| func FloorListController(db *gorm.DB, context context.Context) http.HandlerFunc { | func FloorListController(db *gorm.DB, context context.Context) http.HandlerFunc { | ||||
| return func(w http.ResponseWriter, r *http.Request) { | return func(w http.ResponseWriter, r *http.Request) { | ||||
| var floors []model.Floor | |||||
| if err := db.WithContext(context).Find(&floors).Error; err != nil { | |||||
| response.InternalError(w, "failed to list floors", err) | |||||
| query := r.URL.Query() | |||||
| lStr := query.Get("limit") | |||||
| if lStr == "" { | |||||
| lStr = "100" | |||||
| } | |||||
| limit, err := strconv.Atoi(lStr) | |||||
| if err != nil { | |||||
| slog.Error("invalid limit parameter", "value", lStr) | |||||
| response.BadRequest(w, "invalid limit parameter") | |||||
| return | |||||
| } | |||||
| oStr := query.Get("offset") | |||||
| if oStr == "" { | |||||
| oStr = "0" | |||||
| } | |||||
| offset, err := strconv.Atoi(oStr) | |||||
| if err != nil { | |||||
| slog.Error("invalid offset parameter", "value", oStr) | |||||
| response.BadRequest(w, "invalid offset parameter") | |||||
| return | return | ||||
| } | } | ||||
| if err := db.WithContext(context).Find(&floors).Error; err != nil { | |||||
| var floors []model.Floor | |||||
| if err := db.WithContext(context).Limit(limit).Offset(offset).Find(&floors).Error; err != nil { | |||||
| response.InternalError(w, "failed to list floors", err) | response.InternalError(w, "failed to list floors", err) | ||||
| return | return | ||||
| } | } | ||||
| @@ -3,7 +3,9 @@ package controller | |||||
| import ( | import ( | ||||
| "context" | "context" | ||||
| "encoding/json" | "encoding/json" | ||||
| "log/slog" | |||||
| "net/http" | "net/http" | ||||
| "strconv" | |||||
| "github.com/AFASystems/presence/internal/pkg/api/response" | "github.com/AFASystems/presence/internal/pkg/api/response" | ||||
| "github.com/AFASystems/presence/internal/pkg/model" | "github.com/AFASystems/presence/internal/pkg/model" | ||||
| @@ -35,8 +37,32 @@ func GatewayAddController(db *gorm.DB, context context.Context) http.HandlerFunc | |||||
| func GatewayListController(db *gorm.DB, context context.Context) http.HandlerFunc { | func GatewayListController(db *gorm.DB, context context.Context) http.HandlerFunc { | ||||
| return func(w http.ResponseWriter, r *http.Request) { | return func(w http.ResponseWriter, r *http.Request) { | ||||
| query := r.URL.Query() | |||||
| lStr := query.Get("limit") | |||||
| if lStr == "" { | |||||
| lStr = "100" | |||||
| } | |||||
| limit, err := strconv.Atoi(lStr) | |||||
| if err != nil { | |||||
| slog.Error("invalid limit parameter", "value", lStr) | |||||
| response.BadRequest(w, "invalid limit parameter") | |||||
| return | |||||
| } | |||||
| oStr := query.Get("offset") | |||||
| if oStr == "" { | |||||
| oStr = "0" | |||||
| } | |||||
| offset, err := strconv.Atoi(oStr) | |||||
| if err != nil { | |||||
| slog.Error("invalid offset parameter", "value", oStr) | |||||
| response.BadRequest(w, "invalid offset parameter") | |||||
| return | |||||
| } | |||||
| var gateways []model.Gateway | var gateways []model.Gateway | ||||
| if err := db.WithContext(context).Find(&gateways).Error; err != nil { | |||||
| if err := db.WithContext(context).Limit(limit).Offset(offset).Find(&gateways).Error; err != nil { | |||||
| response.InternalError(w, "failed to list gateways", err) | response.InternalError(w, "failed to list gateways", err) | ||||
| return | return | ||||
| } | } | ||||
| @@ -5,6 +5,7 @@ import ( | |||||
| "encoding/json" | "encoding/json" | ||||
| "log/slog" | "log/slog" | ||||
| "net/http" | "net/http" | ||||
| "strconv" | |||||
| "github.com/AFASystems/presence/internal/pkg/api/response" | "github.com/AFASystems/presence/internal/pkg/api/response" | ||||
| "github.com/AFASystems/presence/internal/pkg/kafkaclient" | "github.com/AFASystems/presence/internal/pkg/kafkaclient" | ||||
| @@ -67,8 +68,32 @@ func TrackerAdd(db *gorm.DB, writer *kafka.Writer, context context.Context) http | |||||
| func TrackerList(db *gorm.DB, context context.Context) http.HandlerFunc { | func TrackerList(db *gorm.DB, context context.Context) http.HandlerFunc { | ||||
| return func(w http.ResponseWriter, r *http.Request) { | return func(w http.ResponseWriter, r *http.Request) { | ||||
| query := r.URL.Query() | |||||
| lStr := query.Get("limit") | |||||
| if lStr == "" { | |||||
| lStr = "100" | |||||
| } | |||||
| limit, err := strconv.Atoi(lStr) | |||||
| if err != nil { | |||||
| slog.Error("invalid limit parameter", "value", lStr) | |||||
| response.BadRequest(w, "invalid limit parameter") | |||||
| return | |||||
| } | |||||
| oStr := query.Get("offset") | |||||
| if oStr == "" { | |||||
| oStr = "0" | |||||
| } | |||||
| offset, err := strconv.Atoi(oStr) | |||||
| if err != nil { | |||||
| slog.Error("invalid offset parameter", "value", oStr) | |||||
| response.BadRequest(w, "invalid offset parameter") | |||||
| return | |||||
| } | |||||
| var list []model.Tracker | var list []model.Tracker | ||||
| if err := db.WithContext(context).Find(&list).Error; err != nil { | |||||
| if err := db.WithContext(context).Limit(limit).Offset(offset).Find(&list).Error; err != nil { | |||||
| response.InternalError(w, "failed to list trackers", err) | response.InternalError(w, "failed to list trackers", err) | ||||
| return | return | ||||
| } | } | ||||
| @@ -3,7 +3,9 @@ package controller | |||||
| import ( | import ( | ||||
| "context" | "context" | ||||
| "encoding/json" | "encoding/json" | ||||
| "log/slog" | |||||
| "net/http" | "net/http" | ||||
| "strconv" | |||||
| "github.com/AFASystems/presence/internal/pkg/api/response" | "github.com/AFASystems/presence/internal/pkg/api/response" | ||||
| "github.com/AFASystems/presence/internal/pkg/model" | "github.com/AFASystems/presence/internal/pkg/model" | ||||
| @@ -34,8 +36,32 @@ func TrackerZoneAddController(db *gorm.DB, context context.Context) http.Handler | |||||
| func TrackerZoneListController(db *gorm.DB, context context.Context) http.HandlerFunc { | func TrackerZoneListController(db *gorm.DB, context context.Context) http.HandlerFunc { | ||||
| return func(w http.ResponseWriter, r *http.Request) { | return func(w http.ResponseWriter, r *http.Request) { | ||||
| query := r.URL.Query() | |||||
| lStr := query.Get("limit") | |||||
| if lStr == "" { | |||||
| lStr = "100" | |||||
| } | |||||
| limit, err := strconv.Atoi(lStr) | |||||
| if err != nil { | |||||
| slog.Error("invalid limit parameter", "value", lStr) | |||||
| response.BadRequest(w, "invalid limit parameter") | |||||
| return | |||||
| } | |||||
| oStr := query.Get("offset") | |||||
| if oStr == "" { | |||||
| oStr = "0" | |||||
| } | |||||
| offset, err := strconv.Atoi(oStr) | |||||
| if err != nil { | |||||
| slog.Error("invalid offset parameter", "value", oStr) | |||||
| response.BadRequest(w, "invalid offset parameter") | |||||
| return | |||||
| } | |||||
| var list []model.TrackerZones | var list []model.TrackerZones | ||||
| if err := db.WithContext(context).Find(&list).Error; err != nil { | |||||
| if err := db.WithContext(context).Limit(limit).Offset(offset).Find(&list).Error; err != nil { | |||||
| response.InternalError(w, "failed to list tracker zones", err) | response.InternalError(w, "failed to list tracker zones", err) | ||||
| return | return | ||||
| } | } | ||||
| @@ -3,7 +3,9 @@ package controller | |||||
| import ( | import ( | ||||
| "context" | "context" | ||||
| "encoding/json" | "encoding/json" | ||||
| "log/slog" | |||||
| "net/http" | "net/http" | ||||
| "strconv" | |||||
| "github.com/AFASystems/presence/internal/pkg/api/response" | "github.com/AFASystems/presence/internal/pkg/api/response" | ||||
| "github.com/AFASystems/presence/internal/pkg/model" | "github.com/AFASystems/presence/internal/pkg/model" | ||||
| @@ -34,8 +36,32 @@ func ZoneAddController(db *gorm.DB, context context.Context) http.HandlerFunc { | |||||
| func ZoneListController(db *gorm.DB, context context.Context) http.HandlerFunc { | func ZoneListController(db *gorm.DB, context context.Context) http.HandlerFunc { | ||||
| return func(w http.ResponseWriter, r *http.Request) { | return func(w http.ResponseWriter, r *http.Request) { | ||||
| query := r.URL.Query() | |||||
| lStr := query.Get("limit") | |||||
| if lStr == "" { | |||||
| lStr = "100" | |||||
| } | |||||
| limit, err := strconv.Atoi(lStr) | |||||
| if err != nil { | |||||
| slog.Error("invalid limit parameter", "value", lStr) | |||||
| response.BadRequest(w, "invalid limit parameter") | |||||
| return | |||||
| } | |||||
| oStr := query.Get("offset") | |||||
| if oStr == "" { | |||||
| oStr = "0" | |||||
| } | |||||
| offset, err := strconv.Atoi(oStr) | |||||
| if err != nil { | |||||
| slog.Error("invalid offset parameter", "value", oStr) | |||||
| response.BadRequest(w, "invalid offset parameter") | |||||
| return | |||||
| } | |||||
| var zones []model.Zone | var zones []model.Zone | ||||
| if err := db.WithContext(context).Find(&zones).Error; err != nil { | |||||
| if err := db.WithContext(context).Limit(limit).Offset(offset).Find(&zones).Error; err != nil { | |||||
| response.InternalError(w, "failed to list zones", err) | response.InternalError(w, "failed to list zones", err) | ||||
| return | return | ||||
| } | } | ||||
| @@ -4,13 +4,13 @@ import "time" | |||||
| type Tracks struct { | type Tracks struct { | ||||
| ID uint `gorm:"unique;primaryKey;autoIncrement"` | ID uint `gorm:"unique;primaryKey;autoIncrement"` | ||||
| UUID string `json:"id" gorm:"foreignKey"` | |||||
| Timestamp time.Time `json:"timestamp"` | |||||
| UUID string `json:"id" gorm:"foreignKey;index:idx_tracks_uuid_ts,priority:1"` | |||||
| Timestamp time.Time `json:"timestamp" gorm:"index:idx_tracks_tracker_ts,priority:2;index:idx_tracks_uuid_ts,priority:2"` | |||||
| Type string `json:"type"` | Type string `json:"type"` | ||||
| Status string `json:"status"` | Status string `json:"status"` | ||||
| Gateway string `json:"gateway" gorm:"index"` | Gateway string `json:"gateway" gorm:"index"` | ||||
| GatewayMac string `json:"gatewayMac"` | GatewayMac string `json:"gatewayMac"` | ||||
| Tracker string `json:"tracker" gorm:"index"` | |||||
| Tracker string `json:"tracker" gorm:"index:idx_tracks_tracker_ts,priority:1"` | |||||
| TrackerMac string `json:"trackerMac"` | TrackerMac string `json:"trackerMac"` | ||||
| Subject string `json:"subject"` | Subject string `json:"subject"` | ||||
| SubjectName string `json:"subjectName"` | SubjectName string `json:"subjectName"` | ||||
| @@ -21,9 +21,9 @@ func DeleteAlertByTrackerID(trackerID string, db *gorm.DB, ctx context.Context) | |||||
| return nil | return nil | ||||
| } | } | ||||
| func GetAllAlerts(db *gorm.DB, ctx context.Context) ([]model.Alert, error) { | |||||
| func GetAllAlerts(db *gorm.DB, ctx context.Context, limit, offset int) ([]model.Alert, error) { | |||||
| var alerts []model.Alert | var alerts []model.Alert | ||||
| if err := db.WithContext(ctx).Find(&alerts).Error; err != nil { | |||||
| if err := db.WithContext(ctx).Limit(limit).Offset(offset).Find(&alerts).Error; err != nil { | |||||
| return []model.Alert{}, err | return []model.Alert{}, err | ||||
| } | } | ||||