This is an automated email from the ASF dual-hosted git repository.

zrhoffman pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new e835435e47 Update Traffic Stats to use TO APIv5 (#7765)
e835435e47 is described below

commit e835435e47175f84a04234d15183ab7b61cc2825
Author: ocket8888 <[email protected]>
AuthorDate: Wed Aug 30 17:00:43 2023 -0600

    Update Traffic Stats to use TO APIv5 (#7765)
    
    * Update Traffic Stats to use TO APIv5
    
    * update changelog
---
 CHANGELOG.md                        |  1 +
 traffic_stats/traffic_stats.go      | 43 +++++++++++++++++++++----------------
 traffic_stats/traffic_stats_test.go | 43 +++++++++++++++++++------------------
 3 files changed, 48 insertions(+), 39 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 573550766c..b8670e95ba 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -76,6 +76,7 @@ The format is based on [Keep a 
Changelog](http://keepachangelog.com/en/1.0.0/).
 - [#7716](https://github.com/apache/trafficcontrol/pull/7716) *Apache Traffic 
Server* Use GCC 11 for building.
 - [#7742](https://github.com/apache/trafficcontrol/pull/7742) *Traffic Ops* 
Changed api tests to supply the absolute path of certs.
 - [#7718](https://github.com/apache/trafficcontrol/pull/7718) *Traffic Ops* 
`/servers` endpoint now responds with RFC3339 timestamps for all timestamp 
fields. Cleaned up naming conventions and superfluous data.
+- [#7765](https://github.com/apache/trafficcontrol/pull/7765) *Traffic Stats* 
now uses Traffic Ops APIv5
 
 ### Fixed
 - [RFC3339](https://github.com/apache/trafficcontrol/issues/5911)
diff --git a/traffic_stats/traffic_stats.go b/traffic_stats/traffic_stats.go
index ed1c694469..b13c2097ab 100644
--- a/traffic_stats/traffic_stats.go
+++ b/traffic_stats/traffic_stats.go
@@ -40,7 +40,7 @@ import (
        "github.com/apache/trafficcontrol/lib/go-log"
        "github.com/apache/trafficcontrol/lib/go-tc"
        "github.com/apache/trafficcontrol/lib/go-util"
-       client "github.com/apache/trafficcontrol/traffic_ops/v3-client"
+       client "github.com/apache/trafficcontrol/traffic_ops/v5-client"
 
        "github.com/Shopify/sarama"
        "github.com/cihub/seelog"
@@ -163,7 +163,7 @@ var useSeelog bool = true
 // about caches, cachegroups, and health urls
 type RunningConfig struct {
        HealthUrls      map[string]map[string][]string // the 1st map key is 
CDN_name, the second is DsStats or CacheStats
-       CacheMap        map[string]tc.Server           // map hostName to cache
+       CacheMap        map[string]tc.ServerV5         // map hostName to cache
        LastSummaryTime time.Time
 }
 
@@ -622,7 +622,7 @@ func calcDailyMaxGbps(client influx.Client, bp 
influx.BatchPoints, startTime tim
                                        value = value / kilobitsToGigabits
                                        statTime, _ := time.Parse(time.RFC3339, 
t)
                                        infof("max gbps for cdn %v = %v", cdn, 
value)
-                                       var statsSummary tc.StatsSummary
+                                       var statsSummary tc.StatsSummaryV5
                                        statsSummary.CDNName = util.StrPtr(cdn)
                                        statsSummary.DeliveryService = 
util.StrPtr("all")
                                        statsSummary.StatName = 
util.StrPtr("daily_maxgbps")
@@ -682,7 +682,7 @@ func calcDailyBytesServed(client influx.Client, bp 
influx.BatchPoints, startTime
                        bytesServedTB := bytesServed / bytesToTerabytes
                        infof("TBytes served for cdn %v = %v", cdn, 
bytesServedTB)
                        //write to Traffic Ops
-                       var statsSummary tc.StatsSummary
+                       var statsSummary tc.StatsSummaryV5
                        statsSummary.CDNName = util.StrPtr(cdn)
                        statsSummary.DeliveryService = util.StrPtr("all")
                        statsSummary.StatName = util.StrPtr("daily_bytesserved")
@@ -725,14 +725,14 @@ func queryDB(con influx.Client, cmd string, database 
string) (res []influx.Resul
        return
 }
 
-func writeSummaryStats(config StartupConfig, statsSummary tc.StatsSummary) {
+func writeSummaryStats(config StartupConfig, statsSummary tc.StatsSummaryV5) {
        to, _, err := client.LoginWithAgent(config.ToURL, config.ToUser, 
config.ToPasswd, true, UserAgent, false, 
time.Duration(config.ToRequestTimeoutSeconds)*time.Second)
        if err != nil {
                newErr := fmt.Errorf("Could not store summary stats! Error 
logging in to %v: %v", config.ToURL, err)
                errorln(newErr)
                return
        }
-       _, _, err = to.CreateSummaryStats(statsSummary)
+       _, _, err = to.CreateSummaryStats(statsSummary, client.RequestOptions{})
        if err != nil {
                errorf("could not create summary stats: %v", err)
        }
@@ -750,7 +750,7 @@ func getToData(config StartupConfig, init bool, configChan 
chan RunningConfig) {
                return
        }
 
-       servers, _, err := to.GetServers(nil)
+       servers, _, err := to.GetServers(client.RequestOptions{})
        if err != nil {
                msg := fmt.Sprintf("Error getting server list from %v: %v ", 
config.ToURL, err)
                if init {
@@ -760,14 +760,14 @@ func getToData(config StartupConfig, init bool, 
configChan chan RunningConfig) {
                return
        }
 
-       runningConfig.CacheMap = make(map[string]tc.Server)
-       for _, server := range servers {
+       runningConfig.CacheMap = make(map[string]tc.ServerV5)
+       for _, server := range servers.Response {
                runningConfig.CacheMap[server.HostName] = server
        }
 
        cacheStatPath := "/publish/CacheStats?hc=1&wildcard=1&stats="
        dsStatPath := "/publish/DsStats?hc=1&wildcard=1&stats="
-       parameters, _, err := to.GetParametersByProfileName("TRAFFIC_STATS")
+       parameters, _, err := to.GetParametersByProfileName("TRAFFIC_STATS", 
client.RequestOptions{})
        if err != nil {
                msg := fmt.Sprintf("Error getting parameter list from %v: %v", 
config.ToURL, err)
                if init {
@@ -777,7 +777,7 @@ func getToData(config StartupConfig, init bool, configChan 
chan RunningConfig) {
                return
        }
 
-       for _, param := range parameters {
+       for _, param := range parameters.Response {
                if param.Name == "DsStats" {
                        statName := param.Value
                        dsStatPath += "," + statName
@@ -790,7 +790,13 @@ func getToData(config StartupConfig, init bool, configChan 
chan RunningConfig) {
 
        setHealthURLs(config, &runningConfig, cacheStatPath, dsStatPath)
 
-       lastSummaryTimeResponse, _, err := 
to.GetSummaryStatsLastUpdated(util.StrPtr("daily_maxgbps"))
+       opts := client.RequestOptions{
+               QueryParameters: url.Values{
+                       "lastSummaryDate": {"true"},
+                       "statName":        {"daily_maxgbps"},
+               },
+       }
+       lastSummaryTimeResponse, _, err := to.GetSummaryStatsLastUpdated(opts)
        if err != nil {
                errorf("unable to get summary stats last updated: %v", err)
        } else if lastSummaryTimeResponse.Response.SummaryTime == nil {
@@ -811,7 +817,7 @@ func setHealthURLs(config StartupConfig, runningConfig 
*RunningConfig, cacheStat
                }
 
                if server.Type == tc.MonitorTypeName && server.Status == 
config.StatusToMon {
-                       cdnName := server.CDNName
+                       cdnName := server.CDN
                        if cdnName == "" {
                                errorln("Unable to find CDN name for " + 
server.HostName + ".. skipping")
                                continue
@@ -820,15 +826,16 @@ func setHealthURLs(config StartupConfig, runningConfig 
*RunningConfig, cacheStat
                        if runningConfig.HealthUrls[cdnName] == nil {
                                runningConfig.HealthUrls[cdnName] = 
make(map[string][]string)
                        }
-                       healthURL := "http://"; + server.HostName + "." + 
server.DomainName + ":" + strconv.Itoa(server.TCPPort) + cacheStatPath
+                       tcpPort := util.CoalesceToDefault(server.TCPPort)
+                       healthURL := "http://"; + server.HostName + "." + 
server.DomainName + ":" + strconv.Itoa(tcpPort) + cacheStatPath
                        runningConfig.HealthUrls[cdnName]["CacheStats"] = 
append(runningConfig.HealthUrls[cdnName]["CacheStats"], healthURL)
-                       healthURL = "http://"; + server.HostName + "." + 
server.DomainName + ":" + strconv.Itoa(server.TCPPort) + dsStatPath
+                       healthURL = "http://"; + server.HostName + "." + 
server.DomainName + ":" + strconv.Itoa(tcpPort) + dsStatPath
                        runningConfig.HealthUrls[cdnName]["DsStats"] = 
append(runningConfig.HealthUrls[cdnName]["DsStats"], healthURL)
                }
        }
 }
 
-func calcMetrics(cdnName string, urls []string, cacheMap map[string]tc.Server, 
config StartupConfig) {
+func calcMetrics(cdnName string, urls []string, cacheMap 
map[string]tc.ServerV5, config StartupConfig) {
        sampleTime := time.Now().Unix()
        // get the data from trafficMonitor
        var trafMonData []byte
@@ -952,7 +959,7 @@ func calcDsValues(tmData []byte, cdnName string, sampleTime 
int64, config Startu
        return nil
 }
 
-func calcCacheValues(trafmonData []byte, cdnName string, sampleTime int64, 
cacheMap map[string]tc.Server, config StartupConfig) error {
+func calcCacheValues(trafmonData []byte, cdnName string, sampleTime int64, 
cacheMap map[string]tc.ServerV5, config StartupConfig) error {
        var jData tc.LegacyStats
        err := json.Unmarshal(trafmonData, &jData)
        if err != nil {
@@ -998,7 +1005,7 @@ func calcCacheValues(trafmonData []byte, cdnName string, 
sampleTime int64, cache
                        }
 
                        tags := map[string]string{
-                               "cachegroup": cache.Cachegroup,
+                               "cachegroup": cache.CacheGroup,
                                "hostname":   string(cacheName),
                                "cdn":        cdnName,
                                "type":       cache.Type,
diff --git a/traffic_stats/traffic_stats_test.go 
b/traffic_stats/traffic_stats_test.go
index 985fa7637d..76008656f9 100644
--- a/traffic_stats/traffic_stats_test.go
+++ b/traffic_stats/traffic_stats_test.go
@@ -27,13 +27,14 @@ import (
        "time"
 
        "github.com/apache/trafficcontrol/lib/go-tc"
+       "github.com/apache/trafficcontrol/lib/go-util"
        influx "github.com/influxdata/influxdb/client/v2"
 )
 
 func TestCalcCacheValuesWithInvalidValue(t *testing.T) {
        stats := make(map[string][]tc.ResultStatVal)
        caches := make(map[tc.CacheName]map[string][]tc.ResultStatVal)
-       cacheMap := make(map[string]tc.Server)
+       cacheMap := make(map[string]tc.ServerV5)
        resultSatsVal := []tc.ResultStatVal{
                {
                        Span: 0,
@@ -43,7 +44,7 @@ func TestCalcCacheValuesWithInvalidValue(t *testing.T) {
        }
        stats["maxKbps"] = resultSatsVal
        caches["cache1"] = stats
-       cacheMap["cache1"] = tc.Server{}
+       cacheMap["cache1"] = tc.ServerV5{}
        config := StartupConfig{
                BpsChan: make(chan influx.BatchPoints),
        }
@@ -76,7 +77,7 @@ func TestCalcCacheValuesWithInvalidValue(t *testing.T) {
 func TestCalcCacheValuesWithEmptyInterface(t *testing.T) {
        stats := make(map[string][]tc.ResultStatVal)
        caches := make(map[tc.CacheName]map[string][]tc.ResultStatVal)
-       cacheMap := make(map[string]tc.Server)
+       cacheMap := make(map[string]tc.ServerV5)
        resultSatsVal := []tc.ResultStatVal{
                {
                        Span: 0,
@@ -85,7 +86,7 @@ func TestCalcCacheValuesWithEmptyInterface(t *testing.T) {
        }
        stats["maxKbps"] = resultSatsVal
        caches["cache1"] = stats
-       cacheMap["cache1"] = tc.Server{}
+       cacheMap["cache1"] = tc.ServerV5{}
        config := StartupConfig{
                BpsChan: make(chan influx.BatchPoints),
        }
@@ -118,7 +119,7 @@ func TestCalcCacheValuesWithEmptyInterface(t *testing.T) {
 func TestCalcCacheValues(t *testing.T) {
        stats := make(map[string][]tc.ResultStatVal)
        caches := make(map[tc.CacheName]map[string][]tc.ResultStatVal)
-       cacheMap := make(map[string]tc.Server)
+       cacheMap := make(map[string]tc.ServerV5)
        resultSatsVal := []tc.ResultStatVal{
                {
                        Span: 0,
@@ -128,7 +129,7 @@ func TestCalcCacheValues(t *testing.T) {
        }
        stats["maxKbps"] = resultSatsVal
        caches["cache1"] = stats
-       cacheMap["cache1"] = tc.Server{}
+       cacheMap["cache1"] = tc.ServerV5{}
        config := StartupConfig{
                BpsChan: make(chan influx.BatchPoints),
        }
@@ -163,61 +164,61 @@ func TestSetHealthURLs(t *testing.T) {
                StatusToMon: tc.CacheStatusOffline.String(),
        }
        runningCfg := RunningConfig{}
-       runningCfg.CacheMap = map[string]tc.Server{
+       runningCfg.CacheMap = map[string]tc.ServerV5{
                "tm1": {
-                       CDNName:    "foo",
+                       CDN:        "foo",
                        DomainName: "example.org",
                        HostName:   "tm1",
                        Status:     tc.CacheStatusOffline.String(),
-                       TCPPort:    8080,
+                       TCPPort:    util.Ptr(8080),
                        Type:       tc.MonitorTypeName,
                },
                "tm2": {
-                       CDNName:    "bar",
+                       CDN:        "bar",
                        DomainName: "example.org",
                        HostName:   "tm2",
                        Status:     tc.CacheStatusOffline.String(),
-                       TCPPort:    8080,
+                       TCPPort:    util.Ptr(8080),
                        Type:       tc.MonitorTypeName,
                },
                "tm3": {
-                       CDNName:    "foo",
+                       CDN:        "foo",
                        DomainName: "example.org",
                        HostName:   "tm3",
                        Status:     tc.CacheStatusOffline.String(),
-                       TCPPort:    8080,
+                       TCPPort:    util.Ptr(8080),
                        Type:       tc.MonitorTypeName,
                },
                "tm4": {
-                       CDNName:    "bar",
+                       CDN:        "bar",
                        DomainName: "example.org",
                        HostName:   "tm4",
                        Status:     tc.CacheStatusOffline.String(),
-                       TCPPort:    8080,
+                       TCPPort:    util.Ptr(8080),
                        Type:       tc.MonitorTypeName,
                },
                "tm5": {
-                       CDNName:    "foo",
+                       CDN:        "foo",
                        DomainName: "example.org",
                        HostName:   "tm5",
                        Status:     tc.CacheStatusOnline.String(),
-                       TCPPort:    8080,
+                       TCPPort:    util.Ptr(8080),
                        Type:       tc.MonitorTypeName,
                },
                "tm6": {
-                       CDNName:    "bar",
+                       CDN:        "bar",
                        DomainName: "example.org",
                        HostName:   "tm6",
                        Status:     tc.CacheStatusOnline.String(),
-                       TCPPort:    8080,
+                       TCPPort:    util.Ptr(8080),
                        Type:       tc.MonitorTypeName,
                },
                "cache1": {
-                       CDNName:    "foo",
+                       CDN:        "foo",
                        DomainName: "example.org",
                        HostName:   "cache1",
                        Status:     tc.CacheStatusOffline.String(),
-                       TCPPort:    8080,
+                       TCPPort:    util.Ptr(8080),
                        Type:       tc.CacheTypeEdge.String(),
                },
        }

Reply via email to