/
test.go
148 lines (118 loc) 路 3.5 KB
/
test.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
147
148
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package controllers
import (
"encoding/base64"
"encoding/json"
"fmt"
"strings"
"github.com/astaxie/beego"
"github.com/microsoft/mouselog/detect"
"github.com/microsoft/mouselog/object"
)
type ApiController struct {
beego.Controller
}
type response struct {
Status string `json:"status"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
func base64Decode(src []byte) ([]byte, error) {
return base64.StdEncoding.DecodeString(string(src))
}
func (c *ApiController) UploadTrace() {
var resp response
websiteId := c.Input().Get("websiteId")
sessionId := c.Input().Get("sessionId")
impressionId := c.Input().Get("impressionId")
userAgent := c.getUserAgent()
clientIp := c.getClientIp()
userId := c.Input().Get("userId")
queryConfig := c.Input().Get("queryConfig")
beegoSessionId := c.StartSession().SessionID()
if sessionId == "" {
sessionId = beegoSessionId
}
website := object.GetWebsite(websiteId)
if website == nil {
resp = response{Status: "ok", Msg: "this website is not monitored", Data: ""}
c.Data["json"] = resp
c.ServeJSON()
return
}
trackConfig := object.ParseTrackConfig(website.TrackConfig)
if queryConfig == "1" {
resp = response{Status: "ok", Msg: "config", Data: trackConfig}
c.Data["json"] = resp
c.ServeJSON()
return
}
var data []byte
if c.Ctx.Request.Method == "GET" {
data = []byte(c.Input().Get("data"))
} else { // Method == "POST"
data = c.Ctx.Input.RequestBody
}
if trackConfig.Encoder == "base64" {
var err error
data, err = base64Decode(data)
if err != nil {
panic(err)
}
}
var t object.Trace
t.LoadFromJson(data)
referrer := t.Referrer
isDashboardUser := strings.HasPrefix(referrer, "http://localhost") || strings.HasPrefix(referrer, "https://mouselog.org")
fmt.Printf("%s, %s, %s, %s, isDashboardUser=%v\n", websiteId, sessionId, impressionId, referrer, isDashboardUser)
if isDashboardUser {
resp = response{Status: "ok", Msg: "dashboard user is not monitored", Data: ""}
c.Data["json"] = resp
c.ServeJSON()
return
}
object.AddSession(sessionId, websiteId, userAgent, clientIp, userId)
object.AddImpression(impressionId, sessionId, websiteId, userId, &t)
object.AppendTraceToImpression(impressionId, websiteId, &t)
// Only return traces for test page for visualization (websiteId == "mouselog")
if websiteId != "mouselog" {
msg := fmt.Sprintf("%d events received", len(t.Events))
resp = response{Status: "ok", Msg: msg, Data: ""}
c.Data["json"] = resp
c.ServeJSON()
return
}
ss, _ := GetOrCreateDataset(sessionId)
if len(t.Events) > 0 {
fmt.Printf("Read event [%s]: (%d, %f, %d, %d)\n", sessionId, t.Id, t.Events[0].Timestamp, t.Events[0].X, t.Events[0].Y)
} else {
fmt.Printf("Read event [%s]: (%d, <empty>)\n", sessionId, t.Id)
}
if len(t.Events) != 0 {
ss.AddTrace(&t)
}
c.Data["json"] = detect.GetDetectResult(ss, t.Id)
c.ServeJSON()
}
func (c *ApiController) ClearTrace() {
sessionId := c.StartSession().SessionID()
data := c.Ctx.Input.RequestBody
var t object.Trace
err := json.Unmarshal(data, &t)
if err != nil {
panic(err)
}
ss, _ := GetOrCreateDataset(sessionId)
if t2, ok := ss.TraceMap[t.Id]; ok {
delete(ss.TraceMap, t.Id)
for i, t3 := range ss.Traces {
if t2 == t3 {
ss.Traces = append(ss.Traces[:i], ss.Traces[i+1:]...)
}
}
}
fmt.Printf("Clear event [%s]: (%s, <empty>)\n", sessionId, t.Id)
c.Data["json"] = detect.GetDetectResult(ss, t.Id)
c.ServeJSON()
}