3.1 KPI Management Quick-Start Guide

The input data in this example use case consists of sales numbers in CSV format. This dataset is from here on, referred to as "sales". The data is collected in real-time from the regions "APAC", "AMERICAS", and "EMEA". We want to calculate the total-, average, and number of sales per minute. These numbers will be our KPIs, broken down per country and region.

Example - Input data

timestampregioncountryamount
2017-03-08T13:53:52.123EMEASweden123.50
2017-03-08T13:53:56.123APACIndia

12.12

2017-03-08T13:53:59.123AMERICASUS425.23

Step-by-Step Instructions

  1. Configure the service model.

    The service model describes your data, which KPIs to generate and how to calculate them. A JSON representation is used to describe the model, which includes the following top-level objects:

    • dimension
    • tree
    • metric
    • kpi
    • threshold (optional)

    Configure the service model using a text editor, or in a KPI profile in the Desktop. To create a new KPI profile profile, click the New Configuration button in the upper left part of the  Desktop window, and then select KPI Profile from the menu.

    Start with the dimension and tree objects. The dimensions describe the fields of your data that are used for grouping and the tree the relation between them. The identifying fields in the input data are region and country. A region has one or more countries. The data type is sales. In the dimension object we specify each of our identifying fields as separate objects, with the datatype and field in the body.

     Click here to expand...
    Dimensions
    "dimension": {
        "Region": {
          "sales": "region"
        },
        "Country": {
          "sales": "country"
        }
      },
      "tree": {
        "tree1": {
          "Region": {
            "Country": {
            }
          }
        }
      }


    Define the metrics using the amount field in the input data:

    • totalSales - For total sales, sum up the amount for each record by using the sum function on the expression expr, which contains the amount field. 
    • avgSales  - For average sales  use the avg function instead of sum.
    • numSales - To count the number of records, use the conditional function isSet in the expression. This function evaluates to 1 if there is a value in amount or 0 if there is no value. Use the function sum to sum up the 1s and 0s.

 Click here to expand...
Metrics
"metric": {
  "totalSales" : {
    "fun": "sum",
    "expr": {
      "sales": "amount"
     }
  },
  "avgSales" : {
    "fun": "avg",
    "expr": {
      "sales": "amount"
     }
  },
  "numSales" : {
    "fun": "sum",
    "expr": {
      "sales": "isSet(amount)"
     }
  }
} 

Define the KPIs. The expected output is the total sales, average sales, and number of sales per region and country in 60 second periods. 

Use the  property node to describe where in the topology the KPI should be calculated and windowSize to set the period length. Use the names of the metrics defined above in the expr property.  

 Click here to expand...
KPIs
"kpi": { 
  "Region.TotalSales": { 
    "node": [ 
      "tree1", 
      "Region"
    ], 
    "windowSize": 60, 
    "expr": "totalSales" 
  },
  "Region.AvgSales": { 
    "node": [ 
      "tree1", 
      "Region" 
    ], 
    "windowSize": 60, 
    "expr": "avgSales" 
  },
  "Region.NumberOfSales": { 
    "node": [ 
      "tree1", 
      "Region" 
    ],
    "windowSize": 60, 
    "expr": "numSales" 
  },
  "Country.TotalSales": { 
    "node": [ 
      "tree1",
      "Region", 
      "Country"
    ], 
    "windowSize": 60, 
    "expr": "totalSales" 
  },
  "Country.AvgSales": { 
    "node": [ 
      "tree1", 
      "Region",
      "Country"
    ], 
    "windowSize": 60, 
    "expr": "avgSales" 
  },
  "Country.NumberOfSales": { 
    "node": [ 
      "tree1", 
      "Region",
      "Country"
    ],
    "windowSize": 60, 
    "expr": "numSales"
  }
}

Combine all the objects above for a complete representation of the model.

 Click here to expand...
Complete Model
{
  "dimension": {
      "Region": {
        "sales": "region"
      },
      "Country": {
        "sales": "country"
      }
    },
    "tree": {
      "tree1": {
        "Region": {
          "Country": {
          }
        }
      }
    },
  "metric": {
    "totalSales" : {
      "fun": "sum",
      "expr": {
        "sales": "amount"
       }
    },
    "avgSales" : {
      "fun": "avg",
      "expr": {
        "sales": "amount"
       }
    },
    "numSales" : {
      "fun": "sum",
      "expr": {
        "sales": "isSet(amount)"
       }
    }
  },
 "kpi": { 
  "Region.TotalSales": { 
    "node": [ 
      "tree1", 
      "Region"
    ], 
    "windowSize": 60, 
    "expr": "totalSales" 
  },
  "Region.AvgSales": { 
    "node": [ 
      "tree1", 
      "Region" 
    ], 
    "windowSize": 60, 
    "expr": "avgSales" 
  },
  "Region.NumberOfSales": { 
    "node": [ 
      "tree1", 
      "Region" 
    ],
    "windowSize": 60, 
    "expr": "numSales" 
  },
  "Country.TotalSales": { 
    "node": [ 
      "tree1",
      "Region", 
      "Country"
    ], 
    "windowSize": 60, 
    "expr": "totalSales" 
  },
  "Country.AvgSales": { 
    "node": [ 
      "tree1", 
      "Region",
      "Country"
    ], 
    "windowSize": 60, 
    "expr": "avgSales" 
  },
  "Country.NumberOfSales": { 
    "node": [ 
      "tree1", 
      "Region",
      "Country"
    ],
    "windowSize": 60, 
    "expr": "numSales"
  }
}
}

If you have used configured the service model in an external text editor, open the Desktop and paste it into a KPI profile. Save the profile with the name SalesModel in the folder kpisales.



  1. Create the real-time workflow. In this guide we will use Pulse agents to simulate sales data coming from three different sources, EMEA, AMERICAS, and APAC.

    1. Add three Pulse agents and an Analysis agent.
       

      Workflow - Pulse Agents

      Configure the Pulse agents as follows:

      • AMERICAS will send 1000 TPS -  Set Time Unit to MILLISECONDS and Interval to 100
      • EMEA will send 500 TPS - Set Time Unit to MILLISECONDS and Interval to 200
      • APAC will send 250 TPS - Set Time Unit to MILLISECONDS and Interval to 400

      To be able to identify the data, set the data to the region name. 

      Pulse agent configuration

      The pulse agents only sends us a simple event containing the name of the region, the other data that will be used in the KPI calculations are generated in the connected Analysis agent.

      The APL code below creates the input to KPI Management.


       Click here to expand...
       Click here to expand...
      APL code
      list<string> americas = listCreate(string, "US", "Canada", "Mexico", "Brazil", "Argentina", "Cuba", "Colombia");
      list<string> emea = listCreate(string, "Sweden", "UK", "Portugal", "Italy", "France", "Germany", "Norway", "Spain", "Finland", "Denmark");
      list<string> apac = listCreate(string, "India", "China", "Japan", "Thailand", "Australia", "Indonesia", "Malaysia","South Korea");
      
      consume {
          // create KDR - the input for the KPI CLusterIn agent
          kpimanagement.KDR kdr = udrCreate(kpimanagement.KDR);
          // The KDR has a type field - we set this to the value we had for our data type in the model
          kdr.type = "sales";
          // It also has a timestamp field - lets populate that from the current time but using seconds.
          kdr.timestamp = dateCreateNowMilliseconds() / 1000;
          
          string region = baToStr(input.Data);
          // the  data in our use case (country, city, amount) we will put in the values field of the kdr.
      	map<string, any> sales = mapCreate(string,any);
          mapSet(sales, "region", region);
          // lets create a random amount between 1 and 1000
          int amount = randomInt(1000);
      
          // set amount and city depending on the region
          if (region == "AMERICAS") {
             mapSet(sales, "amount", amount * 1.25d);
             mapSet(sales, "country", randomCountry(americas));
          } else if (region == "EMEA") {
             mapSet(sales, "amount", amount * 1.0d);
             mapSet(sales, "country", randomCountry(emea));
          } else if (region == "APAC") {
             mapSet(sales, "amount", amount * 0.65d);
             mapSet(sales, "country", randomCountry(apac));
          } else {
             mapSet(sales, "amount", 0.0d);
             mapSet(sales, "country", "UNKNOWN");
             debug("Unknown region:" + region);
          }
          kdr.values = sales;
          udrRoute(kdr);
      
      }
      
      // pick a random country from a list
      string randomCountry(list<string> countries) {
          int index = randomInt(listSize(countries));
          return listGet(countries, index);
      }




  1. Add a KPI agent and configure it to use the KPI profile that you created in step 1. Set Delay to 0.

    Workflow - KPI agent

    KPI agent configuration


  2. Add another Analysis agent for debugging of the KPIs. 

    Final workflow configuration 

  3. Add the APL code below to the Analysis agent.

     Click here to expand...
    APL code
    string format = "yyyy-MM-dd'T'HH:mm:ss:S";
    consume {
        // input is KPIAggregatedOutput which contains a list of
        // KPIOutput
        list<kpimanagement.KPIOutput> kpis = input.kpiOutputUDRs;
        // loop the KPIs and debug
        string dateStr = "";
        for (int i = 0; i < listSize(kpis); i++) {
            kpimanagement.KPIOutput kpi = listGet(kpis, i);
            dateToString(dateStr, dateCreateFromMilliseconds(kpi.periodStart * 1000), format);
            debug("Period start: " + dateStr + ", instance: " + kpi.instancePath  + ", KPI: " + kpi.kpi + ", Value:" + kpi.value + ", Samples: " + kpi.sampleCount);
        }
    }


  4. Open the workflow configuration in the Workflow Monitor. Enable debugging and select events for the KPI agent and the Analysis agent that produces the debug output.
     
  5. The calculated KPIs will be displayed in the debug output in the Workflow Monitor.

    Note!

    It will take a minute before the output is displayed due to the configuration of the windowSize property in the service model.