statistic.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. // This function gives the current slot in the 288- 5 minutes interval of the day
  85. func (c *Collector) GetCurrentRealtimeStatIntervalId() int {
  86. now := time.Now()
  87. startOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).Unix()
  88. secondsSinceStartOfDay := now.Unix() - startOfDay
  89. interval := secondsSinceStartOfDay / (5 * 60)
  90. return int(interval)
  91. }
  92. func (c *Collector) Close() {
  93. //Stop the ticker
  94. c.rtdataStopChan <- true
  95. //Write the buffered data into database
  96. c.SaveSummaryOfDay()
  97. }
  98. // Main function to record all the inbound traffics
  99. // Note that this function run in go routine and might have concurrent R/W issue
  100. // Please make sure there is no racing paramters in this function
  101. func (c *Collector) RecordRequest(ri RequestInfo) {
  102. go func() {
  103. c.DailySummary.TotalRequest++
  104. if ri.Succ {
  105. c.DailySummary.ValidRequest++
  106. } else {
  107. c.DailySummary.ErrorRequest++
  108. }
  109. //Store the request info into correct types of maps
  110. ft, ok := c.DailySummary.ForwardTypes.Load(ri.ForwardType)
  111. if !ok {
  112. c.DailySummary.ForwardTypes.Store(ri.ForwardType, 1)
  113. } else {
  114. c.DailySummary.ForwardTypes.Store(ri.ForwardType, ft.(int)+1)
  115. }
  116. originISO := strings.ToLower(ri.RequestOriginalCountryISOCode)
  117. fo, ok := c.DailySummary.RequestOrigin.Load(originISO)
  118. if !ok {
  119. c.DailySummary.RequestOrigin.Store(originISO, 1)
  120. } else {
  121. c.DailySummary.RequestOrigin.Store(originISO, fo.(int)+1)
  122. }
  123. //Filter out CF forwarded requests
  124. if strings.Contains(ri.IpAddr, ",") {
  125. ips := strings.Split(strings.TrimSpace(ri.IpAddr), ",")
  126. if len(ips) >= 1 && IsValidIPAddress(strings.TrimPrefix(ips[0], "[")) {
  127. //Example when forwarded from CF: 158.250.160.114,109.21.249.211
  128. //Or IPv6 [15c4:cbb4:cc98:4291:ffc1:3a46:06a1:51a7],109.21.249.211
  129. ri.IpAddr = ips[0]
  130. }
  131. }
  132. fi, ok := c.DailySummary.RequestClientIp.Load(ri.IpAddr)
  133. if !ok {
  134. c.DailySummary.RequestClientIp.Store(ri.IpAddr, 1)
  135. } else {
  136. c.DailySummary.RequestClientIp.Store(ri.IpAddr, fi.(int)+1)
  137. }
  138. //Record the referer
  139. p := bluemonday.StripTagsPolicy()
  140. filteredReferer := p.Sanitize(
  141. ri.Referer,
  142. )
  143. rf, ok := c.DailySummary.Referer.Load(filteredReferer)
  144. if !ok {
  145. c.DailySummary.Referer.Store(filteredReferer, 1)
  146. } else {
  147. c.DailySummary.Referer.Store(filteredReferer, rf.(int)+1)
  148. }
  149. //Record the UserAgent
  150. ua, ok := c.DailySummary.UserAgent.Load(ri.UserAgent)
  151. if !ok {
  152. c.DailySummary.UserAgent.Store(ri.UserAgent, 1)
  153. } else {
  154. c.DailySummary.UserAgent.Store(ri.UserAgent, ua.(int)+1)
  155. }
  156. //ADD MORE HERE IF NEEDED
  157. //Record request URL, if it is a page
  158. ext := filepath.Ext(ri.RequestURL)
  159. if ext != "" && !isWebPageExtension(ext) {
  160. return
  161. }
  162. ru, ok := c.DailySummary.RequestURL.Load(ri.RequestURL)
  163. if !ok {
  164. c.DailySummary.RequestURL.Store(ri.RequestURL, 1)
  165. } else {
  166. c.DailySummary.RequestURL.Store(ri.RequestURL, ru.(int)+1)
  167. }
  168. }()
  169. }
  170. // nightly task
  171. func (c *Collector) ScheduleResetRealtimeStats() chan bool {
  172. doneCh := make(chan bool)
  173. go func() {
  174. defer close(doneCh)
  175. for {
  176. // calculate duration until next midnight
  177. now := time.Now()
  178. // Get midnight of the next day in the local time zone
  179. midnight := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 0, 0, 0, now.Location())
  180. // Calculate the duration until midnight
  181. duration := midnight.Sub(now)
  182. select {
  183. case <-time.After(duration):
  184. // store daily summary to database and reset summary
  185. c.SaveSummaryOfDay()
  186. c.DailySummary = newDailySummary()
  187. case <-doneCh:
  188. // stop the routine
  189. return
  190. }
  191. }
  192. }()
  193. return doneCh
  194. }
  195. func newDailySummary() *DailySummary {
  196. return &DailySummary{
  197. TotalRequest: 0,
  198. ErrorRequest: 0,
  199. ValidRequest: 0,
  200. ForwardTypes: &sync.Map{},
  201. RequestOrigin: &sync.Map{},
  202. RequestClientIp: &sync.Map{},
  203. Referer: &sync.Map{},
  204. UserAgent: &sync.Map{},
  205. RequestURL: &sync.Map{},
  206. }
  207. }