file.go 3.1 KB

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