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

Dockerfile

# This is a template for a Dockerfile used to run acts in Actor system.
# The base image name below is set during the act build, based on user settings.
# IMPORTANT: The base image must set a correct working directory, such as /usr/src/app or /home/user
FROM apify/actor-node-basic:v0.21.10

# Second, copy just package.json and package-lock.json since it should be
# the only file that affects "npm install" in the next step, to speed up the build
COPY package*.json ./

# Install NPM packages, skip optional and development dependencies to
# keep the image small. Avoid logging too much and print the dependency
# tree for debugging
RUN npm --quiet set progress=false \
 && npm install --only=prod --no-optional \
 && echo "Installed NPM packages:" \
 && (npm list --all || true) \
 && echo "Node.js version:" \
 && node --version \
 && echo "NPM version:" \
 && npm --version

# Copy source code to container
# Do this in the last step, to have fast build if only the source code changed
COPY  . ./

# NOTE: The CMD is already defined by the base image.
# Uncomment this for local node inspector debugging:
# CMD [ "node", "--inspect=0.0.0.0:9229", "main.js" ]

package.json

{
    "name": "apify-project",
    "version": "0.0.1",
    "description": "",
    "author": "It's not you it's me",
    "license": "ISC",
    "dependencies": {
        "apify": "0.21.10",
        "moment": "latest",
        "bluebird": "latest",
        "underscore": "latest"
    },
    "scripts": {
        "start": "node main.js"
    }
}

main.js

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;