analytics.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. Title string
  155. Content string
  156. CanonicalURL string
  157. FootingQuote string
  158. SessionCount int
  159. Date string
  160. URLHits map[string]map[string]int
  161. }
  162. type fileData struct {
  163. Date string
  164. Entries map[string][]action
  165. }
  166. type action struct {
  167. Page string
  168. Query string
  169. }
  170. func (a analytics) readSavedData(td time.Time) map[string][]action {
  171. fileName := a.Directory + td.Format("/2006/01/02/") + a.Name + td.Format("2006-01-02")
  172. entries := map[string][]action{}
  173. if _, err := os.Stat(fileName); os.IsNotExist(err) {
  174. } else {
  175. bs, err := ioutil.ReadFile(fileName)
  176. if err != nil {
  177. a.logger(err)
  178. return entries
  179. }
  180. r, err := zlib.NewReader(bytes.NewReader(bs))
  181. if err != nil {
  182. a.logger(err)
  183. return entries
  184. }
  185. jsonBytes := bytes.NewBuffer([]byte{})
  186. _, err = io.Copy(jsonBytes, r)
  187. if err != nil {
  188. a.logger(err)
  189. return entries
  190. }
  191. r.Close()
  192. err = json.Unmarshal(jsonBytes.Bytes(), &entries)
  193. if err != nil {
  194. a.logger(err)
  195. }
  196. }
  197. return entries
  198. }
  199. func (a analytics) insert(ip string, act action) {
  200. ts := time.Now().Format("2006-01-02")
  201. stamps := a.IPEntries[ts]
  202. if stamps == nil {
  203. a.IPEntries[ts] = map[string][]action{}
  204. }
  205. if len(a.HashIPSecret) > 0 {
  206. hash := sha256.New()
  207. ip = ts + ip + a.HashIPSecret
  208. inpIP := strings.NewReader(ip)
  209. if _, err := io.Copy(hash, inpIP); err != nil {
  210. a.logger(err)
  211. }
  212. sum := hash.Sum(nil)
  213. ip = string(sum)
  214. }
  215. entries := stamps[ip]
  216. if entries == nil {
  217. entries = []action{}
  218. }
  219. entries = append(entries, act)
  220. a.IPEntries[ts][ip] = entries
  221. }
  222. func (a analytics) writeFile() error {
  223. ts := time.Now().Format("/2006/01/02")
  224. err := os.MkdirAll(a.Directory+ts, os.ModePerm)
  225. if err != nil {
  226. return err
  227. }
  228. a.Mux.Lock()
  229. defer a.Mux.Unlock()
  230. for k, e := range a.IPEntries {
  231. data, err := json.Marshal(e)
  232. if err != nil {
  233. return err
  234. }
  235. f, err := os.Create(a.Directory + ts + "/" + a.Name + k)
  236. if err != nil {
  237. return err
  238. }
  239. var b bytes.Buffer
  240. w := zlib.NewWriter(&b)
  241. w.Write(data)
  242. w.Close()
  243. defer f.Close()
  244. _, err = f.Write(b.Bytes())
  245. if err != nil {
  246. return err
  247. }
  248. }
  249. return nil
  250. }
  251. const HTML = `
  252. {{ define "layout" }}
  253. <!DOCTYPE html>
  254. <html lang="en">
  255. <head></head>
  256. <body>
  257. <style type="text/css">
  258. .tg {border-collapse:collapse;border-spacing:0;}
  259. .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;}
  260. .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;}
  261. .tg .tg-0lax{text-align:left;vertical-align:top}
  262. </style>
  263. <script>
  264. function UpdateQueryString(key, value, url) {
  265. if (!url) url = window.location.href;
  266. var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi"),
  267. hash;
  268. if (re.test(url)) {
  269. if (typeof value !== 'undefined' && value !== null) {
  270. return url.replace(re, '$1' + key + "=" + value + '$2$3');
  271. }
  272. else {
  273. hash = url.split('#');
  274. url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '');
  275. if (typeof hash[1] !== 'undefined' && hash[1] !== null) {
  276. url += '#' + hash[1];
  277. }
  278. return url;
  279. }
  280. }
  281. else {
  282. if (typeof value !== 'undefined' && value !== null) {
  283. var separator = url.indexOf('?') !== -1 ? '&' : '?';
  284. hash = url.split('#');
  285. url = hash[0] + separator + key + '=' + value;
  286. if (typeof hash[1] !== 'undefined' && hash[1] !== null) {
  287. url += '#' + hash[1];
  288. }
  289. return url;
  290. }
  291. else {
  292. return url;
  293. }
  294. }
  295. }
  296. function chooseDate(object) {
  297. window.location.href = UpdateQueryString("date", object.value, window.location.href)
  298. }
  299. </script>
  300. <section id="about">
  301. <div class="container-fluid align-self-center">
  302. <div class="row d-flex justify-content-center">
  303. <div class="col-12 text-center align-self-center">
  304. <h1>{{.Date}}</h1>
  305. <input type="date" id="date" value="{{.Date}}" onchange="chooseDate(this)">
  306. <h2>Unique Sessions Today: {{.SessionCount}}</h2>
  307. <h3>Page Views</h3>
  308. {{range $Category, $URLS := .URLHits}}
  309. <h5> /{{$Category}}</h5>
  310. <table class="tg" style="undefined;table-layout: fixed; width: 320px">
  311. <colgroup>
  312. <col style="width: 70px">
  313. <col style="width: 250px">
  314. </colgroup>
  315. <thead>
  316. <tr>
  317. <th class="tg-0lax">Page Views</th>
  318. <th class="tg-0lax">URL</th>
  319. </tr>
  320. </thead>
  321. <tbody>
  322. {{range $URL, $count := $URLS}}
  323. <tr>
  324. <td class="tg-0lax">{{$count}} </td>
  325. <td class="tg-0lax">{{$URL}}</td>
  326. </tr>
  327. {{end}}
  328. </tbody>
  329. </table>
  330. {{ end }}
  331. </div>
  332. </div>
  333. </div>
  334. </section>
  335. </body>
  336. </html>
  337. {{ end }}
  338. `