diff --git a/internal/olsrd/hosts_parser.go b/internal/olsrd/hosts_parser.go index 14707168..7cb5f7a5 100644 --- a/internal/olsrd/hosts_parser.go +++ b/internal/olsrd/hosts_parser.go @@ -5,6 +5,7 @@ import ( "net" "net/url" "os" + "regexp" "strings" "sync/atomic" ) @@ -12,8 +13,9 @@ import ( const hostsFile = "/var/run/hosts_olsr" type HostsParser struct { - currentHosts []*AREDNHost - isParsing atomic.Bool + currentHosts []*AREDNHost + arednNodesCount int + isParsing atomic.Bool } func NewHostsParser() *HostsParser { @@ -28,6 +30,10 @@ func (p *HostsParser) GetHostsCount() int { return len(p.currentHosts) } +func (p *HostsParser) GetAREDNHostsCount() int { + return p.arednNodesCount +} + func (p *HostsParser) GetHostsPaginated(page int, limit int, filter string) []*AREDNHost { ret := []*AREDNHost{} for _, host := range p.currentHosts { @@ -54,10 +60,11 @@ func (p *HostsParser) Parse() (err error) { } p.isParsing.Store(true) defer p.isParsing.Store(false) - hosts, err := parseHosts() + hosts, arednCount, err := parseHosts() if err != nil { return } + p.arednNodesCount = arednCount p.currentHosts = hosts return } @@ -96,7 +103,7 @@ func (h *AREDNHost) String() string { // Lines with only whitespace or that are empty are ignored // //nolint:golint,gocyclo -func parseHosts() (ret []*AREDNHost, err error) { +func parseHosts() (ret []*AREDNHost, arednCount int, err error) { hostsFile, err := os.ReadFile(hostsFile) if err != nil { return @@ -147,6 +154,9 @@ func parseHosts() (ret []*AREDNHost, err error) { } if strings.Contains(split[1], ".") { + if regexp.MustCompile(`mid\d+\.`).MatchString(split[1]) { + arednCount++ + } continue } diff --git a/internal/server/api/controllers/v1/olsr.go b/internal/server/api/controllers/v1/olsr.go index a1c46062..d79f6fee 100644 --- a/internal/server/api/controllers/v1/olsr.go +++ b/internal/server/api/controllers/v1/olsr.go @@ -52,6 +52,17 @@ func GETOLSRHosts(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"nodes": nodes, "total": total}) } +func GETOLSRHostsCount(c *gin.Context) { + olsrdParser, ok := c.MustGet("OLSRDHostParser").(*olsrd.HostsParser) + if !ok { + fmt.Println("POSTLogin: OLSRDHostParser not found in context") + c.JSON(http.StatusInternalServerError, gin.H{"error": "Try again later"}) + return + } + + c.JSON(http.StatusOK, gin.H{"nodes": olsrdParser.GetAREDNHostsCount(), "total": olsrdParser.GetHostsCount()}) +} + func GETOLSRRunning(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"running": olsrd.IsRunning()}) } diff --git a/internal/server/api/routes.go b/internal/server/api/routes.go index d14c8c2d..34f644c9 100644 --- a/internal/server/api/routes.go +++ b/internal/server/api/routes.go @@ -50,6 +50,7 @@ func v1(group *gin.RouterGroup, config *config.Config) { v1OLSR := group.Group("/olsr") v1OLSR.GET("/hosts", v1Controllers.GETOLSRHosts) + v1OLSR.GET("/hosts/count", v1Controllers.GETOLSRHostsCount) v1OLSR.GET("/running", v1Controllers.GETOLSRRunning) v1VTun := group.Group("/vtun")