{
    "$schema": "file:///usr/local/lib/node_modules/@apica-io/url-xi/config/url-xi-schema-v1-0.json",
    "name": "Application Insight Demo",
    "description": "Get metrics from Microsoft Application insight",
    "variables": [
        {
            "key": "api_key",
            "type": "string",
            "usage": "input",
            "value": "DEMO_KEY",
            "hideValue": true
        },
        {
            "key": "application",
            "type": "string",
            "usage": "input",
            "value": "DEMO_APP"
        },
        {
            "key": "aggregation",
            "type": "string",
            "usage": "",
            "value": "'let arr=['max','min','avg'];arr[Math.floor(Math.random() * arr.length)]'"
        },
        {
            "key": "pageViewName",
            "type": "string",
            "usage": "inResponse"
        },
        {
            "key": "pageViewIndex",
            "type": "number",
            "usage": "",
            "value": 0
        },
        {
            "key": "pageViewDuration",
            "type": "number",
            "usage": "returnValue",
            "value": 0
        }
    ],
    "baseURL": "https://api.applicationinsights.io",
    "config": {
        "headers": {
            "x-api-key": "{{api_key}}"
        }
    },
    "steps": [
        {
            "name": "Get Page views",
            "requests": [
                {
                    "config": {
                        "method": "post",
                        "url": "/v1/apps/{{application}}/metrics",
                        "data": [
                            {
                                "id": "Page Views duration per path for Edge",
                                "parameters": {
                                    "metricId": "pageViews/duration",
                                    "aggregation": "{{aggregation}}",
                                    "timespan": "PT240H",
                                    "segment": "pageView/name,pageView/urlPath",
                                    "filter": "startswith(client/browser,'Edg')"
                                }
                            }
                        ]
                    },
                    "extractors": [
                        {
                            "type": "jsonpath",
                            "expression": "$[0].body.value.segments[*].pageView/name",
                            "variable": "pageViewIndex",
                            "index": true
                        },
                        {
                            "type": "jsonpath",
                            "expression": "$[0].body.value.segments[{{pageViewIndex}}].pageView/name",
                            "variable": "pageViewName"
                        },
                        {
                            "type": "jsonpath",
                            "expression": "$[0].body.value.segments[{{pageViewIndex}}].segments[0].pageViews/duration.{{aggregation}}",
                            "variable": "pageViewDuration"
                        },
                        {
                            "type": "jsonpath",
                            "expression": "$[0].id",
                            "variable": "id"
                        }
                    ],
                    "assertions": [
                        {
                            "type": "javaScript",
                            "value": "{{pageViewDuration}}",
                            "description": "Duration should be greater than 1 ms",
                            "expression": "value > 1",
                            "failStep": true
                        },
                        {
                            "type": "regexp",
                            "value": "{{aggregation}}",
                            "description": "Aggregation must be max,min or avg",
                            "expression": "(max|min|avg)",
                            "failStep": true
                        }
                    ]
                }
            ]
        }
    ]
}