Congestion Insights

The SDK below demonstrates how to use the Congestion Insights functionality in a network. It allows an application to access current or predicted congestion levels in a specific area of the network.

With the Congestion capability insights, an application can determine if the geographic area where the mobile device is located is going to experience congestion that might affect use cases such as bandwidth or latency. This way, the app might proactively move functionality to another area or use functionalities, such as QoD or Network Slicing to avoid congestion.

Congestion levels can be accessed either through polling or a webhook subscription mechanism. Polling involves making a request and receiving an immediate response with the current or predicted congestion level. As for the webhook subscription, an application can receive notifications about changes in the congestion levels.

Since Congestion is also supposed to be able to provide current actual congestion levels, it can also be used as a diagnostic tool to determine why network performance may be poor. So, in addition to allowing proactive measures, it can also be used reactively.

Polling congestion levels

First, you can poll the current congestion level for a given device:

import network_as_code as nac
 
from datetime import datetime, timezone, timedelta
 
from network_as_code.models.device import Device, DeviceIpv4Addr
 
client = nac.NetworkAsCodeClient(
    token="<your-application-key-here>"
)
 
# Create a device object for the mobile device we want to use
my_device = client.devices.get(
    "device@bestcsp.net",
    ipv4_address = DeviceIpv4Addr(
        public_address="192.0.2.3",
        private_address="192.0.2.204",
        public_port=80
    ),
    ipv6_address = "2001:db8:1234:5678:9abc:def0:fedc:ba98",
    phone_number = "36721601234567"
)
 
# Returns a string with the current congestion level,
# e.g.: "low", "high", "medium" or "none"
congestion = my_device.get_congestion()
 
# Access the congestion level object just created like this:
print(congestion.level)

What does this code do?

It calls a method to get the congestion on the device object previously created. As a result, the method return the Congestion object. It then demonstrates how to show the congestion level from the returned Congestion object.

Getting predictions or historical data

You can also get congestion predictions or historical data between two timestamps. This allows anticipating congestion during a specified amount of time.

The SDK below calls a method to get congestion levels passing the device information and timestamps that for the time span you wish to consult, as parameters. It iterates over the returned Congestion objects and prints the timestamp and congestion level for each prediction.

# Get historical data between two timestamps
# Set the duration/time difference with the timedelta function
congestion = my_device.get_congestion(
    start=datetime.now(timezone.utc),
    end=datetime.now(timezone.utc) + timedelta(hours=3)
)

NOTE: The subscription expire time can be defined with an ISO 8601 formatted date string, for example "2024-03-28T12:40:20.398Z", or simply create a date-time object as shown in the code snippet examples above.

Congestion level parameters

Here is a parameters table describing each parameter used in the code:

ParametersDescription
startStart timestamp for retrieving congestion data. It should be an ISO 8601 formatted date string or a date-time object as shown above.
endEnd timestamp for retrieving congestion data. It should be an ISO 8601 formatted date string or a date-time object as shown above.

Last updated on May 21, 2024