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