analytics.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. package analytics
  2. import (
  3. "bytes"
  4. "compress/zlib"
  5. "crypto/sha256"
  6. "encoding/json"
  7. "fmt"
  8. "html/template"
  9. "io"
  10. "io/ioutil"
  11. "net/http"
  12. "os"
  13. "strings"
  14. "sync"
  15. "time"
  16. )
  17. type Analyzer interface {
  18. Dashboard(w http.ResponseWriter, r *http.Request)
  19. InsertRequest(r *http.Request)
  20. }
  21. type AnalyticsConfiguration struct {
  22. HashIPSecret string
  23. GroupByURLSegment int
  24. EntriesByURLSegment int
  25. WriteScheduleSeconds int
  26. Name string
  27. Password string
  28. Directory string
  29. UserAgentBlackList []string
  30. }
  31. type analytics struct {
  32. HashIPSecret string
  33. groupBy int
  34. entriesBy int
  35. WriteScheduleSeconds int
  36. Password string
  37. Name string
  38. Directory string
  39. Mux *sync.RWMutex
  40. logger func(...interface{}) (int, error)
  41. UserAgentBlackList []string
  42. IPEntries map[string]map[string][]action
  43. }
  44. func NewAnalytics(config AnalyticsConfiguration, logger func(...interface{}) (int, error)) Analyzer {
  45. if logger == nil {
  46. logger = fmt.Println
  47. }
  48. ana := &analytics{
  49. Name: config.Name,
  50. Password: config.Password,
  51. groupBy: config.GroupByURLSegment,
  52. entriesBy: config.EntriesByURLSegment,
  53. HashIPSecret: config.HashIPSecret,
  54. WriteScheduleSeconds: config.WriteScheduleSeconds,
  55. Directory: config.Directory,
  56. UserAgentBlackList: config.UserAgentBlackList,
  57. Mux: &sync.RWMutex{},
  58. logger: logger,
  59. }
  60. ana.IPEntries = map[string]map[string][]action{}
  61. ana.IPEntries[time.Now().Local().Format("2006-01-02")] = ana.readSavedData(time.Now().Local())
  62. ana.scheduleWrite()
  63. return ana
  64. }
  65. func (a analytics) scheduleWrite() {
  66. ticker := time.NewTicker(time.Duration(a.WriteScheduleSeconds) * time.Second)
  67. quit := make(chan struct{})
  68. go func() {
  69. for {
  70. select {
  71. case <-ticker.C:
  72. err := a.writeFile()
  73. if err != nil {
  74. a.logger(err)
  75. }
  76. case <-quit:
  77. ticker.Stop()
  78. return
  79. }
  80. }
  81. }()
  82. }
  83. var DefaultUserAgentBlacklist = []string{
  84. "wget", "python", "perl", "msnbot", "netresearch", "bot",
  85. "archive", "crawl", "googlebot", "msn", "archive", "php",
  86. "panscient", "berry", "yandex", "bing", "fluffy",
  87. }
  88. func (a analytics) InsertRequest(r *http.Request) {
  89. ua := strings.ToLower(r.UserAgent())
  90. bots := a.UserAgentBlackList
  91. for _, b := range bots {
  92. if strings.Contains(strings.ToLower(ua), b) {
  93. return
  94. }
  95. }
  96. act := action{Page: r.URL.Path, Query: r.URL.RawQuery}
  97. a.Mux.Lock()
  98. defer a.Mux.Unlock()
  99. a.insert(r.RemoteAddr, act)
  100. }
  101. func (a analytics) Dashboard(w http.ResponseWriter, r *http.Request) {
  102. q := r.URL.Query()
  103. if len(a.Password) > 0 && (len(q["k"]) == 0 || len(q["k"][0]) == 0 || q["k"][0] != a.Password) {
  104. a.logger(fmt.Errorf("Unauthorized"))
  105. w.WriteHeader(http.StatusUnauthorized)
  106. w.Write(nil)
  107. return
  108. }
  109. date := time.Now()
  110. var err error
  111. if len(q["date"]) > 0 {
  112. date, err = time.Parse("2006-01-02", q["date"][0])
  113. if err != nil {
  114. a.logger(err)
  115. w.WriteHeader(http.StatusBadRequest)
  116. w.Write(nil)
  117. return
  118. }
  119. }
  120. var data map[string][]action
  121. if date.Format("2006-01-02") == time.Now().Format("2006-01-02") {
  122. data = a.IPEntries[date.Format("2006-01-02")]
  123. } else {
  124. data = a.readSavedData(date)
  125. }
  126. entries := len(data)
  127. urlHits := map[string]map[string]int{}
  128. for _, actions := range data {
  129. for _, act := range actions {
  130. pParts := strings.Split(act.Page, "/")
  131. groupBy := pParts[a.groupBy]
  132. dataEntry := strings.Join(pParts[a.entriesBy:], "/")
  133. _, ok := urlHits[groupBy]
  134. if !ok {
  135. urlHits[groupBy] = map[string]int{}
  136. }
  137. urlHits[groupBy][dataEntry] = urlHits[groupBy][dataEntry] + 1
  138. }
  139. }
  140. dd := dashData{SessionCount: entries, URLHits: urlHits, Date: date.Format("2006-01-02")}
  141. t, err := template.New("").Parse(HTML)
  142. if err != nil {
  143. a.logger(err)
  144. w.WriteHeader(http.StatusInternalServerError)
  145. w.Write(nil)
  146. return
  147. }
  148. err = t.ExecuteTemplate(w, "layout", dd)
  149. if err != nil {
  150. a.logger(err)
  151. }
  152. }
  153. type dashData struct {
  154. SessionCount int
  155. Date string
  156. URLHits map[string]map[string]int
  157. }
  158. type action struct {
  159. Page string
  160. Query string
  161. }
  162. func (a analytics) readSavedData(td time.Time) map[string][]action {
  163. fileName := a.Directory + td.Format("/2006/01/02/") + a.Name + td.Format("2006-01-02")
  164. entries := map[string][]action{}
  165. if _, err := os.Stat(fileName); os.IsNotExist(err) {
  166. } else {
  167. bs, err := ioutil.ReadFile(fileName)
  168. if err != nil {
  169. a.logger(err)
  170. return entries
  171. }
  172. r, err := zlib.NewReader(bytes.NewReader(bs))
  173. if err != nil {
  174. a.logger(err)
  175. return entries
  176. }
  177. jsonBytes := bytes.NewBuffer([]byte{})
  178. _, err = io.Copy(jsonBytes, r)
  179. if err != nil {
  180. a.logger(err)
  181. return entries
  182. }
  183. r.Close()
  184. err = json.Unmarshal(jsonBytes.Bytes(), &entries)
  185. if err != nil {
  186. a.logger(err)
  187. }
  188. }
  189. return entries
  190. }
  191. func (a analytics) insert(ip string, act action) {
  192. ts := time.Now().Format("2006-01-02")
  193. stamps := a.IPEntries[ts]
  194. if stamps == nil {
  195. a.IPEntries[ts] = map[string][]action{}
  196. }
  197. if len(a.HashIPSecret) > 0 {
  198. hash := sha256.New()
  199. ip = ts + ip + a.HashIPSecret
  200. inpIP := strings.NewReader(ip)
  201. if _, err := io.Copy(hash, inpIP); err != nil {
  202. a.logger(err)
  203. }
  204. sum := hash.Sum(nil)
  205. ip = string(sum)
  206. }
  207. entries := stamps[ip]
  208. if entries == nil {
  209. entries = []action{}
  210. }
  211. entries = append(entries, act)
  212. a.IPEntries[ts][ip] = entries
  213. }
  214. func (a analytics) writeFile() error {
  215. ts := time.Now().Format("/2006/01/02")
  216. err := os.MkdirAll(a.Directory+ts, os.ModePerm)
  217. if err != nil {
  218. return err
  219. }
  220. a.Mux.Lock()
  221. defer a.Mux.Unlock()
  222. for k, e := range a.IPEntries {
  223. data, err := json.Marshal(e)
  224. if err != nil {
  225. return err
  226. }
  227. f, err := os.Create(a.Directory + ts + "/" + a.Name + k)
  228. if err != nil {
  229. return err
  230. }
  231. var b bytes.Buffer
  232. w := zlib.NewWriter(&b)
  233. w.Write(data)
  234. w.Close()
  235. defer f.Close()
  236. _, err = f.Write(b.Bytes())
  237. if err != nil {
  238. return err
  239. }
  240. }
  241. return nil
  242. }
  243. const HTML = `
  244. {{ define "layout" }}
  245. <!DOCTYPE html>
  246. <html lang="en">
  247. <head></head>
  248. <body>
  249. <style type="text/css">
  250. .tg {border-collapse:collapse;border-spacing:0;}
  251. .tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; overflow:hidden;padding:10px 5px;word-break:normal;}
  252. .tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px; font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}
  253. .tg .tg-0lax{text-align:left;vertical-align:top}
  254. </style>
  255. <script>
  256. function UpdateQueryString(key, value, url) {
  257. if (!url) url = window.location.href;
  258. var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi"),
  259. hash;
  260. if (re.test(url)) {
  261. if (typeof value !== 'undefined' && value !== null) {
  262. return url.replace(re, '$1' + key + "=" + value + '$2$3');
  263. }
  264. else {
  265. hash = url.split('#');
  266. url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '');
  267. if (typeof hash[1] !== 'undefined' && hash[1] !== null) {
  268. url += '#' + hash[1];
  269. }
  270. return url;
  271. }
  272. }
  273. else {
  274. if (typeof value !== 'undefined' && value !== null) {
  275. var separator = url.indexOf('?') !== -1 ? '&' : '?';
  276. hash = url.split('#');
  277. url = hash[0] + separator + key + '=' + value;
  278. if (typeof hash[1] !== 'undefined' && hash[1] !== null) {
  279. url += '#' + hash[1];
  280. }
  281. return url;
  282. }
  283. else {
  284. return url;
  285. }
  286. }
  287. }
  288. function chooseDate(object) {
  289. window.location.href = UpdateQueryString("date", object.value, window.location.href)
  290. }
  291. </script>
  292. <section id="about">
  293. <div class="container-fluid align-self-center">
  294. <div class="row d-flex justify-content-center">
  295. <div class="col-12 text-center align-self-center">
  296. <h1>{{.Date}}</h1>
  297. <input type="date" id="date" value="{{.Date}}" onchange="chooseDate(this)">
  298. <h2>Unique Sessions Today: {{.SessionCount}}</h2>
  299. <h3>Page Views</h3>
  300. {{range $Category, $URLS := .URLHits}}
  301. <h5> /{{$Category}}</h5>
  302. <table class="tg" style="undefined;table-layout: fixed; width: 320px">
  303. <colgroup>
  304. <col style="width: 70px">
  305. <col style="width: 250px">
  306. </colgroup>
  307. <thead>
  308. <tr>
  309. <th class="tg-0lax">Page Views</th>
  310. <th class="tg-0lax">URL</th>
  311. </tr>
  312. </thead>
  313. <tbody>
  314. {{range $URL, $count := $URLS}}
  315. <tr>
  316. <td class="tg-0lax">{{$count}} </td>
  317. <td class="tg-0lax">{{$URL}}</td>
  318. </tr>
  319. {{end}}
  320. </tbody>
  321. </table>
  322. {{ end }}
  323. </div>
  324. </div>
  325. </div>
  326. </section>
  327. </body>
  328. </html>
  329. {{ end }}
  330. `