Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.3 kB
2
Indexable
Never
def update_polling_counts_for_batch(aggregated_values, timestamp):
    client = boto3.client('dynamodb', region_name='us-east-1')
    table_name = os.getenv("MEGLO_DEVICE_HEALTH_TABLE_NAME", default=None)
    polling_type = os.getenv("LAMBDA_TYPE", default=None)

    current_month_attributes = __get_current_month_ddb_attributes()
    polling_attribute = current_month_attributes["POLLING_COUNT"]
    err_attribute = current_month_attributes["POLLING_ERROR"]
    conn_err_attribute = current_month_attributes["CONNECTION_ERROR"]

    # These are all Counter type
    aggregated_polling_count = aggregated_values["POLLING_COUNT"]
    aggregated_polling_errors = aggregated_values["POLLING_ERROR"]
    aggregated_connection_errors = aggregated_values["CONNECTION_ERROR"]
    data_centers = aggregated_polling_count.keys()

    write_requests = []

    for data_center in data_centers:
        polling_count = aggregated_polling_count[data_center]
        polling_errors = aggregated_polling_errors[data_center]
        connection_errors = aggregated_connection_errors[data_center]

        write_request = {
            "Update": {
                "TableName": table_name,
                "Key": {
                    "DATA_CENTER": {"S": data_center.lower()},
                    "POLLING_TYPE": {"S": polling_type.lower()}
                },
                "UpdateExpression": (
                    f"SET {polling_attribute} = {polling_attribute} + :count, "
                    f"{err_attribute} = {err_attribute} + :errors, "
                    f"{conn_err_attribute} = {conn_err_attribute} + :conn_errors, "
                    f"{POLLING_COUNT_CURRENT_MIN} = :count, "
                    f"{ERROR_COUNT_CURRENT_MIN} = :errors, "
                    f"{LAST_PROCESSED} = :last_processed"
                ),
                "ExpressionAttributeValues": {
                    ":count": {"N": str(polling_count)},
                    ":errors": {"N": str(polling_errors)},
                    ":conn_errors": {"N": str(connection_errors)},
                    ":last_processed": {"N": str(timestamp)}
                }
            }
        }

        write_requests.append(write_request)

    # Execute batch write operations
    response = client.batch_write_item(RequestItems={table_name: write_requests})

    return response