-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalytics.go
146 lines (130 loc) · 3.79 KB
/
analytics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"encoding/json"
"errors"
"fmt"
// "gopkg.in/pg.v4"
"net/http"
)
type AnalyticsPage struct {
Page string
Referrer string
}
type AnalyticsIndex struct {
SummonerName string
Enemy string
Role string
RememberMe bool
Referral string // url
}
func (a *AnalyticsIndex) SetSummonerName(name string) {
a.SummonerName = NormalizeSummonerName(name)[0]
}
func (a *AnalyticsIndex) SetEnemy(name string) {
a.Enemy = NormalizeChampion(name)
}
type AnalyticsMatchup struct {
SummonerName string
Enemy string
Role string
Click string // url
}
func (a *AnalyticsMatchup) SetSummonerName(name string) {
a.SummonerName = NormalizeSummonerName(name)[0]
}
func (a *AnalyticsMatchup) SetEnemy(name string) {
a.Enemy = NormalizeChampion(name)
}
type AnalyticsExternalLink struct {
Url string
Page string // which page user was on
}
var createTableSql = []string{
`CREATE TABLE IF NOT EXISTS analytics_pages (
page text,
referrer text)`,
`CREATE TABLE IF NOT EXISTS analytics_indices (
summoner_name text,
enemy text,
role text,
remember_me boolean,
referral text)`,
`CREATE TABLE IF NOT EXISTS analytics_matchups (
summoner_name text,
enemy text,
role text,
click text)`,
`CREATE TABLE IF NOT EXISTS analytics_external_links (
url text,
page text)`,
}
func AnalyzeIndex(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
decoder := json.NewDecoder(r.Body)
var a AnalyticsIndex
err := decoder.Decode(&a)
if err != nil {
fmt.Println("Failed to decode index analyics data", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
a.SetSummonerName(a.SummonerName)
a.SetEnemy(a.Enemy)
fmt.Println(a)
err = db.Create(&a)
if err != nil {
fmt.Println("Failed to save index analyics", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
} else {
http.Error(w, errors.New("not found").Error(), http.StatusNotFound)
}
}
func AnalyzeMatchup(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
decoder := json.NewDecoder(r.Body)
var a AnalyticsMatchup
err := decoder.Decode(&a)
if err != nil {
fmt.Println("Failed to decode matchup analyics", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
a.SetSummonerName(a.SummonerName)
a.SetEnemy(a.Enemy)
fmt.Println(a)
err = db.Create(&a)
if err != nil {
fmt.Println("Failed to save matchup analyics", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
} else {
http.Error(w, errors.New("not found").Error(), http.StatusNotFound)
}
}
func AnalyzeExternalLink(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
decoder := json.NewDecoder(r.Body)
var a AnalyticsExternalLink
err := decoder.Decode(&a)
if err != nil {
fmt.Println("Failed to decode external link analyics", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
fmt.Println(a)
err = db.Create(&a)
if err != nil {
fmt.Println("Failed to save external link analyics", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
} else {
http.Error(w, errors.New("not found").Error(), http.StatusNotFound)
}
}