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): 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 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 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 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): 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 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)