<commit_before>/*! This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

"use strict";

const {setTimeout, clearTimeout, setInterval, clearInterval} = require("sdk/timers");
const {PersistentObject} = require("./storage");
const sp = require("sdk/simple-prefs");
const prefs = sp.prefs;
const {Cu, Cc, Ci} = require("chrome");
const unload = require("sdk/system/unload").when;
const {dumpUpdateObject, handleCmd, isEnabled} = require("./debug");
const observerService = Cc["@mozilla.org/observer-service;1"]
                      .getService(Ci.nsIObserverService);

const timerDataAddress = "timer.data";
let timerData;

const tickHandlers = [];
const userActiveHandlers = [];
const userInactiveHandlers = [];

let activityObs;
let activity;
let tickInterval;

let startTimeMs;

const init = function(){
  return PersistentObject("simplePref", {address: timerDataAddress})
  .then((obj)=> {
    timerData = obj;
  }).then(_init);
}

const _init = function(){
  console.log("initializing timer");

  console.time("timer init");

  startTimeMs = Number(require('./experiment').info.startTimeMs);
  
  if (!("elapsedTime" in timerData))
  timerData.elapsedTime = 0;

  timerData.elapsedTotalTime = elapsedTotalTime();

  if (!("silenceStart" in timerData))
    timerData.silenceStart = -1;

  silenceLeft(); //to update the silence status;

  watchActivity();

  tickInterval = setInterval(tick, prefs["timer.tick_length_s"]*1000);

  let f = function(pref){
    clearInterval(tickInterval);
    tickInterval = setInterval(tick, prefs["timer.tick_length_s"]*1000);
  };

  sp.on("timer.tick_length_s", f);
  unload(function(){sp.removeListener("timer.tick_length_s", f)});

  f = function(pref){
    timerData.elapsedTotalTime = elapsedTotalTime();
  };

  sp.on("experiment.startTimeMs", f);
  unload(function(){sp.removeListener("experiment.startTimeMs", f)});

  debug.init();
  debug.update({silenceStatus: true});

  console.timeEnd("timer init");
}

const elapsedTotalTime = function(stage){
  let exp = require("./experiment");

  if (!stage){
    let ett = (Date.now() - startTimeMs) / (1000 * prefs["timer.tick_length_s"]);

    return ett;
  }

  let stageTimes = exp.stageTimes;

  if (!stageTimes[stage])
    return 0;

  // in the stage
  if (!stageTimes[stage].duration){
    let ett = (Date.now() - Number(stageTimes[stage].start)) / (1000 * prefs["timer.tick_length_s"]);

    return ett;
  }

  // stage has ended
  let ett = stageTimes[stage].duration;

  return ett;
}

const watchActivity = function(){

  activity = {
    minor_inactive_s: 0,
    last_minor_inactive_s: 0,
    minor_active_s: 0,
    active_s: 0,
    active: false
  };

  let activeCounter, inactiveCounter;


  activityObs = {
    observe: function(subject, topic, data){
      switch(topic){
        case "user-interaction-active":

          // require('./stats').event("activeTick");
          // console.log("active " + elapsedTime());
          clearInterval(inactiveCounter);
          if (activity.minor_inactive_s) activity.last_minor_inactive_s = activity.minor_inactive_s;
          activity.minor_inactive_s = 0;
          if (!activeCounter){
            activeCounter = setInterval(function(){
              activity.minor_active_s += 1;
              activity.active_s += 1;
              debug.update();

            }, 1000);
          }
          activity.active = true;

          if (activity.minor_active_s == 0){ // to call the handlers only once
            userActiveHandlers.forEach((fn) => fn());
            require('./stats').event("activation");
          }

        break;
        case "user-interaction-inactive":
          require('./stats').event("inactiveTick");
          console.log("user inactive (minor)");
          clearInterval(activeCounter);
          activeCounter = null;
          activity.minor_active_s = 0;
          inactiveCounter = setInterval(function(){
            activity.minor_inactive_s += 1;

            if (activity.active)
              activity.active_s += 1;

            if (activity.minor_inactive_s > prefs["timer.inactive_threshold_s"] && activity.active){
              deactivate();
            }
            debug.update();
          }, 1000);
        break;
      }
    },
    register: function(){
      observerService.addObserver(this, "user-interaction-active", false);
      observerService.addObserver(this, "user-interaction-inactive", false);
    },
    unregister: function(){
      observerService.removeObserver(this, "user-interaction-inactive");
      observerService.removeObserver(this, "user-interaction-active");
    }
  };

  unload(function(){if (activityObs) activityObs.unregister();});

  activityObs.register();
}
<commit_msg>now logs long insactivativity times<commit_after>/*! This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

"use strict";

const {setTimeout, clearTimeout, setInterval, clearInterval} = require("sdk/timers");
const {PersistentObject} = require("./storage");
const sp = require("sdk/simple-prefs");
const prefs = sp.prefs;
const {Cu, Cc, Ci} = require("chrome");
const unload = require("sdk/system/unload").when;
const {dumpUpdateObject, handleCmd, isEnabled} = require("./debug");
const observerService = Cc["@mozilla.org/observer-service;1"]
                      .getService(Ci.nsIObserverService);

const timerDataAddress = "timer.data";
let timerData;

const tickHandlers = [];
const userActiveHandlers = [];
const userInactiveHandlers = [];

let activityObs;
let activity;
let tickInterval;

let startTimeMs;

const init = function(){
  return PersistentObject("simplePref", {address: timerDataAddress})
  .then((obj)=> {
    timerData = obj;
  }).then(_init);
}

const _init = function(){
  console.log("initializing timer");

  console.time("timer init");

  startTimeMs = Number(require('./experiment').info.startTimeMs);
  
  if (!("elapsedTime" in timerData))
  timerData.elapsedTime = 0;

  timerData.elapsedTotalTime = elapsedTotalTime();

  if (!("silenceStart" in timerData))
    timerData.silenceStart = -1;

  silenceLeft(); //to update the silence status;

  watchActivity();

  tickInterval = setInterval(tick, prefs["timer.tick_length_s"]*1000);

  let f = function(pref){
    clearInterval(tickInterval);
    tickInterval = setInterval(tick, prefs["timer.tick_length_s"]*1000);
  };

  sp.on("timer.tick_length_s", f);
  unload(function(){sp.removeListener("timer.tick_length_s", f)});

  f = function(pref){
    timerData.elapsedTotalTime = elapsedTotalTime();
  };

  sp.on("experiment.startTimeMs", f);
  unload(function(){sp.removeListener("experiment.startTimeMs", f)});

  debug.init();
  debug.update({silenceStatus: true});

  console.timeEnd("timer init");
}

const elapsedTotalTime = function(stage){
  let exp = require("./experiment");

  if (!stage){
    let ett = (Date.now() - startTimeMs) / (1000 * prefs["timer.tick_length_s"]);

    return ett;
  }

  let stageTimes = exp.stageTimes;

  if (!stageTimes[stage])
    return 0;

  // in the stage
  if (!stageTimes[stage].duration){
    let ett = (Date.now() - Number(stageTimes[stage].start)) / (1000 * prefs["timer.tick_length_s"]);

    return ett;
  }

  // stage has ended
  let ett = stageTimes[stage].duration;

  return ett;
}

const watchActivity = function(){

  activity = {
    minor_inactive_s: 0,
    last_minor_inactive_s: 0,
    minor_active_s: 0,
    active_s: 0,
    active: false
  };

  let activeCounter, inactiveCounter;


  activityObs = {
    observe: function(subject, topic, data){
      switch(topic){
        case "user-interaction-active":

          // require('./stats').event("activeTick");
          // console.log("active " + elapsedTime());
          clearInterval(inactiveCounter);
          if (activity.minor_inactive_s) activity.last_minor_inactive_s = activity.minor_inactive_s;
          activity.minor_inactive_s = 0;
          if (!activeCounter){
            activeCounter = setInterval(function(){
              activity.minor_active_s += 1;
              activity.active_s += 1;
              debug.update();

            }, 1000);
          }
          activity.active = true;

          if (activity.minor_active_s == 0){ // to call the handlers only once
            userActiveHandlers.forEach((fn) => fn());
            require('./stats').event("activation", {}, {inactivity: activity.last_minor_inactive_s}, {inactivity: 'average'});

            if (isRecentlyActive(10, 10 * 60)){
              require('./logger').logLongInactivityBack({length: activity.last_minor_inactive_s});

            }
          }

        break;
        case "user-interaction-inactive":
          require('./stats').event("inactiveTick");
          console.log("user inactive (minor)");
          clearInterval(activeCounter);
          activeCounter = null;
          activity.minor_active_s = 0;
          inactiveCounter = setInterval(function(){
            activity.minor_inactive_s += 1;

            if (activity.active)
              activity.active_s += 1;

            if (activity.minor_inactive_s > prefs["timer.inactive_threshold_s"] && activity.active){
              deactivate();
            }

            if (activity.minor_inactive_s == 10 * 60){
              require('./logger').logLongInactivity();
            }
            
            debug.update();
          }, 1000);
        break;
      }
    },
    register: function(){
      observerService.addObserver(this, "user-interaction-active", false);
      observerService.addObserver(this, "user-interaction-inactive", false);
    },
    unregister: function(){
      observerService.removeObserver(this, "user-interaction-inactive");
      observerService.removeObserver(this, "user-interaction-active");
    }
  };

  unload(function(){if (activityObs) activityObs.unregister();});

  activityObs.register();
}

{'fraction_non_alphanumeric': 0.14178366620526003, 'fraction_numerical': 0.009491793553490212, 'mean_word_length': 3.0737011679420054, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 9, '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': '2234386', 'n_tokens_mistral': 3169, 'n_tokens_neox': 2961, 'n_words': 629}