def HandleGetAggregate(request): """Juno request handler to retrieve Aggregate Projections. Get Parameters: taba - Taba Name to retrieve Aggregates for, or blank for all Taba Names. block - Taba Name Block ID to retrieve Aggregates for, or blank for all. Response: Aggregate Projections, serialized depending on the Accept MIME type. Accept Types: text/plain (default) - Human readable new-line separated. text/json - JSON serialized. """ # Parse and validate query parameters. name = request.input('taba') block = request.input('block') if name and block: juno.status(400) juno.append('Cannot specify both "taba" and "block"') return names = [name] if name else None blocks = block.split(',') if block else None return _AggregateResponse(names, blocks, _GetAccept(request))
def HandleGetProjection(request): """Juno request handler to retrieve raw Projection(s) from the DB. Get Parameters: client - Client ID to get Projection(s) for, or blank for all Clients IDs. taba - Taba Name to get Projection(s) for, or blank for all Taba Names. block - Taba Name Block to get Projections for, or blank for all. Response: Projection objects, serialized depending on the Accept MIME type. Accept Types: text/plain (default) - Human readable new-line separated. text/json - JSON serialized. """ # Parse and validate query parameters. client_id = request.input('client') name = request.input('taba') block = request.input('block') if name and block: juno.status(400) juno.append('Cannot specify both "taba" and "block"') return blocks = block.split(',') if block else None names = [name] if name else None return _ProjectionResponse(client_id, names, blocks, _GetAccept(request))
def HandleGetTabaNames(request): """Juno request handler to retrieve Taba Names. Get Parameters: client - Client ID to retrieve registered Taba Names for. If blank, then the Taba Names entry for all Clients is retrieved. Response: List of Taba Names, serialized depending on the Accept MIME type. Accept Types: text/plain (default) - Human readable new-line separated. text/json - JSON serialized list. """ req_client_id = request.input('client') if not req_client_id: names = global_taba_server.GetNames() else: names = global_taba_server.GetNamesForClient(req_client_id) # Render the Taba Names according to the requested format. accept = request.raw.get('HTTP_ACCEPT') or 'text/plain' accept = accept.lower() if accept == 'text/json': juno.append(cjson.encode(names)) else: juno.append('\n'.join(names))
def HandleGetType(request): """Juno request handler to retrieve the Taba Type for a Taba Name. Get Parameters: taba - (Required) The Taba Name to retrieve the Taba Type for. Response: Taba Type, serialized depending on the Accept MIME type. Accept Types: text/plain (default) - Human readable. text/json - JSON serialized. """ name = request.input('taba') if name: names = [name] else: names = None types = global_taba_server.GetTabaTypes(names) # Render the types depending on the Accept header. accept = request.raw.get('HTTP_ACCEPT') or 'text/plain' accept = accept.lower() if accept == 'text/json': juno.append(cjson.encode(dict(types))) else: for type in types: juno.append('%s: %s\n' % type)
def HandleGetAggretateBatch(request): """Juno request handler to retrieve a batch of Aggregates from the DB. Post Body - a JSON dictionary with the following fields: taba - List of Taba Name to retrieve Aggregates for, or blank for all. block - List of Taba Name Block IDs to retrieve, or blank for all. Response: Aggregate Projections, serialized depending on the Accept MIME type. Accept Types: text/plain (default) - Human readable new-line separated. text/json - JSON serialized. """ # Parse and validate query parameters. body_enc = request.raw['request_body_bytes'].read() params = cjson.decode(body_enc) names = params['taba'] if 'taba' in params else None blocks = params['block'] if 'block' in params else None if names and blocks: juno.status(400) juno.append('Cannot specify both "taba" and "block"') return return _AggregateResponse(names, blocks, _GetAccept(request))
def HandleGetRawBatch(request): """Juno request handler to retrieve a batch of raw States from the DB by Name. Post Body - a JSON dictionary with the following fields: client - Client ID to get State objects or, or blank for all. taba - List of Taba Names to get State objects for, or blank for all. block - List of Taba Name Block IDs to get State objects, or blank for all. Response: State objects, serialized depending on the Accept MIME type. Accept Types: text/plain (default) - Human readable new-line separated. text/json - JSON serialized. """ # Parse and validate query parameters. body_enc = request.raw['request_body_bytes'].read() params = cjson.decode(body_enc) client_id = params['client'] if 'client' in params else None names = params['taba'] if 'taba' in params else None blocks = params['block'] if 'block' in params else None if names and blocks: juno.status(400) juno.append('Cannot specify both "names" and "block"') return return _RawResponse(client_id, names, blocks, _GetAccept(request))
def HandleUpgrade(request): """Juno request handler to start a State Upgrade process. Spawns a background greenlet to perform the operation and returns immediately. """ force = request.input('force') if not force: force = False gevent.spawn(global_taba_server.Upgrade, force) juno.append("Started\n")
def _RawResponse(clients_param, names_param, blocks, accept): """Retrieve raw State results, and add them to the response. Args: clients_param - Param object containing either client_id or client glob to retrieve State objects for. names_param - Param object containing either List of Taba Names, or a names_glob to retrieve State objects for. blocks - List of Taba Name Block IDs to retrieve State objects for. accept - MIME type in which to format the output. """ clients, names = _GetClientsAndNames(clients_param, names_param) if (clients != None and len(clients) == 0) \ or (names != None and len(names) == 0): states = [] else: # Get the requested State objects. states = global_taba_server.GetStates(clients, names, blocks) # Format the response in accordance with the Accept header. if accept == 'text/json': juno.append(cjson.encode(dict([s for s in states]))) else: for (state_client_id, state_name), state in states: juno.append("(").append(state_client_id).append(", ") juno.append(state_name).append("): ") juno.append(cjson.encode(state)).append("\n")
def _ProjectionResponse(clients_param, names_param, blocks, accept): """Retrieve Projection results, and add them to the response. Args: clients_param - Param object containing either client_id or client glob to retrieve Projections for. names_param - Param object containing either List of Taba Names, or a names_glob to retrieve Projections for. blocks - List of Taba Name Block IDs to retrieve Projections for. accept - MIME type in which to format the output. """ clients, names = _GetClientsAndNames(clients_param, names_param) if (clients != None and len(clients) == 0) \ or (names != None and len(names) == 0): projections = [] else: # Retrieve the requested Projections. projections = global_taba_server.GetProjections(clients, names, blocks) # Render the Projection objects according to the requested format. if accept == 'text/json': juno.append(cjson.encode(dict([p for p in projections]))) else: for (proj_client_id, proj_name), projection in projections: juno.append("(").append(proj_client_id).append(", ") juno.append(proj_name).append("): ") juno.append(cjson.encode(projection)).append("\n")
def HandleStatus(request): """Juno request handler for retrieving the status of the Taba Agent. The status is returned in the body of the response as a JSON string. """ accept = request.raw.get('HTTP_ACCEPT') or 'text/json' accept = accept.lower() agent_status = global_taba_agent.Status() if accept == 'text/plain': for key, val in agent_status.iteritems(): juno.append('%s: %s\n' % (key, val)) else: juno.append(cjson.encode(agent_status))
def HandleDeleteName(request): """Juno request handler to delete all States for a Taba Name. Get Parameters: taba - (Required) The Taba Name to delete States for. """ name = request.input('taba') if not name: juno.status(400) juno.append('Must specify "taba" parameter') return client_id = request.input('client') LOG.info("Starting Delete Taba (%s, %s)" % (client_id, name)) global_taba_server.DeleteTaba(name, client_id)
def HandleStatus(request): """Juno request handler to retrieve the taba Server status. Accept Types: text/plain (default) - Human readable new-line separated. text/json - JSON serialized. """ accept = request.raw.get('HTTP_ACCEPT') or 'text/json' accept = accept.lower() agent_status = global_taba_server.Status() if accept == 'text/plain': for key, val in agent_status.iteritems(): juno.append('%s: %s\n' % (key, val)) else: juno.append(cjson.encode(agent_status))
def HandleGetClients(request): """Juno request handler to retrieve all the Client IDs. Response: List of Client IDs, serialized depending on the Accept MIME type. Accept Types: text/plain (default) - Human readable new-line separated. text/json - JSON serialized list. """ clients = global_taba_server.GetClients() # Render the Client IDs according to the requested format. accept = request.raw.get('HTTP_ACCEPT') or 'text/plain' accept = accept.lower() if accept == 'text/json': juno.append(cjson.encode(clients)) else: juno.append('\n'.join(clients))
def _AggregateResponse(names, blocks, accept): """Retrieve Aggregate results, and add them to the response. Args: names - List of Taba Names to retrieve Aggregates for. blocks - List of Taba Name Block IDs to retrieve Aggregates for. accept - MIME type in which to format the output. """ names = names_param.value if names_param.isGlob: all_names = [] names = fnmatch.filter(global_taba_server.GetNames(), names_param.value) if names != None and len(names) == 0: aggregates = [] else: # Retrieve the requested Aggregates. aggregates = global_taba_server.GetAggregates(names, blocks) # Format the response in accordance to the Accept header. if accept == 'text/json': juno.append(cjson.encode(dict([a for a in aggregates]))) else: for agg_name, aggregate in aggregates: juno.append(agg_name).append(": ").append(cjson.encode(aggregate)) juno.append("\n")
def HandleGetProjection(request): """Juno request handler to retrieve raw Projection(s) from the DB. Get Parameters: client - Client ID to get Projection(s) for, or blank for all Clients IDs. client_glob - Client ID to get Projection(s) for. taba - Taba Name to get Projection(s) for, or blank for all Taba Names. taba_glob - Taba Glob to get Projection(s) for. block - Taba Name Block to get Projections for, or blank for all. Response: Projection objects, serialized depending on the Accept MIME type. Accept Types: text/plain (default) - Human readable new-line separated. text/json - JSON serialized. """ # Parse and validate query parameters. client_id = request.input('client') name = request.input('taba') block = request.input('block') # add glob params for taba and client name_glob = request.input('taba_glob') client_glob = request.input('client_glob') if (name or name_glob) and block: juno.status(400) juno.append('Cannot specify both "taba" and "block"') return # add rule.. only one of taba or taba_glob if name and name_glob: juno.status(400) juno.append('Cannot specify both "taba_glob" and "taba"') return # add rule.. only one of client_id or client_glob if client_id and client_glob: juno.status(400) juno.append('Cannot specify both "client_glob" and "client"') return blocks = block.split(',') if block else None names = [name] if name else None names_param = Param(name_glob, True) if name_glob else Param(names, False) clients_param = Param(client_glob, True) if client_glob \ else Param(client_id, False) return _ProjectionResponse(clients_param, names_param, blocks, _GetAccept(request))
def _RawResponse(client_id, names, blocks, accept): """Retrieve raw State results, and add them to the response. Args: client_id - Client ID string to retrieve State objects for. names - List of Taba Names to retrieve State objects for. blocks - List of Taba Name Block IDs to retrieve State objects for. accept - MIME type in which to format the output. """ # Get the requested State objects. states = global_taba_server.GetStates(client_id, names, blocks) # Format the response in accordance with the Accept header. if accept == 'text/json': juno.append(cjson.encode(dict([s for s in states]))) else: for (state_client_id, state_name), state in states: juno.append("(").append(state_client_id).append(", ") juno.append(state_name).append("): ") juno.append(cjson.encode(state)).append("\n")
def _ProjectionResponse(client_id, names, blocks, accept): """Retrieve Projection results, and add them to the response. Args: client_id - Client ID string to retrieve Projections for. names - List of Taba Names to retrieve Projections for. blocks - List of Taba Name Block IDs to retrieve Projections for. accept - MIME type in which to format the output. """ # Retrieve the requested Projections. projections = global_taba_server.GetProjections(client_id, names, blocks) # Render the Projection objects according to the requested format. if accept == 'text/json': juno.append(cjson.encode(dict([p for p in projections]))) else: for (proj_client_id, proj_name), projection in projections: juno.append("(").append(proj_client_id).append(", ") juno.append(proj_name).append("): ") juno.append(cjson.encode(projection)).append("\n")
def HandlePost(request): """Juno request handler for receiving posted Taba Events from a Taba Client. The request is expected to be a POST where the body follows the protocol: client_id\n[serialized_event\n]*\n The request is expected to be in plain-text. Only one client should be present in the request. The client_id and serialized events cannot contain '\n' characters. """ # Extract the Client ID and encoded Taba Events. body = request.raw['request_body_bytes'] try: client_id = body.readline().strip() events = [l.strip() for l in body.readlines() if l.strip()] except Exception: juno.status(400) juno.append("Could not parse document body") return # Check that the parameters are at least present. if not client_id: juno.status(400) juno.append("Client ID must be the first line of the POST document") return if not events: juno.status(400) juno.append("No events in POST document") return # Add the events to the Taba Agent buffer. try: global_taba_agent.Buffer(client_id, events) except Exception: LOG.error("Error buffering events") LOG.error(traceback.format_exc()) juno.status(500) juno.append("Error buffering events") return
def HandleGetTaba(request): """Juno Request handler to retrieve Rendered Tabs. Get Parameter: client - Client ID to retrieve Rendered Tabs for. If a Client ID is specified, this handler retrieves Rendered Projections. Otherwise it retrieved Rendered Aggregates. taba - Taba Name to retrieve Rendered Tabs for, or blank to retrieve all. block - Taba Name Block to retrieve Rendered Tabs for, or blank for all. Response: Rendered Tabs, serialized depending on the Accept MIME type. Accept Types: text/plain (default) - Human readable new-line separated. text/json - JSON serialized. """ # Parse and validate query parameters. client_id = request.input('client') name = request.input('taba') block = request.input('block') if name and block: juno.status(400) juno.append('Cannot specify both "taba" and "block"') return LOG.info("Starting Get Taba (%s, %s, %s)" % (client_id, name, block)) start = time.time() blocks = block.split(',') if block else None names = [name] if name else None renders = global_taba_server.GetRendered(client_id, names, blocks) # Render the Projection objects according to the requested format. accept = request.raw.get('HTTP_ACCEPT') or 'text/plain' accept = accept.lower() if accept == 'text/json': juno.append(cjson.encode([r for r in renders])) else: for render in renders: juno.append(render).append('\n') LOG.info("Finished Get Taba (%s, %s, %s) (%.2f)" % \ (client_id, name, block, time.time() - start))
def _AggregateResponse(names, blocks, accept): """Retrieve Aggregate results, and add them to the response. Args: names - List of Taba Names to retrieve Aggregates for. blocks - List of Taba Name Block IDs to retrieve Aggregates for. accept - MIME type in which to format the output. """ # Retrieve the requested Aggregates. aggregates = global_taba_server.GetAggregates(names, blocks) # Format the response in accordance to the Accept header. if accept == 'text/json': juno.append(cjson.encode(dict([a for a in aggregates]))) else: for agg_name, aggregate in aggregates: juno.append(agg_name).append(": ").append(cjson.encode(aggregate)) juno.append("\n")
def HandleGetType(request): """Juno request handler to retrieve the Taba Type for a Taba Name. Get Parameters: taba - The Taba Name to retrieve the Taba Type for. taba_glob - Glob for the Taba Name (One of the above is required) Response: Taba Type, serialized depending on the Accept MIME type. Accept Types: text/plain (default) - Human readable. text/json - JSON serialized. """ name = request.input('taba') name_glob = request.input('taba_glob') if name and name_glob: juno.status(400) juno.append('Cannot specify both "taba_glob" and "taba"') return if name: names = [name] elif name_glob: names = fnmatch.filter(global_taba_server.GetNames(), name_glob) else: names = None types = global_taba_server.GetTabaTypes(names) # Render the types depending on the Accept header. accept = request.raw.get('HTTP_ACCEPT') or 'text/plain' accept = accept.lower() if accept == 'text/json': juno.append(cjson.encode(dict(types))) else: for type in types: juno.append('%s: %s\n' % type)
def HandlePrune(request): """Juno request handler to perform a pruning of the Taba Database. Spawns a background greenlet to perform the operation and returns immediately. """ gevent.spawn(global_taba_server.PruneAll) juno.append("Started\n")