Esempio n. 1
0
 def post(self):
     record = prospective_search.get_document(self.request)
     record_value = simplejson.loads(record.value)
     subscriber_keys = map(db.Key, self.request.get_all("id"))
     subscribers = db.get(subscriber_keys)
     datapoints = []
     stats_json = []
     for subscriber_key, subscriber in zip(subscriber_keys, subscribers):
         if not subscriber:
             prospective_search.unsubscribe(stats.StatsRecord, subscriber_key)
         else:
             datapoints.append(models.getStats(subscriber.datapoint, record.timestamp, duration="day"))
             datapoints.append(models.getStats(subscriber.datapoint, record.timestamp, duration="hour"))
     for datapoint in datapoints:
         if datapoint.datapoint == "active_users":
             try:
                 user = models.getUser(record_value["user"], False)
             except models.UserDoesNotExistError:
                 continue
             last_seen = user.last_seen
             new_last_seen = timestamp.now()
             if datapoint.duration == "day":
                 last_seen = last_seen.replace(hour=0, minute=0, second=0, microsecond=0)
                 new_last_seen = new_last_seen.replace(hour=0, minute=0, second=0, microsecond=0)
             if last_seen < new_last_seen:
                 user.updateLastSeen(new_last_seen)
                 user.save()
             else:
                 continue
         if datapoint.datapoint == "quota":
             datapoint.count = models.getQuota().amount
         else:
             datapoint.increment()
         json = {
             "datapoint": datapoint.datapoint,
             "value": datapoint.count,
             "date": datapoint.date.strftime("%D, %M %d %y"),
             "datestamp": int(time.mktime(datapoint.date.date().timetuple())) * 1000,
             "hour": datapoint.date.hour,
         }
         if datapoint.duration == "day":
             json["hour"] = "total"
         stats_json.append(json)
     db.put(datapoints)
     push = channels.Channel("[email protected]/Web", False)
     push.message = {"stats": stats_json}
     push.send()
     logging.debug(simplejson.dumps(stats_json))
Esempio n. 2
0
 def get(self, duration='hour'):
     datapoints = [
         'registrations', 'links', 'opened_links', 'channels', 'devices',
         'connections', 'active_users', 'quota', 'payments'
     ]
     for datapoint in datapoints:
         datapoint = models.getStats(datapoint, duration=duration)
Esempio n. 3
0
 def get(self, duration="hour"):
     datapoints = [
         "registrations",
         "links",
         "opened_links",
         "channels",
         "devices",
         "connections",
         "active_users",
         "quota",
         "payments",
     ]
     for datapoint in datapoints:
         datapoint = models.getStats(datapoint, duration=duration)
Esempio n. 4
0
 def post(self):
     self.response.headers.add_header("Access-Control-Allow-Origin", "*")
     user = auth.getCurrentUser()
     response = {}
     if user:
         try:
             user_data = models.getUser(user)
         except models.UserDoesNotExistError:
             user_data = models.UserData(user=user).save()
         name = self.request.get('name')
         if not name:
             name = "Chrome"
         try:
             device = models.getDevice("%s/%s" % (user.email(), name))
         except models.DeviceDoesNotExistError:
             device = models.DeviceData(name=name, user=user_data).save()
         receiver = None
         if self.request.get("receiver"):
             try:
                 receiver = models.getDevice(
                     "%s/%s" % (user.email(), self.request.get("receiver")))
             except models.DeviceDoesNotExistError:
                 receiver = models.DeviceData(
                     name=self.request.get("receiver"),
                     user=user_data).save()
         if receiver == None:
             receiver = device
         link = models.LinkData(url=self.request.get('link'),
                                sender=device,
                                receiver=receiver).save()
         if models.getQuota().amount >= models.getStats(
                 'channels').count or user_data.immune():
             channel = channels.Channel(receiver.address, False)
             channel.sendLink(link)
             response['code'] = 200
             response['link'] = link.url
         else:
             response['code'] = 503
             response['link'] = link.url
     else:
         response['code'] = 401
         response['link'] = self.request.get('link')
     self.response.out.write(simplejson.dumps(response))
Esempio n. 5
0
 def __init__(self, address, generate=True, override_quota=False):
     self.address = address
     if generate:
         self.token = memcache.get("token_%s" % self.address)
         if self.token is None:
             device = models.getDevice(self.address)
             if device.token and device.tokenValid():
                 self.token = device.token
             else:
                 if models.getQuota().amount > models.getStats(
                         'channels').count or override_quota:
                     self.token = channel.create_channel(self.address)
                     stats.record("channel_created", simplejson.dumps({
                         "channel": self.address}))
                     self.cached = False
                     device.updateToken(self.token)
                 else:
                     raise OverQuotaError()
                     return False
             memcache.set("token_%s" % self.address, self.token, time=7200)
Esempio n. 6
0
 def __init__(self, address, generate=True, override_quota=False):
     self.address = address
     if generate:
         self.token = memcache.get("token_%s" % self.address)
         if self.token is None:
             device = models.getDevice(self.address)
             if device.token and device.tokenValid():
                 self.token = device.token
             else:
                 if models.getQuota().amount > models.getStats(
                         'channels').count or override_quota:
                     self.token = channel.create_channel(self.address)
                     stats.record(
                         "channel_created",
                         simplejson.dumps({"channel": self.address}))
                     self.cached = False
                     device.updateToken(self.token)
                 else:
                     raise OverQuotaError()
                     return False
             memcache.set("token_%s" % self.address, self.token, time=7200)
Esempio n. 7
0
 def post(self):
     self.response.headers.add_header("Access-Control-Allow-Origin", "*")
     user = auth.getCurrentUser()
     response = {}
     if user:
         try:
             user_data = models.getUser(user)
         except models.UserDoesNotExistError:
             user_data = models.UserData(user=user).save()
         name = self.request.get("name")
         if not name:
             name = "Chrome"
         try:
             device = models.getDevice("%s/%s" % (user.email(), name))
         except models.DeviceDoesNotExistError:
             device = models.DeviceData(name=name, user=user_data).save()
         receiver = None
         if self.request.get("receiver"):
             try:
                 receiver = models.getDevice("%s/%s" % (user.email(), self.request.get("receiver")))
             except models.DeviceDoesNotExistError:
                 receiver = models.DeviceData(name=self.request.get("receiver"), user=user_data).save()
         if receiver == None:
             receiver = device
         link = models.LinkData(url=self.request.get("link"), sender=device, receiver=receiver).save()
         if models.getQuota().amount >= models.getStats("channels").count or user_data.immune():
             channel = channels.Channel(receiver.address, False)
             channel.sendLink(link)
             response["code"] = 200
             response["link"] = link.url
         else:
             response["code"] = 503
             response["link"] = link.url
     else:
         response["code"] = 401
         response["link"] = self.request.get("link")
     self.response.out.write(simplejson.dumps(response))
Esempio n. 8
0
 def post(self):
     record = prospective_search.get_document(self.request)
     record_value = simplejson.loads(record.value)
     subscriber_keys = map(db.Key, self.request.get_all('id'))
     subscribers = db.get(subscriber_keys)
     datapoints = []
     stats_json = []
     for subscriber_key, subscriber in zip(subscriber_keys, subscribers):
         if not subscriber:
             prospective_search.unsubscribe(stats.StatsRecord,
                                            subscriber_key)
         else:
             datapoints.append(
                 models.getStats(subscriber.datapoint,
                                 record.timestamp,
                                 duration='day'))
             datapoints.append(
                 models.getStats(subscriber.datapoint,
                                 record.timestamp,
                                 duration='hour'))
     for datapoint in datapoints:
         if datapoint.datapoint == 'active_users':
             try:
                 user = models.getUser(record_value['user'], False)
             except models.UserDoesNotExistError:
                 continue
             last_seen = user.last_seen
             new_last_seen = timestamp.now()
             if datapoint.duration == "day":
                 last_seen = last_seen.replace(hour=0,
                                               minute=0,
                                               second=0,
                                               microsecond=0)
                 new_last_seen = new_last_seen.replace(hour=0,
                                                       minute=0,
                                                       second=0,
                                                       microsecond=0)
             if last_seen < new_last_seen:
                 user.updateLastSeen(new_last_seen)
                 user.save()
             else:
                 continue
         if datapoint.datapoint == 'quota':
             datapoint.count = models.getQuota().amount
         else:
             datapoint.increment()
         json = {
             'datapoint': datapoint.datapoint,
             'value': datapoint.count,
             'date': datapoint.date.strftime("%D, %M %d %y"),
             'datestamp':
             int(time.mktime(datapoint.date.date().timetuple())) * 1000,
             'hour': datapoint.date.hour
         }
         if datapoint.duration == "day":
             json['hour'] = "total"
         stats_json.append(json)
     db.put(datapoints)
     push = channels.Channel("[email protected]/Web", False)
     push.message = {"stats": stats_json}
     push.send()
     logging.debug(simplejson.dumps(stats_json))