Search code examples
google-apps-scripttriggersquota

Tracking total execution time Google apps script for an account


Google Apps script has the limitation of 6 hours triggered scripts per day per account.

I recently started getting the alerts Service using too much computer time for one day.

Is there a way track the total time of triggered scripts by project?

I found so far this post , but then I'd need to lookup all of my 50 projects.


Solution

    • You want to retrieve the total execution time of functions executed by the time-driven trigger.

    If my understanding is correct, how about this method? By the update of Apps Script API, the method for retrieving the process of project was added. I thought that this can be used for above situation.

    Flow

    1. Retrieve the process list of functions by the method of "processes.list" in Apps Script API.
      • At that time, set the period for retrieving the total execution time using startTime and endTime.
      • As the filter, "TIME_DRIVEN" of the process type is used. By this, the process list of functions executed by the time-driven trigger can be retrieved.
    2. Sum the duration time of each function.

    Sample script:

    Preparation:

    Before you use this script, please enable Apps Script API as follows.

    Enable Apps Script API at API console:
    • On script editor
      • Resources -> Cloud Platform project
      • View API console
      • At Getting started, click "Explore and enable APIs".
      • At left side, click Library.
      • At Search for APIs & services, input "apps script". And click Apps Script API.
      • Click Enable button.
      • If this API has already been enabled, please don't turn off.
    Add scopes:

    Please add the following scopes to the Manifest file (appsscript.json).

    "oauthScopes": [
      "https://www.googleapis.com/auth/script.external_request",
      "https://www.googleapis.com/auth/script.processes"
    ]
    
    • On script editor
      • View -> Show manifest file
      • Add the above property.

    Script:

    Before you run this script, please set startTime and endTime. This is the period for calculating the total time. In this sample script, the total time from 2019-02-07T00:00:00.000Z to 2019-02-08T00:00:00.000Z is calculated.

    function myFunction() {
      var startTime = "2019-02-07T00:00:00.000Z"; // Please set this.
      var endTime = "2019-02-08T00:00:00.000Z"; // Please set this.
    
      var params = {headers: {Authorization: "Bearer " + ScriptApp.getOAuthToken()}};
      var data = [];
      var nextPageToken = "";
      var url = "https://script.googleapis.com/v1/processes";
      do {
        var query = "?fields=%2a" +
        "&pageToken=" + encodeURIComponent(nextPageToken) +
        "&userProcessFilter.types=TIME_DRIVEN" +
        "&userProcessFilter.startTime=" + encodeURIComponent(startTime) +   
        "&userProcessFilter.endTime=" + encodeURIComponent(endTime);
        var res = JSON.parse(UrlFetchApp.fetch(url + query, params).getContentText());
        Array.prototype.push.apply(data, res.processes);
        nextPageToken = res.nextPageToken || "";
      } while (nextPageToken);
      var result = data.reduce(function(obj, e) {
        obj[e.functionName] = obj[e.functionName] ? obj[e.functionName] + Number(e.duration.replace("s", "")) : Number(e.duration.replace("s", ""));
        return obj;
      }, {});
      Logger.log(result)
    }
    

    Result:

    As a sample situation, when 2 functions of myFunction1 and myFunction2 are run by the time-driven trigger, this script returns the following result. The result means that for startTime and endTime, the total execution times of myFunction1 and myFunction2 are 123.456 s and 12.345 s, respectively.

    {
      "myFunction1": 123.456,
      "myFunction2": 12.345
    }
    

    Note:

    • About startTime and endTime, a timestamp in RFC3339 UTC "Zulu" format, accurate to nanoseconds. Example: 2014-10-02T15:01:23.045123456Z. This is from the official document.
    • This is a simple sample script. So please modify it to your situation.

    References:

    If this was not useful for your situation, I apologize.