file.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "log"
  7. "os"
  8. "time"
  9. )
  10. type dumpRecord struct {
  11. Token string
  12. LastSeen string
  13. }
  14. type dumpStats struct {
  15. Decision string
  16. Amount int64
  17. }
  18. type dumpStruct struct {
  19. GOOD []dumpRecord
  20. BAD []dumpRecord
  21. MEH []dumpRecord
  22. STATS []dumpStats
  23. }
  24. // WriteToFile will print any string of text to a file safely by
  25. // checking for errors and syncing at the end.
  26. func writeToFile(filename string, data string) error {
  27. file, err := os.Create(filename)
  28. if err != nil {
  29. return err
  30. }
  31. defer file.Close()
  32. _, err = io.WriteString(file, data)
  33. if err != nil {
  34. return err
  35. }
  36. return file.Sync()
  37. }
  38. func handlepanic() {
  39. if a := recover(); a != nil {
  40. fmt.Println("OPS!: Recovering from:", a)
  41. }
  42. }
  43. func saveBayesToFile() {
  44. var tmpJSON string
  45. var DumpJson = new(dumpStruct)
  46. log.Println("Trying to write json file")
  47. Classifier.BAD.Range(func(key interface{}, value interface{}) bool {
  48. var t dumpRecord
  49. t.Token = key.(string)
  50. t.LastSeen = time.Unix(0, value.(int64)).String()
  51. DumpJson.BAD = append(DumpJson.BAD, t)
  52. return true
  53. })
  54. Classifier.GOOD.Range(func(key interface{}, value interface{}) bool {
  55. var t dumpRecord
  56. t.Token = key.(string)
  57. t.LastSeen = time.Unix(0, value.(int64)).String()
  58. DumpJson.GOOD = append(DumpJson.GOOD, t)
  59. return true
  60. })
  61. Classifier.MEH.Range(func(key interface{}, value interface{}) bool {
  62. var t dumpRecord
  63. t.Token = key.(string)
  64. t.LastSeen = time.Unix(0, value.(int64)).String()
  65. DumpJson.MEH = append(DumpJson.MEH, t)
  66. return true
  67. })
  68. Classifier.STATS.Range(func(key interface{}, value interface{}) bool {
  69. var t dumpStats
  70. t.Decision = key.(string)
  71. t.Amount = value.(int64)
  72. DumpJson.STATS = append(DumpJson.STATS, t)
  73. return true
  74. })
  75. if tmpJ, e := json.MarshalIndent(DumpJson, "", " "); e == nil {
  76. tmpJSON = fmt.Sprintf("%s", tmpJ)
  77. } else {
  78. tmpJSON = e.Error()
  79. }
  80. dumpfile := os.Getenv("DUMPFILE")
  81. if dumpfile == "" {
  82. dumpfile = "bayes.json"
  83. }
  84. if DebugLog {
  85. log.Println(tmpJSON)
  86. } else {
  87. writeToFile(dumpfile, tmpJSON)
  88. log.Println("File saved: ", dumpfile)
  89. }
  90. }
  91. func jsonEngine() {
  92. for {
  93. log.Println("Zardoz Seniority: ", ProxyFlow.seniority)
  94. saveBayesToFile()
  95. time.Sleep(1 * time.Minute)
  96. }
  97. }
  98. func init() {
  99. log.Printf("File Engine Starting")
  100. go jsonEngine()
  101. log.Printf("FIle Engine Started")
  102. }