zardoz/file.go

106 lines
1.9 KiB
Go
Raw Normal View History

2019-11-22 11:35:16 +01:00
package main
import (
2019-12-11 15:25:30 +01:00
"encoding/json"
2019-11-22 11:35:16 +01:00
"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() {
2019-12-09 15:51:56 +01:00
log.Println("Trying to write json file")
2019-12-09 17:06:31 +01:00
defer handlepanic()
2019-12-09 15:51:56 +01:00
2019-12-11 12:10:36 +01:00
dumpfile := os.Getenv("DUMPFILE")
if dumpfile == "" {
2019-12-11 15:25:30 +01:00
dumpfile = "./"
2019-12-11 12:10:36 +01:00
}
2019-11-22 11:35:16 +01:00
2019-12-11 15:25:30 +01:00
var statsREPORT string
2019-11-28 14:47:07 +01:00
2019-12-11 15:25:30 +01:00
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)
badScores, err := json.MarshalIndent(ZClassifier.Learning.bayez.datas[Bad], "", " ")
2019-12-10 13:27:49 +01:00
if err != nil {
2019-12-11 15:25:30 +01:00
badScores = []byte(err.Error())
}
2019-12-11 15:25:30 +01:00
writeToFile(dumpfile+"BAD.json", string(badScores))
log.Println(string(badScores))
2019-12-10 15:39:32 +01:00
2019-12-11 15:25:30 +01:00
goodScores, err := json.MarshalIndent(ZClassifier.Learning.bayez.datas[Good], "", " ")
if err != nil {
goodScores = []byte(err.Error())
}
2019-12-10 15:39:32 +01:00
2019-12-11 15:25:30 +01:00
writeToFile(dumpfile+"GOOD.json", string(goodScores))
2019-12-10 13:33:34 +01:00
2019-12-11 15:25:30 +01:00
log.Println(string(goodScores))
2019-11-24 13:18:22 +01:00
2019-12-11 12:10:36 +01:00
// dumpfile := os.Getenv("DUMPFILE")
// if dumpfile == "" {
// dumpfile = "bayes.json"
// }
2019-12-09 16:32:16 +01:00
2019-12-11 12:10:36 +01:00
// if DebugLog {
// log.Println("DUMP: ", string(tmpJSON)+statsREPORT)
// } else {
// writeToFile(dumpfile, string(tmpJSON)+statsREPORT)
//
// log.Println("File saved: ", dumpfile)
// }
2019-11-22 11:35:16 +01:00
}
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")
}