diff --git a/backend/config.go b/backend/config.go index c7f30ba..74bcbf5 100644 --- a/backend/config.go +++ b/backend/config.go @@ -55,6 +55,8 @@ type Config struct { Ui UiConfig Sources []SourceConfig File string + + instances map[SourceConfig]sources.Source } // Get sources keys form ini diff --git a/backend/neighbours_store.go b/backend/neighbours_store.go index c2810bd..563b093 100644 --- a/backend/neighbours_store.go +++ b/backend/neighbours_store.go @@ -2,7 +2,6 @@ package main import ( "github.com/ecix/alice-lg/backend/api" - "github.com/ecix/alice-lg/backend/sources" "log" "time" @@ -11,26 +10,26 @@ import ( type NeighboursIndex map[string]api.Neighbour type NeighboursStore struct { - neighboursMap map[sources.Source]NeighboursIndex - configMap map[sources.Source]SourceConfig - statusMap map[sources.Source]StoreStatus + neighboursMap map[int]NeighboursIndex + configMap map[int]SourceConfig + statusMap map[int]StoreStatus } func NewNeighboursStore(config *Config) *NeighboursStore { // Build source mapping - neighboursMap := make(map[sources.Source]NeighboursIndex) - configMap := make(map[sources.Source]SourceConfig) - statusMap := make(map[sources.Source]StoreStatus) + neighboursMap := make(map[int]NeighboursIndex) + configMap := make(map[int]SourceConfig) + statusMap := make(map[int]StoreStatus) for _, source := range config.Sources { - instance := source.getInstance() - configMap[instance] = source - statusMap[instance] = StoreStatus{ + sourceId := source.Id + configMap[sourceId] = source + statusMap[sourceId] = StoreStatus{ State: STATE_INIT, } - neighboursMap[instance] = make(NeighboursIndex) + neighboursMap[sourceId] = make(NeighboursIndex) } store := &NeighboursStore{ @@ -61,22 +60,24 @@ func (self *NeighboursStore) init() { } func (self *NeighboursStore) update() { - for source, _ := range self.neighboursMap { + for sourceId, _ := range self.neighboursMap { // Get current state - if self.statusMap[source].State == STATE_UPDATING { + if self.statusMap[sourceId].State == STATE_UPDATING { continue // nothing to do here. really. } // Start updating - self.statusMap[source] = StoreStatus{ + self.statusMap[sourceId] = StoreStatus{ State: STATE_UPDATING, } + source := self.configMap[sourceId].getInstance() + neighboursRes, err := source.Neighbours() neighbours := neighboursRes.Neighbours if err != nil { // That's sad. - self.statusMap[source] = StoreStatus{ + self.statusMap[sourceId] = StoreStatus{ State: STATE_ERROR, LastError: err, LastRefresh: time.Now(), @@ -91,9 +92,9 @@ func (self *NeighboursStore) update() { index[neighbour.Id] = neighbour } - self.neighboursMap[source] = index + self.neighboursMap[sourceId] = index // Update state - self.statusMap[source] = StoreStatus{ + self.statusMap[sourceId] = StoreStatus{ LastRefresh: time.Now(), State: STATE_READY, } @@ -101,14 +102,11 @@ func (self *NeighboursStore) update() { } func (self *NeighboursStore) GetNeighbourAt( - source sources.Source, + sourceId int, id string, ) api.Neighbour { // Lookup neighbour on RS - neighbours := self.neighboursMap[source] - log.Println("Fetching neighbour:", id) - log.Println("neighbour:", neighbours[id]) - log.Println("neighbours:", neighbours) + neighbours := self.neighboursMap[sourceId] return neighbours[id] } @@ -117,11 +115,11 @@ func (self *NeighboursStore) Stats() NeighboursStoreStats { totalNeighbours := 0 rsStats := []RouteServerNeighboursStats{} - for source, neighbours := range self.neighboursMap { - status := self.statusMap[source] + for sourceId, neighbours := range self.neighboursMap { + status := self.statusMap[sourceId] totalNeighbours += len(neighbours) serverStats := RouteServerNeighboursStats{ - Name: self.configMap[source].Name, + Name: self.configMap[sourceId].Name, State: stateToString(status.State), Neighbours: len(neighbours), UpdatedAt: status.LastRefresh, diff --git a/backend/routes_store.go b/backend/routes_store.go index 16d2747..77b31aa 100644 --- a/backend/routes_store.go +++ b/backend/routes_store.go @@ -2,7 +2,6 @@ package main import ( "github.com/ecix/alice-lg/backend/api" - "github.com/ecix/alice-lg/backend/sources" "log" "strings" @@ -10,23 +9,24 @@ import ( ) type RoutesStore struct { - routesMap map[sources.Source]api.RoutesResponse - statusMap map[sources.Source]StoreStatus - configMap map[sources.Source]SourceConfig + routesMap map[int]api.RoutesResponse + statusMap map[int]StoreStatus + configMap map[int]SourceConfig } func NewRoutesStore(config *Config) *RoutesStore { // Build mapping based on source instances - routesMap := make(map[sources.Source]api.RoutesResponse) - statusMap := make(map[sources.Source]StoreStatus) - configMap := make(map[sources.Source]SourceConfig) + routesMap := make(map[int]api.RoutesResponse) + statusMap := make(map[int]StoreStatus) + configMap := make(map[int]SourceConfig) for _, source := range config.Sources { - instance := source.getInstance() - configMap[instance] = source - routesMap[instance] = api.RoutesResponse{} - statusMap[instance] = StoreStatus{ + id := source.Id + + configMap[id] = source + routesMap[id] = api.RoutesResponse{} + statusMap[id] = StoreStatus{ State: STATE_INIT, } } @@ -62,20 +62,22 @@ func (self *RoutesStore) init() { // Update all routes func (self *RoutesStore) update() { - for source, _ := range self.routesMap { + for sourceId, _ := range self.routesMap { + source := self.configMap[sourceId].getInstance() + // Get current update state - if self.statusMap[source].State == STATE_UPDATING { + if self.statusMap[sourceId].State == STATE_UPDATING { continue // nothing to do here } // Set update state - self.statusMap[source] = StoreStatus{ + self.statusMap[sourceId] = StoreStatus{ State: STATE_UPDATING, } routes, err := source.AllRoutes() if err != nil { - self.statusMap[source] = StoreStatus{ + self.statusMap[sourceId] = StoreStatus{ State: STATE_ERROR, LastError: err, LastRefresh: time.Now(), @@ -85,9 +87,9 @@ func (self *RoutesStore) update() { } // Update data - self.routesMap[source] = routes + self.routesMap[sourceId] = routes // Update state - self.statusMap[source] = StoreStatus{ + self.statusMap[sourceId] = StoreStatus{ LastRefresh: time.Now(), State: STATE_READY, } @@ -101,14 +103,14 @@ func (self *RoutesStore) Stats() RoutesStoreStats { rsStats := []RouteServerRoutesStats{} - for source, routes := range self.routesMap { - status := self.statusMap[source] + for sourceId, routes := range self.routesMap { + status := self.statusMap[sourceId] totalImported += len(routes.Imported) totalFiltered += len(routes.Filtered) serverStats := RouteServerRoutesStats{ - Name: self.configMap[source].Name, + Name: self.configMap[sourceId].Name, Routes: RoutesStats{ Filtered: len(routes.Filtered), @@ -172,24 +174,24 @@ func filterRoutes( } func addNeighbour( - source sources.Source, + sourceId int, route api.LookupRoute, ) api.LookupRoute { neighbour := AliceNeighboursStore.GetNeighbourAt( - source, route.NeighbourId) + sourceId, route.NeighbourId) route.Neighbour = neighbour return route } // Single RS lookup func (self *RoutesStore) lookupRs( - source sources.Source, + sourceId int, prefix string, ) chan []api.LookupRoute { response := make(chan []api.LookupRoute) - config := self.configMap[source] - routes := self.routesMap[source] + config := self.configMap[sourceId] + routes := self.routesMap[sourceId] go func() { result := []api.LookupRoute{} @@ -207,11 +209,11 @@ func (self *RoutesStore) lookupRs( // Add Neighbours to results for _, route := range filtered { - result = append(result, addNeighbour(source, route)) + result = append(result, addNeighbour(sourceId, route)) } for _, route := range imported { - result = append(result, addNeighbour(source, route)) + result = append(result, addNeighbour(sourceId, route)) } response <- result @@ -225,8 +227,8 @@ func (self *RoutesStore) Lookup(prefix string) []api.LookupRoute { responses := []chan []api.LookupRoute{} // Dispatch - for source, _ := range self.routesMap { - res := self.lookupRs(source, prefix) + for sourceId, _ := range self.routesMap { + res := self.lookupRs(sourceId, prefix) responses = append(responses, res) }