Skip to content

Commit

Permalink
#39 Implemented statistics module with basic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
vlkv committed May 26, 2013
1 parent c41d086 commit 9d80565
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 73 deletions.
73 changes: 38 additions & 35 deletions reggata-stats/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,58 +4,61 @@
from uuid import uuid4


class Counter(ndb.Model):
name = ndb.StringProperty(indexed=False)
value = ndb.IntegerProperty(indexed=False)
dateTimeCreated = ndb.DateTimeProperty(auto_now_add=True)
class Event(ndb.Model):
appInstanceId = ndb.StringProperty(indexed=True)
name = ndb.StringProperty(indexed=True)
dateCreated = ndb.DateTimeProperty(auto_now_add=True)


class AppInstance(ndb.Model):
id = ndb.StringProperty(indexed=True)
dateCreated = ndb.DateTimeProperty(auto_now_add=True)

class InstanceId(ndb.Model):
uuid = ndb.StringProperty()
dateTimeCreated = ndb.DateTimeProperty(auto_now_add=True)


class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write("Request: \n" + str(self.request))

query = Counter.query()
counters = query.fetch()

counter = None
if len(counters) > 0:
counter = counters[0]
else:
counter = Counter()
counter.name = "startup"
counter.value = 0
counter.value = counter.value + 1
counter.put()


class Registrator(webapp2.RequestHandler):
class RegisterApp(webapp2.RequestHandler):
def get(self):
iid = InstanceId()
iid.uuid = str(uuid4())
iid.put()

ai = AppInstance()
ai.id = str(uuid4())
ai.put()
self.response.headers['Content-Type'] = 'text/plain'
self.response.write(iid.uuid)

#self.response.write("instance_id: " + str(self.request.get("instance_id", None)))
#q = InstanceId.query(InstanceId.uuid == uuid)
#ids = q.fetch()
#if len(ids) > 0:
# self.response.write(" Already registered")
#else:
# self.response.write(" Not registered")
self.response.write(ai.id)


class PutEvent(webapp2.RequestHandler):
def get(self):
appInstanceId = self.request.get("app_instance_id", None)
name = self.request.get("name", None)

self.response.headers['Content-Type'] = 'text/plain'
if appInstanceId is None:
self.response.write("app_instance_id argument is missing")
return
if name is None:
self.response.write("name argument is missing")
return
appInstances = AppInstance.query(AppInstance.id == appInstanceId).fetch()
if len(appInstances) == 0:
self.response.write("app_instance_id is not found in database. Application instance is not registered")
return
appInstanceId = appInstances[0].id

e = Event()
e.appInstanceId = appInstanceId
e.name = name
e.put()
self.response.write("ok")



application = webapp2.WSGIApplication([
('/', MainPage),
('/register', Registrator),
('/register_app', RegisterApp),
('/put_event', PutEvent),
], debug=True)
3 changes: 3 additions & 0 deletions reggata/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
DEFAULT_USER_LOGIN = "user"
DEFAULT_USER_PASSWORD = ""

#STATISTICS_SERVER = "http://reggata-stats.appspot.com"
STATISTICS_SERVER = "http://localhost:8080"


MAX_FILE_SIZE_FOR_FULL_HASHING = 100*1024*1024
MAX_BYTES_FOR_PARTIAL_HASHING = 10*1024*1024
1 change: 0 additions & 1 deletion reggata/errors.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
'''
Created on 17.10.2010
@author: vlkv
Expand Down
41 changes: 6 additions & 35 deletions reggata/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@
from PyQt4.QtGui import QApplication
from PyQt4.QtCore import QCoreApplication
import reggata.consts as consts
import reggata.helpers as hlp
import reggata.statistics as stats
from reggata.user_config import UserConfig
import reggata.reggata_dir_locator
import reggata.logging_default_conf as logging_default_conf
from reggata.gui.main_window import MainWindow
from gui.user_dialogs_facade import UserDialogsFacade
import urllib
import re


logger = logging.getLogger(consts.ROOT_LOGGER + "." + __name__)
Expand Down Expand Up @@ -79,19 +77,6 @@ def configureTranslations(app):
logger.warning("Cannot find translation file {}.".format(qm_filename))


def isReggataInstanceRegistered():
instanceId = UserConfig().get("reggata_instance_id")
return False if hlp.is_none_or_empty(instanceId) else True


def isUserGaveTheAnswerAboutSendStatistics():
sendStatistics = UserConfig().get("send_statistics")
return False if hlp.is_none_or_empty(sendStatistics) else True


def isSendStatisticsAllowed():
sendStatistics = UserConfig().get("send_statistics")
return hlp.stringToBool(sendStatistics)


def askUserAboutSendStatistics(mainWindow):
Expand All @@ -104,22 +89,7 @@ def askUserAboutSendStatistics(mainWindow):
elif res == QtGui.QMessageBox.No:
sendStatistics = False
if sendStatistics is not None:
UserConfig().store("send_statistics", sendStatistics)


def registerReggataInstance():
try:
timeoutSec = 5
with urllib.request.urlopen("http://reggata-stats.appspot.com/register", None, timeoutSec) as f:
response = f.read()
instanceId = response.decode("utf-8")
mobj = re.match(r"[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}", instanceId)
if mobj is None:
raise Exception("Server returned bad instance id, it doesn't look like UUID: " + instanceId)
UserConfig().store("reggata_instance_id", mobj.group(0))

except Exception as ex:
logger.warning("Could not register Reggata instance, reason: " + str(ex))
stats.setSendStatistics(sendStatistics)



Expand All @@ -141,12 +111,13 @@ def main():
mw = MainWindow()
mw.show()

if not isReggataInstanceRegistered() and not isUserGaveTheAnswerAboutSendStatistics():
if not stats.isReggataInstanceRegistered() and not stats.isUserGaveTheAnswerAboutSendStatistics():
askUserAboutSendStatistics(mw)

if isSendStatisticsAllowed() and not isReggataInstanceRegistered():
registerReggataInstance()
if stats.isSendStatisticsAllowed() and not stats.isReggataInstanceRegistered():
stats.registerReggataInstance()

stats.sendEvent("reggata_started")

app.exec_()
logger.info("========= Reggata finished =========")
Expand Down
1 change: 0 additions & 1 deletion reggata/memento.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
'''
Created on 27.07.2012
@author: vlkv
Expand Down
74 changes: 74 additions & 0 deletions reggata/statistics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'''
Created on 26.05.2013
@author: vlkv
'''
from threading import Thread
import urllib
import reggata.helpers as hlp
from reggata.user_config import UserConfig
from reggata import consts
import logging
import re


logger = logging.getLogger(consts.ROOT_LOGGER + "." + __name__)


def reggataInstanceId():
return UserConfig().get("reggata_instance_id")


def isReggataInstanceRegistered():
instanceId = reggataInstanceId()
return False if hlp.is_none_or_empty(instanceId) else True


def registerReggataInstance():
try:
timeoutSec = 5
with urllib.request.urlopen(consts.STATISTICS_SERVER + "/register_app", None, timeoutSec) as f:
response = f.read()
instanceId = response.decode("utf-8")
mobj = re.match(r"[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}", instanceId)
if mobj is None:
raise Exception("Server returned bad instance id, it doesn't look like UUID: " + instanceId)
UserConfig().store("reggata_instance_id", mobj.group(0))

except Exception as ex:
logger.warning("Could not register Reggata instance, reason: " + str(ex))


def isUserGaveTheAnswerAboutSendStatistics():
sendStatistics = UserConfig().get("send_statistics")
return False if hlp.is_none_or_empty(sendStatistics) else True


def setSendStatistics(sendStatistics):
UserConfig().store("send_statistics", sendStatistics)


def isSendStatisticsAllowed():
sendStatistics = UserConfig().get("send_statistics")
return hlp.stringToBool(sendStatistics)


def _sendEvent(instanceId, name):
print("_sendEvent started")
timeoutSec = 5
with urllib.request.urlopen(consts.STATISTICS_SERVER + "/put_event?app_instance_id={}&name={}"
.format(instanceId, name),
None, timeoutSec) as f:
response = f.read()
# TODO: remove this print call
print(str(response))
print("_sendEvent done")

def sendEvent(name):
if not isSendStatisticsAllowed():
return
instanceId = reggataInstanceId()
if hlp.is_none_or_empty(instanceId):
return
t = Thread(target=_sendEvent, args=(instanceId, name))
t.start()

1 change: 0 additions & 1 deletion reggata/user_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
'''
Created on 16.10.2010
@author: vlkv
Expand Down

0 comments on commit 9d80565

Please sign in to comment.