1337.
This commit is contained in:
parent
c1e0f353f8
commit
eb4ec7208f
@ -35,34 +35,34 @@ class BaseView(web.View):
|
|||||||
value=json.dumps(value,default=str),
|
value=json.dumps(value,default=str),
|
||||||
ip=self.ip,
|
ip=self.ip,
|
||||||
created=datetime.now()
|
created=datetime.now()
|
||||||
),['session_id','key'])
|
),['id','key'])
|
||||||
return record
|
return record
|
||||||
|
|
||||||
def get(self,key,default=None):
|
def get(self,key,default=None):
|
||||||
try:
|
try:
|
||||||
return json.loads(self.db['session'].find_one(dict(session_id=self.session_id,key=key))['value'])
|
return json.loads(self.db['session'].find_one(dict(ip=self.ip,key=key))['value'])
|
||||||
except:
|
except:
|
||||||
return default
|
return default
|
||||||
|
|
||||||
def insert(self, table, data):
|
def insert(self, table, data):
|
||||||
data['session_id'] = self.session_id
|
data['ip'] = self.ip
|
||||||
data['created'] = datetime.now()
|
data['created'] = datetime.now()
|
||||||
return self.db[table].insert(data)
|
return self.db[table].insert(data)
|
||||||
|
|
||||||
def find(self,table,data=None):
|
def find(self,table,data=None):
|
||||||
if not data:
|
if not data:
|
||||||
data = {}
|
data = {}
|
||||||
data['session_id'] = self.session_id
|
data['ip'] = self.ip
|
||||||
return [dict(d) for d in self.db[table].find(**data)]
|
return [dict(d) for d in self.db[table].find(**data)]
|
||||||
|
|
||||||
class EventView(BaseView):
|
class EventView(BaseView):
|
||||||
|
|
||||||
document_fields = ['html','title','html','domain','href']
|
document_fields = ['title','html','domain','href']
|
||||||
|
|
||||||
event_fields = ['target','event','tag','scroll_height','scroll_left','scroll_top','client_width','client_height','page_x','page_y','screen_x','screen_y','client_x','client_y','target_x','target_y']
|
event_fields = ['target','event','tag','scroll_height','scroll_left','scroll_top','client_width','client_height','page_x','page_y','screen_x','screen_y','client_x','client_y','target_x','target_y']
|
||||||
|
|
||||||
def is_document_record(self, data):
|
def is_document_record(self, data):
|
||||||
return self.data.get('html') and True or False
|
return data.get('html') and True or False
|
||||||
|
|
||||||
def get_record(self, data):
|
def get_record(self, data):
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ class EventView(BaseView):
|
|||||||
for field in self.document_fields:
|
for field in self.document_fields:
|
||||||
if field in data:
|
if field in data:
|
||||||
record[field] = data[field]
|
record[field] = data[field]
|
||||||
if len(record.keys()) == len(document_fields):
|
if len(record.keys()) == len(self.document_fields):
|
||||||
return record
|
return record
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
@ -81,7 +81,7 @@ class EventView(BaseView):
|
|||||||
for field in self.event_fields:
|
for field in self.event_fields:
|
||||||
if field in data:
|
if field in data:
|
||||||
record[field] = data[field]
|
record[field] = data[field]
|
||||||
if len(record.keys()) == len(event_fields):
|
if len(record.keys()) == len(self.event_fields):
|
||||||
return record
|
return record
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
@ -90,7 +90,7 @@ class EventView(BaseView):
|
|||||||
|
|
||||||
async def get(self):
|
async def get(self):
|
||||||
ws = web.WebSocketResponse()
|
ws = web.WebSocketResponse()
|
||||||
await ws.prepare(request)
|
await ws.prepare(self.request)
|
||||||
|
|
||||||
html = None
|
html = None
|
||||||
visit_id = self.uid()
|
visit_id = self.uid()
|
||||||
@ -100,11 +100,18 @@ class EventView(BaseView):
|
|||||||
if msg.type == web.WSMsgType.TEXT:
|
if msg.type == web.WSMsgType.TEXT:
|
||||||
# Echo the received message back to the client
|
# Echo the received message back to the client
|
||||||
try:
|
try:
|
||||||
data = data.json()
|
data = msg.json()
|
||||||
except:
|
except Exception as ex:
|
||||||
|
print(ex)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
original_data = data
|
||||||
data = self.get_record(data)
|
data = self.get_record(data)
|
||||||
|
if not data:
|
||||||
|
print("Not a valid record:", original_data)
|
||||||
|
exit(1)
|
||||||
|
continue
|
||||||
|
|
||||||
if self.is_document_record(data):
|
if self.is_document_record(data):
|
||||||
html = data.get('html')
|
html = data.get('html')
|
||||||
record = dict(
|
record = dict(
|
||||||
@ -118,9 +125,11 @@ class EventView(BaseView):
|
|||||||
self.app.view_count += 1
|
self.app.view_count += 1
|
||||||
else:
|
else:
|
||||||
data['visit_id'] = visit_id
|
data['visit_id'] = visit_id
|
||||||
|
record = data
|
||||||
self.insert('event', record)
|
self.insert('event', record)
|
||||||
self.app.event_count += 1
|
self.app.event_count += 1
|
||||||
|
|
||||||
|
print(record)
|
||||||
print(self.app.view_count, self.app.event_count)
|
print(self.app.view_count, self.app.event_count)
|
||||||
|
|
||||||
elif msg.type == web.WSMsgType.ERROR:
|
elif msg.type == web.WSMsgType.ERROR:
|
||||||
@ -134,19 +143,15 @@ class Application(BaseApplication):
|
|||||||
def __init__(self, db_path, *args, **kwargs):
|
def __init__(self, db_path, *args, **kwargs):
|
||||||
self.view_count = 0
|
self.view_count = 0
|
||||||
self.event_count = 0
|
self.event_count = 0
|
||||||
super().__init__(db_path=db_path, *args, **kwargs)
|
|
||||||
|
self.template_path = pathlib.Path(__file__).parent.joinpath("templates")
|
||||||
|
super().__init__(template_path=self.template_path,db_path=db_path, *args, **kwargs)
|
||||||
self.router.add_get("/", self.index_handler)
|
self.router.add_get("/", self.index_handler)
|
||||||
self.router.add_view("/event", EventView)
|
self.router.add_view("/event", EventView)
|
||||||
self.router.add_static("/static", pathlib.Path(__file__).parent.joinpath("static"))
|
self.router.add_static("/static", pathlib.Path(__file__).parent.joinpath("static"))
|
||||||
|
|
||||||
|
|
||||||
async def index_handler(self, request):
|
async def index_handler(self, request):
|
||||||
return web.Response(
|
|
||||||
text="\n".join([
|
return await self.render_template("index.html",request, dict(view_count=self.view_count,event_count=self.event_count))
|
||||||
"\n".join([
|
|
||||||
"Under the spreading chestnut tree",
|
|
||||||
"I sold you, and you sold me",
|
|
||||||
"There lie they, and here lie we,",
|
|
||||||
"Under the spreading chestnut tree."
|
|
||||||
]),
|
|
||||||
])
|
|
||||||
,content_type='text/plain')
|
|
||||||
|
@ -5,13 +5,13 @@ class Metriki {
|
|||||||
_isConnected = false
|
_isConnected = false
|
||||||
_isRendered = false
|
_isRendered = false
|
||||||
|
|
||||||
set isRendered() {
|
set isRendered(value) {
|
||||||
|
|
||||||
this._isRendered = true
|
this._isRendered = value
|
||||||
this.mount()
|
this.mount()
|
||||||
}
|
}
|
||||||
set isConnected() {
|
set isConnected(value) {
|
||||||
this._isConnected = true
|
this._isConnected = value
|
||||||
this.mount()
|
this.mount()
|
||||||
}
|
}
|
||||||
get isRendered() {
|
get isRendered() {
|
||||||
@ -22,10 +22,11 @@ class Metriki {
|
|||||||
}
|
}
|
||||||
mount() {
|
mount() {
|
||||||
if(this.isRendered && this.isConnected) {
|
if(this.isRendered && this.isConnected) {
|
||||||
this.ws.send("render")
|
this.emitVisit()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
emit(data){
|
emit(data){
|
||||||
|
console.info(data)
|
||||||
this.ws.send(JSON.stringify(data))
|
this.ws.send(JSON.stringify(data))
|
||||||
}
|
}
|
||||||
emitVisit(){
|
emitVisit(){
|
||||||
@ -37,6 +38,13 @@ class Metriki {
|
|||||||
document.addEventListener('mousemove', (e) => {
|
document.addEventListener('mousemove', (e) => {
|
||||||
me.emit({
|
me.emit({
|
||||||
"event":"mousemove",
|
"event":"mousemove",
|
||||||
|
"tag":e.target.tagName,
|
||||||
|
"scroll_height":document.documentElement.scrollHeight,
|
||||||
|
"scroll_left":document.documentElement.scrollLeft,
|
||||||
|
"scroll_top":document.documentElement.scrollTop,
|
||||||
|
"client_width":document.documentElement.clientWidth,
|
||||||
|
"client_height":document.documentElement.clientHeight,
|
||||||
|
"target":e.target.id || e.target.className || e.target.tagName,
|
||||||
"page_x":e.pageX,
|
"page_x":e.pageX,
|
||||||
"page_y":e.pageY,
|
"page_y":e.pageY,
|
||||||
"screen_x":e.screenX,
|
"screen_x":e.screenX,
|
||||||
@ -50,6 +58,7 @@ class Metriki {
|
|||||||
document.addEventListener('scroll', (e) => {
|
document.addEventListener('scroll', (e) => {
|
||||||
me.emit({
|
me.emit({
|
||||||
"event":"scroll",
|
"event":"scroll",
|
||||||
|
"target":e.target.id || e.target.className || e.target.tagName,
|
||||||
"scroll_height":document.documentElement.scrollHeight,
|
"scroll_height":document.documentElement.scrollHeight,
|
||||||
"scroll_left":document.documentElement.scrollLeft,
|
"scroll_left":document.documentElement.scrollLeft,
|
||||||
"scroll_top":document.documentElement.scrollTop,
|
"scroll_top":document.documentElement.scrollTop,
|
||||||
@ -66,12 +75,12 @@ class Metriki {
|
|||||||
}
|
}
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.url = window.location.href.replace("http://", "").replace("https://", "").replace("www.", "")
|
this.url = window.location.href.replace("http://", "ws://").replace("https://", "wss://").replace("www.", "")
|
||||||
if this.url.endswith("/"):
|
if(this.url.endsWith("/")){
|
||||||
this.url += "event"
|
this.url += "event"
|
||||||
esle
|
}else{
|
||||||
this.url += "/event"
|
this.url += "/event"
|
||||||
|
}
|
||||||
console.log(this.url)
|
console.log(this.url)
|
||||||
const me = this
|
const me = this
|
||||||
this.ws = new WebSocket(this.url)
|
this.ws = new WebSocket(this.url)
|
||||||
|
Loading…
Reference in New Issue
Block a user