123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- 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 = "bayes.json"
- }
- ZClassifier.STATS.busy.Lock()
- defer ZClassifier.STATS.busy.Unlock()
- statsREPORT, err := json.MarshalIndent(ZClassifier.STATS.stats, "", " ")
- if err != nil {
- statsREPORT = []byte(err.Error())
- }
- ZClassifier.Working.busy.Lock()
- defer ZClassifier.Working.busy.Unlock()
- wScores, err := json.MarshalIndent(ZClassifier.Working.sMap, "", " ")
- if err != nil {
- wScores = []byte(err.Error())
- }
- ZClassifier.Learning.busy.Lock()
- defer ZClassifier.Learning.busy.Unlock()
- lScores, err := json.MarshalIndent(ZClassifier.Learning.sMap, "", " ")
- if err != nil {
- lScores = []byte(err.Error())
- }
- report := fmt.Sprintf("STATS: %s\n WORKING: %s\n LEARNING: %s\n", statsREPORT, wScores, lScores)
- writeToFile(dumpfile, report)
- log.Println(report)
- }
- 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")
- }
|