<commit_before>
import amqp = require('amqp');
import mongodb = require('mongodb');
import assert = require('assert');
import sleep = require('sleep');

// sleep to wait some time to get the other services startet.
// TODO: Check until the service port is available
sleep.sleep(3);

var MongoClient = mongodb.MongoClient;

var mydb = null;

// store event in DB
var createDBitem = function(data) {
	assert.notEqual(mydb, null, 'Exiting because db connection is null!');
    
    var collection = mydb.collection('events');
    collection.insert(data, null, function(err, res) {
       assert.equal(null, err, 'Error on writing object to DB');
       console.log('Successfully wrote to DB');
    });
};

MongoClient.connect('mongodb://192.168.59.103/EPCIS', function(err, db) {
  assert.equal(null, err);
  mydb = db;
  console.log("Connected correctly to MongoDB server");
});

var connection = amqp.createConnection({
    host: '192.168.59.103',
    login: 'admin',
    password: '<PASSWORD>'
});

connection.on('ready', function () {
    connection.queue('input.json', { autoDelete: false }, function (queue) {
        queue.bind(routing='input.json');
        console.log(' [*] Waiting for messages. To exit press CTRL+C');

        queue.subscribe(function (msg) {
            var message = msg.data.toString('utf-8');
            //console.log(" [x] Received %s", message);
			
            var jsonObj = JSON.parse(message);
            var aggregationEvents:Array<any> = jsonObj['aggregationEvents'];
            if(aggregationEvents) {
                aggregationEvents.forEach(function (element) {
                    createDBitem(element);
                });
            }
        });
    });
});


connection.on('error', function (err) {

    console.log("Error");
    console.log(err);
});<commit_msg>storage: Concat all supported events to store them<commit_after>
import amqp = require('amqp');
import mongodb = require('mongodb');
import assert = require('assert');
import sleep = require('sleep');

// sleep to wait some time to get the other services startet.
// TODO: Check until the service port is available
sleep.sleep(3);

var MongoClient = mongodb.MongoClient;

var mydb = null;

// store event in DB
var createDBitem = function(data) {
	assert.notEqual(mydb, null, 'Exiting because db connection is null!');
    
    var collection = mydb.collection('events');
    collection.insert(data, null, function(err, res) {
       assert.equal(null, err, 'Error on writing object to DB');
       console.log('Successfully wrote to DB');
    });
};

MongoClient.connect('mongodb://192.168.59.103/EPCIS', function(err, db) {
  assert.equal(null, err);
  mydb = db;
  console.log("Connected correctly to MongoDB server");
});

var connection = amqp.createConnection({
    host: '192.168.59.103',
    login: 'admin',
    password: '<PASSWORD>'
});

connection.on('ready', function () {
    connection.queue('input.json', { autoDelete: false }, function (queue) {
        queue.bind(routing='input.json');
        console.log(' [*] Waiting for messages. To exit press CTRL+C');

        queue.subscribe(function (msg) {
            var message = msg.data.toString('utf-8');
            //console.log(" [x] Received %s", message);
			
            var jsonObj = JSON.parse(message);

            var events = concatEvents(jsonObj);
            if(events) {
                events.forEach(function (element) {
                    createDBitem(element);
                });
            }
        });
    });
});

// just concat all events that we currently support
function concatEvents(obj:any): Array<any> {
    var events:Array<any> = new Array<any>();

    var eventNames:Array<string> = ['objectEvents', 'aggregationEvents', 'transactionEvents'];

    eventNames.forEach(function (eventName) {
        try {
            var tmpEvents:Array<any> = obj[eventName];
            if(tmpEvents) {
                events = events.concat(tmpEvents);
            }
        } catch (err) {
            console.log('Could not add events: ' + eventName + ' Error: ' + err);
        }
    });

    return events;
}

connection.on('error', function (err) {

    console.log("Error");
    console.log(err);
});
{'fraction_non_alphanumeric': 0.1464226289517471, 'fraction_numerical': 0.011409555502733539, 'mean_word_length': 2.832422586520947, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '396778', 'n_tokens_mistral': 1278, 'n_tokens_neox': 1212, 'n_words': 317}