<commit_before>(function() {
  'use strict';

  var MIN_DISTANCE = 50;
  var MIN_STOP_DURATION = 5 * 60 * 1000; // 5 minutes
  var MIN_SPEED = 5 * 1000 / 3600000; // 5 km/h

  angular
    .module('app.trips')
    .controller('TripCtrl', TripCtrl)
    .controller('StopTripCtrl', StopTripCtrl)
    .controller('CommentTripCtrl', CommentTripCtrl);

  TripCtrl.$inject = [
    '$scope', '$state', '$stateParams', '$q', '$ionicModal', '$log',
    'tripsFactory', 'pusherService', 'authService', 'loadService',
    'initGeoService', 'eventsFactory', 'createObjectId', 'toasterService',
    'geolib',
  ];
  /* @ngInject */
  function TripCtrl(
    $scope, $state, $stateParams, $q, $ionicModal, $log,
    tripsFactory, pusherService, authService, loadService,
    initGeoService, eventsFactory, createObjectId, toasterService,
    geolib
  ) {
    var geoService = initGeoService($scope);
    var lastPosition = null;

    $scope.trip = null;
    $scope.canStopTrip = false;
    $scope.goToUser = goToUser;
    $scope.mapClassEvent = mapClassEvent;
    $scope.goToMap = goToMap;
    $scope.stopTrip = stopTrip;
    $scope.closeStopTrip = closeStopTrip;
    $scope.commentTrip = commentTrip;
    $scope.closeCommentTrip = closeCommentTrip;
    $scope.togglePositionTracking = togglePositionTracking;
    $scope.refresh = activate;

    $scope.trackingPosition = false;

    activate();

    pusherService.subscribe($scope, 'trips-' + $stateParams.trip_id, {
      A_TRIP_UPDATED: $scope.refresh.bind($scope),
      A_TRIP_DELETED: $state.go.bind($state, 'app.trips'),
    });

    function activate() {
      $scope.canStopTrip = false;
      $q.all(loadService.loadState($scope, {
        profile: authService.getProfile(),
        trip: tripsFactory.get($stateParams.trip_id),
      }))
      .then(function(data) {
        $scope.profile = data.profile;
        $scope.trip = data.trip.data;
        $scope.segments = computeSegmentFromEvents($scope.trip.events);
        console.log($scope.segments)
        lastPosition = computeLastPositionFromEvents($scope.trip.events);
        render();
      });
    }

    function render() {
      $scope.canStopTrip = $scope.trip.owner_id === $scope.profile._id &&
        !$scope.trip.ended_date;
      $scope.trackingPosition = geoService.watching();
    }

    function computeSegmentFromEvents(events) {
      var segments = events.reduce(function(segments, event) {
        var curSegment = segments[segments.length -1];
        var previousPoint = curSegment.points[curSegment.points.length -1];

        // Non geo events are simply appended
        if('geo' !== event.contents.type.split('-')[1]) {
          curSegment.events.push(event);
          return segments;
        }

        // Geo events are merged according to elapsed time
        if(!previousPoint) {
          curSegment.points.push(event);
          return segments;
        }
        event.elapsedTime = (new Date(event.created_date)).getTime() -
          (new Date(previousPoint.created_date)).getTime();
        event.distance = geolib.getDistance({
          latitude: previousPoint.contents.geo[0],
          longitude: previousPoint.contents.geo[1],
          altitude: previousPoint.contents.geo[1],
        }, {
          latitude: event.contents.geo[0],
          longitude: event.contents.geo[1],
          altitude: event.contents.geo[1],
        });
        if(
          MIN_STOP_DURATION < event.elapsedTime &&
          MIN_SPEED > event.distance / event.elapsedTime
        ) {
          segments.push({
            events: [],
            points: [event],
            distance: event.distance,
            elapsedTime: event.elapsedTime,
          });
        } else {
          curSegment.points.push(event);
          curSegment.distance += event.distance;
          curSegment.elapsedTime += event.elapsedTime;
        }
        return segments;
      }, [{<commit_msg>Segments must have at least two points to be splitted<commit_after>(function() {
  'use strict';

  var MIN_DISTANCE = 50;
  var MIN_STOP_DURATION = 5 * 60 * 1000; // 5 minutes
  var MIN_SPEED = 5 * 1000 / 3600000; // 5 km/h

  angular
    .module('app.trips')
    .controller('TripCtrl', TripCtrl)
    .controller('StopTripCtrl', StopTripCtrl)
    .controller('CommentTripCtrl', CommentTripCtrl);

  TripCtrl.$inject = [
    '$scope', '$state', '$stateParams', '$q', '$ionicModal', '$log',
    'tripsFactory', 'pusherService', 'authService', 'loadService',
    'initGeoService', 'eventsFactory', 'createObjectId', 'toasterService',
    'geolib',
  ];
  /* @ngInject */
  function TripCtrl(
    $scope, $state, $stateParams, $q, $ionicModal, $log,
    tripsFactory, pusherService, authService, loadService,
    initGeoService, eventsFactory, createObjectId, toasterService,
    geolib
  ) {
    var geoService = initGeoService($scope);
    var lastPosition = null;

    $scope.trip = null;
    $scope.canStopTrip = false;
    $scope.goToUser = goToUser;
    $scope.mapClassEvent = mapClassEvent;
    $scope.goToMap = goToMap;
    $scope.stopTrip = stopTrip;
    $scope.closeStopTrip = closeStopTrip;
    $scope.commentTrip = commentTrip;
    $scope.closeCommentTrip = closeCommentTrip;
    $scope.togglePositionTracking = togglePositionTracking;
    $scope.refresh = activate;

    $scope.trackingPosition = false;

    activate();

    pusherService.subscribe($scope, 'trips-' + $stateParams.trip_id, {
      A_TRIP_UPDATED: $scope.refresh.bind($scope),
      A_TRIP_DELETED: $state.go.bind($state, 'app.trips'),
    });

    function activate() {
      $scope.canStopTrip = false;
      $q.all(loadService.loadState($scope, {
        profile: authService.getProfile(),
        trip: tripsFactory.get($stateParams.trip_id),
      }))
      .then(function(data) {
        $scope.profile = data.profile;
        $scope.trip = data.trip.data;
        $scope.segments = computeSegmentFromEvents($scope.trip.events);
        lastPosition = computeLastPositionFromEvents($scope.trip.events);
        render();
      });
    }

    function render() {
      $scope.canStopTrip = $scope.trip.owner_id === $scope.profile._id &&
        !$scope.trip.ended_date;
      $scope.trackingPosition = geoService.watching();
    }

    function computeSegmentFromEvents(events) {
      var segments = events.reduce(function(segments, event) {
        var curSegment = segments[segments.length -1];
        var previousPoint = curSegment.points[curSegment.points.length -1];

        // Non geo events are simply appended
        if('geo' !== event.contents.type.split('-')[1]) {
          curSegment.events.push(event);
          return segments;
        }

        // Geo events are merged according to elapsed time
        if(!previousPoint) {
          curSegment.points.push(event);
          return segments;
        }
        event.elapsedTime = (new Date(event.created_date)).getTime() -
          (new Date(previousPoint.created_date)).getTime();
        event.distance = geolib.getDistance({
          latitude: previousPoint.contents.geo[0],
          longitude: previousPoint.contents.geo[1],
          altitude: previousPoint.contents.geo[1],
        }, {
          latitude: event.contents.geo[0],
          longitude: event.contents.geo[1],
          altitude: event.contents.geo[1],
        });
        if(
          1 < curSegment.points.length &&
          MIN_STOP_DURATION < event.elapsedTime &&
          MIN_SPEED > event.distance / event.elapsedTime
        ) {
          segments.push({
            events: [],
            points: [event],
            distance: event.distance,
            elapsedTime: event.elapsedTime,
          });
        } else {
          curSegment.points.push(event);
          curSegment.distance += event.distance;
          curSegment.elapsedTime += event.elapsedTime;
        }
        return segments;
      }, [{
{'fraction_non_alphanumeric': 0.13163590070019096, 'fraction_numerical': 0.008274984086569064, 'mean_word_length': 2.631992602866389, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, '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': '4377133', 'n_tokens_mistral': 2466, 'n_tokens_neox': 2359, 'n_words': 424}