Actor picture

24 Hour Stats

mtrunkat/24-hour-stats

This act can be used as synchronous API. Returns a JSON containing actor runs finished in the last 24 hours along with information about their default datasets and request queues. Actors might be filtered via input array "actIds".

No credit card required

Author's avatarMarek Trunkát
  • Modified
  • Users2
  • Runs98

Based on the apify/actor-node-basic:v0.21.10 Docker image (see docs).

This file is 121 lines long. Only the first 50 are shown. Show all

const Apify = require('apify');
const moment = require('moment');
const Promise = require('bluebird');
const _ = require('underscore');


const utils = require('apify-client/build/utils');
const originalRequestPromise = utils.requestPromise;
utils.requestPromise = (options, iteration = 0) => {
    const startedAt = new Date();
    //console.log(`${options.method} ${iteration} ${options.url} ${options.body ? options.body.url : 'null'}`);
    return originalRequestPromise(options, iteration)
        .then((response) => {
            //console.log(JSON.stringify(response)); 
            console.log(`${options.url} ${(Date.now() - startedAt) / 1000}`);
            return response;
        });
};

const MAX_OFFSET_LIMIT = 30000;
const RUN_FIELDS = [
    'id', 
    'actId', 
    'startedAt', 
    'finishedAt', 
    'status', 
    'defaultDatasetId', 
    'defaultRequestQueueId', 
    'defaultKeyValueStoreId',
];

const getRunsFromPeriod = async (actId, from, to) => {
    const runs = [];
    let offset = 0;

    while (true) {
        let allFetched = true;
        const response = await Apify.client.acts.listRuns({ actId, offset, desc: true });

        response
            .items
            .filter(run => !run.finishedAt || run.finishedAt > from)
            .forEach((run) => {
                run.actId = actId;
                runs.push(run);
                allFetched = false;
            });

        offset += response.items.length;