-
Notifications
You must be signed in to change notification settings - Fork 12
/
sorting.go
57 lines (51 loc) · 1.27 KB
/
sorting.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
package main
import (
"github.com/hailocab/ctop/types"
"sort"
)
// Define a type for a sorted-map of columnfamily-stats:
type sortedMap struct {
m map[string]types.CFStats
s []string
}
// Return the length of a sorted-map:
func (sm *sortedMap) Len() int {
return len(sm.m)
}
// Handles the different attributes we might sort by:
func (sm *sortedMap) Less(i, j int) bool {
if dataSortedBy == "Reads" {
return sm.m[sm.s[i]].ReadRate > sm.m[sm.s[j]].ReadRate
}
if dataSortedBy == "Writes" {
return sm.m[sm.s[i]].WriteRate > sm.m[sm.s[j]].WriteRate
}
if dataSortedBy == "Space" {
return sm.m[sm.s[i]].LiveDiskSpaceUsed > sm.m[sm.s[j]].LiveDiskSpaceUsed
}
if dataSortedBy == "ReadLatency" {
return sm.m[sm.s[i]].ReadLatency > sm.m[sm.s[j]].ReadLatency
}
if dataSortedBy == "WriteLatency" {
return sm.m[sm.s[i]].WriteLatency > sm.m[sm.s[j]].WriteLatency
}
// Default to "Reads":
return sm.m[sm.s[i]].ReadRate > sm.m[sm.s[j]].ReadRate
}
// Replace two values in a list:
func (sm *sortedMap) Swap(i, j int) {
sm.s[i], sm.s[j] = sm.s[j], sm.s[i]
}
// Return keys in order:
func sortedKeys(m map[string]types.CFStats) []string {
sm := new(sortedMap)
sm.m = m
sm.s = make([]string, len(m))
i := 0
for key := range m {
sm.s[i] = key
i++
}
sort.Sort(sm)
return sm.s
}