def get(self): id = int(self.request.url.rsplit('/', 1)[1]) org = Organization.get_by_id(id) newhearts = Heart.all().ancestor(org.key()).filter('title =', '').fetch(2000) flatlines = Flatline.all().filter("active =", True).fetch(2000) maintenancehearts = Heart.all().ancestor(org.key()).filter('maintenance_day !=', None).order('-maintenance_day').fetch(2000) self.response.headers['Content-Type'] = 'application/json' self.response.out.write(json.dumps({ 'newhearts': map(indextransform, newhearts), 'flatlines': map(flatlinetransform, flatlines), 'maintenancehearts': map(heartlisttransform, maintenancehearts) }))
def send_report(org, rangestart): hearts = Heart.all().ancestor(org.key()).filter('title !=', '').count() oldflatlines = Flatline.all().filter("start >", rangestart).order("-start").fetch(2000) oldflatlinesactive = Flatline.all().filter("end >", rangestart).order("-end").fetch(2000) oldflatlines = list(set(oldflatlines) | set(oldflatlinesactive)) alltime = hearts*24*60*60*7 if hearts > 0 else 1 downtime = sum(map(lambda x: x.seconds, map(lambda x: (x.end if x.end is not None else datetime.utcnow()) - (x.start if x.start < rangestart else rangestart),oldflatlines))) availablility = 1 - float(downtime)/alltime; message = mail.EmailMessage(sender="*****@*****.**", subject=org.title + " weekly report") message.to = map(lambda user: user, org.users) message.body = """ Here is the weekly report of how your hearts are doing. Availablility: """+str(availablility)+""" Downtime: """+str(downtime)+""" seconds Flatlines: """+str(len(oldflatlines))+""" Details for 7 days backwards can be found here: https://heartrate-monitor.appspot.com/app/organizations/"""+str(org.key().id())+"""/report Check out the current state of your hearts at: https://heartrate-monitor.appspot.com/app/organizations/"""+str(org.key().id())+""" Kind regards https://heartrate-monitor.appspot.com/app/ """ try: message.send() except: print 'failed to send mail' pass
def get(self): id = int(self.request.url.rsplit('/', 3)[1]) org = Organization.get_by_id(id) key = self.request.url.rsplit('/', 1)[1] heart = Heart.get_by_key_name(key, parent=org) flatlines = Flatline.all().ancestor(heart).order("-start").fetch(10) self.response.headers['Content-Type'] = 'application/json' self.response.out.write(json.dumps({'title': heart.title or heart.key().id_or_name(), 'time_zone': heart.time_zone, 'cron': heart.cron, 'threshold': heart.threshold, 'last_pulse': str(heart.last_pulse), 'flatlines': map(lambda f: {'start': str(f.start), 'end': str(f.end), 'active': str(f.active)}, flatlines)}))
def get(self): hearts = Heart.all().filter( 'last_pulse <', datetime.datetime.utcnow() - datetime.timedelta(minutes=5)).fetch(5000) for heart in hearts: heart.check_flatLine() self.response.write('ok')
def get(self): id = int(self.request.url.rsplit('/', 2)[1]) org = Organization.get_by_id(id) hearts = Heart.all().ancestor(org.key()).order("-created").fetch(2000) self.response.headers['Content-Type'] = 'application/json' self.response.out.write(json.dumps({ 'title': org.title, 'hearts': map(indextransform, hearts) }))
def delete(self): id = int(self.request.url.rsplit('/', 3)[1]) org = Organization.get_by_id(id) key = self.request.url.rsplit('/', 1)[1] heart = Heart.get_by_key_name(key, parent=org) flatlines = Flatline.all(keys_only=True).ancestor(heart).order("-start") heart.delete() for f in flatlines: db.delete(f)
def get(self): org_id = self.request.url.rsplit('/', 1)[1] if not org_id.isdigit(): self.abort(404) org = Organization.get_by_id(int(org_id)) if org is None: self.abort(404) newhearts = Heart.all().ancestor(org.key()).filter('title =', '').fetch(2000) maintenancehearts = Heart.all().ancestor(org.key()).filter('maintenance_day !=', None).order('-maintenance_day').fetch(2000) flatlines = Flatline.all().filter("active =", True).fetch(2000) self.response.headers['Content-Type'] = 'application/json' self.response.out.write(json.dumps({ 'title': org.title, 'newhearts': map(indextransform, newhearts), 'flatlines': map(flatlinetransform, flatlines), 'maintenancehearts': map(heartlisttransform, maintenancehearts), 'users': org.users, 'alert_email' : org.alert_email, }))
def get(self): id = int(self.request.url.rsplit('/', 2)[1]) org = Organization.get_by_id(id) hearts = Heart.all().ancestor(org.key()).order("-created").fetch(2000) # Activate hearts before deactivated onces hearts.sort( cmp=lambda x,y: -1 if x.threshold > 0 else 1 ) self.response.headers['Content-Type'] = 'application/json' self.response.out.write(json.dumps({ 'organization': indextransform( org ), 'hearts': map(heartlisttransform, hearts) }))
def get(self): id = int(self.request.url.rsplit('/', 1)[1]) org = Organization.get_by_id(id) newhearts = Heart.all().ancestor(org.key()).filter('title =', '').fetch(2000) flatlines = Flatline.all().filter("active =", True).fetch(2000) self.response.headers['Content-Type'] = 'application/json' self.response.out.write( json.dumps({ 'newhearts': map(indextransform, newhearts), 'flatlines': map(flatlinetransform, flatlines), }))
def put(self): payload = json.loads(self.request.body) id = int(self.request.url.rsplit('/', 3)[1]) org = Organization.get_by_id(id) key = self.request.url.rsplit('/', 1)[1] heart = Heart.get_by_key_name(key, parent=org) heart.title = str(payload['title']) heart.threshold = int(payload['threshold']) heart.cron = str(payload['cron']) heart.time_zone = str(payload['time_zone']) croniter(heart.cron) heart.put()
def get(self): id = int(self.request.url.rsplit('/', 1)[1]) org = Organization.get_by_id(id) newhearts = Heart.all().ancestor(org.key()).filter('title =', '').fetch(2000) flatlines = Flatline.all().filter("active =", True).fetch(2000) self.response.headers['Content-Type'] = 'application/json' self.response.out.write(json.dumps({ 'title': org.title, 'newhearts': map(indextransform, newhearts), 'flatlines': map(flatlinetransform, flatlines), 'users': org.users, 'alert_email' : org.alert_email }))
def put(self): payload = json.loads(self.request.body) id = int(self.request.url.rsplit('/', 3)[1]) org = Organization.get_by_id(id) key = self.request.url.rsplit('/', 1)[1] heart = Heart.get_by_key_name(key, parent=org) heart.title = str(payload['title']) heart.check_deactivation(int(payload['threshold'])) heart.threshold = int(payload['threshold']) heart.maintenance_day = datetime.strptime(payload['maintenance_day'], '%Y-%m-%d').date() if len( payload['maintenance_day'] ) > 0 else None heart.cron = str(payload['cron']) heart.time_zone = str(payload['time_zone']) croniter(heart.cron) heart.check_maintenance() heart.put()
def get(self): id = int(self.request.url.rsplit('/', 2)[1]) org = Organization.get_by_id(id) rangestart = datetime.utcnow() - timedelta(days=7) hearts = Heart.all().ancestor(org.key()).filter('title !=', '').count() oldflatlines = Flatline.all().filter("start >", rangestart).order("-start").fetch(2000) oldflatlinesactive = Flatline.all().filter("end >", rangestart).order("-end").fetch(2000) oldflatlines = list(set(oldflatlines) | set(oldflatlinesactive)) alltime = hearts*24*60*60*7 if hearts > 0 else 1 downtime = sum(map(lambda x: x.seconds, map(lambda x: (x.end if x.end is not None else datetime.utcnow()) - (x.start if x.start < rangestart else rangestart),oldflatlines))) self.response.headers['Content-Type'] = 'application/json' self.response.out.write(json.dumps({ 'organization': indextransform( org ), 'flatlines': map(flatlinetransform, oldflatlines), 'availablility' : 1 - float(downtime)/alltime, 'downtime' : downtime, 'hearts': hearts }))
def get(self): id = int(self.request.url.rsplit('/', 3)[1]) org = Organization.get_by_id(id) key = self.request.url.rsplit('/', 1)[1] heart = Heart.get_by_key_name(key, parent=org) flatlines = Flatline.all().ancestor(heart).order("-active").order("-start").order("-end").fetch(10) response = {'organization': indextransform(org), 'title': heart.title or heart.key().id_or_name(), 'time_zone': heart.time_zone, 'cron': heart.cron, 'threshold': heart.threshold, 'last_pulse': str(heart.last_pulse or ''), 'maintenance_day': str(heart.maintenance_day or ''), 'flatlines': map(flatlinetransform, flatlines), 'calculated_flatline': str(heart.calculate_next_flatline() or '') } self.response.headers['Content-Type'] = 'application/json' self.response.out.write(json.dumps(response))
def get(self): hearts = Heart.all().filter('last_pulse <', datetime.datetime.utcnow() - datetime.timedelta(minutes=5)).fetch(5000) for heart in hearts: heart.checkFlatLine() self.response.write('ok')
def testPulseQueueConsumed(self): pulse_time = datetime.utcnow() self.testapp.post('/pulse', {'id': 'testheart', 'org':str(self.org.key().id()), 'pulse_time': str(pulse_time) }) pulse = Heart.all().get() self.assertEqual(pulse.last_pulse, pulse_time)
def get_heart(self, id): return Heart.get_or_insert(id, parent=self)
def testPulseQueueConsumedWithWierdTime(self): pulse_time = datetime(2017,04,16,17,01,29) self.testapp.post('/pulse', {'id': 'testheart', 'org':str(self.org.key().id()), 'pulse_time': str('2017-04-16 17:01:29') }) pulse = Heart.all().get() self.assertEqual(pulse.last_pulse, pulse_time)