statistic.go 6.7 KB

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