Convert static node,js request to iterative batch to avoid API limit

153 Views Asked by At

TL;DR - I'm not a developer and need some help troubleshooting an error I'm getting in code below

Update: I've got some working code, but I'm getting an intermittent error in visual studio code for an unhandled promise rejection and I'm not sure why. Sometimes the code runs just fine, other times, I get the error in the first code block

Response server [ny-front-api31] session [384923350]: 
{"code":"INTERNAL_SERVERL_ERROR","message":"Internal server error 
occurred","objectType":"KalturaAPIException","args":[]}    
(node:34340) UnhandledPromiseRejectionWarning: #<Object>
(node:34340) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 
This error originated either by throwing inside of an async function without 
a catch block, or by rejecting a promise which was not handled with 
.catch(). (rejection id: 1)
(node:34340) [DEP0018] DeprecationWarning: Unhandled promise rejections are 
deprecated. In the future, promise rejections that are not handled will 
terminate the Node.js process with a non-zero exit code.

Here is the code I am running:

const kaltura = require('kaltura-client');
const sql = require('./UserEngagementSQL');
const moment = require('moment');

const config = new kaltura.Configuration();
config.serviceUrl = 'https://www.kaltura.com';
const client = new kaltura.Client(config);

function process(pageIndex, start, end) {
    return new Promise(function (resolve, reject) {
        kaltura.services.session.start(
            "omitted",
            "omitted",
            kaltura.enums.SessionType.ADMIN,
            omitted)
            .completion((success, ks) => {
                if (!success) throw new Error(ks.message);
                client.setKs(ks);
                let reportType = kaltura.enums.ReportType.USER_ENGAGEMENT;
                let reportInputFilter = new kaltura.objects.EndUserReportInputFilter();
                reportInputFilter.fromDay = start;
                reportInputFilter.toDay = end;
                let pager = new kaltura.objects.FilterPager();
                pager.pageIndex = pageIndex;
                pager.pageSize = 500;
                let order = "";
                let objectIds = "";

                kaltura.services.report.getTable(reportType, reportInputFilter, pager, order, objectIds)
                    .execute(client)
                    .then(async result => {
                        if (result.data){
                            const data = result.data.split(';');
                            for (var i = 0; i < data.length; i++) {
                                const row = data[i].split(',');
                                if (row[0].length > 0) {
                                    await sql.insert(row);
                                }
                            }
                            resolve(data.length);
                        }else{
                            resolve(0);
                        } 
                    });

            })
            .execute(client);
    });
}

async function main() {
    const initDate = moment('08/01/2018');
    const startDate = initDate.format('YYYYMMDD');
    const endDate = moment(startDate).add(30, 'day').format('YYYYMMDD');
    let startingIndex = 1;

    //console.log(startDate, endDate);
    let shouldRun = true;
    while(shouldRun){
        let processed =  await process(startingIndex++, startDate, endDate);
        console.log('Number of processed records ' + processed);
        shouldRun = processed > 0;
        console.log('Processing page ' + startingIndex + ' Should Keep running ' + shouldRun);
    }
}

main();

Original Post: I'm working with the Kaltura API and they've got some pretty good starter console queries that are almost exactly what I need as a non-developer to get the data I am after. I needed the below code to be able to get all records for a date range. I have a 10000 record limit per call, so I needed loop through the request and continually check for records

0

There are 0 best solutions below