fixed tests, fixed linter errors, restructred packages

This commit is contained in:
Annika Hannig 2021-10-22 22:35:40 +02:00
parent 58e3ea8aab
commit 977e4db816
8 changed files with 59 additions and 158 deletions

View File

@ -26,7 +26,7 @@ func makeTestRoute() *Route {
func makeTestLookupRoute() *LookupRoute {
route := &LookupRoute{
Route: Route{
Route: &Route{
BGP: &BGPInfo{
Communities: []Community{
Community{23, 42},

View File

@ -1,31 +0,0 @@
package api
// AppStatus contains application status information
type AppStatus struct {
Version string `json:"version"`
Routes RoutesStoreStats `json:"routes"`
Neighbors NeighborsStoreStats `json:"neighbors"`
}
// NewAppStatus calculates the application status,
// and perform health checks on backends.
//
// TODO: Rename this.
func NewAppStatus() (*AppStatus, error) {
routesStatus := RoutesStoreStats{}
if AliceRoutesStore != nil {
routesStatus = AliceRoutesStore.Stats()
}
neighborsStatus := NeighborsStoreStats{}
if AliceRoutesStore != nil {
neighborsStatus = AliceNeighborsStore.Stats()
}
status := &AppStatus{
Version: Version,
Routes: routesStatus,
Neighbors: neighborsStatus,
}
return status, nil
}

View File

@ -1,4 +1,4 @@
package store
package api
import (
"log"

View File

@ -1,37 +0,0 @@
package config
// Version Alice (set during the build)
var Version = "unknown"
// Build is the current revision pointing at HEAD
var Build = "unknown"
// AppStatus contains application status information
type AppStatus struct {
Version string `json:"version"`
Routes RoutesStoreStats `json:"routes"`
Neighbors NeighborsStoreStats `json:"neighbors"`
}
// NewAppStatus calculates the application status,
// and perform health checks on backends.
//
// TODO: Rename this.
func NewAppStatus() (*AppStatus, error) {
routesStatus := RoutesStoreStats{}
if AliceRoutesStore != nil {
routesStatus = AliceRoutesStore.Stats()
}
neighborsStatus := NeighborsStoreStats{}
if AliceRoutesStore != nil {
neighborsStatus = AliceNeighborsStore.Stats()
}
status := &AppStatus{
Version: Version,
Routes: routesStatus,
Neighbors: neighborsStatus,
}
return status, nil
}

7
pkg/config/version.go Normal file
View File

@ -0,0 +1,7 @@
package config
// Version Alice (set during the build)
var Version = "unknown"
// Build is the current revision pointing at HEAD
var Build = "unknown"

40
pkg/http/status.go Normal file
View File

@ -0,0 +1,40 @@
package http
import (
"github.com/alice-lg/alice-lg/pkg/api"
"github.com/alice-lg/alice-lg/pkg/config"
"github.com/alice-lg/alice-lg/pkg/store"
)
// AppStatus contains application status information
type AppStatus struct {
Version string `json:"version"`
Routes api.RoutesStoreStats `json:"routes"`
Neighbors api.NeighborsStoreStats `json:"neighbors"`
}
// CollectAppStatus initializes the application
// status with stats gathered from the various
// application modules.
func CollectAppStatus(
routesStore *store.RoutesStore,
neighborsStore *store.NeighborsStore,
) (*AppStatus, error) {
routesStatus := &api.RoutesStoreStats{}
if routesStore != nil {
routesStatus = routesStore.Stats()
}
neighborsStatus := &api.NeighborsStoreStats{}
if neighborsStore != nil {
neighborsStatus = neighborsStore.Stats()
}
status := &AppStatus{
Version: config.Version,
Routes: routesStatus,
Neighbors: neighborsStatus,
}
return status, nil
}

View File

@ -8,14 +8,14 @@ import (
// Store State Constants
const (
STATE_INIT = iota
STATE_READY
STATE_UPDATING
STATE_ERROR
StateInit = iota
StateReady
StateUpdating
StateError
)
// StoreStatus defines a status the store can be in
type StoreStatus struct {
// Status defines a status the store can be in
type Status struct {
LastRefresh time.Time
LastError error
State int
@ -24,13 +24,13 @@ type StoreStatus struct {
// Helper: stateToString
func stateToString(state int) string {
switch state {
case STATE_INIT:
case StateInit:
return "INIT"
case STATE_READY:
case StateReady:
return "READY"
case STATE_UPDATING:
case StateUpdating:
return "UPDATING"
case STATE_ERROR:
case StateError:
return "ERROR"
}
return "INVALID"

View File

@ -1,78 +0,0 @@
package store
import (
"log"
"time"
)
// Routes Store
type RoutesStats struct {
Filtered int `json:"filtered"`
Imported int `json:"imported"`
}
type RouteServerRoutesStats struct {
Name string `json:"name"`
Routes RoutesStats `json:"routes"`
State string `json:"state"`
UpdatedAt time.Time `json:"updated_at"`
}
type RoutesStoreStats struct {
TotalRoutes RoutesStats `json:"total_routes"`
RouteServers []RouteServerRoutesStats `json:"route_servers"`
}
// Write stats to the log
func (stats RoutesStoreStats) Log() {
log.Println("Routes store:")
log.Println(" Routes Imported:",
stats.TotalRoutes.Imported,
"Filtered:",
stats.TotalRoutes.Filtered)
log.Println(" Routeservers:")
for _, rs := range stats.RouteServers {
log.Println(" -", rs.Name)
log.Println(" State:", rs.State)
log.Println(" UpdatedAt:", rs.UpdatedAt)
log.Println(" Routes Imported:",
rs.Routes.Imported,
"Filtered:",
rs.Routes.Filtered)
}
}
// Neighbors Store
type RouteServerNeighborsStats struct {
Name string `json:"name"`
State string `json:"state"`
Neighbors int `json:"neighbors"`
UpdatedAt time.Time `json:"updated_at"`
}
type NeighborsStoreStats struct {
TotalNeighbors int `json:"total_neighbors"`
RouteServers []RouteServerNeighborsStats `json:"route_servers"`
}
// Print stats
func (stats NeighborsStoreStats) Log() {
log.Println("Neighbors store:")
log.Println(" Neighbors:",
stats.TotalNeighbors)
for _, rs := range stats.RouteServers {
log.Println(" -", rs.Name)
log.Println(" State:", rs.State)
log.Println(" UpdatedAt:", rs.UpdatedAt)
log.Println(" Neighbors:",
rs.Neighbors)
}
}