<commit_before>var $ = require('jquery');
var m = require('mithril');
var $osf = require('js/osfHelpers');
var LogText = require('js/logTextParser');

var xhrconfig = function (xhr) {
    xhr.withCredentials = true;
    xhr.setRequestHeader('Content-Type', 'application/vnd.api+json');
    xhr.setRequestHeader('Accept', 'application/vnd.api+json; ext=bulk');

};

var LogWrap = {
    controller: function(args){
        var self = this;
        self.userId = args.userId;
        self.activityLogs = m.prop();
        self.eventFilter = false;
        self.dateEnd = new Date();
        self.dateBegin = new Date();
        self.today = new Date();
        self.page = 1;
        self.cache = [];

        self.getLogs = function(init, reset) {
            if (!(init || reset)  && self.cache[self.page - 1]){
                self.activityLogs(self.cache[self.page - 1]);
                return
            }
            var query = {
                'embed': ['nodes', 'user', 'linked_node', 'template_node'],
                'page': ((self.page/2) | 0) + 1,
                'page[size]': 20
            };
            if (self.eventFilter) {
                query['filter[action]'] = self.eventFilter;
            }
            if (init || reset) {
                query['aggregate'] = 1;
            }
            if (!init) {
                query['filter[date][lte]'] = self.dateEnd.toISOString();
                query['filter[date][gte]'] = self.dateBegin.toISOString();
            }
            var url = $osf.apiV2Url('users/' + self.userId + '/node_logs/', { query : query});
            var promise = m.request({method : 'GET', url : url, config : xhrconfig});
            promise.then(function(result){
                result.data.map(function(log){
                    log.attributes.formattableDate = new $osf.FormattableDate(log.attributes.date);
                });
                if (init) {
                    self.lastDay = new Date(result.data[0].attributes.date);
                    self.dateEnd = self.lastDay;
                    self.lastDay.setMonth(self.lastDay.getMonth() - 1);
                    self.dateBegin = self.lastDay;
                    self.firstDay = new Date(result.links.meta.last_log_date);
                }
                if (init || reset){
                    self.totalEvents = result.links.meta.total;
                    self.eventNumbers = result.links.meta.aggregates;
                    self.cache = [];
                }
                if (!init) {
                    self.cache.push(result.data.slice(0,9));
                    self.cache.push(result.data.slice(10,19));
                    self.activityLogs(self.cache[self.page - 1]);
                }
                self.lastPage = (result.links.meta.total / result.links.meta.per_page | 0) + 1;
            });
            return promise;
        };
        self.getLogs(true, false);
        self.getLogs();
    },
    view: function(ctrl, args){
        var fileEvents = (ctrl.eventNumbers.files/ctrl.totalEvents)*100 | 0;
        var commentEvents = (ctrl.eventNumbers.comments/ctrl.totalEvents)*100 | 0;
        var wikiEvents = (ctrl.eventNumbers.wiki/ctrl.totalEvents)*100 | 0;
        var nodeEvents = (ctrl.eventNumbers.nodes/ctrl.totalEvents)*100 | 0;
        var otherEvents = 100 - (fileEvents + commentEvents + wikiEvents + nodeEvents);
        var addSlider = function(ele, isInitialized){
            if (!isInitialized) {
                $("#recentActivitySlider").slider({
                    min: Number(ctrl.firstDay),
                    max: Number(ctrl.today),
                    values: [Number(ctrl.dateBegin), Number(ctrl.dateEnd)],
                    range: true,
                    stop: function (event, ui) {
                        ctrl.page = 1;
                        ctrl.dateBegin = new Date(ui.values[0]);
                        ctrl.dateEnd = new Date(ui.values[1]);
                        ctrl.getLogs(false, true);
                    },
                    slide: function (event, ui) {

                    }
                });
            }
            else {
                $("#recentActivitySlider").slider('option', "values", [Number(ctrl.dateBegin), Number(ctrl.dateEnd)]);
            }
        };
        return m('.panel.panel-default', [
            m('.panel-heading', 'Recent Activity'),
            m('.panel-body',
            m('.fb-activity-list.m-t-md', [
                m('#rASliderLabel'),
                m('#recentActivitySlider', {style: {margin: '10px'}, config: addSlider}),
                m('.progress', [
                    m('.progress-bar' + (ctrl.eventFilter === 'file' ? '.active.progress-bar-striped' : '.muted'), {style: {width: fileEvents+'%'}},
                        m('a', {onclick: function(){
                            ctrl.eventFilter = ctrl.eventFilter === 'file' ? false : 'file';
                            ctrl.page = 1;
                            ctrl.cache = [];
                            ctrl.getLogs();
                        }}, 'Files')
                    ),
                    m('.progress-bar.progress-bar-warning' + (ctrl.eventFilter === 'project' ? '.active.progress-bar-striped' : '.muted'), {style: {width: nodeEvents+'%'}},
                        m('a', {onclick: function(){
                            ctrl.eventFilter = ctrl.eventFilter === 'project' ? false : 'project';
                            ctrl.page = 1;
                            ctrl.cache = [];
                            ctrl.getLogs();
                        }}, 'Nodes')
                    ),
                    m('.progress-bar.progress-bar-info' + (ctrl.eventFilter === 'comment' ? '.active.progress-bar-striped' : '.muted'), {style: {width: commentEvents+'%'}},
                        m('a', {onclick: function(){
                            ctrl.eventFilter = ctrl.eventFilter === 'comment' ? false : 'comment';
                            ctrl.page = 1;
                            ctrl.cache = [];
                            ctrl.getLogs();
                        }}, 'Comments')
                    ),
                    m('.progress-bar.progress-bar-danger' + (ctrl.eventFilter === 'wiki' ? '.active.progress-bar-striped' : '.muted'), {style: {width: wikiEvents+'%'}},
                        m('a', {onclick: function(){
                            ctrl.eventFilter = ctrl.eventFilter === 'wiki' ? false : 'wiki';
                            ctrl.page = 1;
                            ctrl.cache = [];
                            ctrl.getLogs();
                        }}, 'Wiki')
                    ),
                    m('.progress-bar.progress-bar-success.muted', {style: {width: otherEvents+'%'}}, 'Other')
                ]),
                ctrl.activityLogs() ? ctrl.activityLogs().map(function(item){

                    return m('.fb-activity-item', [
                        m('span.text-muted.m-r-xs', item.attributes.formattableDate.local),
                        m.component(LogText,item)
                    ]);
                }) : m('p','No activity in this time range.'),
                m('.btn-group', {textAlign: 'center'}, [
                    m('button.btn.btn-primary' + (ctrl.page > 1 ? '' : '.disabled'), {onclick: function(){
                        ctrl.page--;
                        ctrl.getLogs()
                    }}, 'Previous'),
                    m('button.btn.btn-primary' + (ctrl.lastPage > ctrl.page ? '' : '.disabled'), {onclick: function(){
                        ctrl.page++;<commit_msg>got this date thigns to work<commit_after>var $ = require('jquery');
var m = require('mithril');
var moment = require('moment');
var $osf = require('js/osfHelpers');
var LogText = require('js/logTextParser');

var xhrconfig = function (xhr) {
    xhr.withCredentials = true;
    xhr.setRequestHeader('Content-Type', 'application/vnd.api+json');
    xhr.setRequestHeader('Accept', 'application/vnd.api+json; ext=bulk');

};

var LogWrap = {
    controller: function(args){
        var self = this;
        self.userId = args.userId;
        self.activityLogs = m.prop();
        self.eventFilter = false;
        self.dateEnd = moment.utc();
        self.dateBegin = moment.utc();
        self.today = moment.utc();
        self.page = 1;
        self.cache = [];

        self.getLogs = function(init, reset) {
            if (!(init || reset)  && self.cache[self.page - 1]){
                self.activityLogs(self.cache[self.page - 1]);
                return
            }
            var query = {
                'embed': ['nodes', 'user', 'linked_node', 'template_node'],
                'page': ((self.page/2) | 0) + 1,
                'page[size]': 20
            };
            if (self.eventFilter) {
                query['filter[action]'] = self.eventFilter;
            }
            if (init || reset) {
                query['aggregate'] = 1;
            }
            if (!init) {
                query['filter[date][lte]'] = self.dateEnd.toISOString();
                query['filter[date][gte]'] = self.dateBegin.toISOString();
            }
            var url = $osf.apiV2Url('users/' + self.userId + '/node_logs/', { query : query});
            var promise = m.request({method : 'GET', url : url, config : xhrconfig});
            promise.then(function(result){
                result.data.map(function(log){
                    log.attributes.formattableDate = new $osf.FormattableDate(log.attributes.date);
                });
                if (init) {
                    self.lastDay = moment.utc(result.data[0].attributes.date);
                    self.dateEnd = self.lastDay;
                    self.dateBegin = moment.utc(result.data[0].attributes.date).subtract(1, 'months');
                    self.firstDay = moment.utc(result.links.meta.last_log_date);
                }
                if (init || reset){
                    self.totalEvents = result.links.meta.total;
                    self.eventNumbers = result.links.meta.aggregates;
                    self.cache = [];
                }
                if (!init) {
                    self.cache.push(result.data.slice(0,9));
                    self.cache.push(result.data.slice(10,19));
                    self.activityLogs(self.cache[self.page - 1]);
                }
                self.lastPage = (result.links.meta.total / result.links.meta.per_page | 0) + 1;
            });
            return promise;
        };

        self.callLogs = function(filter) {
            self.eventFilter = self.eventFilter === filter ? false : filter;
            self.page = 1;
            self.cache = [];
            self.getLogs();
        };

        self.getLogs(true, false);
    },
    view: function(ctrl, args){
        var fileEvents = (ctrl.eventNumbers.files/ctrl.totalEvents)*100 | 0;
        var commentEvents = (ctrl.eventNumbers.comments/ctrl.totalEvents)*100 | 0;
        var wikiEvents = (ctrl.eventNumbers.wiki/ctrl.totalEvents)*100 | 0;
        var nodeEvents = (ctrl.eventNumbers.nodes/ctrl.totalEvents)*100 | 0;
        var otherEvents = 100 - (fileEvents + commentEvents + wikiEvents + nodeEvents);
        var addSlider = function(ele, isInitialized){
            if (!isInitialized) {
                $("#recentActivitySlider").slider({
                    min: Number(ctrl.firstDay.format('x')),
                    max: Number(ctrl.today.format('x')),
                    values: [Number(ctrl.dateBegin.format('x')), Number(ctrl.dateEnd.format('x'))],
                    range: true,
                    stop: function (event, ui) {
                        ctrl.page = 1;
                        ctrl.dateBegin = moment.utc(ui.values[0]);
                        ctrl.dateEnd = moment.utc(ui.values[1]);
                        ctrl.getLogs(false, true);
                    },
                    slide: function (event, ui) {

                    }
                });
                ctrl.getLogs();
            }
            else {
                $("#recentActivitySlider").slider('option', "values", [Number(ctrl.dateBegin.format('x')), Number(ctrl.dateEnd.format('x'))]);
            }
        };
        return m('.panel.panel-default', [
            m('.panel-heading', 'Recent Activity'),
            m('.panel-body',
            m('.fb-activity-list.m-t-md', [
                m('#rASliderLabel'),
                m('#recentActivitySlider', {style: {margin: '10px'}, config: addSlider}),
                m('.progress', [
                    m('.progress-bar' + (ctrl.eventFilter === 'file' ? '.active.progress-bar-striped' : '.muted'), {style: {width: fileEvents+'%'}},
                        m('a', {onclick: function(){
                            ctrl.callLogs('file');
                        }}, 'Files')
                    ),
                    m('.progress-bar.progress-bar-warning' + (ctrl.eventFilter === 'project' ? '.active.progress-bar-striped' : '.muted'), {style: {width: nodeEvents+'%'}},
                        m('a', {onclick: function(){
                            ctrl.callLogs('project');
                        }}, 'Nodes')
                    ),
                    m('.progress-bar.progress-bar-info' + (ctrl.eventFilter === 'comment' ? '.active.progress-bar-striped' : '.muted'), {style: {width: commentEvents+'%'}},
                        m('a', {onclick: function(){
                            ctrl.callLogs('comment');
                        }}, 'Comments')
                    ),
                    m('.progress-bar.progress-bar-danger' + (ctrl.eventFilter === 'wiki' ? '.active.progress-bar-striped' : '.muted'), {style: {width: wikiEvents+'%'}},
                        m('a', {onclick: function(){
                            ctrl.callLogs('wiki');
                        }}, 'Wiki')
                    ),
                    m('.progress-bar.progress-bar-success.muted', {style: {width: otherEvents+'%'}}, 'Other')
                ]),
                (ctrl.activityLogs() && (ctrl.activityLogs().length > 0))? ctrl.activityLogs().map(function(item){

                    return m('.fb-activity-item', [
                        m('span.text-muted.m-r-xs', item.attributes.formattableDate.local),
                        m.component(LogText,item)
                    ]);
                }) : m('p','No activity in this time range.'),
                m('.btn-group', {textAlign: 'center'}, [
                    m('button.btn.btn-primary' + (ctrl.page > 1 ? '' : '.disabled'), {onclick: function(){
                        ctrl.page--;
                        ctrl.getLogs()
                    }}, 'Previous'),
                    m('button.btn.btn-primary' + (ctrl.lastPage > ctrl.page ? '' : '.disabled'), {onclick: function(){
                        ctrl.page++;
{'fraction_non_alphanumeric': 0.15438146071211403, 'fraction_numerical': 0.006688737247483278, 'mean_word_length': 1.4213970227384263, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '4480532', 'n_tokens_mistral': 4169, 'n_tokens_neox': 3918, 'n_words': 670}