statistic.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package statistic
  2. import (
  3. "path/filepath"
  4. "strings"
  5. "sync"
  6. "time"
  7. "github.com/microcosm-cc/bluemonday"
  8. "imuslab.com/zoraxy/mod/database"
  9. )
  10. /*
  11. Statistic Package
  12. This packet is designed to collection information
  13. and store them for future analysis
  14. */
  15. // Faststat, a interval summary for all collected data and avoid
  16. // looping through every data everytime a overview is needed
  17. type DailySummary struct {
  18. TotalRequest int64 //Total request of the day
  19. ErrorRequest int64 //Invalid request of the day, including error or not found
  20. ValidRequest int64 //Valid request of the day
  21. //Type counters
  22. ForwardTypes *sync.Map //Map that hold the forward types
  23. RequestOrigin *sync.Map //Map that hold [country ISO code]: visitor counter
  24. RequestClientIp *sync.Map //Map that hold all unique request IPs
  25. Referer *sync.Map //Map that store where the user was refered from
  26. UserAgent *sync.Map //Map that store the useragent of the request
  27. RequestURL *sync.Map //Request URL of the request object
  28. }
  29. type RequestInfo struct {
  30. IpAddr string
  31. RequestOriginalCountryISOCode string
  32. Succ bool
  33. StatusCode int
  34. ForwardType string
  35. Referer string
  36. UserAgent string
  37. RequestURL string
  38. Target string
  39. }
  40. type CollectorOption struct {
  41. Database *database.Database
  42. }
  43. type Collector struct {
  44. rtdataStopChan chan bool
  45. DailySummary *DailySummary
  46. Option *CollectorOption
  47. }
  48. func NewStatisticCollector(option CollectorOption) (*Collector, error) {
  49. option.Database.NewTable("stats")
  50. //Create the collector object
  51. thisCollector := Collector{
  52. DailySummary: newDailySummary(),
  53. Option: &option,
  54. }
  55. //Load the stat if exists for today
  56. //This will exists if the program was forcefully restarted
  57. year, month, day := time.Now().Date()
  58. summary := thisCollector.LoadSummaryOfDay(year, month, day)
  59. if summary != nil {
  60. thisCollector.DailySummary = summary
  61. }
  62. //Schedule the realtime statistic clearing at midnight everyday
  63. rtstatStopChan := thisCollector.ScheduleResetRealtimeStats()
  64. thisCollector.rtdataStopChan = rtstatStopChan
  65. return &thisCollector, nil
  66. }
  67. // Write the current in-memory summary to database file
  68. func (c *Collector) SaveSummaryOfDay() {
  69. //When it is called in 0:00am, make sure it is stored as yesterday key
  70. t := time.Now().Add(-30 * time.Second)
  71. summaryKey := t.Format("2006_01_02")
  72. saveData := DailySummaryToExport(*c.DailySummary)
  73. c.Option.Database.Write("stats", summaryKey, saveData)
  74. }
  75. // Load the summary of a day given
  76. func (c *Collector) LoadSummaryOfDay(year int, month time.Month, day int) *DailySummary {
  77. date := time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.Local)
  78. summaryKey := date.Format("2006_01_02")
  79. targetSummaryExport := DailySummaryExport{}
  80. c.Option.Database.Read("stats", summaryKey, &targetSummaryExport)
  81. targetSummary := DailySummaryExportToSummary(targetSummaryExport)
  82. return &targetSummary
  83. }
  84. // Reset today summary, for debug or restoring injections
  85. func (c *Collector) ResetSummaryOfDay() {
  86. c.DailySummary = newDailySummary()
  87. }
  88. // This function gives the current slot in the 288- 5 minutes interval of the day
  89. func (c *Collector) GetCurrentRealtimeStatIntervalId() int {
  90. now := time.Now()
  91. startOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Unix()
  92. secondsSinceStartOfDay := now.Unix() - startOfDay
  93. interval := secondsSinceStartOfDay / (5 * 60)
  94. return int(interval)
  95. }
  96. func (c *Collector) Close() {
  97. //Stop the ticker
  98. c.rtdataStopChan <- true
  99. //Write the buffered data into database
  100. c.SaveSummaryOfDay()
  101. }
  102. // Main function to record all the inbound traffics
  103. // Note that this function run in go routine and might have concurrent R/W issue
  104. // Please make sure there is no racing paramters in this function
  105. func (c *Collector) RecordRequest(ri RequestInfo) {
  106. go func() {
  107. c.DailySummary.TotalRequest++
  108. if ri.Succ {
  109. c.DailySummary.ValidRequest++
  110. } else {
  111. c.DailySummary.ErrorRequest++
  112. }
  113. //Store the request info into correct types of maps
  114. ft, ok := c.DailySummary.ForwardTypes.Load(ri.ForwardType)
  115. if !ok {
  116. c.DailySummary.ForwardTypes.Store(ri.ForwardType, 1)
  117. } else {
  118. c.DailySummary.ForwardTypes.Store(ri.ForwardType, ft.(int)+1)
  119. }
  120. originISO := strings.ToLower(ri.RequestOriginalCountryISOCode)
  121. fo, ok := c.DailySummary.RequestOrigin.Load(originISO)
  122. if !ok {
  123. c.DailySummary.RequestOrigin.Store(originISO, 1)
  124. } else {
  125. c.DailySummary.RequestOrigin.Store(originISO, fo.(int)+1)
  126. }
  127. //Filter out CF forwarded requests
  128. if strings.Contains(ri.IpAddr, ",") {
  129. ips := strings.Split(strings.TrimSpace(ri.IpAddr), ",")
  130. if len(ips) >= 1 && IsValidIPAddress(strings.TrimPrefix(ips[0], "[")) {
  131. //Example when forwarded from CF: 158.250.160.114,109.21.249.211
  132. //Or IPv6 [15c4:cbb4:cc98:4291:ffc1:3a46:06a1:51a7],109.21.249.211
  133. ri.IpAddr = ips[0]
  134. }
  135. }
  136. fi, ok := c.DailySummary.RequestClientIp.Load(ri.IpAddr)
  137. if !ok {
  138. c.DailySummary.RequestClientIp.Store(ri.IpAddr, 1)
  139. } else {
  140. c.DailySummary.RequestClientIp.Store(ri.IpAddr, fi.(int)+1)
  141. }
  142. //Record the referer
  143. p := bluemonday.StripTagsPolicy()
  144. filteredReferer := p.Sanitize(
  145. ri.Referer,
  146. )
  147. rf, ok := c.DailySummary.Referer.Load(filteredReferer)
  148. if !ok {
  149. c.DailySummary.Referer.Store(filteredReferer, 1)
  150. } else {
  151. c.DailySummary.Referer.Store(filteredReferer, rf.(int)+1)
  152. }
  153. //Record the UserAgent
  154. ua, ok := c.DailySummary.UserAgent.Load(ri.UserAgent)
  155. if !ok {
  156. c.DailySummary.UserAgent.Store(ri.UserAgent, 1)
  157. } else {
  158. c.DailySummary.UserAgent.Store(ri.UserAgent, ua.(int)+1)
  159. }
  160. //ADD MORE HERE IF NEEDED
  161. //Record request URL, if it is a page
  162. ext := filepath.Ext(ri.RequestURL)
  163. if ext != "" && !isWebPageExtension(ext) {
  164. return
  165. }
  166. ru, ok := c.DailySummary.RequestURL.Load(ri.RequestURL)
  167. if !ok {
  168. c.DailySummary.RequestURL.Store(ri.RequestURL, 1)
  169. } else {
  170. c.DailySummary.RequestURL.Store(ri.RequestURL, ru.(int)+1)
  171. }
  172. }()
  173. }
  174. // nightly task
  175. func (c *Collector) ScheduleResetRealtimeStats() chan bool {
  176. doneCh := make(chan bool)
  177. go func() {
  178. defer close(doneCh)
  179. for {
  180. // calculate duration until next midnight
  181. now := time.Now()
  182. // Get midnight of the next day in the local time zone
  183. midnight := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, now.Location())
  184. // Calculate the duration until midnight
  185. duration := midnight.Sub(now)
  186. select {
  187. case <-time.After(duration):
  188. // store daily summary to database and reset summary
  189. c.SaveSummaryOfDay()
  190. c.DailySummary = newDailySummary()
  191. case <-doneCh:
  192. // stop the routine
  193. return
  194. }
  195. }
  196. }()
  197. return doneCh
  198. }
  199. func newDailySummary() *DailySummary {
  200. return &DailySummary{
  201. TotalRequest: 0,
  202. ErrorRequest: 0,
  203. ValidRequest: 0,
  204. ForwardTypes: &sync.Map{},
  205. RequestOrigin: &sync.Map{},
  206. RequestClientIp: &sync.Map{},
  207. Referer: &sync.Map{},
  208. UserAgent: &sync.Map{},
  209. RequestURL: &sync.Map{},
  210. }
  211. }