file.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package main
  2. import (
  3. // "encoding/json"
  4. "fmt"
  5. "io"
  6. "log"
  7. "os"
  8. "time"
  9. )
  10. // WriteToFile will print any string of text to a file safely by
  11. // checking for errors and syncing at the end.
  12. func writeToFile(filename string, data string) error {
  13. file, err := os.Create(filename)
  14. if err != nil {
  15. return err
  16. }
  17. defer file.Close()
  18. _, err = io.WriteString(file, data)
  19. if err != nil {
  20. return err
  21. }
  22. return file.Sync()
  23. }
  24. func handlepanic() {
  25. if a := recover(); a != nil {
  26. fmt.Println("OPS!: Recovering from:", a)
  27. }
  28. }
  29. func saveBayesToFile() {
  30. log.Println("Trying to write json file")
  31. defer handlepanic()
  32. dumpfile := os.Getenv("DUMPFILE")
  33. if dumpfile == "" {
  34. dumpfile = "bayes.json"
  35. }
  36. err := ZClassifier.Learning.bayez.WriteClassesToFile(dumpfile)
  37. if err != nil {
  38. log.Println("Error Writing file: ", dumpfile, err.Error())
  39. }
  40. // var statsREPORT string
  41. // statsREPORT = "\n"
  42. // Classifier.STATS.Range(func(key interface{}, value interface{}) bool {
  43. // statsREPORT += fmt.Sprintf("{\"%s\" : \"%d\"}\n", key.(string), value.(int64))
  44. // return true
  45. // })
  46. // dumpfile := os.Getenv("DUMPFILE")
  47. // if dumpfile == "" {
  48. // dumpfile = "bayes.json"
  49. // }
  50. // if DebugLog {
  51. // log.Println("DUMP: ", string(tmpJSON)+statsREPORT)
  52. // } else {
  53. // writeToFile(dumpfile, string(tmpJSON)+statsREPORT)
  54. //
  55. // log.Println("File saved: ", dumpfile)
  56. // }
  57. }
  58. func jsonEngine() {
  59. for {
  60. log.Println("Zardoz Seniority: ", ProxyFlow.seniority)
  61. saveBayesToFile()
  62. time.Sleep(1 * time.Minute)
  63. }
  64. }
  65. func init() {
  66. log.Printf("File Engine Starting")
  67. go jsonEngine()
  68. log.Printf("FIle Engine Started")
  69. }