remove dumps and trash
add some new files and services add hwtools that shows the current and last GWs - not yet included the user stats (if any available)
This commit is contained in:
45
server/services/datacollector/arenaAttack.go
Normal file
45
server/services/datacollector/arenaAttack.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package datacollector
|
||||
|
||||
import (
|
||||
"hwcollector/server/models"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func init() {
|
||||
processes["arenaAttack"] = new(DoArenaAttack)
|
||||
}
|
||||
|
||||
// DoArenaAttack ...
|
||||
type DoArenaAttack struct {
|
||||
data []byte
|
||||
}
|
||||
|
||||
// Start ...
|
||||
func (x *DoArenaAttack) Start() {
|
||||
}
|
||||
|
||||
// SetJSONByte ...
|
||||
func (x *DoArenaAttack) SetJSONByte(b []byte) {
|
||||
x.data = b
|
||||
}
|
||||
|
||||
// SetJSON ...
|
||||
func (x *DoArenaAttack) SetJSON(s string) {
|
||||
x.data = []byte(s)
|
||||
}
|
||||
|
||||
// GetUserDetails ...
|
||||
func (x *DoArenaAttack) GetUserDetails() ([]*models.User, error) {
|
||||
var users []*models.User
|
||||
|
||||
result := gjson.GetBytes(x.data, "response.result.response.enemies.#.user")
|
||||
for _, jsonValue := range result.Array() {
|
||||
user := new(models.User)
|
||||
if err := user.UnmarshalUserJSON([]byte(jsonValue.Raw)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
users = append(users, user)
|
||||
}
|
||||
return users, nil
|
||||
}
|
||||
1464
server/services/datacollector/arenaAttack_test.go
Normal file
1464
server/services/datacollector/arenaAttack_test.go
Normal file
File diff suppressed because it is too large
Load Diff
45
server/services/datacollector/arenaFindEnemies.go
Normal file
45
server/services/datacollector/arenaFindEnemies.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package datacollector
|
||||
|
||||
import (
|
||||
"hwcollector/server/models"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
func init() {
|
||||
processes["arenaFindEnemies"] = new(DoArenaFindEnemies)
|
||||
}
|
||||
|
||||
// DoArenaFindEnemies ...
|
||||
type DoArenaFindEnemies struct {
|
||||
data []byte
|
||||
}
|
||||
|
||||
// Start ...
|
||||
func (x *DoArenaFindEnemies) Start() {
|
||||
}
|
||||
|
||||
// SetJSONByte ...
|
||||
func (x *DoArenaFindEnemies) SetJSONByte(b []byte) {
|
||||
x.data = b
|
||||
}
|
||||
|
||||
// SetJSON ...
|
||||
func (x *DoArenaFindEnemies) SetJSON(s string) {
|
||||
x.data = []byte(s)
|
||||
}
|
||||
|
||||
// GetUserDetails ...
|
||||
func (x *DoArenaFindEnemies) GetUserDetails() ([]*models.User, error) {
|
||||
var users []*models.User
|
||||
|
||||
result := gjson.GetBytes(x.data, "response.result.response.enemies.#.user")
|
||||
for _, jsonValue := range result.Array() {
|
||||
user := new(models.User)
|
||||
if err := user.UnmarshalUserJSON([]byte(jsonValue.Raw)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
users = append(users, user)
|
||||
}
|
||||
return users, nil
|
||||
}
|
||||
140
server/services/datacollector/clanWarGetInfo.go
Normal file
140
server/services/datacollector/clanWarGetInfo.go
Normal file
@@ -0,0 +1,140 @@
|
||||
package datacollector
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"hwcollector/server/models"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
"github.com/tidwall/sjson"
|
||||
)
|
||||
|
||||
func init() {
|
||||
processes["clanWarGetInfo"] = new(DoClanWarGetInfo)
|
||||
}
|
||||
|
||||
const (
|
||||
IdentClanWarGetInfo = "clanWarGetInfo"
|
||||
)
|
||||
|
||||
// DoClanWarGetInfo ...
|
||||
type DoClanWarGetInfo struct {
|
||||
data []byte
|
||||
result []byte
|
||||
}
|
||||
|
||||
// Start ...
|
||||
func (x *DoClanWarGetInfo) Start() {
|
||||
}
|
||||
|
||||
// SetJSONByte ...
|
||||
func (x *DoClanWarGetInfo) SetJSONByte(b []byte) {
|
||||
x.data = b
|
||||
}
|
||||
|
||||
// SetJSON ...
|
||||
func (x *DoClanWarGetInfo) SetJSON(s string) {
|
||||
x.data = []byte(s)
|
||||
}
|
||||
|
||||
// GetUserDetails ...
|
||||
func (x *DoClanWarGetInfo) GetUserDetails() ([]*models.User, error) {
|
||||
var users []*models.User
|
||||
|
||||
result := gjson.GetBytes(x.data, "response.result.response.enemies.#.user")
|
||||
for _, jsonValue := range result.Array() {
|
||||
user := new(models.User)
|
||||
if err := user.UnmarshalUserJSON([]byte(jsonValue.Raw)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
users = append(users, user)
|
||||
}
|
||||
return users, nil
|
||||
}
|
||||
|
||||
// GetObject ...
|
||||
func (x *DoClanWarGetInfo) GetObject() []byte {
|
||||
date := gjson.GetBytes(x.data, `date`)
|
||||
if !date.Exists() {
|
||||
fmt.Printf("GetObject - error: %s\n", "can't find date")
|
||||
return []byte("")
|
||||
}
|
||||
|
||||
dat := gjson.GetBytes(x.data, `response.result.response`)
|
||||
if !dat.Exists() {
|
||||
fmt.Printf("GetObject - error: %s\n", "can't find results")
|
||||
return []byte("")
|
||||
}
|
||||
|
||||
warEnd := dat.Get("endTime")
|
||||
|
||||
ts, err := strconv.Atoi(warEnd.String())
|
||||
if err != nil {
|
||||
return []byte("")
|
||||
}
|
||||
tsunix := time.Unix(int64(ts), 0)
|
||||
tsunix = tsunix.Add(-12 * time.Hour)
|
||||
dat2, err := sjson.SetRawBytes([]byte(dat.Raw), "warDate", []byte(fmt.Sprintf("%d", tsunix.Unix())))
|
||||
if err != nil {
|
||||
fmt.Printf("GetObject - error: %s\n", err.Error())
|
||||
return []byte("")
|
||||
}
|
||||
|
||||
return dat2
|
||||
}
|
||||
|
||||
// GenerateArrayFromDir ...
|
||||
func (x *DoClanWarGetInfo) GenerateArrayFromDir(ident string) {
|
||||
dirPath, _ := os.Getwd()
|
||||
dirPathHWtools := filepath.Join(dirPath, "hwtools", "data", ident)
|
||||
dirPath = filepath.Join(dirPath, "data", ident)
|
||||
// fmt.Printf("%s\n", dirPath)
|
||||
returnData := []byte("[]")
|
||||
|
||||
err := filepath.Walk(dirPath,
|
||||
func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
//fmt.Printf("%s\n", path)
|
||||
if strings.HasSuffix(info.Name(), ".json") {
|
||||
x.data, err = readFile(path)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
returnData, err = sjson.SetRawBytes(returnData, "-1", x.GetObject())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
mdir := filepath.Base(dirPathHWtools)
|
||||
os.MkdirAll(mdir, os.ModePerm)
|
||||
|
||||
returnData = runJQUniqe(returnData)
|
||||
|
||||
err = ioutil.WriteFile(dirPathHWtools+".json", returnData, 0644)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
// GetJSONResult ...
|
||||
func (x *DoClanWarGetInfo) GetJSONResult() []byte {
|
||||
return x.result
|
||||
}
|
||||
@@ -1 +1,137 @@
|
||||
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
|
||||
}
|
||||
|
||||
113
server/services/datacollector/datacollector_test.go
Normal file
113
server/services/datacollector/datacollector_test.go
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user