package resolveLogs import ( "encoding/json" "strings" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils" "github.com/TeaOSLab/EdgeUser/internal/web/actions/default/httpdns/httpdnsutils" timeutil "github.com/iwind/TeaGo/utils/time" ) type IndexAction struct { actionutils.ParentAction } func (this *IndexAction) Init() { this.Nav("httpdns", "resolveLogs", "") } func (this *IndexAction) RunGet(params struct { ClusterId int64 AppId string Domain string Status string Keyword string }) { httpdnsutils.AddLeftMenu(this.Parent()) if params.ClusterId > 0 { this.Data["clusterId"] = params.ClusterId } else { this.Data["clusterId"] = "" } this.Data["appId"] = params.AppId this.Data["domain"] = params.Domain this.Data["status"] = params.Status this.Data["keyword"] = params.Keyword clusterResp, err := this.RPC().HTTPDNSClusterRPC().FindAllHTTPDNSClusters(this.UserContext(), &pb.FindAllHTTPDNSClustersRequest{}) if err != nil { this.ErrorPage(err) return } clusters := make([]map[string]interface{}, 0, len(clusterResp.GetClusters())) clusterDomainMap := map[int64]string{} for _, cluster := range clusterResp.GetClusters() { serviceDomain := strings.TrimSpace(cluster.GetServiceDomain()) port := "443" if rawTLS := cluster.GetTlsPolicyJSON(); len(rawTLS) > 0 { var tlsConfig map[string]interface{} if err := json.Unmarshal(rawTLS, &tlsConfig); err == nil { if listenRaw, ok := tlsConfig["listen"]; ok && listenRaw != nil { if data, err := json.Marshal(listenRaw); err == nil { var listenAddresses []map[string]interface{} if err := json.Unmarshal(data, &listenAddresses); err == nil { if len(listenAddresses) > 0 { if portRange, ok := listenAddresses[0]["portRange"].(string); ok && len(portRange) > 0 { port = portRange } } } } } } } apiAddress := "https://" + serviceDomain + ":" + port displayName := apiAddress if len(serviceDomain) == 0 { displayName = cluster.GetName() } clusters = append(clusters, map[string]interface{}{ "id": cluster.GetId(), "name": cluster.GetName(), "serviceDomain": serviceDomain, "displayName": displayName, }) clusterDomainMap[cluster.GetId()] = apiAddress } this.Data["clusters"] = clusters logResp, err := this.RPC().HTTPDNSAccessLogRPC().ListHTTPDNSAccessLogs(this.UserContext(), &pb.ListHTTPDNSAccessLogsRequest{ Day: "", ClusterId: params.ClusterId, NodeId: 0, AppId: strings.TrimSpace(params.AppId), Domain: strings.TrimSpace(params.Domain), Status: strings.TrimSpace(params.Status), Keyword: strings.TrimSpace(params.Keyword), Offset: 0, Size: 100, }) if err != nil { this.ErrorPage(err) return } logs := make([]map[string]interface{}, 0, len(logResp.GetLogs())) for _, item := range logResp.GetLogs() { createdTime := "" if item.GetCreatedAt() > 0 { createdTime = timeutil.FormatTime("Y-m-d H:i:s", item.GetCreatedAt()) } status := item.GetStatus() if len(status) == 0 { status = "failed" } errorCode := item.GetErrorCode() if len(errorCode) == 0 { errorCode = "none" } logs = append(logs, map[string]interface{}{ "time": createdTime, "clusterId": item.GetClusterId(), "serviceDomain": func() string { serviceDomain := strings.TrimSpace(clusterDomainMap[item.GetClusterId()]) if len(serviceDomain) > 0 { return serviceDomain } if len(strings.TrimSpace(item.GetClusterName())) > 0 { return item.GetClusterName() } return "-" }(), "appName": item.GetAppName(), "appId": item.GetAppId(), "domain": item.GetDomain(), "query": item.GetQtype(), "clientIp": item.GetClientIP(), "os": item.GetOs(), "sdkVersion": item.GetSdkVersion(), "ips": item.GetResultIPs(), "status": status, "errorCode": errorCode, "costMs": item.GetCostMs(), }) } this.Data["resolveLogs"] = logs this.Show() }