Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Merge pull request #48 from sloppyio/stats-regex-fix
Browse files Browse the repository at this point in the history
fix(stats): show stats from projects with dashes in name
  • Loading branch information
jzaefferer authored Dec 19, 2018
2 parents a479a26 + f943cb8 commit 48a9267
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 91 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 1.13.1 (2018-12-17)

* fix(stats): show stats from projects with dashes in name

## 1.13.0 (2018-11-26)

* feat: support tcp ports, via port_mappings[].service_port
Expand Down
54 changes: 6 additions & 48 deletions command/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (c *StatsCommand) Help() string {
helpText := `
Usage: sloppy stats [OPTIONS] PROJECT
Displays usage statistics of running instances(memory, traffic)
Displays usage statistics of running instances (memory, traffic)
Options:
-a, --all Show all instances (default shows just running instances)
Expand Down Expand Up @@ -82,7 +82,7 @@ func (c *StatsCommand) Run(args []string) int {
var buf bytes.Buffer
w := new(tabwriter.Writer)
w.Init(&buf, 0, 8, 0, '\t', 0)
fmt.Fprintf(w, "CONTAINER \t CPU %% \t MEM / LIMIT \t MEM %% \t NET I/O Extern \t NET I/O Intern \t MAX VOLUME %% \t LAST UPDATE\n")
fmt.Fprintf(w, "CONTAINER \t CPU %% \t MEM / LIMIT \t MEM %% \t NET I/O Extern \t NET I/O Intern\n")

var keys []string
var latest api.Timestamp
Expand Down Expand Up @@ -129,20 +129,16 @@ type stat struct {
InternalNetworkTx float64
ExternalNetworkRx float64
ExternalNetworkTx float64
Volumes int // VolumeCount
Volume float64 // VolumePercentage
}

func (s *stat) String() string {
return fmt.Sprintf("%s/%s-%s \t %.1f%% \t %s / %.f MiB \t %.1f%% \t %s / %s \t %s / %s \t %.1f%% \t %s",
return fmt.Sprintf("%s/%s-%s \t %.1f%% \t %s / %.f MiB \t %.1f%% \t %s / %s \t %s / %s",
s.Service, s.App, s.ID[:6],
s.CPU,
humanByte(s.Memory), s.MemoryLimit,
float64(s.Memory/(1<<20))/float64(s.MemoryLimit)*100,
humanByte(s.ExternalNetworkRx), humanByte(s.ExternalNetworkTx),
humanByte(s.InternalNetworkRx), humanByte(s.InternalNetworkTx),
s.Volume,
humanDuration(time.Now().Sub(s.Time.Time)),
)
}

Expand All @@ -164,12 +160,6 @@ type MetricFetchResult struct {
err error
}

func (m Metrics) Len() int { return len(m) }

// Volume metrics needs to be the last one due to their missing uuid
func (m Metrics) Less(i, j int) bool { return strings.Contains(m[j].metricName, "volume") }
func (m Metrics) Swap(i, j int) { m[i], m[j] = m[j], m[i] }

// Collect collects all statistics from all running apps, aggregate and merge them.
func (c *StatsCommand) collect(project *api.Project) (map[string]*stat, error) {
quit := make(chan struct{})
Expand Down Expand Up @@ -231,34 +221,27 @@ func (c *StatsCommand) fetchAll(project *api.Project, quit chan struct{}) <-chan

func (c *StatsCommand) aggregate(metrics []*Metric) (map[string]*stat, error) {
stats := make(map[string]*stat)
regex := regexp.MustCompile(`^(\w+)-(\w+).([a-z0-9-]{36}|([a-z/+]+)$)`)
var id string
regex := regexp.MustCompile(`^(\w+)-([\w-]+).([a-z0-9-]{36}|([a-z/+]+)$)`)
var p stat
for _, metric := range metrics {
matches := regex.FindStringSubmatch(metric.seriesName)
if len(matches) == 0 || len(matches) < 3 {
return nil, fmt.Errorf("invalid metric series name %q", metric.metricName)
}
uuid := matches[3]
// Volume metrics does not contain a uuid, use the previous one
if !strings.Contains(metric.metricName, "volume") {
id = uuid
return nil, fmt.Errorf("invalid metric series name %q", metric.seriesName)
}
id := matches[3]

s := &stat{
App: *metric.app.ID,
Service: metric.service,
Time: api.Timestamp{Time: metric.time},
ID: id,
MemoryLimit: float64(*metric.app.Memory),
Volumes: len(metric.app.Volumes),
CPU: p.CPU,
Memory: p.Memory,
ExternalNetworkRx: p.ExternalNetworkRx,
InternalNetworkRx: p.InternalNetworkRx,
ExternalNetworkTx: p.ExternalNetworkTx,
InternalNetworkTx: p.InternalNetworkTx,
Volume: p.Volume,
}

switch metric.metricName {
Expand All @@ -278,10 +261,6 @@ func (c *StatsCommand) aggregate(metrics []*Metric) (map[string]*stat, error) {
break
}
s.InternalNetworkTx = metric.value
case "container_volume_usage_percentage":
if s.Volumes > 0 && metric.value > s.Volume {
s.Volume = metric.value
}
}
stats[id] = s
p = *s // merge next with previous one
Expand All @@ -305,7 +284,6 @@ func (c *StatsCommand) toMetricSlice(app *api.App, serviceId string, metrics api
}
}
}
sort.Sort(result)
return result
}

Expand All @@ -321,23 +299,3 @@ func humanByte(size float64) string {

return fmt.Sprintf("%.3g %s", size, abbrs[i])
}

// HumanDuration returns a human-readable approximation of a duration
func humanDuration(d time.Duration) string {
if seconds := int(d.Seconds()); seconds < 1 {
return "Less than a second"
} else if seconds < 60 {
return fmt.Sprintf("%d seconds", seconds)
} else if minutes := int(d.Minutes()); minutes == 1 {
return "About a minute"
} else if minutes < 60 {
return fmt.Sprintf("%d minutes", minutes)
} else if hours := int(d.Hours()); hours == 1 {
return "About an hour"
} else if hours < 48 {
return fmt.Sprintf("%d hours", hours)
} else {
return fmt.Sprintf("%d days", hours/24)
}

}
4 changes: 2 additions & 2 deletions command/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func TestStatsCommand_withAllFlag(t *testing.T) {
}

// FIXME - table head tab differs between tests
// want := `CONTAINER CPU % MEM / LIMIT MEM % NET I/O Extern NET I/O Intern MAX VOLUME % LAST UPDATE
//frontend/node-59f7ed 0.0% 128 MiB / 1024 MiB 12.5% 7.71 B / 0 B 18.1 B / 0 B 23.4% 2 hours`
// want := `CONTAINER CPU % MEM / LIMIT MEM % NET I/O Extern NET I/O Intern
//frontend/node-59f7ed 0.0% 128 MiB / 1024 MiB 12.5% 7.71 B / 0 B 18.1 B / 0 B`

if exitCode := c.Run(args); exitCode != 0 {
t.Error(mockUI.ErrorWriter.String())
Expand Down
40 changes: 0 additions & 40 deletions command/testdata/letschat_response_stats.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,46 +198,6 @@
]
}
]
},
{
"metric": "container_volume_usage_percentage",
"unit": "percentage",
"values": [
{
"name": "USERNAME-letschat_frontend_node./var/www",
"data": [
{
"x": 1502197200000,
"y": 12.2
},
{
"x": 1502198400000,
"y": 12.2
},
{
"x": 1502199600000,
"y": 12.2
}
]
},
{
"name": "USERNAME-letschat_frontend_node./var/test",
"data": [
{
"x": 1502197200000,
"y": 23.4
},
{
"x": 1502198400000,
"y": 23.4
},
{
"x": 1502199600000,
"y": 23.4
}
]
}
]
}
]
}
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.13.0
1.13.1

0 comments on commit 48a9267

Please sign in to comment.