Rename type JSON → Node

master
Buddy Sandidge 5 years ago
parent b42ee353fe
commit 0cf0be261a

@ -254,34 +254,34 @@ func (v *Value) MetricName() string {
}
// IsValue true if node has children
func (j *JSON) IsValue() bool {
if j.Children == nil {
func (n *Node) IsValue() bool {
if n.Children == nil {
return false
}
return len(j.Children) == 0
return len(n.Children) == 0
}
// IsRoot returns true if json is a root node
func (j *JSON) IsRoot() bool {
return j.ID == 0
func (n *Node) IsRoot() bool {
return n.ID == 0
}
// IsSensor returns true if json node is for a sensor type
func (j *JSON) IsSensor() bool {
if j.ImageURL == "" || j.Text == "" {
func (n *Node) IsSensor() bool {
if n.ImageURL == "" || n.Text == "" {
return false
}
_, ok := sensorToType[j.Text]
_, ok := sensorToType[n.Text]
return ok
}
// SensorType returns type of sensor
func (j *JSON) SensorType() Sensor {
st, _ := sensorToType[j.Text]
func (n *Node) SensorType() Sensor {
st, _ := sensorToType[n.Text]
// Figure out if data is in GB (Data) or MB (SmallData)
if st == Data {
if len(j.Children) > 0 {
val := j.Children[0]
if len(n.Children) > 0 {
val := n.Children[0]
if strings.HasSuffix(val.Value, "MB") {
st = SmallData
}
@ -291,17 +291,17 @@ func (j *JSON) SensorType() Sensor {
}
// IsHardware returns true if json node is for hardware
func (j *JSON) IsHardware() bool {
if j.ImageURL == "" || j.Text == "" {
func (n *Node) IsHardware() bool {
if n.ImageURL == "" || n.Text == "" {
return false
}
_, ok := imageToHardware[path.Base(j.ImageURL)]
_, ok := imageToHardware[path.Base(n.ImageURL)]
return ok
}
// HardwareType returns HardwareType of node
func (j *JSON) HardwareType() HardwareType {
ht, _ := imageToHardware[path.Base(j.ImageURL)]
func (n *Node) HardwareType() HardwareType {
ht, _ := imageToHardware[path.Base(n.ImageURL)]
return ht
}
@ -309,38 +309,38 @@ func (j *JSON) HardwareType() HardwareType {
type Visitor func(v Value) error
// Walk from json root
func (j *JSON) Walk(fn Visitor) error {
return j.walk(fn, Value{Hardware: []Hardware{}}, 0, 0)
func (n *Node) Walk(fn Visitor) error {
return n.walk(fn, Value{Hardware: []Hardware{}}, 0, 0)
}
func (j *JSON) childDeviceTotals() map[HardwareType]int {
func (n *Node) childDeviceTotals() map[HardwareType]int {
totals := map[HardwareType]int{}
for _, child := range j.Children {
for _, child := range n.Children {
totals[child.HardwareType()]++
}
return totals
}
func (j *JSON) walk(fn Visitor, ctx Value, hwIndex, hwTotal int) error {
if j.IsValue() {
ctx.Label = j.Text
ctx.Value = j.Value
func (n *Node) walk(fn Visitor, ctx Value, hwIndex, hwTotal int) error {
if n.IsValue() {
ctx.Label = n.Text
ctx.Value = n.Value
return fn(ctx)
}
if j.IsSensor() {
ctx.Unit = j.SensorType()
if n.IsSensor() {
ctx.Unit = n.SensorType()
}
if j.IsHardware() {
if n.IsHardware() {
ctx.Hardware = append(ctx.Hardware, Hardware{
Type: j.HardwareType(),
Value: j.Text,
Type: n.HardwareType(),
Value: n.Text,
TypeIndex: hwIndex,
TypeCount: hwTotal,
})
}
totals := j.childDeviceTotals()
totals := n.childDeviceTotals()
deviceIndex := map[HardwareType]int{}
for _, child := range j.Children {
for _, child := range n.Children {
deviceType := child.HardwareType()
index := deviceIndex[deviceType]
if err := child.walk(fn, ctx, index, totals[deviceType]); err != nil {
@ -352,9 +352,9 @@ func (j *JSON) walk(fn Visitor, ctx Value, hwIndex, hwTotal int) error {
}
// Values from json root
func (j *JSON) Values() ([]Value, error) {
func (n *Node) Values() ([]Value, error) {
ret := []Value{}
err := j.Walk(func(val Value) error {
err := n.Walk(func(val Value) error {
ret = append(ret, val)
return nil
})
@ -364,15 +364,15 @@ func (j *JSON) Values() ([]Value, error) {
return ret, nil
}
// JSON from data
type JSON struct {
// Node from data
type Node struct {
ID int `json:"id"`
ImageURL string
Max string
Min string
Text string
Value string
Children []JSON
Children []Node
}
// Client for open hardware monitor
@ -382,7 +382,7 @@ type Client struct {
}
// Fetch requests
func (c *Client) Fetch() (*JSON, error) {
func (c *Client) Fetch() (*Node, error) {
client := http.Client{Timeout: c.Timeout}
resp, err := client.Get(c.URL.String())
if err != nil {
@ -393,8 +393,8 @@ func (c *Client) Fetch() (*JSON, error) {
}
// Decode json
func (c *Client) Decode(r io.Reader) (*JSON, error) {
node := &JSON{}
func (c *Client) Decode(r io.Reader) (*Node, error) {
node := &Node{}
decoder := json.NewDecoder(r)
if err := decoder.Decode(node); err != nil {
return nil, err
@ -403,25 +403,25 @@ func (c *Client) Decode(r io.Reader) (*JSON, error) {
}
// Stringify tree
func (j *JSON) Stringify() string {
return j.stringify(0)
func (n *Node) Stringify() string {
return n.stringify(0)
}
func (j *JSON) stringify(indent int) string {
func (n *Node) stringify(indent int) string {
prefix := ""
for i := 0; i < indent; i++ {
prefix += " "
}
ret := prefix + j.Text
if j.Value != "" {
ret += ": " + j.Value
ret := prefix + n.Text
if n.Value != "" {
ret += ": " + n.Value
}
if j.Max != "" && j.Min != "" && j.Max != "-" && j.Min != "-" {
ret += fmt.Sprintf(" (%s - %s)", j.Min, j.Max)
if n.Max != "" && n.Min != "" && n.Max != "-" && n.Min != "-" {
ret += fmt.Sprintf(" (%s - %s)", n.Min, n.Max)
}
ret += "\n"
for _, child := range j.Children {
for _, child := range n.Children {
ret += child.stringify(indent + 1)
}

Loading…
Cancel
Save