Growth

Created by Vijendra Sawant, Modified on Fri, 03 Nov 2023 at 08:57 PM by Vijendra Sawant


/*
Calculation Script name - WhizCalGrowth
Description - Percentage change in any metric when compared for two time periods. 
              Growth in Metric Value between Time period T1 and T2 is calculated as ((Value for T2) - (Value for T1) ) / (Value for T1)
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 : getComputationObj
                purpose : get instance of class present in WhizCalDynamicPeriodLib.
                2.Function name : compute
                purpose : Add aggregation to query and fetch data.
                input parameters : metadataQuery, object of Growth class
                output parameters : dataframe
                3.Function name : createAggrs
                purpose : get instance of class present in WhizCalDynamicPeriodLib.
                input parameters : 

              
Key variables - query : implicit variable

                WhizCalDynamicPeriodLib : The name of the object present in WhizCalDynamicPeriodLib.js(library script).
                                         excution scripts can call WhizCalDynamicPeriodLib scripts functions using given object.

                WhizCalLib: The name of the object present in WhizCalLib.js(library script).

                primaryAggr:Reference variable of Aggregation object which is internally exposed by framework,
                            contains reference of first aggregation of MetadataQuery.

                dynamicPeriodObj: object created in library script and passed to execution Script.
                 format: dynamicPeriodObj = {
                            aggrName: "outputMetricName",
                            operation:"percentChange",
                            period1: "2021-01-01T00:00:00.000Z/2021-02-01T23:59:59.999Z"
                            period2: "2022-01-01T00:00:00.000Z/2022-02-01T23:59:59.999Z",
                        }
*/
//**Start of Function code **//

log.info("******************** WhizCalGrowth **********************");

class Growth {
    createAggrs(query, dynamicPeriodObj) {
        //copy(String copyPrefix, MetadataQuery query):Creates deep copy of this aggregation,
        //  name of each aggregation is set as (copyPrefix+current name).
        //interval(String isoInterval):Creates a filter which checks if default time-series column value is withing given time range specified by iso format interval
        const aggrP1 = primaryAggr.copy("aggrP1", query).filter(Filter.interval(dynamicPeriodObj.period1));

        const aggrP2 = primaryAggr.copy("aggrP2", query).filter(Filter.interval(dynamicPeriodObj.period2));

        //calling the function from library script 
        return WhizCalLib.identifyOperation(dynamicPeriodObj.operation, dynamicPeriodObj.aggrName, aggrP1, aggrP2, 0.0);
    };

    compute(query, dynamicPeriodObj) {
        //Creates deep copy from existing query. 
        query = query.copy();

        //replaceOrderingColumn(String currentOrderingColum,String replacementColum)
        //replaces any ordering clauses in the query with given column with ordering on new column
        query.replaceOrderingColumn(primaryAggr.column(), outputMetricName);

        //Adds given aggregation/post-aggregation to the query. 
        query.aggr(this.createAggrs(query, dynamicPeriodObj));

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

//Name for metadata query, this helps logging and identification of query.
query.name("WhizCalGrowth_Query");

//calling the function from library script 
WhizCalDynamicPeriodLib.getComputationObj().compute(query, new Growth());

//** End of Function code **//


/*
 ** configuration to be set in calculation metric **

default_filter: In this section's dynamicArgs sub-section,we have added configurable properties.
                1.operator- type of operation eg.Sum,Division etc

**The configuration below should be copied and pasted to calculate the Market Volume.*
*/

"default_filter": {
    "dynamicArgs": {
        "operator": "percentChange"
    }
}

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