Created by Vijendra Sawant, Modified on Fri, 01 Dec 2023 at 04:30 PM by Vijendra Sawant


This function calculated the average value of a metric based on the time or any dimension that quantifies the metric.

Calculation Script name - Average
Description - calculate the average value of a metric based on the time or
                 any dimension that quantifies the metric.
Product Version - v66
Script Version - 1.0

***Disclaimer - This is proprietary code of Whiz.AI. If you want to build on top of this computation script please make a copy/clone of this script and then alter the script which is copied or cloned. When version upgrade happens there are chances that this script may get modified hence any customization done by customer/partner developer may be overwritten.

Key Functions - 1.Function name:compute
                purpose : calculate average
                input parameters :metadataQuery
                output parameters : dataframe
Key variables - query : implicit variable 
                baseMetric :metric on which computation will be performed
                aggregations :list of aggregation
                outputmetricName : final computation name (dynamically crated)
                dataAccessManager :This class is exposed to scripts by Framework through implicit variable dataAccessManager.
                                   This acts as gateway to data exposed by the framework. A query can be fired through this instance to get the data.
//** Start of script

(() => {
    log.info("************************ WhizCalAverage *******************");

    function compute(query) {
        //Name for metadata query, this helps logging and identification of query.

        // get all the aggregation from entity-configuration in list format 
        const avgParam = addonProps["defaultFilter"]["dynamicArgs"]["avgParam"];

        //Returns the column set for this aggregation
        //it will give primary aggregation column like TRx,NRx
        const baseMetric = primaryAggr.column();

        // converts list to aggregation as druid understands the aggregation.
        const aggregations = Aggregation.parseList(avgParam);

        //Creates shallow copy of this aggregation setting the name of the new aggregation as per specified value. 
        //This is mostly helpful if aggregation is to be renamed, discarding the original aggregation
        aggregations[2] = aggregations[2].copyWithName(outputMetricName);

        //Adds the given aggregation/post-aggregation to the query. 

        /*  Replaces given column to all places applicable; currently Dimensions, Aggregations, Filters, SortOrders
             might be extended to more known places , specifically useful for changing marker column patterns
             with actual column 
        You must match the first parameter to the column name supplied in the configuration's numerator aggregation.
        query.replaceColumn("__baseMetric", baseMetric);

        //Replaces any ordering clauses in the query with given column with ordering on new column
        query.replaceOrderingColumn(baseMetric, outputMetricName);

        //Fires the MetadataQuery passed, returns DataFrame representing the response of the query
        return dataAccessManager.fireQuery(query);

    return compute(query);

Following Configuration needs to be done for Function in Admin Console :-

 ** configuration to be set in calculation metric **

default_filter: In this section's dynamicArgs sub-section, we will have the information related to the necessary aggregations required.
avgParam : avgParam is arraylist of metadataQuery configuration.
           This block elements are passed to script as a parameter. 
avgParam[0] : Map at index 0 in a avgParam list
             its a numerator part where in column we will pass Base metric. 
    type : In here we specify which type of calculation we want.
    Column : It is used to specify on which column given type of operation needs to perform. 

avgParam[1] : Map at index 1 in a avgParam list
             Its a denominator part where type of calculation is decided 
    type : In here we specify which type of  denominator calculation we want.
    Column : It is used to specify on which column countDistinct operation needs to perform. 

avgParam[2] : Here division Aggregation is specified.
    value : name of aggregation used in numerator
    denominator : name of aggregation used in denominator
    divByZeroResponse : if denominator is 0, this value should be returned

**The configuration below should be copied and pasted to calculate the Average.*

"default_filter": {
    "dynamicArgs": {
        "avgParam": [
                "name": "numMetric",
                "type": "sum",
                "column": "__baseMetric"
                "name": "Count",
                "type": "count"
                "name": "Average",
                "type": "division",
                "value": "numMetric",
                "denominator": "Count",
                "divByZeroResponse": 0

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article