1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00

95 lines
2.4 KiB
Go
Raw Normal View History

2017-05-19 17:40:22 +02:00
package birdwatcher
// Parsers and helpers
import (
"time"
2017-05-19 17:56:01 +02:00
"github.com/ecix/alice-lg/backend/api"
2017-05-19 17:40:22 +02:00
)
2017-05-19 17:56:01 +02:00
const SERVER_TIME = time.RFC3339Nano
2017-05-19 17:40:22 +02:00
const SERVER_TIME_SHORT = "2006-01-02 15:04:05"
const SERVER_TIME_EXT = "Mon, 2 Jan 2006 15:04:05 +0000"
// Convert server time string to time
func parseServerTime(value interface{}, layout, timezone string) (time.Time, error) {
svalue, ok := value.(string)
if !ok {
return time.Time{}, nil
}
loc, err := time.LoadLocation(timezone)
if err != nil {
return time.Time{}, err
}
t, err := time.ParseInLocation(layout, svalue, loc)
return t, err
}
2017-05-19 17:56:01 +02:00
// Make api status from response:
// The api status is always included in a birdwatcher response
2017-05-22 11:09:22 +02:00
func parseApiStatus(bird ClientResponse, config Config) (api.ApiStatus, error) {
2017-05-19 17:56:01 +02:00
birdApi := bird["api"].(map[string]interface{})
ttl, err := parseServerTime(
bird["ttl"],
SERVER_TIME,
config.Timezone,
)
2017-05-22 10:27:19 +02:00
if err != nil {
2017-05-22 11:09:22 +02:00
return api.ApiStatus{}, err
2017-05-22 10:27:19 +02:00
}
2017-05-19 17:56:01 +02:00
status := api.ApiStatus{
Version: birdApi["Version"].(string),
ResultFromCache: birdApi["result_from_cache"].(bool),
Ttl: ttl,
}
2017-05-22 11:09:22 +02:00
return status, nil
}
2017-05-22 11:46:50 +02:00
// Parse neighbour uptime
func parseNeighbourUptime(uptime interface{}, config Config) time.Duration {
serverTime, _ := parseServerTime(uptime, SERVER_TIME_SHORT, config.Timezone)
return time.Since(serverTime)
}
2017-05-22 11:09:22 +02:00
// Parse neighbours response
2017-05-22 11:46:50 +02:00
func parseNeighbours(bird ClientResponse, config Config) ([]api.Neighbour, error) {
neighbours := []api.Neighbour{}
protocols := bird["protocols"].(map[string]interface{})
// Iterate over protocols map:
for protocolId, proto := range protocols {
protocol := proto.(map[string]interface{})
routes := protocol["routes"].(map[string]interface{})
uptime := parseNeighbourUptime(protocol["state_changed"], config)
neighbour := api.Neighbour{
Id: protocolId,
Address: protocol["neighbor_address"].(string),
Asn: int(protocol["neighbor_as"].(float64)),
State: protocol["state"].(string),
Description: protocol["description"].(string),
RoutesReceived: int(routes["imported"].(float64)),
RoutesExported: int(routes["exported"].(float64)),
RoutesFiltered: int(routes["filtered"].(float64)),
RoutesPreferred: int(routes["preferred"].(float64)),
Uptime: uptime,
Details: protocol,
}
neighbours = append(neighbours, neighbour)
}
2017-05-22 11:09:22 +02:00
2017-05-22 11:46:50 +02:00
return neighbours, nil
2017-05-19 17:56:01 +02:00
}