<commit_before>/**
 * Created by elad.benedict on 8/26/2015.
 */

var Q = require('q');
var _ = require('underscore');
var kalturaVO = require('./kaltura-client-lib/KalturaVO');
var kalturaClient = require('./kaltura-client-lib/KalturaClient');
var kalturaClientBase = require('./kaltura-client-lib/KalturaClientBase');
var kalturaTypes = require('./kaltura-client-lib/KalturaTypes');
var config = require('./../common/Configuration');
var logger = require('./logger/logger')(module);

var BackendClient = (function(){

    var createClientConfig = function () {
        var conf = new kalturaClientBase.KalturaConfiguration(partnerId);
        conf.serviceUrl = config.get('backendClient').serviceUrl;
        return conf;
    };

    var convertToMs = function (minutes) {
        return minutes * 60 * 1000;
    };

    var lastSessionTime = null;
    var adminSecret = config.get('backendClient').adminSecret;
    var partnerId = config.get('backendClient').partnerId;
    var mediaServerHostname = config.get('mediaServer').hostname;
    var client = null;
    var clientConfig = createClientConfig();
    var sessionDuration = convertToMs(config.get('backendClient').ksSessionRefreshIntervalMinutes);

    var BackendClient = {};

    var createSession = function () {

        var deferred = Q.defer();
        client = new kalturaClient.KalturaClient(clientConfig);
        client.session.start(function(ks, err){
            if (err){
                logger.error("Failed to create session", err);
                deferred.reject(err);
            }
            if(ks){
                logger.info('session created successfully. ks: ' + ks);
                client.setSessionId(ks);
                deferred.resolve();
            }
        }, adminSecret, null, kalturaTypes.KalturaSessionType.ADMIN, partnerId, null, null);

        return deferred.promise;
    };

    var getLiveEntries = function () {

        var deferred = Q.defer();
        var filter = new kalturaVO.KalturaLiveStreamEntryFilter();
        filter.isLive = kalturaTypes.KalturaNullableBoolean.TRUE_VALUE;
        filter.hasMediaServerHostname = mediaServerHostname;
        var pager = null;
        client.liveStream.listAction(function (results) {
            if (results) {
                var dvrWindow = config.get("dvrWindow");
                var objs = _.map(results.objects, function (item) {
                    if (item.dvrStatus === kalturaTypes.KalturaDVRStatus.DISABLED) {
                        dvrWindow = config.get("liveDvrWindow");
                    }
                    return {
                        "dvrWindow": dvrWindow,
                        "entryId" : item.id
                    };
                });
                logger.debug('Got live entries: ' + JSON.stringify(objs));
                deferred.resolve(objs);
            }
            else {<commit_msg>variable initialization was done in the wrong place (and consequently wasn't initialized in subsequent iterations)<commit_after>/**
 * Created by elad.benedict on 8/26/2015.
 */

var Q = require('q');
var _ = require('underscore');
var kalturaVO = require('./kaltura-client-lib/KalturaVO');
var kalturaClient = require('./kaltura-client-lib/KalturaClient');
var kalturaClientBase = require('./kaltura-client-lib/KalturaClientBase');
var kalturaTypes = require('./kaltura-client-lib/KalturaTypes');
var config = require('./../common/Configuration');
var logger = require('./logger/logger')(module);

var BackendClient = (function(){

    var createClientConfig = function () {
        var conf = new kalturaClientBase.KalturaConfiguration(partnerId);
        conf.serviceUrl = config.get('backendClient').serviceUrl;
        return conf;
    };

    var convertToMs = function (minutes) {
        return minutes * 60 * 1000;
    };

    var lastSessionTime = null;
    var adminSecret = config.get('backendClient').adminSecret;
    var partnerId = config.get('backendClient').partnerId;
    var mediaServerHostname = config.get('mediaServer').hostname;
    var client = null;
    var clientConfig = createClientConfig();
    var sessionDuration = convertToMs(config.get('backendClient').ksSessionRefreshIntervalMinutes);

    var BackendClient = {};

    var createSession = function () {

        var deferred = Q.defer();
        client = new kalturaClient.KalturaClient(clientConfig);
        client.session.start(function(ks, err){
            if (err){
                logger.error("Failed to create session", err);
                deferred.reject(err);
            }
            if(ks){
                logger.info('session created successfully. ks: ' + ks);
                client.setSessionId(ks);
                deferred.resolve();
            }
        }, adminSecret, null, kalturaTypes.KalturaSessionType.ADMIN, partnerId, null, null);

        return deferred.promise;
    };

    var getLiveEntries = function () {

        var deferred = Q.defer();
        var filter = new kalturaVO.KalturaLiveStreamEntryFilter();
        filter.isLive = kalturaTypes.KalturaNullableBoolean.TRUE_VALUE;
        filter.hasMediaServerHostname = mediaServerHostname;
        var pager = null;
        client.liveStream.listAction(function (results) {
            if (results) {
                var objs = _.map(results.objects, function (item) {
                    var dvrWindow = config.get("dvrWindow");
                    if (item.dvrStatus === kalturaTypes.KalturaDVRStatus.DISABLED) {
                        dvrWindow = config.get("liveDvrWindow");
                    }
                    return {
                        "dvrWindow": dvrWindow,
                        "entryId" : item.id
                    };
                });
                logger.debug('Got live entries: ' + JSON.stringify(objs));
                deferred.resolve(objs);
            }
            else {
{'fraction_non_alphanumeric': 0.11671494292042937, 'fraction_numerical': 0.004430056227636736, 'mean_word_length': 2.266555370061213, 'pattern_counts': {'":': 2, '<': 3, '<?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': '2716844', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1564, 'n_words': 322}