<commit_before>'use strict';

var kafkaConsumer = function (esClient) {
    var getElasticSearchData = require('../tracesConverter').toElasticSearch;
    var Q = require('q');

    return {
        addTraces: function (playerId, versionId, gameplayId, data) {
            var deferred = Q.defer();

            var traces = [];
            var i;
            for (i = 0; i < data.length; ++i) {
                var realtimeData = getElasticSearchData(data[i]);
                if (realtimeData) {
                    traces.push(realtimeData);
                }
            }
            if (traces.length === 1) {
                esClient.create({
                    index: versionId,
                    type: 'traces',
                    body: traces[0]
                }, function (error, response) {
                    if (error) {
                        return deferred.reject(error);
                    }
                    deferred.resolve();
                });
            } else {

                var indexCommand = {create: {_index: versionId, _type: 'traces'}};
                var commands = [];
                for (i = 0; i < traces.length; ++i) {
                    commands.push(indexCommand, traces[i]);
                }

                esClient.bulk({
                    body: commands
                }, function (error, response) {
                    if (error) {
                        return deferred.reject(error);
                    }
                    deferred.resolve();
                });
            }

            return deferred.promise;<commit_msg>Fixed elasticsearch error when sending one trace<commit_after>'use strict';

var kafkaConsumer = function (esClient) {
    var getElasticSearchData = require('../tracesConverter').toElasticSearch;
    var Q = require('q');

    return {
        addTraces: function (playerId, versionId, gameplayId, data) {
            var deferred = Q.defer();

            var traces = [];
            var i;
            for (i = 0; i < data.length; ++i) {
                var realtimeData = getElasticSearchData(data[i]);
                if (realtimeData) {
                    traces.push(realtimeData);
                }
            }


            var indexCommand = {create: {_index: versionId, _type: 'traces'}};
            var commands = [];
            for (i = 0; i < traces.length; ++i) {
                commands.push(indexCommand, traces[i]);
            }

            esClient.bulk({
                body: commands
            }, function (error, response) {
                if (error) {
                    console.log(error);
                    return deferred.reject(error);
                }
                deferred.resolve();
            });

            return deferred.promise;
{'fraction_non_alphanumeric': 0.11027298850574713, 'fraction_numerical': 0.0021551724137931034, 'mean_word_length': 1.2120730738681493, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4798676', 'n_tokens_mistral': 734, 'n_tokens_neox': 671, 'n_words': 161}