Skip to content

Instantly share code, notes, and snippets.

@grimpy
Last active July 14, 2022 19:01
Show Gist options
  • Save grimpy/9c087e59810fcf2b6c5f870028b6cce1 to your computer and use it in GitHub Desktop.
Save grimpy/9c087e59810fcf2b6c5f870028b6cce1 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"log"
)
type Usage struct {
IP string
Connections int
RxBytes int64
RxPackets int64
TxBytes int64
TxPackets int64
}
func (a *Usage) UnmarshalJSON(b []byte) error {
row := []interface{}{}
if err := json.Unmarshal(b, &row); err != nil {
return err
}
a.IP = row[0].(string)
a.Connections = int(row[1].(float64))
a.RxBytes = int64(row[2].(float64))
a.RxPackets = int64(row[3].(float64))
a.TxBytes = int64(row[4].(float64))
a.TxPackets = int64(row[5].(float64))
return nil
}
type AllUsage struct {
Data []Usage `json:"data"`
Columns []string `json:"columns"`
}
func main() {
data := `{"columns":["ip","conns","rx_bytes","rx_pkts","tx_bytes","tx_pkts"],"data":[["192.168.1.3",49696,2628175385,2659880,55066470,1003162],["192.168.1.24",10041,463614449,498139,19305939,209256],["192.168.1.98",2546,295968431,221923,5485406,29572],["192.168.1.97",61126,235012840,250040,26214901,132459],["192.168.1.80",12492,90917682,93215,7744563,33601],["192.168.1.21",4642,21735476,59719,4862780,31633],["192.168.1.94",20164,11189262,43500,5446658,43895],["192.168.1.5",8805,9865985,17105,7242133,17953],["192.168.2.2",33454,3153765,21368,554444,8679],["192.168.1.30",1509,1214044,2059,668581,2748],["192.168.1.12",6844,735386,6594,3388692,16999],["192.168.1.177",831,582732,1247,331983,1379],["192.168.1.6",365,163282,457,58538,508],["192.168.1.8",1409,147000,1750,133076,1751],["192.168.1.174",513,70918,206,10768,87],["192.168.1.4",44,34712,75,26192,100],["192.168.1.109",68,25267,193,45116,196],["192.168.1.201",42,7516,26,9104,29],["192.168.1.202",38,7492,26,12276,29],["192.168.1.200",39,7492,26,11047,29],["192.168.1.13",140,5232,48,3048,48],["192.168.1.9",117,3040,40,3040,40],["192.168.1.247",2110,0,0,111496,724]]}`
usage := &AllUsage{}
json.Unmarshal([]byte(data), usage)
log.Printf("usage %d %s", len(usage.Data), usage.Data)
log.Printf("Columns %d %s", len(usage.Columns), usage.Columns)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment