<commit_before>Template.status.onCreated(function() {
    var instance = this;
    instance.state = new ReactiveVar();
    instance.todayEvents = function() {
        return getToday(EventLog, Session.get("now"));
    };
    instance.timer = Meteor.setInterval(function() {
        Session.set("now", moment().toDate());
    }, 1000);
    instance.subscribe("logToday", Session.get("now"));
    instance.autorun(function() {
        instance.state.set(getState(instance.todayEvents()));
    });
});
Template.status.onDestroyed(function() {
    var instance = this;
    Meteor.clearInterval(instance.timer);
});

function getState(todayCollection) {
    var todayEvents = todayCollection.fetch();
    var state = {};
    state.foodEvents = [];
    state.totalSleep = moment.duration(0);
    todayEvents.forEach(function(item) {
        if (item.activity == "sleep") {
            if (!state.lastSleep || item.timestamp > state.lastSleep.timestamp) {
                state.lastSleep = item;
            }
            if (item.endtimestamp) {
                var start = moment.utc(item.timestamp);
                var end = moment.utc(item.endtimestamp);
                var duration = moment.duration(end.diff(start));
                state.totalSleep += duration;
            }
        } else if (item.activity == "food") {
            state.foodEvents.push(item);
            if (!state.lastFood || item.timestamp > state.lastFood.timestamp) {
                state.lastFood = item;
            }
        }
    });
    if (state.lastSleep) {
        state.lastState = "asleep";
        state.lastTransition = state.lastSleep.timestamp;
        state.lastAwake = undefined;
        if (state.lastSleep.endtimestamp) {
            state.lastState = "awake";
            state.lastTransition = state.lastSleep.endtimestamp;
            state.lastAwake = state.lastSleep.endtimestamp;
        }
    }
    return state;
}
var stateIconLookup = {
    awake: "status-state-icon-awake",
    asleep: "status-state-icon-asleep"
};
Template.status.helpers({
    stateIconClass: function() {
        var state = Template.instance().state.get();
        if (state && state.lastState) {
            return stateIconLookup[state.lastState];
        }
    },
    stateTextTimer: function() {
        var state = Template.instance().state.get();
        if (state) {
            var now = Session.get("now");
            var then = state.lastTransition;
            var timer = moment(now).diff(moment(then));
            return moment.utc(timer).format("HH:mm:ss");
        }
    },
    stateText: function() {
        var state = Template.instance().state.get();
        if (state) {
            var outputText;
            if (state.lastState == "awake") outputText = "Ludvig is awake!";
            else outputText = "Ludvig is asleep";
            return outputText;
        }
    },
    stateTextTransition: function() {
        var state = Template.instance().state.get();<commit_msg>Show status in document title<commit_after>Template.status.onCreated(function() {
    var instance = this;
    instance.state = new ReactiveVar();
    instance.todayEvents = function() {
        return getToday(EventLog, Session.get("now"));
    };
    instance.timer = Meteor.setInterval(function() {
        Session.set("now", moment().toDate());
    }, 1000);
    instance.subscribe("logToday", Session.get("now"));
    instance.autorun(function() {
        instance.state.set(getState(instance.todayEvents()));
    });
});
Template.status.onDestroyed(function() {
    var instance = this;
    Meteor.clearInterval(instance.timer);
});

function getState(todayCollection) {
    var todayEvents = todayCollection.fetch();
    var state = {};
    state.foodEvents = [];
    state.totalSleep = moment.duration(0);
    todayEvents.forEach(function(item) {
        if (item.activity == "sleep") {
            if (!state.lastSleep || item.timestamp > state.lastSleep.timestamp) {
                state.lastSleep = item;
            }
            if (item.endtimestamp) {
                var start = moment.utc(item.timestamp);
                var end = moment.utc(item.endtimestamp);
                var duration = moment.duration(end.diff(start));
                state.totalSleep += duration;
            }
        } else if (item.activity == "food") {
            state.foodEvents.push(item);
            if (!state.lastFood || item.timestamp > state.lastFood.timestamp) {
                state.lastFood = item;
            }
        }
    });
    if (state.lastSleep) {
        state.lastState = "asleep";
        state.lastTransition = state.lastSleep.timestamp;
        state.lastAwake = undefined;
        if (state.lastSleep.endtimestamp) {
            state.lastState = "awake";
            state.lastTransition = state.lastSleep.endtimestamp;
            state.lastAwake = state.lastSleep.endtimestamp;
        }
    }
    return state;
}
var stateIconLookup = {
    awake: "status-state-icon-awake",
    asleep: "status-state-icon-asleep"
};
Template.status.helpers({
    stateIconClass: function() {
        var state = Template.instance().state.get();
        if (state && state.lastState) {
            return stateIconLookup[state.lastState];
        }
    },
    stateTextTimer: function() {
        var state = Template.instance().state.get();
        if (state) {
            var now = Session.get("now");
            var then = state.lastTransition;
            var timer = moment(now).diff(moment(then));
            return moment.utc(timer).format("HH:mm:ss");
        }
    },
    stateText: function() {
        var state = Template.instance().state.get();
        if (state) {
            var outputText;
            var titleText;
            if (state.lastState == "awake") {
                outputText = "Ludvig is awake!";
            }
            else { 
                outputText = "Ludvig is asleep";
            }
            document.title = outputText + " | Luddelogg";
            return outputText;
        }
    },
    stateTextTransition: function() {
        var state = Template.instance().state.get();
{'fraction_non_alphanumeric': 0.1340745585349902, 'fraction_numerical': 0.0016350555918901242, 'mean_word_length': 2.114562118126273, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '5864706', 'n_tokens_mistral': 1733, 'n_tokens_neox': 1677, 'n_words': 310}