def GetAllEvents(self): """Convenience HTML form for testing.""" accept_type = self.GetAcceptTypeHtmlOrJson() self.response.headers['Content-Type'] = accept_type event_list = events.GetEventList() if 'html' in accept_type: # TODO(jeff.carollo): Extract out to Django templates. self.response.out.write( """ <html><head><title>Events</title></head><body> <pre><code>%s</code><pre> </body></html> """ % '<br/>'.join([ json.dumps(model_to_dict.ModelToDict(event), indent=2) for event in event_list])) self.response.out.write('\n') return if 'json' in accept_type: response = dict() response['kind'] = 'mrtaskman#event_list' response['event_list'] = [ model_to_dict.ModelToDict(event) for event in event_list] json.dump(response, self.response.out, indent=2) self.response.out.write('\n') return # Should never get here. logging.error('Sending 500 because we could not determine a Content-Type.') self.response.out.write('Accept type not text/html or application/json.') self.response.set_status(500) return
def GetBugs(urls, limit=_MAX_RESULTS_CAP): """Returns a list of objects containing the mapping of url to bugs. TODO (jason.stredwick): Change the URL_BUG_MAP kind to isolate the break down of the url into components into a single result for a given url. Args: urls: A list or urls used to retrieve bugs. ([string]) limit: The max number of results to fetch. (integer) Returns: An object. ([{url: string, [kind.Kind.BUG]}]) Raises: Error: Raised if an error occurs accessing bug references. """ if limit > _MAX_RESULTS_CAP: limit = _MAX_RESULTS_CAP results = [] # For each url create a relevance mapping to related bugs. for url in urls: url_components = url_bug_map.PrepareUrl(url) results_dict = {} # Track which bugs have already been added. queries = GetQueriesForUrl(url_components) for (key, query) in queries: if not query: results.append({'url': key, 'bugs': []}) continue mappings = query.fetch(limit) if not mappings: results.append({'url': key, 'bugs': []}) continue result = [] keys = [] for mapping in mappings: try: bug_key = mapping.bug.key() id = bug_key.id() except Exception, e: raise Error(e) if id in results_dict: continue results_dict[id] = True keys.append(bug_key) if keys: try: result = db.get(keys) except Exception, e: raise Error(e) result = [model_to_dict.ModelToDict(r) for r in result if r] for r in result: r['kind'] = kind.Kind.BUG results.append({'url': key, 'bugs': result})
def get(self, executor): """Gets first scheduled (unassigned) task for a given executor.""" task = tasks.GetOldestTaskForCapability(executor) if task is None: self.response.set_status(404) return response = model_to_dict.ModelToDict(task) response['kind'] = 'mrtaskman#task' json.dump(response, self.response.out, indent=2) self.response.headers['Content-Type'] = 'application/json' self.response.out.write('\n')
def get(self, executor): """Lists tasks for a given executor.""" limit = self.request.get('limit', 1000) limit = int(limit) task_list = tasks.GetByExecutor(executor, limit) response = {} response['kind'] = 'mrtaskman#task_list' response['tasks'] = [ model_to_dict.ModelToDict(task) for task in task_list ] json.dump(response, self.response.out, indent=2) self.response.headers['Content-Type'] = 'application/json' self.response.out.write('\n')
def get(self, package_name, package_version): """Retrieves basic info about a package.""" package = packages.GetPackageByNameAndVersion(package_name, package_version) if not package: self.response.set_status(404) return package_files = packages.GetPackageFilesByPackageNameAndVersion( package_name, package_version) response = model_to_dict.ModelToDict(package) response['kind'] = 'mrtaskman#package' response_files = [] for package_file in package_files: file_info = model_to_dict.ModelToDict(package_file) file_info['kind'] = 'mrtaskman#file_info' del file_info['blob'] response_files.append(file_info) response['files'] = response_files self.response.headers['Content-Type'] = 'application/json' json.dump(response, self.response.out, indent=2) self.response.out.write('\n')
def get(self): name = self.request.get('name', None) if not name: self.response.out.write('Param "name" is required.') self.response.set_status(400) return name = name.decode('utf-8') task_list = tasks.GetByName(name) response = {} response['kind'] = 'mrtaskman#task_list' logging.info('tasks: %s', task_list) response['tasks'] = [ model_to_dict.ModelToDict(task) for task in task_list ] json.dump(response, self.response.out, indent=2) self.response.headers['Content-Type'] = 'application/json' self.response.out.write('\n')
def get(self, task_id): """Retrieves a single task given by task_id.""" counter.incr('Tasks.Get') # TODO(jeff.carollo): Specify request and response format.""" task_id = int(task_id) task = tasks.GetById(task_id) if task is None: counter.incr('Tasks.Get.404') self.error(404) return # Success. Write response. self.response.headers['Content-Type'] = 'application/json' response = model_to_dict.ModelToDict(task) response['kind'] = 'mrtaskman#task' json.dump(response, self.response.out, indent=2) self.response.out.write('\n') counter.incr('Tasks.Get.200')
def get(self, id): """Get a bug entry using the given id. Args: id: The id for the bug to retrieve. (integer) Raises: Error: The id did not match a stored bug. """ logging.info('Update bug handler; bugs.handlers.access.AccessHandler') id = int(id) try: bug_model = bug.Get(id) response = model_to_dict.ModelToDict(bug_model) response['kind'] = kind.Kind.BUG self.WriteResponse(bug_model) except bug.InvalidIdError: raise Error('Failed to find bug [id=%s].' % id, code=400) except base.Error, e: raise Error(e)
self.response.out.write('\n') self.response.set_status(400) return counters = counter.Batch() for executor_name in executor_capabilities: counters.incr('Executors.%s.Assign' % executor_name) counters.commit() task = tasks.Assign(worker, executor_capabilities) self.response.headers['Content-Type'] = 'application/json' if task is None: return response = model_to_dict.ModelToDict(task) response['kind'] = 'mrtaskman#task' json.dump(response, self.response.out, indent=2) self.response.out.write('\n') counter.incr('Tasks.Assign.200') class TaskCompleteUrlHandler(webapp2.RequestHandler): """In case our task complete URL expires.""" def get(self, task_id): counter.incr('Tasks.GetTaskCompleteUrl') try: task_id = int(task_id) except: counter.incr('Tasks.GetTaskCompleteUrl.400') self.response.set_status(400)
self.response.set_status(400) return try: event = events.CreateEvent(event) except events.ClientEventError, e: self.response.out.write('Error creating event: %s' % e.message) self.response.set_status(400) return except events.ServerEventError, e: self.response.out.write('Error creating event: %s' % e.message) self.response.set_status(500) return self.response.headers['Content-Type'] = 'application/json' response = model_to_dict.ModelToDict(event) response['kind'] = 'mrtaskman#event' json.dump(response, self.response.out, indent=2) self.response.out.write('\n') def GetAcceptTypeHtmlOrJson(self): """Parses Accept header and determines whether to send HTML or JSON. Defaults to 'application/json' unless HTML comes first in Accept line. Returns: Accept type as str. """ accept = self.request.headers.get('Accept', '') accepts = accept.split(';') accept = 'application/json'
urlfiles) except packages.DuplicatePackageError: self.DeleteAllBlobs(blob_infos) self.response.out.write('Package %s.%s already exists.\n' % (package_name, package_version)) self.response.set_status(400) return if not package: self.response.out.write( 'Unable to create package (unknown reason).\n') self.DeleteAllBlobs(blob_infos) self.response.set_status(500) self.response.headers['Content-Type'] = 'application/json' response = model_to_dict.ModelToDict(package) response['kind'] = 'mrtaskman#create_package_response' json.dump(response, self.response.out, indent=2) self.response.out.write('\n') def GetAcceptTypeHtmlOrJson(self): """Parses Accept header and determines whether to send HTML or JSON. Defaults to 'application/json' unless HTML comes first in Accept line. Returns: Accept type as str. """ accept = self.request.headers.get('Accept', '') accepts = accept.split(';') accept = 'application/json'