Skip to content

Commit

Permalink
Updated cache/cache files
Browse files Browse the repository at this point in the history
  • Loading branch information
rimashah25 committed Jun 29, 2023
1 parent 86ffcb5 commit 853a4ec
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
12 changes: 12 additions & 0 deletions traffic_monitor/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"fmt"
"io"
"regexp"
"strconv"
"time"

"github.com/apache/trafficcontrol/lib/go-log"
Expand Down Expand Up @@ -306,6 +307,17 @@ func (handler Handler) Handle(id string, rdr io.Reader, format string, reqTime t
handler.resultChan <- result
return
}
if val, ok := miscStats["plugin.system_stats.timestamp_ms"]; ok {
valString := fmt.Sprintf("%s", val)
valInt, valErr := strconv.ParseInt(valString, 10, 64)
if valErr != nil {
log.Errorf("parse error '%v'", valErr)
result.Error = valErr
handler.resultChan <- result
return
}
result.Time = time.UnixMilli(valInt)
}
if value, ok := miscStats[rfc.Via]; ok {
result.ID = fmt.Sprintf("%v", value)
}
Expand Down
39 changes: 39 additions & 0 deletions traffic_monitor/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ package cache
*/

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"testing"

"github.com/apache/trafficcontrol/lib/go-tc"
"github.com/apache/trafficcontrol/lib/go-util"
"github.com/apache/trafficcontrol/traffic_monitor/poller"
"github.com/apache/trafficcontrol/traffic_monitor/todata"
)

Expand Down Expand Up @@ -95,3 +100,37 @@ func TestComputeStatGbps(t *testing.T) {
}
}
}

func TestParseAndDecode(t *testing.T) {
file, err := ioutil.ReadFile("stats_over_http.json")
if err != nil {
t.Fatal(err)
}

pl := &poller.HTTPPollCtx{HTTPHeader: http.Header{}}
ctx := interface{}(pl)
ctx.(*poller.HTTPPollCtx).HTTPHeader.Set("Content-Type", "text/json")

decoder, err := GetDecoder("stats_over_http")
if err != nil {
t.Errorf("decoder error, expected: nil, got: %v", err)
}

_, miscStats, err := decoder.Parse("1", bytes.NewReader(file), ctx)
if err != nil {
t.Errorf("decoder parse error, expected: nil, got: %v", err)
}

if len(miscStats) < 1 {
t.Errorf("empty miscStats structure")
}

if val, ok := miscStats["plugin.system_stats.timestamp_ms"]; ok {
valString := fmt.Sprintf("%s", val)
if valString != "1684784877939" {
t.Errorf("unable to read `plugin.system_stats.timestamp_ms`")
}
} else {
t.Errorf("plugin.system_stats.timestamp_ms field was not found in the json file")
}
}

0 comments on commit 853a4ec

Please sign in to comment.