1

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

mdd
  • 43
  • 4
  • It's not clear how you want your iteration to work? Do you want to call `getTable()` for one day, get those results, write them to the sql database, then repeat that for the next day in your sequence until you've finally gotten all the days? Or, do you need to break the iteration into even smaller granules? – jfriend00 Oct 04 '18 at 02:44
  • @jfriend00 if I knew that each request would result in less than 10000 records I could just do one loop for each day, but since there is a possibility that more than 10000 records exist in a single pull, I think I need to loop through 2 different ways, right? Otherwise, yes, I'd be willing to call getTable() for a day, write to the db, and then repeat. – mdd Oct 04 '18 at 14:30
  • So, what are you asking here? Are you asking how to reliably break the query into less than 10,000 record each? Are you asking how to avoid rate limiting by the service? Are you asking how to chain multiple asynchronous requests one after another in sequence? I don't know what you're really asking. FYI, I could help with the last two (rate limiting or sequencing), but not the first one because I don't know how your queries or the service work. – jfriend00 Oct 04 '18 at 21:12
  • @jfriend00 yes! I'm asking how to chain multiple asynchronous requests one after another in sequence! Apologies, sometimes knowing the right question to ask is the hardest part. – mdd Oct 05 '18 at 14:48
  • By the way, notice that you init `startingIndex` with 1 but when call to `process` you increment it so the first time you call `process` it is actually give you index 2... – dWinder Nov 27 '18 at 15:25

0 Answers0