add some new files and services add hwtools that shows the current and last GWs - not yet included the user stats (if any available)
138 lines
3.1 KiB
Go
138 lines
3.1 KiB
Go
package datacollector
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"hwcollector/server/models"
|
|
"io/ioutil"
|
|
"log"
|
|
"os/exec"
|
|
|
|
"github.com/tidwall/gjson"
|
|
"github.com/tidwall/sjson"
|
|
)
|
|
|
|
// ErrNotExist path not found
|
|
var ErrNotExist = errors.New("The Path not exist")
|
|
|
|
// TypeInterface for all the registred collectors
|
|
type TypeInterface interface {
|
|
|
|
// Set Data
|
|
SetJSON(string)
|
|
SetJSONByte([]byte)
|
|
|
|
// Start Processing
|
|
Start()
|
|
|
|
// GetUserDetails
|
|
// Collects the user details from the JSON report and prepares them for further processing.
|
|
GetUserDetails() ([]*models.User, error)
|
|
}
|
|
|
|
var processes = make(map[string]TypeInterface)
|
|
|
|
// Start ...
|
|
func Start() {
|
|
fmt.Println("Datacollector.Start started")
|
|
for x, y := range processes {
|
|
fmt.Printf("Process: %s\n", x)
|
|
m, ok := y.(interface{ GenerateArrayFromDir(string) })
|
|
if ok {
|
|
fmt.Printf("Process: %s GenerateArrayFromDir\n", x)
|
|
m.GenerateArrayFromDir(IdentClanWarGetInfo)
|
|
}
|
|
_ = x
|
|
|
|
}
|
|
}
|
|
|
|
func GetDataFor(name string, json []byte) (returnData []byte, err error) {
|
|
req := gjson.GetBytes(json, `request`)
|
|
if !req.Exists() {
|
|
return []byte{}, fmt.Errorf("error %d: %v", 1, err)
|
|
}
|
|
|
|
returnData, err = helperGetSetData("response.body.date", "data", json, returnData)
|
|
if err != nil {
|
|
return []byte{}, fmt.Errorf("error %d: %v", 2, err)
|
|
}
|
|
|
|
returnData, err = helperGetSetData("request.headers", "headers", json, returnData)
|
|
if err != nil {
|
|
return []byte{}, fmt.Errorf("error %d: %v", 3, err)
|
|
}
|
|
|
|
call := req.Get(`body.calls.#(name=="` + name + `")`)
|
|
if !call.Exists() {
|
|
return []byte{}, fmt.Errorf("error %d: %v", 4, err)
|
|
}
|
|
ident := call.Get("ident")
|
|
if !ident.Exists() {
|
|
return []byte{}, fmt.Errorf("error %d: %v", 5, err)
|
|
}
|
|
|
|
returnData, err = helperGetSetData(`response.body.results.#(ident=="`+ident.String()+`")`, "response", json, returnData)
|
|
if err != nil {
|
|
return []byte{}, fmt.Errorf("error %d: %v", 6, err)
|
|
}
|
|
return
|
|
}
|
|
|
|
func helperGetSetData(getPath, setObj string, json, jsonOut []byte) (returnData []byte, err error) {
|
|
// read & set DATE
|
|
data := gjson.GetBytes(json, getPath)
|
|
if !data.Exists() {
|
|
return []byte{}, ErrNotExist
|
|
|
|
}
|
|
returnData, err = sjson.SetBytes(jsonOut, setObj, data.Value())
|
|
if err != nil {
|
|
return []byte{}, err
|
|
}
|
|
return
|
|
|
|
}
|
|
|
|
func helperGetIdent(data []byte, name string) (string, error) {
|
|
|
|
call := gjson.GetBytes(data, `request.call`)
|
|
if !call.Exists() {
|
|
return "", fmt.Errorf("helperGetIdent error: %s", fmt.Sprintf("request.calls with name '%s' not found", name))
|
|
}
|
|
ident := call.Get("ident")
|
|
if !ident.Exists() {
|
|
return "", fmt.Errorf("helperGetIdent error: %s", "ident not found")
|
|
}
|
|
return ident.String(), nil
|
|
}
|
|
|
|
func readFile(path string) ([]byte, error) {
|
|
// read file
|
|
data, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
return []byte(""), err
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
func runJQUniqe(in []byte) []byte {
|
|
cmd := exec.Command("jq", "unique | unique_by(.warDate)|sort_by(.warDate)|reverse|.[:25]")
|
|
stdin, err := cmd.StdinPipe()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
go func() {
|
|
defer stdin.Close()
|
|
stdin.Write(in)
|
|
}()
|
|
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
return out
|
|
}
|