123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- package main
- import (
- "encoding/json"
- "fmt"
- "io"
- "log"
- "os"
- "time"
- )
- type dumpRecord struct {
- Token string
- LastSeen string
- }
- type dumpStats struct {
- Decision string
- Amount int64
- }
- type dumpStruct struct {
- GOOD []dumpRecord
- BAD []dumpRecord
- MEH []dumpRecord
- STATS []dumpStats
- }
- // WriteToFile will print any string of text to a file safely by
- // checking for errors and syncing at the end.
- func writeToFile(filename string, data string) error {
- file, err := os.Create(filename)
- if err != nil {
- return err
- }
- defer file.Close()
- _, err = io.WriteString(file, data)
- if err != nil {
- return err
- }
- return file.Sync()
- }
- func handlepanic() {
- if a := recover(); a != nil {
- fmt.Println("OPS!: Recovering from:", a)
- }
- }
- func saveBayesToFile() {
- var tmpJSON string
- var DumpJson = new(dumpStruct)
- log.Println("Trying to write json file")
- Classifier.BAD.Range(func(key interface{}, value interface{}) bool {
- var t dumpRecord
- t.Token = key.(string)
- t.LastSeen = time.Unix(0, value.(int64)).String()
- DumpJson.BAD = append(DumpJson.BAD, t)
- return true
- })
- Classifier.GOOD.Range(func(key interface{}, value interface{}) bool {
- var t dumpRecord
- t.Token = key.(string)
- t.LastSeen = time.Unix(0, value.(int64)).String()
- DumpJson.GOOD = append(DumpJson.GOOD, t)
- return true
- })
- Classifier.MEH.Range(func(key interface{}, value interface{}) bool {
- var t dumpRecord
- t.Token = key.(string)
- t.LastSeen = time.Unix(0, value.(int64)).String()
- DumpJson.MEH = append(DumpJson.MEH, t)
- return true
- })
- Classifier.STATS.Range(func(key interface{}, value interface{}) bool {
- var t dumpStats
- t.Decision = key.(string)
- t.Amount = value.(int64)
- DumpJson.STATS = append(DumpJson.STATS, t)
- return true
- })
- if tmpJ, e := json.MarshalIndent(DumpJson, "", " "); e == nil {
- tmpJSON = fmt.Sprintf("%s", tmpJ)
- } else {
- tmpJSON = e.Error()
- }
- dumpfile := os.Getenv("DUMPFILE")
- if dumpfile == "" {
- dumpfile = "bayes.json"
- }
- if DebugLog {
- log.Println(tmpJSON)
- } else {
- writeToFile(dumpfile, tmpJSON)
- log.Println("File saved: ", dumpfile)
- }
- }
- func jsonEngine() {
- for {
- log.Println("Zardoz Seniority: ", ProxyFlow.seniority)
- saveBayesToFile()
- time.Sleep(1 * time.Minute)
- }
- }
- func init() {
- log.Printf("File Engine Starting")
- go jsonEngine()
- log.Printf("FIle Engine Started")
- }
|