function results()

in source/results-parser/lib/parser/index.js [22:91]


function results(content, testId) {
    console.log(`Processing results, testId: ${testId}`);

    try {
        const options = {
            nativeType: true,
            compact: true,
            ignoreAttributes: false
        };
        const json = parser.xml2js(content, options);
        const jsonData = json.FinalStatus;
        let labels = [];
        let result = {};

        console.log(`xml to json: ${JSON.stringify(jsonData, null, 2)}`);

        // loop through results
        for (let i = 0; i < jsonData.Group.length; i++) {
            const group = jsonData.Group[i];
            const label = group._attributes.label;
            let stats = {
                rc: []
            };

            // loop through group results
            for (let r in group) {
                if (r !== '_attributes' && r !== 'perc' && r !== 'rc') {
                    stats[r] = group[r].value._text;
                }
            }

            // loop through response codes, rc is a object for single responses array for multiple
            if (Array.isArray(group.rc)) {
                for (let j = 0; j < group.rc.length; j++) {
                    if (group.rc[j]._attributes.param !== '200') {
                        stats.rc.push({ code: group.rc[j]._attributes.param, count: parseInt(group.rc[j]._attributes.value) });
                    }
                }
            } else {
                if (group.rc._attributes.param !== '200') {
                    stats.rc.push({ code: group.rc._attributes.param, count: parseInt(group.rc._attributes.value) });
                }
            }

            // loop through percentiles and rename/convert keys to strings
            for (let j = 0; j < group.perc.length; j++) {
                const perc = 'p' + group.perc[j]._attributes.param.replace('.', '_');
                stats[perc] = group.perc[j].value._text;
            }
            // check if the resuts are for the group (label '') or for a specific label
            // label '' is the average results for all the labels.
            if (label) {
                stats.label = label;
                labels.push(stats);
            } else {
                result = stats;
            }
        }
        result.testDuration = jsonData.TestDuration._text;

        return {
            stats: result,
            labels,
            duration: jsonData.TestDuration._text
        };
    } catch (error) {
        console.error('results function error', error);
        throw error;
    }
}