Toggle menu

countHistories()

Description

This function counts histories on the database. The histories that are counted can be selected by a filter, combination of filters, or "buckets". Buckets are used to define date ranges, and a count of histories in each range is returned. Results can also be grouped by label.

Parameters

NameTypeDescription
datasourceString, optionalThe name of the datasource from where histories will be retrieved. This parameter overrides the same parameter in the worker configuration which in turn overrides the default datasource
bucketsObject, optionalDefine a set of date ranges and count the histories in each range by created or last updated. If the count is zero nothing is returned (ie there isn't an empty bucket in the results).

buckets and filters cannot be supplied in the same call
 buckets.keyString, requiredEither created or lastupdated
buckets.ranges[n].nameString, requiredA name for the bucket, returned with the results
buckets.ranges[n].fromInteger, requiredThe beginning of the range (inclusive) measured in ms since the epoch
buckets.ranges[n].toInteger, requiredThe end of the range (exclusive) measured in ms since the epoch
filterFilter-object, optionalThe filter specifies which histories will be counted. The syntax of filter objects relates to many methods and is explained in Filter Objects
filtersArray, optionalThe filters array contains filter-objects and specifies multiple groups of histories to count

filter and filters cannot be supplied in the same call
getsqlBoolean, optionalIf true the return structure will include an element called "sql" which contains the query and the parameters which were used to retrieve the history data from the database. This option returns a large amount of data and is intended for testing and debugging

This parameter cannot be used with the filters parameter - it only works when counting all histories or when using a single filter
groupbyString or array of strings, optionalA single history label (labela/labelb/labelc/labeld/labele) or array of labels. Results will be grouped by matching label(s) and a count returned for each group

groupby and filters cannot be supplied in the same call
sealedBoolean, optionalIf sealed is true, then only histories that have been switched into read-only mode will be searched. If sealed is false then only writable histories will be searched. If sealed is not supplied, then all histories will be searched

Returns

If no parameters, or the filter parameter was set, the result structure will contain an integer - the number of histories that match.

If the filters parameter was set, the result structure will contain an array of integers - the numbers of histories that match each filter.

If the groupby parameter was set, the result structure will contain an array of objects. Each object will have a count (integer) and the label(s) grouped by.

If the buckets parameter was set, the result structure will contain an array of objects, with each object representing a bucket.

Examples

Count with Filters

This first example counts two types of history.

function(params, credentials) {
    let resp = this.callWorkerMethod("history", "countHistories", {
        "filters": [{
            "key": "labela",
            "EQ": "Feedback Review"
        }, {
            "key": "labela",
            "EQ": "My History"
        }]
    });
    return resp;
}

{
    "id": 145,
    "result": [6, 0],
    "jsonrpc": "2.0"
}

Group Results from a Single Filter

This example counts histories created between December 2018 and December 2019 and groups the results by their labela value.

function(params, credentials) {
    let resp = this.callWorkerMethod("history", "countHistories", {
        "filter": {
            "AND": [{
                "key": "created",
                "GT": 1544616000000
            }, {
                "key": "created",
                "LT": 1576152000000
            }]
        },
        "groupby": ["labela"]
    });
    return resp;
}

{
    "id": 149,
    "result": [{
        "count": 4,
        "labela": "File References"
    }, {
        "count": 3,
        "labela": "Form submitted"
    }, {
        "count": 3,
        "labela": "LiveChat"
    }, {
        "count": 4,
        "labela": "Object Export"
    }, {
        "count": 124,
        "labela": "Workflow"
    }],
    "jsonrpc": "2.0"
}

Count Histories Created Each Month Using Buckets

This example still looks for histories created between December 2018 and December 2019 but also uses buckets. Only histories that fit into the defined buckets are counted, those created in the other months of the year are ignored.

function(params, credentials) {
    let resp = this.callWorkerMethod("history", "countHistories", {
        "filter": {
            "AND": [{
                "key": "created",
                "GT": 1544616000000
            }, {
                "key": "created",
                "LT": 1576152000000
            }]
        },
        "buckets": {
            "key": "created",
            "ranges": [
                {"name": "jul", "from":1561939200000, "to":1564617600000},
                {"name": "aug", "from":1564617600000, "to":1567296000000},
                {"name": "sep", "from":1567296000000, "to":1569888000000}
            ]
        }
    });
    return resp;
}

{
    "id": 153,
    "result": [{
        "bucket": "jul",
        "count": 31
    }, {
        "bucket": "aug",
        "count": 31
    }, {
        "bucket": "sep",
        "count": 31
    }],
    "jsonrpc": "2.0"
}

Combine Buckets and Groups

Finally, you can use buckets and groupby in combination. Histories are grouped by label within their date ranges.

function(params, credentials) {
    let resp = this.callWorkerMethod("history", "countHistories", {
        "filter": {
            "AND": [{
                "key": "created",
                "GT": 1561939200000
            }, {
                "key": "created",
                "LT": 1576152000000
            }]
        },
        "buckets": {
            "key": "lastupdated",
            "ranges": [
                {"name": "jul", "from":1561939200000, "to":1564617600000},
                {"name": "aug", "from":1564617600000, "to":1567296000000},
                {"name": "sep", "from":1567296000000, "to":1569888000000}
            ]
        },
        "groupby": "labela"
    });
    return resp;
}

{
    "id": 157,
    "result": [{
        "bucket": "jul",
        "count": 31,
        "labela": "Workflow"
    }, {
        "bucket": "aug",
        "count": 31,
        "labela": "Workflow"
    }, {
        "bucket": "sep",
        "count": 1,
        "labela": "File References"
    }, {
        "bucket": "sep",
        "count": 30,
        "labela": "Workflow"
    }],
    "jsonrpc": "2.0"
}

Last modified on August 01, 2023

Share this page

Facebook icon Twitter icon email icon

Print

print icon