Пример #1
0
def get_device_data_list__a(
        timestamp: int,
        device_type_list: List[str],
        command_list: Optional[List[str]] = []) -> List[Dict[str, Any]]:
    """ Get list of device_data items by given timestamp and device types """

    results: List[Dict[str, Any]] = []

    with pymongo.MongoClient(DB_URI) as client:
        table = client.get_default_database()[netcat.DBT_INFO]

        query_params = {
            "filter": {
                "snapshot_timestamp": timestamp,
                "device_type": {
                    "$in": device_type_list
                }
            },
            "projection": _projection(command_list),
        }

        netcat.LOGGER.debug(
            f"{netcat.fn()}: table='{netcat.DBT_INFO}', {query_params=}")

    return [
        netcat.decompress_device_data(_) for _ in table.find(**query_params)
    ]
Пример #2
0
def get_device_data_list__c(device_name: str,
                            command_list: Optional[List[str]] = [],
                            search_depth: int = 0) -> List[Dict[str, Any]]:
    """ Get list of device_data items up to given search depth by device name  """

    results: List[Dict[str, Any]] = []

    with pymongo.MongoClient(DB_URI) as client:
        table = client.get_default_database()[netcat.DBT_INFO]

        query_params = {
            "filter": {
                "device_name": device_name
            },
            "projection": _projection(command_list),
            "sort": [("snapshot_timestamp", pymongo.DESCENDING)],
            "limit": search_depth,
        }

        netcat.LOGGER.debug(
            f"{netcat.fn()}: table='{netcat.DBT_INFO}', {query_params=}")

    return [
        netcat.decompress_device_data(_) for _ in table.find(**query_params)
    ]
Пример #3
0
def get_device_data(device_name: str, timestamp: int, command_list: Optional[List[str]] = []) -> Dict[str, Any]:
    """ Get output of all or given command(s) from device_name / timestamp document """

    query_params = {
        "TableName": netcat.DBT_INFO,
        "Key": {"device_name": {"S": device_name}, "snapshot_timestamp": {"N": str(timestamp)}},
        "ProjectionExpression": _projection(command_list),
    }

    netcat.LOGGER.debug(f"{netcat.fn()}: {query_params=}")

    return netcat.decompress_device_data(_fold(DB_CLIENT.get_item(**query_params).get("Item", {})))
Пример #4
0
def get_device_data_list__c(device_name: str, command_list: Optional[List[str]] = [], search_depth: int = 0) -> List[Dict[str, Any]]:
    """ Get list of device_data items up to given search depth by device name  """

    query_params = {
        "TableName": netcat.DBT_INFO,
        "KeyConditionExpression": "device_name = :device_name",
        "ExpressionAttributeValues": {":device_name": {"S": device_name}},
        "ProjectionExpression": _projection(command_list),
    }

    netcat.LOGGER.debug(f"{netcat.fn()}: {query_params=}")

    return [netcat.decompress_device_data(_) for _ in _get_list(query_params, search_depth)]
Пример #5
0
def load_latest_backup(device_name: str) -> Dict[str, Any]:
    """ Returns latest config backup for given device name """

    try:
        with pymongo.MongoClient(DB_URI) as client:
            return netcat.decompress_device_data(
                client.get_default_database()[netcat.DBT_BACKUP].find_one(
                    filter={"device_name": device_name},
                    sort=[("snapshot_timestamp", pymongo.DESCENDING)],
                    projection={"_id": False}) or {})

    except pymongo.errors.PyMongoError as exception:
        raise netcat.CustomException(
            f"Unable to load latest config backup, PyMongo exception: '{exception}'"
        )
def load_latest_backup(device_name: str) -> Dict[str, Any]:
    """ Returns latest config backup for given device name """

    query_params = {
        "TableName": netcat.DBT_BACKUP,
        "KeyConditionExpression": "device_name = :device_name",
        "ExpressionAttributeValues": {
            ":device_name": {
                "S": device_name
            }
        },
    }

    netcat.LOGGER.debug(f"{netcat.fn()}: {query_params=}")

    return netcat.decompress_device_data(
        next(iter(_get_list(query_params, search_depth=1)), {}))
Пример #7
0
def get_device_data_list__b(device_name_list: List[str], command_list: Optional[List[str]]) -> List[Dict[str, Any]]:
    """ Get list of device_data items by device name list and latest timestamp available for each device """

    results: List[Dict[str, Any]] = []

    for device_name in device_name_list:

        query_params = {
            "TableName": netcat.DBT_INFO,
            "KeyConditionExpression": "device_name = :device_name",
            "ExpressionAttributeValues": {":device_name": {"S": device_name}},
            "ProjectionExpression": _projection(command_list),
        }

        netcat.LOGGER.debug(f"{netcat.fn()}: {query_params=}")

        results += _get_list(query_params, search_depth=1)

    return [netcat.decompress_device_data(_) for _ in results]
Пример #8
0
def get_device_data_list__a(timestamp: int, device_type_list: List[str], command_list: Optional[List[str]] = []) -> List[Dict[str, Any]]:
    """ Get list of device_data items by given timestamp and device types """

    results: List[Dict[str, Any]] = []

    for device_type in device_type_list:

        query_params = {
            "TableName": netcat.DBT_INFO,
            "IndexName": "type-timestamp-index",
            "KeyConditionExpression": "device_type = :device_type AND snapshot_timestamp = :snapshot_timestamp",
            "ExpressionAttributeValues": {":device_type": {"S": device_type}, ":snapshot_timestamp": {"N": str(timestamp)}},
            "ProjectionExpression": _projection(command_list),
        }

        netcat.LOGGER.debug(f"{netcat.fn()}: {query_params=}")

        results += _get_list(query_params)

    return [netcat.decompress_device_data(_) for _ in results]
Пример #9
0
def get_device_data(device_name: str,
                    timestamp: int,
                    command_list: Optional[List[str]] = []) -> Dict[str, Any]:
    """ Get output of all or given command(s) from device_name / timestamp document """

    results: List[Dict[str, Any]] = []

    with pymongo.MongoClient(DB_URI) as client:
        table = client.get_default_database()[netcat.DBT_INFO]

        query_params = {
            "filter": {
                "device_name": device_name,
                "snapshot_timestamp": timestamp
            },
            "projection": _projection(command_list),
        }

        netcat.LOGGER.debug(
            f"{netcat.fn()}: table='{netcat.DBT_INFO}', {query_params=}")

    return netcat.decompress_device_data(table.find_one(**query_params))
Пример #10
0
            query_params = {
                "filter": {
                    "device_name": device_name
                },
                "projection": _projection(command_list),
                "sort": [("snapshot_timestamp", pymongo.DESCENDING)],
            }

            netcat.LOGGER.debug(
                f"{netcat.fn()}: table='{netcat.DBT_INFO}', {query_params=}")

            if result := table.find_one(**query_params):
                results.append(result)

    return [netcat.decompress_device_data(_) for _ in results]


def get_device_data_list__c(device_name: str,
                            command_list: Optional[List[str]] = [],
                            search_depth: int = 0) -> List[Dict[str, Any]]:
    """ Get list of device_data items up to given search depth by device name  """

    results: List[Dict[str, Any]] = []

    with pymongo.MongoClient(DB_URI) as client:
        table = client.get_default_database()[netcat.DBT_INFO]

        query_params = {
            "filter": {
                "device_name": device_name