import webapp2
import csv
import time

from google.appengine.api import logservice

class Level1Handler(webapp2.RequestHandler):
  def get(self):
    self.response.headers['content-type'] = 'text/plain'    
    # Create a csv writer that outputs to the HTTP response
    w = csv.writer(self.response)
    # Fetch last 5 minutes of request logs
    for r in logservice.fetch(start_time=time.time()-5*60):
      # Render them as plain text in CSV format in the response body
      w.writerow([r.start_time,r.method,r.resource,
                  r.status,r.latency,r.response_size,
                  r.user_agent if r.user_agent else "NULL"])

app = webapp2.WSGIApplication([('/solution/level1', Level1Handler)], debug=True)