95 linhas
1,7 KiB
Go
95 linhas
1,7 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
// 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() {
|
|
|
|
log.Println("Trying to write json file")
|
|
defer handlepanic()
|
|
|
|
dumpfile := os.Getenv("DUMPFILE")
|
|
if dumpfile == "" {
|
|
dumpfile = "./"
|
|
}
|
|
|
|
var statsREPORT string
|
|
|
|
statsREPORT = "\n"
|
|
|
|
ZClassifier.STATS.Range(func(key interface{}, value interface{}) bool {
|
|
|
|
statsREPORT += fmt.Sprintf("{\"%s\" : \"%d\"}\n", key.(string), value.(int64))
|
|
return true
|
|
})
|
|
|
|
writeToFile(dumpfile+"stats.json", statsREPORT)
|
|
|
|
ZClassifier.Learning.busy.Lock()
|
|
defer ZClassifier.Learning.busy.Unlock()
|
|
|
|
badScores, err := json.MarshalIndent(ZClassifier.Learning.bayez.datas[Bad], "", " ")
|
|
if err != nil {
|
|
badScores = []byte(err.Error())
|
|
}
|
|
|
|
writeToFile(dumpfile+"BAD.json", string(badScores))
|
|
log.Println(string(badScores))
|
|
|
|
goodScores, err := json.MarshalIndent(ZClassifier.Learning.bayez.datas[Good], "", " ")
|
|
if err != nil {
|
|
goodScores = []byte(err.Error())
|
|
}
|
|
|
|
writeToFile(dumpfile+"GOOD.json", string(goodScores))
|
|
|
|
log.Println(string(goodScores))
|
|
|
|
}
|
|
|
|
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")
|
|
}
|