From 85d7f60698f877c17efd3d60e61953a0ef63a70f Mon Sep 17 00:00:00 2001 From: vishnuajayccst Date: Tue, 26 Aug 2025 15:00:57 +0530 Subject: [PATCH] ImplementedGetAllCanaryGroups method and related utilities for group filtering --- http/group_service_connector.go | 91 ++++++++++++++++++++++++++++++++- 1 file changed, 89 insertions(+), 2 deletions(-) diff --git a/http/group_service_connector.go b/http/group_service_connector.go index 6dbea85..795f855 100644 --- a/http/group_service_connector.go +++ b/http/group_service_connector.go @@ -2,7 +2,9 @@ package http import ( "crypto/tls" + "encoding/json" "fmt" + "strings" proto2 "github.com/rdkcentral/xconfadmin/taggingapi/proto/generated" "github.com/rdkcentral/xconfadmin/util" @@ -15,8 +17,9 @@ import ( var groupServiceName string const ( - GetGroupsMembers = "%s/v2/ft/%s" - GetAllGroups = "%s/v2/ft" + GetGroupsMembers = "%s/v2/ft/%s" + GetAllGroups = "%s/v2/ft" + GetAllCanaryGroupsPath = "%s/v2/gs/list/all?format=json" ) type GroupServiceConnector struct { @@ -77,3 +80,87 @@ func unmarshalXdasHashes(bytes []byte) (*proto2.XdasHashes, error) { } return &groups, nil } + +func (c *GroupServiceConnector) GetAllCanaryGroups() ([]string, error) { + url := fmt.Sprintf(GetAllCanaryGroupsPath, c.GetGroupServiceHost()) + + headers := map[string]string{ + "Accept": "application/json", + "Connection": "close", + } + + rbytes, err := c.DoRequest(HttpGet, url, headers, nil) + if err != nil { + return nil, fmt.Errorf("failed to fetch canary groups: %v", err) + } + + if len(rbytes) == 0 { + return nil, fmt.Errorf("received empty response from canary service") + } + + var response map[string]interface{} + err = json.Unmarshal(rbytes, &response) + if err != nil { + return nil, fmt.Errorf("failed to unmarshal JSON response: %v", err) + } + + meta, ok := response["meta"].(map[string]interface{}) + if !ok { + return []string{}, nil + } + + groups := make([]string, 0, len(meta)) + + for groupName, groupData := range meta { + if !c.isTimestampEndedGroup(groupName) { + continue + } + + if groupInfo, ok := groupData.(map[string]interface{}); ok { + if counter, exists := groupInfo["counter"]; exists { + groups = append(groups, fmt.Sprintf("%s (Size: %s)", groupName, counter)) + } else { + groups = append(groups, groupName) + } + } else { + groups = append(groups, groupName) + } + } + return groups, nil +} + +func (c *GroupServiceConnector) isTimestampEndedGroup(groupName string) bool { + + if len(groupName) < 6 { + return false + } + + parts := strings.Split(groupName, "_") + if len(parts) < 2 { + return false + } + + lastPart := parts[len(parts)-1] + if len(lastPart) >= 10 && c.isNumeric(lastPart) { + return true + } + + if len(parts) >= 3 { + secondLastPart := parts[len(parts)-2] + return c.isNumeric(lastPart) && c.isNumeric(secondLastPart) + } + + return false +} + +func (c *GroupServiceConnector) isNumeric(s string) bool { + if len(s) == 0 { + return false + } + for _, char := range s { + if char < '0' || char > '9' { + return false + } + } + return true +}