Analytics

Enable Analytics through the alwaysAI Device Agent

Configure the Device Agent

You can send information from your device to the alwaysAI cloud securely using the Device Agent. These instructions assume you have provisioned your device using the default script parameters and have the Device agent running (i.e. the provisioning script was not run with the --provision-only flag set).

The Device Agent must be configured to enable Analytics Pass-through support. Confirm that the ALWAYSAI_ANALYTICS_PASSTHROUGH environment variable is set:

$ pm2 env 0 | grep ALWAYSAI
ALWAYSAI_ANALYTICS_PASSTHROUGH: 1
ALWAYSAI_LOG_TO_CONSOLE: 
ALWAYSAI_LOG_LEVEL: debug
ALWAYSAI_DEVICE_AGENT_MODE: cloud

Then confirm the RabbitMQ container is up and running:

$ docker ps
CONTAINER ID   IMAGE           COMMAND                  CREATED          STATUS          PORTS                                                                    NAMES
596157124a4b   rabbitmq:3.11   "docker-entrypoint.s…"   32 minutes ago   Up 21 minutes   4369/tcp, 5671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:5672->5672/tcp   alwaysAIRabbitMQContainer

Configure the Application

First, you must publish analytics from your application using publish_analytics. Whenever this command is used, it will publish the contents of the analytics message to the analytics endpoints you have enabled. Each core computer vision service has it’s own publish_analytics method, which is called on the instance of the class. Or, you can published any JSON-serializable message with edgeiq.publish_analytics(). For instance, to publish object detection results you can use:

results = obj_detect.detect_objects(frame, confidence_level=.5)
try:
    obj_detect.publish_analytics(results, tag=frame_count)
except edgeiq.PublishError as e:
    # Retry publish
except edgeiq.ConnectionError as e:
    # Save state and exit app to reconnect

Next, the ALWAYSAI_CONNECT_TO_DEVICE_AGENT environment variable must be set. There are a few options:

Using the alwaysAI Console

Once an app has been deployed to a device, you can add, modify, and remove environment variables using the environment variable tab of the app details panel. Add a new environment variable called ALWAYSAI_CONNECT_TO_DEVICE_AGENT and set it to 1.

In a docker-compose.yaml

Add the following section to the service for your app in a docker-compose.yaml file. If you don’t yet have a docker-compose.yaml file in your app source, you can generate a template one to edit by running the aai app generate docker-compose command. Note that this will only take effect in production deployments.

environment:
  - ALWAYSAI_CONNECT_TO_DEVICE_AGENT=1

Enable Publishing to Cloud

The application analytics configuration can be set either in the app source when you publish it or once the app has been deployed to a device using the alwaysAI Console.

In your app source

To enable cloud publishing in your app configurations run the following commands in your app directory. The first enables cloud publishing, and the second publishes a new application release with the updated config so it can be deployed.

$ aai app analytics enable-cloud-publish
$ aai app publish

This can be added to the app release process so that cloud publishing is always enabled for production deployments.

From the alwaysAI Console

In the alwaysAI Console, navigate to the device the application has been deployed to and open the configuration tab in the app panel. In the app config, add an analytics section that looks like the following:

"analytics": {
  "enable_cloud_publish": true
}

So, a valid alwaysai.app.json for publishing analytics might look like this:

{
  "scripts": {
    "start": "python app.py"
  },
  "models": {
    "alwaysai/mobilenet_ssd": 4
  },
  "analytics": {
    "enable_cloud_publish": true
  }
}

Press “Update” to update the application configuration on the device.

Capture the Analytics Stream

You can test that analytics are being viewed with wscat, using your application’s project ID and a secure API key:

$ wscat -c "wss://analytics.alwaysai.co?projectId=[PROJECT_ID]&apiKey=[API_KEY]"

Please contact the alwaysAI team if you need a secure API key.