Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Update code and its link

...

Code Block
linenumberstrue
angular.module('timing', [])
.directive('focusInput', function($timeout) {
  return {
    link: function(scope, element, attrs) {
      element.bind('click', function() {
        $timeout(function() {
          element.parent().find('input')[0].focus();
        });
      });
    }
  };
})
.controller('MainCtrl', [
  '$scope','$http','$window',
  function($scope,$http,$window){
    $scope.timings = [];
    $scope.eci = $window.location.search.substring(1);

    var bURL = '/sky/event/'+$scope.eci+'/eid/timing/started';
    $scope.addTiming = function() {
      var pURL = bURL + "?number=" + $scope.number + "&name=" + $scope.name;
      return $http.post(pURL).success(function(data){
        $scope.getAll();
        $scope.number='';
        $scope.name='';
      });
    };

    var iURL = '/sky/event/'+$scope.eci+'/eid/timing/finished';
    $scope.finished = function(number) {
      var pURL = iURL + "?number=" + number;
      return $http.post(pURL).success(function(data){
        $scope.getAll();
      });
    };

    var gURL = '/sky/cloud/'+$scope.eci+'/timing_tracker/entries';
    $scope.getAll = function() {
      return $http.get(gURL).success(function(data){
        angular.copy(data, $scope.timings);
      });
    };

    $scope.getAll();

    $scope.timeDiff = function(timing) {
      var bgn_sec = Math.floorround(Date.parse(timing.time_out)/1000);
      var end_sec = Math.floorround(Date.parse(timing.time_in)/1000);
      var sec_num = end_sec - bgn_sec;
      var hours   = Math.floor(sec_num / 3600);
      var minutes = Math.floor((sec_num - (hours * 3600)) / 60);
      var seconds = sec_num - (hours * 3600) - (minutes * 60);
  
      if (hours   < 10) {hours   = "0"+hours;}
      if (minutes < 10) {minutes = "0"+minutes;}
      if (seconds < 10) {seconds = "0"+seconds;}
      return hours+':'+minutes+':'+seconds;
    }
  }
]);

...

Install the timing_tracker ruleset (described in detail below). Enter this URL to its source code: https://raw.githubusercontent.com/b1conrad/krl-samplePicolab/pico_lessons/master/spa/timing_tracker.krl

You will probably also find it informative to install the pre-registered logging ruleset, and enable logging in the "Logging" tab.

...

Code Block
linenumberstrue
ruleset timing_tracker {
  meta {
    shares entries
  }
  global {
    entries = function() {
      ent:timings.defaultsTo({}).values()
    }
  }
  rule timing_first_use {
    select when timing started or timing finished
    if ent:timings then noop()
    notfired {
      ent:timings := {}
    }
  }
  rule timing_started {
    select when timing started number re#n0*(\d+)#i setting(ordinal_string)
    pre {
      key = "N" + ordinal_string
    }
    if ent:timings >< key then noop()
    notfired {
      ent:timings{key} := {
        "ordinal": ordinal_string.as("Number"),
        "number": event:attr("number"),
        "name": event:attr("name"),
        "time_out": time:now() }
    }
  }
  rule timing_finished {
    select when timing finished number re#n0*(\d+)#i setting(ordinal_string)
    pre {
      key = "N" + ordinal_string
    }
    if ent:timings >< key then noop()
    fired {
      ent:timings{ := ent:timings.put([key,"time_in"]}, := time:now())
    }
  }
}

Rule timing_finished (lines 31-40) selects when the event with domain timing and type finished occurs, provided it has an attribute number matching the regular expression in line 32. If it does, the rule is selected with ordinal_string set to the number. In the rule prelude, a normalized key is built from the ordinal value. If there is already a timing with that key, the rule fires, performing no action, and in its fired postlude sets (to the current time) an attribute time_in on the map for that timing.

...