Utils/Histogram.js

/**Copyright (c) 2009-2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.**/
"use strict";const Utils=require("./Utils"),Validator=require("./Validator"),InvalidInputError=require("../Errors/InvalidInputError");
/** 
 * Class which defines Histogram
 * @memberof mdxWebApiCore.Utils
 * */
class Histogram{static#e=5;static#t=20;static#i=1e3;
/**
      * returns default histogram bucket count  
      * @public
      * @static
      * @returns {number} - Default histogram bucket count (an integer) is returned
      * @example
      * const mdx = require("@nvidia-mdx/web-api-core");
      * let defaultHistogramBucketCount = mdx.Utils.Histogram.getDefaultHistogramBucketCount();
      */
static getDefaultHistogramBucketCount(){return this.#t}
/**
      * returns max histogram bucket count  
      * @public
      * @static
      * @returns {number} - Max histogram bucket count (an integer) is returned
      * @example
      * const mdx = require("@nvidia-mdx/web-api-core");
      * let maxHistogramBucketCount = mdx.Utils.Histogram.getMaxHistogramBucketCount();
      */static getMaxHistogramBucketCount(){return this.#i}
/**
      * returns bucket size in seconds  
      * @public
      * @static
      * @param {Object} input - Input object.
      * @param {number} [input.bucketCount=20] - bucketCount must be an integer.
      * @param {string} input.fromTimestamp
      * @param {string} input.toTimestamp
      * @returns {number} - Bucket size in seconds (an integer) is returned
      * @example
      * const mdx = require("@nvidia-mdx/web-api-core");
      * let bucketSizeInSec = mdx.Utils.Histogram.computeBucketSizeInSec({fromTimestamp: "2023-01-12T11:20:10.000Z", toTimestamp: "2023-01-12T14:20:10.000Z"});
      */static computeBucketSizeInSec(e){const t={type:"object",additionalProperties:{not:!0,errorMessage:"Invalid additional Input ${0#}."},properties:{bucketCount:{type:"integer",minimum:1,default:this.#t,maximum:this.#i,errorMessage:{type:"bucketCount is not an integer.",minimum:"bucketCount can have a minimum value of 1.",maximum:`bucketCount can have a maximum value of ${this.#i}.`}},fromTimestamp:{type:"string"},toTimestamp:{type:"string"}},required:["fromTimestamp","toTimestamp"],errorMessage:{required:"Input should have required properties 'fromTimestamp' and 'toTimestamp'."}};let i=Validator.validateJsonSchema(e,t);if(!i.valid)throw new InvalidInputError(i.reason);let a=Validator.isValidTimeRange(e.fromTimestamp,e.toTimestamp);if(!a.valid)throw new InvalidInputError(a.reason);if(!Number.isFinite(e.bucketCount))throw new InvalidInputError("bucketCount is not a finite integer.");let r=new Date(e.toTimestamp)-new Date(e.fromTimestamp),o=Math.ceil(r/(1e3*e.bucketCount)),n=o%this.#e;return 0!=n&&(o=o+this.#e-n),o}
/**
      * returns an empty histogram
      * @public
      * @static
      * @param {Object} input - Input object.
      * @param {number} input.bucketSizeInSec - bucketSizeInSec must be an integer.
      * @param {string} input.fromTimestamp
      * @param {string} input.toTimestamp
      * @returns {Array<start:string,end:string>} - An empty histogram with start and end timestamps are returned
      * @example
      * const mdx = require("@nvidia-mdx/web-api-core");
      * let emptyHistogram = mdx.Utils.Histogram.getEmptyHistogram({ bucketSizeInSec:600, fromTimestamp: "2023-01-12T11:20:10.000Z", toTimestamp: "2023-01-12T14:20:10.000Z"});
      */static getEmptyHistogram(e){let t=Validator.validateJsonSchema(e,{type:"object",additionalProperties:{not:!0,errorMessage:"Invalid additional Input ${0#}."},properties:{bucketSizeInSec:{type:"integer",minimum:1,errorMessage:{type:"bucketSizeInSec is not an integer.",minimum:"bucketSizeInSec can have a minimum value of 1."}},fromTimestamp:{type:"string"},toTimestamp:{type:"string"}},required:["bucketSizeInSec","fromTimestamp","toTimestamp"],errorMessage:{required:"Input should have required properties 'bucketSizeInSec', 'fromTimestamp' and 'toTimestamp'."}});if(!t.valid)throw new InvalidInputError(t.reason);let i=Validator.isValidTimeRange(e.fromTimestamp,e.toTimestamp);if(!i.valid)throw new InvalidInputError(i.reason);let a=new Array,r=new Date(Math.floor(new Date(e.fromTimestamp)/(1e3*e.bucketSizeInSec))*(1e3*e.bucketSizeInSec)).toISOString();for(;Utils.tsCompare(r,"<",e.toTimestamp);){let t=new Date(new Date(r).valueOf()+1e3*e.bucketSizeInSec).toISOString();a.push({start:r,end:t}),r=t}return a}}module.exports=Histogram;