From ac156bcd1231153f53d320340ad307620c581b1f Mon Sep 17 00:00:00 2001 From: David Newhall II Date: Wed, 17 Apr 2019 02:06:06 -0700 Subject: [PATCH] a little more cleanup --- core/unifi/clients_influx.go | 16 ++++++++-------- core/unifi/parsers.go | 29 +++++++++++------------------ core/unifi/uap_influx.go | 3 +-- core/unifi/usg_influx.go | 5 ++--- core/unifi/usw_influx.go | 7 +++---- 5 files changed, 25 insertions(+), 35 deletions(-) diff --git a/core/unifi/clients_influx.go b/core/unifi/clients_influx.go index 51e0c8e2..70adf58f 100644 --- a/core/unifi/clients_influx.go +++ b/core/unifi/clients_influx.go @@ -10,15 +10,15 @@ import ( // Points generates Unifi Client datapoints for InfluxDB. // These points can be passed directly to influx. func (c UCL) Points() ([]*influx.Point, error) { - var points []*influx.Point // Fix name and hostname fields. Sometimes one or the other is blank. - if c.Name == "" && c.Hostname != "" { - c.Name = c.Hostname - } else if c.Hostname == "" && c.Name != "" { - c.Hostname = c.Name - } else if c.Hostname == "" && c.Name == "" { + switch { + case c.Hostname == "" && c.Name == "": c.Hostname = "-no-name-" c.Name = "-no-name-" + case c.Hostname == "" && c.Name != "": + c.Hostname = c.Name + case c.Name == "" && c.Hostname != "": + c.Name = c.Hostname } tags := map[string]string{ "id": c.ID, @@ -100,7 +100,7 @@ func (c UCL) Points() ([]*influx.Point, error) { } pt, err := influx.NewPoint("clients", tags, fields, time.Now()) if err == nil { - points = append(points, pt) + return nil, err } - return points, err + return []*influx.Point{pt}, nil } diff --git a/core/unifi/parsers.go b/core/unifi/parsers.go index 5b324282..70c2d193 100644 --- a/core/unifi/parsers.go +++ b/core/unifi/parsers.go @@ -7,47 +7,40 @@ func (u *Unifi) parseDevices(data []json.RawMessage) *Devices { devices := new(Devices) for _, r := range data { // Loop each item in the raw JSON message, detect its type and unmarshal it. - var obj map[string]interface{} - var uap UAP - var usg USG - var usw USW - - if u.unmarshalDevice("interface{}", &obj, r) != nil { + assetType := "" + if o := make(map[string]interface{}); u.unmarshalDevice("map", r, &o) != nil { continue - } - assetType := "" - if t, ok := obj["type"].(string); ok { + } else if t, ok := o["type"].(string); ok { assetType = t } u.dLogf("Unmarshalling Device Type: %v", assetType) + // Choose which type to unmarshal into based on the "type" json key. switch assetType { // Unmarshal again into the correct type.. case "uap": - if u.unmarshalDevice(assetType, &uap, r) == nil { + if uap := (UAP{}); u.unmarshalDevice(assetType, r, &uap) == nil { devices.UAPs = append(devices.UAPs, uap) } case "ugw", "usg": // in case they ever fix the name in the api. - if u.unmarshalDevice(assetType, &usg, r) == nil { + if usg := (USG{}); u.unmarshalDevice(assetType, r, &usg) == nil { devices.USGs = append(devices.USGs, usg) } case "usw": - if u.unmarshalDevice(assetType, &usw, r) == nil { + if usw := (USW{}); u.unmarshalDevice(assetType, r, &usw) == nil { devices.USWs = append(devices.USWs, usw) } default: u.eLogf("unknown asset type - %v - skipping", assetType) - continue } } return devices } // unmarshalDevice handles logging for the unmarshal operations in parseDevices(). -func (u *Unifi) unmarshalDevice(device string, ptr interface{}, payload json.RawMessage) error { - err := json.Unmarshal(payload, ptr) - if err != nil { - u.eLogf("json.Unmarshal(%v): %v", device, err) +func (u *Unifi) unmarshalDevice(dev string, data json.RawMessage, v interface{}) (err error) { + if err = json.Unmarshal(data, v); err != nil { + u.eLogf("json.Unmarshal(%v): %v", dev, err) u.eLogf("Enable Debug Logging to output the failed payload.") - json, err := payload.MarshalJSON() + json, err := data.MarshalJSON() u.dLogf("Failed Payload: %s (marshal err: %v)", json, err) u.dLogf("The above payload can prove useful during torubleshooting when you open an Issue:") u.dLogf("==- https://github.com/golift/unifi/issues/new -==") diff --git a/core/unifi/uap_influx.go b/core/unifi/uap_influx.go index 36789de8..7ae5affa 100644 --- a/core/unifi/uap_influx.go +++ b/core/unifi/uap_influx.go @@ -13,7 +13,6 @@ func (u UAP) Points() ([]*influx.Point, error) { /* I generally suck at InfluxDB, so if I got the tags/fields wrong, please send me a PR or open an Issue to address my faults. Thanks! */ - var points []*influx.Point tags := map[string]string{ "id": u.ID, "mac": u.Mac, @@ -174,7 +173,7 @@ func (u UAP) Points() ([]*influx.Point, error) { if err != nil { return nil, err } - points = append(points, pt) + points := []*influx.Point{pt} for _, p := range u.RadioTable { tags := map[string]string{ "device_name": u.Name, diff --git a/core/unifi/usg_influx.go b/core/unifi/usg_influx.go index 9b169e54..a2a52779 100644 --- a/core/unifi/usg_influx.go +++ b/core/unifi/usg_influx.go @@ -10,7 +10,6 @@ import ( // Points generates Unifi Gateway datapoints for InfluxDB. // These points can be passed directly to influx. func (u USG) Points() ([]*influx.Point, error) { - var points []*influx.Point tags := map[string]string{ "id": u.ID, "mac": u.Mac, @@ -130,7 +129,7 @@ func (u USG) Points() ([]*influx.Point, error) { if err != nil { return nil, err } - points = append(points, pt) + points := []*influx.Point{pt} for _, p := range u.NetworkTable { tags := map[string]string{ "device_name": u.Name, @@ -183,5 +182,5 @@ func (u USG) Points() ([]*influx.Point, error) { } points = append(points, pt) } - return points, err + return points, nil } diff --git a/core/unifi/usw_influx.go b/core/unifi/usw_influx.go index 94a7f9df..810148de 100644 --- a/core/unifi/usw_influx.go +++ b/core/unifi/usw_influx.go @@ -10,7 +10,6 @@ import ( // Points generates Unifi Switch datapoints for InfluxDB. // These points can be passed directly to influx. func (u USW) Points() ([]*influx.Point, error) { - var points []*influx.Point tags := map[string]string{ "id": u.ID, "mac": u.Mac, @@ -112,8 +111,8 @@ func (u USW) Points() ([]*influx.Point, error) { // Add the port stats too. } pt, err := influx.NewPoint("usw", tags, fields, time.Now()) - if err == nil { - points = append(points, pt) + if err != nil { + return nil, err } - return points, err + return []*influx.Point{pt}, nil }