Moved units to be with themselves in common, updated convertUnit and parseLine to be public for testing
This commit is contained in:
parent
5122c83609
commit
0904b13546
38
common.go
38
common.go
@ -12,17 +12,6 @@ type JshFrame struct {
|
|||||||
StdErr interface{}
|
StdErr interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size prefixes as integers, using binary representation
|
|
||||||
type Unit int
|
|
||||||
|
|
||||||
const (
|
|
||||||
B Unit = 2 ^ 0
|
|
||||||
KB Unit = 2 ^ 10
|
|
||||||
MB Unit = 2 ^ 20
|
|
||||||
GB Unit = 2 ^ 30
|
|
||||||
TB Unit = 2 ^ 40
|
|
||||||
)
|
|
||||||
|
|
||||||
// Converts a JshFrame into a JSON string
|
// Converts a JshFrame into a JSON string
|
||||||
func (j JshFrame) ToJson() *string {
|
func (j JshFrame) ToJson() *string {
|
||||||
jsonOut, err := json.Marshal(j)
|
jsonOut, err := json.Marshal(j)
|
||||||
@ -54,3 +43,30 @@ func OutputFrames(queue chan *JshFrame, done chan bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Size prefixes as integers, using binary representation
|
||||||
|
type Unit int
|
||||||
|
|
||||||
|
const (
|
||||||
|
B Unit = 2 ^ 0
|
||||||
|
KB Unit = 2 ^ 10
|
||||||
|
MB Unit = 2 ^ 20
|
||||||
|
GB Unit = 2 ^ 30
|
||||||
|
TB Unit = 2 ^ 40
|
||||||
|
)
|
||||||
|
|
||||||
|
func (u Unit) ToString() string {
|
||||||
|
if u == B {
|
||||||
|
return "B"
|
||||||
|
} else if u == KB {
|
||||||
|
return "kB"
|
||||||
|
} else if u == MB {
|
||||||
|
return "mB"
|
||||||
|
} else if u == GB {
|
||||||
|
return "gB"
|
||||||
|
} else if u == TB {
|
||||||
|
return "tB"
|
||||||
|
} else {
|
||||||
|
return fmt.Sprintf("Unknown type, values is %d", u)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
func convertUnit(stringUnit string) (jsh.Unit, error) {
|
func ConvertUnit(stringUnit string) (jsh.Unit, error) {
|
||||||
switch stringUnit {
|
switch stringUnit {
|
||||||
case "B":
|
case "B":
|
||||||
return jsh.B, nil
|
return jsh.B, nil
|
||||||
@ -30,7 +30,7 @@ func convertUnit(stringUnit string) (jsh.Unit, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseLine(line string) (string, jsh.MemStat, error) {
|
func ParseLine(line string) (string, jsh.MemStat, error) {
|
||||||
// Recognizes a alphanumeric or () word, the : character, whitespace, the number we're looking for,
|
// Recognizes a alphanumeric or () word, the : character, whitespace, the number we're looking for,
|
||||||
// more whitespace, and possibly the unit
|
// more whitespace, and possibly the unit
|
||||||
lineRegex := regexp.MustCompile("(?P<key>^[\\w()]+):\\s+(?P<val>\\d+)(\\s+)?(?P<unit>\\w+)?")
|
lineRegex := regexp.MustCompile("(?P<key>^[\\w()]+):\\s+(?P<val>\\d+)(\\s+)?(?P<unit>\\w+)?")
|
||||||
@ -48,7 +48,7 @@ func parseLine(line string) (string, jsh.MemStat, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", jsh.MemStat{}, err
|
return "", jsh.MemStat{}, err
|
||||||
}
|
}
|
||||||
unit, err := convertUnit(matchedVals["unit"])
|
unit, err := ConvertUnit(matchedVals["unit"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", jsh.MemStat{}, err
|
return "", jsh.MemStat{}, err
|
||||||
}
|
}
|
||||||
@ -67,7 +67,7 @@ func parseMemInfo() jsh.JshFrame {
|
|||||||
errors := []error{}
|
errors := []error{}
|
||||||
// Read in each line of the meminfo file, and place it in the map
|
// Read in each line of the meminfo file, and place it in the map
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
key, val, err := parseLine(scanner.Text())
|
key, val, err := ParseLine(scanner.Text())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errors = append(errors, err)
|
errors = append(errors, err)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user