def ping_site(site): """ Utility function which, when passed a site object, sends an HTTP GET request and logs the response """ try: # first off make a request response = fetch(url=site.url, allow_truncated=True) # log everything to the debug log logging.debug("Made request for %s and got %s" % (site.url, response.status_code)) # record the event ping = Ping( site = site, code = response.status_code, ) # then do special things based on the request response if response.status_code == '404': logging.error("Page not found at %s" % site.url) ping.up = False elif response.status_code == '500': logging.error("Error on %s" % site.url) ping.up = False # save the event ping.put() except DownloadError, e: # we also want to watch out for if the site doesn't # respond at all ping = Ping( site = site, code = 408, # 408 is request timeout up = False, ) ping.put() logging.error("Error accessing %s: %s" % (site.url, e))
def get(self): user = User.get_by_id(users.get_current_user().user_id()) urlsafe_key = self.request.get('page') tqx = self.request.get('tqx') try: logging.info('Get page by urlsafe %s' % urlsafe_key) key = ndb.Key(urlsafe=urlsafe_key) page = key.get() except: logging.error('Error generating Page object from key.id().') page = None if page: if page.user == user.key: description = { 'date': ('datetime', 'Fecha'), 'resp_time': ('number', 'Tiempo de respuesta'), } data = [] # Query the data query = Ping.query(Ping.page == page.key) query = query.order(-Ping.date) # fetch 288 pings representing the last 24 hrs pings = query.fetch(288) for ping in pings: entry = { 'date': ping.date, 'resp_time': ping.resp_time, } data.append(entry) data_table = gviz_api.DataTable(description) data_table.LoadData(data) self.response.headers['Content-Type'] = 'text/plain' self.response.out.write(data_table.ToJSonResponse( columns_order=('date', 'resp_time'), order_by="date", req_id=tqx[6:] if tqx else 0)) else: # Return 401 Unauthorized logging.error('Unauthorized') self.response.set_status(401) self.response.out.write('Unauthorized') else: # Return 404 Not Found logging.error('Not Found') self.response.set_status(404) self.response.out.write('Not Found')
def save_ping(): ping = Ping() ping.page = page.key ping.resp_time = resp_time ping.resp_code = result.status_code ping.put() # Update Last Ping data page.last_ping = datetime.now() page.put()
def get(self): current_user = users.get_current_user() template_args = {} # Make sure a User entity gets created. if current_user: user = User.get_by_id(current_user.user_id()) if not user: self.redirect('/register') else: q = Page.query(ancestor=user.key) q = q.order(Page.name) pages = q.fetch(10) # Issue asynchronous fetch operations per page for page in pages: query = Ping.query(Ping.page == page.key) query = query.order(-Ping.date) # fetch 288 pings representing the last 24 hrs page.pings_future = query.fetch_async(288) # Reading fetch results and calculate avg latency for page in pages: pings = page.pings_future.get_result() total_resp_time = 0 if pings: for p in pings: total_resp_time += p.resp_time page.avg_resp_time = total_resp_time/len(pings) template_args.update({ 'pages': pages, }) self.render_template('index.html', **template_args)
def write(): new_timestamp = Ping(timestamp=str(time.time())) db.session.add(new_timestamp) db.session.commit() return