forked from rancher/agent
/
aggregated_stats.go
59 lines (50 loc) · 1.49 KB
/
aggregated_stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package stats
import (
"encoding/json"
"io"
)
type AggregatedStats []AggregatedStat
type AggregatedStat struct {
ID string `json:"id,omitempty"`
ResourceType string `json:"resourceType,omitempty"`
MemLimit uint64 `json:"memLimit,omitempty"`
containerStats
}
func convertToAggregatedStats(id string, containerIds map[string]string, resourceType string, stats []containerInfo, memLimit uint64) []AggregatedStats {
totalAggregatedStats := []AggregatedStats{}
if len(stats) == 0 {
return totalAggregatedStats
}
totalAggregatedStat := []AggregatedStat{}
for j := 0; j < len(stats); j++ {
data := containerStats{}
if len(stats[j].Stats) != 0 {
data = stats[j].Stats[0]
}
aggStats := AggregatedStat{id, resourceType, memLimit, data}
if id == "" {
aggStats.ID = containerIds[stats[j].ID]
}
totalAggregatedStat = append(totalAggregatedStat, aggStats)
}
totalAggregatedStats = append(totalAggregatedStats, totalAggregatedStat)
return totalAggregatedStats
}
func writeAggregatedStats(id string, containerIds map[string]string, resourceType string, infos []containerInfo, memLimit uint64, writer io.Writer) error {
aggregatedStats := convertToAggregatedStats(id, containerIds, resourceType, infos, memLimit)
for _, stat := range aggregatedStats {
data, err := json.Marshal(stat)
if err != nil {
return err
}
_, err = writer.Write(data)
if err != nil {
return err
}
_, err = writer.Write([]byte("\n"))
if err != nil {
return err
}
}
return nil
}