async def filter(cls, **kwargs): user_id = kwargs['uid'] user_name = kwargs['username'] q = PayloadBuilder().SELECT("id", "uname", "role_id").WHERE(['enabled', '=', 't']) if user_id is not None: q = q.AND_WHERE(['id', '=', user_id]) if user_name is not None: q = q.AND_WHERE(['uname', '=', user_name]) storage_client = connect.get_storage_async() q_payload = PayloadBuilder(q.chain_payload()).payload() result = await storage_client.query_tbl_with_payload( 'users', q_payload) return result['rows']
async def get_asset_tracker_events(request): """ Args: request: Returns: asset track records :Example: curl -X GET http://localhost:8081/fledge/track curl -X GET http://localhost:8081/fledge/track?asset=XXX curl -X GET http://localhost:8081/fledge/track?event=XXX curl -X GET http://localhost:8081/fledge/track?service=XXX """ payload = PayloadBuilder().SELECT("asset", "event", "service", "fledge", "plugin", "ts") \ .ALIAS("return", ("ts", 'timestamp')).FORMAT("return", ("ts", "YYYY-MM-DD HH24:MI:SS.MS")) \ .WHERE(['1', '=', 1]) if 'asset' in request.query and request.query['asset'] != '': asset = urllib.parse.unquote(request.query['asset']) payload.AND_WHERE(['asset', '=', asset]) if 'event' in request.query and request.query['event'] != '': event = request.query['event'] payload.AND_WHERE(['event', '=', event]) if 'service' in request.query and request.query['service'] != '': service = urllib.parse.unquote(request.query['service']) payload.AND_WHERE(['service', '=', service]) storage_client = connect.get_storage_async() payload = PayloadBuilder(payload.chain_payload()) try: result = await storage_client.query_tbl_with_payload( 'asset_tracker', payload.payload()) response = result['rows'] except KeyError: raise web.HTTPBadRequest(reason=result['message']) except Exception as ex: raise web.HTTPInternalServerError(reason=ex) return web.json_response({'track': response})
async def get_audit_entries(request): """ Returns a list of audit trail entries sorted with most recent first and total count (including the criteria search if applied) :Example: curl -X GET http://localhost:8081/fledge/audit curl -X GET http://localhost:8081/fledge/audit?limit=5 curl -X GET http://localhost:8081/fledge/audit?limit=5&skip=3 curl -X GET http://localhost:8081/fledge/audit?skip=2 curl -X GET http://localhost:8081/fledge/audit?source=PURGE curl -X GET http://localhost:8081/fledge/audit?source=NTFSD,NTFSN,NTFAD,NTFST,NTFDL,NTFCL curl -X GET http://localhost:8081/fledge/audit?severity=FAILURE curl -X GET http://localhost:8081/fledge/audit?source=LOGGN&severity=INFORMATION&limit=10 """ limit = __DEFAULT_LIMIT if 'limit' in request.query and request.query['limit'] != '': try: limit = int(request.query['limit']) if limit < 0: raise ValueError except ValueError: raise web.HTTPBadRequest(reason="Limit must be a positive integer") offset = __DEFAULT_OFFSET if 'skip' in request.query and request.query['skip'] != '': try: offset = int(request.query['skip']) if offset < 0: raise ValueError except ValueError: raise web.HTTPBadRequest(reason="Skip/Offset must be a positive integer") source = None source_list = [] if 'source' in request.query and request.query['source'] != '': try: source = request.query.get('source') source_list = source.split(',') # SELECT * FROM log_codes storage_client = connect.get_storage_async() result = await storage_client.query_tbl("log_codes") log_codes = [key['code'] for key in result['rows']] for code in source_list: if code not in log_codes: raise ValueError(code) except ValueError as e: raise web.HTTPBadRequest(reason="{} is not a valid source".format(str(e))) severity = None if 'severity' in request.query and request.query['severity'] != '': try: severity = Severity[request.query['severity'].upper()].value except KeyError as ex: raise web.HTTPBadRequest(reason="{} is not a valid severity".format(ex)) try: # HACK: This way when we can more future we do not get an exponential # explosion of if statements payload = PayloadBuilder().SELECT("code", "level", "log", "ts")\ .ALIAS("return", ("ts", 'timestamp')).FORMAT("return", ("ts", "YYYY-MM-DD HH24:MI:SS.MS"))\ .WHERE(['1', '=', 1]) if source is not None: if len(source_list) == 1: payload.AND_WHERE(['code', '=', source]) else: payload = PayloadBuilder().SELECT("code", "level", "log", "ts") \ .ALIAS("return", ("ts", 'timestamp')).FORMAT("return", ("ts", "YYYY-MM-DD HH24:MI:SS.MS")) payload.WHERE(['code', 'in', source_list]) if severity is not None: payload.AND_WHERE(['level', '=', severity]) _and_where_payload = payload.chain_payload() # SELECT *, count(*) OVER() FROM log - No support yet from storage layer # TODO: FOGL-740, FOGL-663 once ^^ resolved we should replace below storage call for getting total rows _and_where_copy = copy.deepcopy(_and_where_payload) total_count_payload = PayloadBuilder(_and_where_copy).AGGREGATE(["count", "*"])\ .ALIAS("aggregate", ("*", "count", "count")).payload() # SELECT count (*) FROM log <_and_where_payload> storage_client = connect.get_storage_async() result = await storage_client.query_tbl_with_payload('log', total_count_payload) total_count = result['rows'][0]['count'] payload = PayloadBuilder(_and_where_payload) payload.ORDER_BY(['ts', 'desc']) payload.LIMIT(limit) if offset > 0: payload.OFFSET(offset) # SELECT * FROM log <payload.payload()> results = await storage_client.query_tbl_with_payload('log', payload.payload()) res = [] for row in results['rows']: r = dict() r["details"] = row["log"] severity_level = int(row["level"]) r["severity"] = Severity(severity_level).name if severity_level in (0, 1, 2, 4) else "UNKNOWN" r["source"] = row["code"] r["timestamp"] = row["timestamp"] res.append(r) except Exception as ex: raise web.HTTPInternalServerError(reason=str(ex)) return web.json_response({'audit': res, 'totalCount': total_count})