<commit_before>'use strict';

require('keen-dataviz/dist/keen-dataviz.min.css');

var $osf = require('js/osfHelpers');
var keenDataviz = require('keen-dataviz');
var keenAnalysis = require('keen-analysis');

var KeenViz = function(){
    var self = this;

    /**
    * Manages the connections to Keen.  Needs a project id and valid read key for that project.
    *
    * @property keenClient
    * @type {keenAnalysis}
    */
    self.keenClient = new keenAnalysis({
        projectId: window.contextVars.keen.public.projectId,
        readKey : window.contextVars.keen.public.readKey,
    });


    // show visits to this project over the last week
    self.visitsByDay = function() {
        var query = {
            'type':'count_unique',
            'params' : {
                event_collection: 'pageviews',
                timeframe: 'this_7_days',
                interval: 'daily',
                target_property: 'anon.id'
            }
        };

        var dataviz = new keenDataviz()
                .el('#visits')
                .chartType('line')
                .chartOptions({
                    tooltip: {
                        format: {
                            name: function(){return 'Visits';}
                        }
                    }
                });

        self.buildChart(query, dataviz);
    };


    // show most common referrers to this project from the last week
    self.topReferrers = function() {
        var query = {
            type: 'count_unique',
            params: {
                event_collection: 'pageviews',
                timeframe: 'this_7_days',
                target_property: 'anon.id',
                group_by: 'referrer.info.domain'
            }
        };

        var dataviz = new keenDataviz()
                .el('#topReferrers')
                .chartType('pie')
                .chartOptions({
                    tooltip:{
                        format:{
                            name: function(){return 'Visits';}
                        }
                    },
                });

        self.buildChart(query, dataviz);
    };

    // The number of visits by hour across last week
    self.visitsServerTime = function() {
        var query = {
            'type': 'count_unique',
            'params': {
                event_collection: 'pageviews',
                timeframe: 'this_7_days',
                target_property: 'anon.id',
                group_by: 'time.local.hour_of_day',
            }
        };

        var dataviz = new keenDataviz()
                .el('#serverTimeVisits')
                .chartType('bar')
                .chartOptions({
                    tooltip:{
                        format:{
                            name: function(){return 'Visits';}
                        }
                    },
                    axis: {
                        x: {
                            label: {
                                text: 'Hour of Day',
                                position: 'outer-center',
                            },
                            tick: {
                                centered: true,
                                values: ['0', '4', '8', '12', '16', '20'],
                            },
                        },
                    },
                });

        // make sure all hours of the day 0-23 are present
        var munger = function() {
            var foundHours = {};
            for (var i=this.dataset.matrix.length-1; i>0; i--) {
                var row = this.dataset.selectRow(i);
                foundHours[ row[0] ] = row[1];
                this.dataset.deleteRow(i);
            }

            for (var hour=0; hour<24; hour++) {
                var stringyNum = '' + hour;
                this.dataset.appendRow(stringyNum, [ foundHours[stringyNum] || 0 ]);
            }
        };
        self.buildChart(query, dataviz, munger);

    };

    // most popular sub-pages of this project
    self.popularPages = function() {
        var query = {
            type: 'count_unique',
            params: {
                event_collection: 'pageviews',
                timeframe: 'this_7_days',<commit_msg>set default analytics period to today - 30 days<commit_after>'use strict';

require('keen-dataviz/dist/keen-dataviz.min.css');

var $osf = require('js/osfHelpers');
var keenDataviz = require('keen-dataviz');
var keenAnalysis = require('keen-analysis');

var KeenViz = function(){
    var self = this;

    var defaultTimeframe = 'this_30_days';

    /**
    * Manages the connections to Keen.  Needs a project id and valid read key for that project.
    *
    * @property keenClient
    * @type {keenAnalysis}
    */
    self.keenClient = new keenAnalysis({
        projectId: window.contextVars.keen.public.projectId,
        readKey : window.contextVars.keen.public.readKey,
    });


    // show visits to this project over the last week
    self.visitsByDay = function() {
        var query = {
            'type':'count_unique',
            'params' : {
                event_collection: 'pageviews',
                timeframe: defaultTimeframe,
                interval: 'daily',
                target_property: 'anon.id'
            }
        };

        var dataviz = new keenDataviz()
                .el('#visits')
                .chartType('line')
                .chartOptions({
                    tooltip: {
                        format: {
                            name: function(){return 'Visits';}
                        }
                    }
                });

        self.buildChart(query, dataviz);
    };


    // show most common referrers to this project from the last week
    self.topReferrers = function() {
        var query = {
            type: 'count_unique',
            params: {
                event_collection: 'pageviews',
                timeframe: defaultTimeframe,
                target_property: 'anon.id',
                group_by: 'referrer.info.domain'
            }
        };

        var dataviz = new keenDataviz()
                .el('#topReferrers')
                .chartType('pie')
                .chartOptions({
                    tooltip:{
                        format:{
                            name: function(){return 'Visits';}
                        }
                    },
                });

        self.buildChart(query, dataviz);
    };

    // The number of visits by hour across last week
    self.visitsServerTime = function() {
        var query = {
            'type': 'count_unique',
            'params': {
                event_collection: 'pageviews',
                timeframe: defaultTimeframe,
                target_property: 'anon.id',
                group_by: 'time.local.hour_of_day',
            }
        };

        var dataviz = new keenDataviz()
                .el('#serverTimeVisits')
                .chartType('bar')
                .chartOptions({
                    tooltip:{
                        format:{
                            name: function(){return 'Visits';}
                        }
                    },
                    axis: {
                        x: {
                            label: {
                                text: 'Hour of Day',
                                position: 'outer-center',
                            },
                            tick: {
                                centered: true,
                                values: ['0', '4', '8', '12', '16', '20'],
                            },
                        },
                    },
                });

        // make sure all hours of the day 0-23 are present
        var munger = function() {
            var foundHours = {};
            for (var i=this.dataset.matrix.length-1; i>0; i--) {
                var row = this.dataset.selectRow(i);
                foundHours[ row[0] ] = row[1];
                this.dataset.deleteRow(i);
            }

            for (var hour=0; hour<24; hour++) {
                var stringyNum = '' + hour;
                this.dataset.appendRow(stringyNum, [ foundHours[stringyNum] || 0 ]);
            }
        };
        self.buildChart(query, dataviz, munger);

    };

    // most popular sub-pages of this project
    self.popularPages = function() {
        var query = {
            type: 'count_unique',
            params: {
                event_collection: 'pageviews',
                timeframe: defaultTimeframe,
{'fraction_non_alphanumeric': 0.10911016949152542, 'fraction_numerical': 0.005649717514124294, 'mean_word_length': 1.1142075143070416, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4603414', 'n_tokens_mistral': 2287, 'n_tokens_neox': 2124, 'n_words': 479}