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