<commit_before>angular.module('viaggia.services.timetable', [])

/**
 * A SERVICE TO WORK WITH TIMETABLE DATA BOTH FROM DB AND FROM SERVER
 */
.factory('ttService', function ($http, $q, $filter, Config, DataManager) {
  var calendarCache = {};

  var ttMapData = {};
  var ttStopData = {};

  var getStopsData = function(agencies) {
    var res = [];
    agencies.forEach(function(a) {
      var local = localStorage[Config.getAppId()+"_stops_"+a];
      if (local) {
        local = JSON.parse(local);
        local.forEach(function(s) {
          s.agencyId = a;
          res.push(s);
        });
      }
    });
    return res;
  };

  var toTrimmedList = function(str) {
    if (!str) return [];
    try {
      var arr = str.split(',');
      var res = [];
      arr.forEach(function(e) {res.push(e.trim());});
      return res;
    } catch(e) {
      console.error('ERROR PARSING ARRAY: '+e);
      return [];
    }
  }

  var getDelays = function(agency, route, date) {
    var deferred = $q.defer();
    var d = new Date(date);
    d.setHours(0);
    d.setMinutes(0);
    d.setSeconds(0);
    d.setMilliseconds(0);
    var from = d.getTime();
    d.setHours(23);
    d.setMinutes(59);
    var to = d.getTime();
    route = encodeURIComponent(route);
    $http.get(Config.getServerURL()+'/gettransitdelays/'+agency+'/'+route+'/'+from+'/'+to,
              Config.getHTTPConfig())
      .success(function(data) {
        if (data && data.delays) deferred.resolve(data.delays[0]);
      })
      .error(function(err) {
        deferred.reject(err);
      });


    return deferred.promise;
  }

  /**
   * transform compressed string to matrix of times. string is a sequence of all trip times separated with '|'. Each time (if presented) has 4 chars.
   */
  var uncompressTime = function(str, rowsCount) {
    if (!str) return [];
    var res = [];
    var column = [];
    var counter = 0;
    for (var i = 0; i < str.length; i++) {
      var c = str.charAt(i);
      if (c == ' ') continue;
      if (c == '|') column.push("");
      else {
        column.push(str.substr(i,2)+":"+str.substr(i+2,2));
        i += 3;
      }
      counter++;
      if (counter == rowsCount) {
        res.push(column);
        column = [];
        counter = 0;
      }
    }
    return res;
  }

  /**
   * Read TT from DB: given a agency/route/date, get a hash and then read stopIds, name,s tripIds, and times from DB.
   * Due to the problem with the plugin (does not escape JSON strings), it is necessary to do 5 DB calls in a row.
   * Parallel calls seems to end up in DB lock sometimes.
   */
  var dataFromHash = function(agency, route, date, deferred, readRoutes) {
    var cal = calendarCache[agency][route];
    dateStr = $filter('date')(date,'yyyyMMdd');
    var hash = route+'_'+cal.mapping[cal.entries[dateStr]];
    var errCB = function(err){
      deferred.reject(err);
    };
    var result = {stops:[],tripIds:[],times:[],stopIds:[], routeIds: []};
    DataManager.doQuery("SELECT * FROM route WHERE agencyID = '"+agency+"' AND linehash = '"+hash+"'",[])
    .then(function(data) {
      if (!data || !data[0] || !data[0].stopsNames) {
        deferred.resolve(result);
        return;
      }
      result.stops = toTrimmedList(data[0].stopsNames);
      result.tripIds = toTrimmedList(data[0].tripIds);
      result.times = uncompressTime(data[0].times,result.stops.length);
      deferred.notify(result);
      getDelays(agency, route, date).then(function(delays){
        result.delays = delays;
        deferred.resolve(result);
      }, function() {
        deferred.resolve(result);
      });
//      DataManager.doQuery("SELECT stopsNames FROM route WHERE agencyID = '"+agency+"' AND linehash = '"+hash+"'",[])
//      .then(function(data) {
//        result.stops = toTrimmedList(data);
//        DataManager.doQuery("SELECT tripIds FROM route WHERE agencyID = '"+agency+"' AND linehash = '"+hash+"'",[])
//        .then(function(data) {<commit_msg>Fix for empty data table loading.<commit_after>angular.module('viaggia.services.timetable', [])

/**
 * A SERVICE TO WORK WITH TIMETABLE DATA BOTH FROM DB AND FROM SERVER
 */
.factory('ttService', function ($http, $q, $filter, Config, DataManager) {
  var calendarCache = {};

  var ttMapData = {};
  var ttStopData = {};

  var getStopsData = function(agencies) {
    var res = [];
    agencies.forEach(function(a) {
      var local = localStorage[Config.getAppId()+"_stops_"+a];
      if (local) {
        local = JSON.parse(local);
        local.forEach(function(s) {
          s.agencyId = a;
          res.push(s);
        });
      }
    });
    return res;
  };

  var toTrimmedList = function(str) {
    if (!str) return [];
    try {
      var arr = str.split(',');
      var res = [];
      arr.forEach(function(e) {res.push(e.trim());});
      return res;
    } catch(e) {
      console.error('ERROR PARSING ARRAY: '+e);
      return [];
    }
  }

  var getDelays = function(agency, route, date) {
    var deferred = $q.defer();
    var d = new Date(date);
    d.setHours(0);
    d.setMinutes(0);
    d.setSeconds(0);
    d.setMilliseconds(0);
    var from = d.getTime();
    d.setHours(23);
    d.setMinutes(59);
    var to = d.getTime();
    route = encodeURIComponent(route);
    $http.get(Config.getServerURL()+'/gettransitdelays/'+agency+'/'+route+'/'+from+'/'+to,
              Config.getHTTPConfig())
      .success(function(data) {
        if (data && data.delays) deferred.resolve(data.delays[0]);
      })
      .error(function(err) {
        deferred.reject(err);
      });


    return deferred.promise;
  }

  /**
   * transform compressed string to matrix of times. string is a sequence of all trip times separated with '|'. Each time (if presented) has 4 chars.
   */
  var uncompressTime = function(str, rowsCount) {
    if (!str) return [];
    var res = [];
    var column = [];
    var counter = 0;
    for (var i = 0; i < str.length; i++) {
      var c = str.charAt(i);
      if (c == ' ') continue;
      if (c == '|') column.push("");
      else {
        column.push(str.substr(i,2)+":"+str.substr(i+2,2));
        i += 3;
      }
      counter++;
      if (counter == rowsCount) {
        res.push(column);
        column = [];
        counter = 0;
      }
    }
    return res;
  }

  /**
   * Read TT from DB: given a agency/route/date, get a hash and then read stopIds, name,s tripIds, and times from DB.
   * Due to the problem with the plugin (does not escape JSON strings), it is necessary to do 5 DB calls in a row.
   * Parallel calls seems to end up in DB lock sometimes.
   */
  var dataFromHash = function(agency, route, date, deferred, readRoutes) {
    var cal = calendarCache[agency][route];
    dateStr = $filter('date')(date,'yyyyMMdd');
    var hash = route+'_'+cal.mapping[cal.entries[dateStr]];
    var errCB = function(err){
      deferred.reject(err);
    };
    var result = {stops:[],tripIds:[],times:[],stopIds:[], routeIds: []};
    DataManager.doQuery("SELECT * FROM route WHERE agencyID = '"+agency+"' AND linehash = '"+hash+"'",[])
    .then(function(data) {
      if (!data || !data[0] || !data[0].stopsNames) {
        deferred.notify(result);
        deferred.resolve(result);
        return;
      }
      result.stops = toTrimmedList(data[0].stopsNames);
      result.tripIds = toTrimmedList(data[0].tripIds);
      result.times = uncompressTime(data[0].times,result.stops.length);
      deferred.notify(result);
      getDelays(agency, route, date).then(function(delays){
        result.delays = delays;
        deferred.resolve(result);
      }, function() {
        deferred.resolve(result);
      });
//      DataManager.doQuery("SELECT stopsNames FROM route WHERE agencyID = '"+agency+"' AND linehash = '"+hash+"'",[])
//      .then(function(data) {
//        result.stops = toTrimmedList(data);
//        DataManager.doQuery("SELECT tripIds FROM route WHERE agencyID = '"+agency+"' AND linehash = '"+hash+"'",[])
//        .then(function(data) {
{'fraction_non_alphanumeric': 0.16589457869037186, 'fraction_numerical': 0.0057593589583072495, 'mean_word_length': 2.625964593735815, 'pattern_counts': {'":': 2, '<': 5, '<?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': '2387158', 'n_tokens_mistral': 2665, 'n_tokens_neox': 2590, 'n_words': 614}