<commit_before>$(function() {

    // Magic underscore settings to allow underscore templates to play
    // nicely with Rails ERB templates!
    _.templateSettings = {
        interpolate: /\{\{\=(.+?)\}\}/g,
        evaluate: /\{\{(.+?)\}\}/g
    };

    // Models
    var Session = Backbone.Model.extend({
        defaults: {
            'owner': '',
            'repo': ''
        },
        initialize: function() {
            var self = this;
            _.bindAll(this, 'getURL');
            $.getJSON('/sessions/get', function(response) {
                var data = response.data;
                var token = null;
                if (response.status == "ok" && data.token) {
                    console.log('session created!');
                    token = data.token;
                }
                self.set('token', token);
            });
        },
        getURL: function() {
            var self = this;
            return self.get('owner') + '/' + self.get('repo');
        }
    });
    var session = new Session();

    var GithubUser = Backbone.Model.extend({
        initialize: function(user) {
            var name = user.login ? user.login : '';
            var gravatar = user.gravatar_id ? user.gravatar_id : '';
            var url = user.html_url ? user.html_url : '';

            this.set('name', name);
            this.set('gravatar', gravatar);
            this.set('url', url);
        },
        getLink: function() {
            var rval = ['<a href="' + this.get('url') + '" title="' + this.get('name') + '">',
                        this.getIcon(),
                        this.get('name'),
                        '</a>'].join('');

            return rval;
        },
        getIcon: function() {
            var rval = ['<img src="http://www.gravatar.com/avatar/',
                        this.get('gravatar'),
                        '?s=40" title="' + this.get('name') + '">'].join('');

            return rval;
        }
    });

    var Issue = Backbone.Model.extend({
        getClosedTime: function() {
            var closed = this.get('closed_at') || 0;
            var date = new Date(closed);
            return date.getTime() / 1000;
        },
        getCreatedTime: function() {
            var created = this.get('created_at') || 0;
            var date = new Date(created);
            return date.getTime() / 1000;
        },
        getLabels: function() {
            var labels = this.get('labels') || [];
            return _.map(labels, function(label) {
                return label.name;
            });
        },
        getLabelString: function() {
            return _.reduce(this.getLabels(), function(memo, label) {
                return memo + label + ',';
            }, '');
        },
        getWeight: function() {
            var self = this;
            var weight = 1;
            var re = /Weight: (\d+)/;
            var label = _.find(self.getLabels(), function(label) {
                return re.test(label);
            });
            var myArray = re.exec(label);
            if (myArray != null) {
                weight = parseInt(myArray[1], 10);
            }
            return weight;
        },
        createLink: function() {
            var rval = '';

            var assignee = this.get('assignee') || null;
            var creator = this.get('user');
            var title = this.escape('title');
            var url = this.get('html_url');

            var created_at = new Date(this.get('created_at'));
            var month = created_at.getMonth() + 1;
            var issue_created = month + '/' + created_at.getDate() + '/' + created_at.getFullYear();

            var closed_at = this.get('closed_at');
            var work_duration_details = '';
            if (closed_at) {
                var closed_date = new Date(closed_at);
                var month = closed_date.getMonth() + 1;
                var issue_closed = month + '/' + closed_date.getDate() + '/' + closed_date.getFullYear();
                work_duration_details = issue_created + ' - ' + issue_closed;
            }
            else {
                var closed_date = new Date(Date.now());
                work_duration_details = issue_created + ' - ???';
            }

            var work_duration = moment(closed_date).from(created_at, true);

            if (creator && title && url) {
                var creator_user = new GithubUser(creator);
                var assigned_user = assignee ? new GithubUser(assignee) : null;
                rval = ['<ul>',
                        '<li class="title"><a target="_blank" href="' + url + '" title="' + title + '">',
                        title,
                        '<small>' +
                        'created by ' + creator_user.get('name'),
                        '</small>',
                        '</a></li>',
                        '<li class="whoswho">',
                        creator_user.getIcon(),
                        '<ins>&rArr;</ins>',
                        assigned_user ? assigned_user.getIcon() : '<ins class="annon">?</ins>',
                        '</li>',
                        '<li class="countdown">',
                        '<ins>' + work_duration + '</ins>',
                        '<ins>' + work_duration_details + '</ins>',
                        '</li>',
                        '</ul>'].join('');
            }

            return rval;
        }
    });
    var IssuesBase = Backbone.Collection.extend({
        model: Issue,
        comparator: function(issue) {
            return issue.get('closed_at');
        },
        url: function() {
            var token = session.get('token');
            var owner = session.get('owner');
            var repo = session.get('repo');

            // Build initial url string.
            var url = ['https://api.github.com',
                       '/repos/'+owner+'/'+repo+'/issues',
                       '?access_token='+token,
                       ''].join('');

            // If any parameter properties exist, append then to the URL string.
            if (this.state) {
                url += '&state='+this.state;
            }
            if (this.milestoneId) {
                url += '&milestone='+this.milestoneId;
            }
            if (this.since) {
                var _since = this.since();
                url += '&since='+_since;
            }
            if (this.direction) {
                url += '&direction='+this.direction;
            }

            return url;
        },
        parse: function(response) {
            return response;
        },
        getDateSince: function(days) {
            var d = new Date();
            d.setDate(d.getDate()-days);
            return d.toISOString();
        },
        /*
         * parse_link_header()
         *
         * Parse the Github Link HTTP header used for pageination
         * http://developer.github.com/v3/#pagination
         */
        parseLinkHeader: function (header) {
          if (header.length == 0) {
              throw new Error("input must not be of zero length");
          }

          // Split parts by comma
          var parts = header.split(',');
          var links = {};<commit_msg>Add getTotalWeight method to IssuesBase #48<commit_after>$(function() {

    // Magic underscore settings to allow underscore templates to play
    // nicely with Rails ERB templates!
    _.templateSettings = {
        interpolate: /\{\{\=(.+?)\}\}/g,
        evaluate: /\{\{(.+?)\}\}/g
    };

    // Models
    var Session = Backbone.Model.extend({
        defaults: {
            'owner': '',
            'repo': ''
        },
        initialize: function() {
            var self = this;
            _.bindAll(this, 'getURL');
            $.getJSON('/sessions/get', function(response) {
                var data = response.data;
                var token = null;
                if (response.status == "ok" && data.token) {
                    console.log('session created!');
                    token = data.token;
                }
                self.set('token', token);
            });
        },
        getURL: function() {
            var self = this;
            return self.get('owner') + '/' + self.get('repo');
        }
    });
    var session = new Session();

    var GithubUser = Backbone.Model.extend({
        initialize: function(user) {
            var name = user.login ? user.login : '';
            var gravatar = user.gravatar_id ? user.gravatar_id : '';
            var url = user.html_url ? user.html_url : '';

            this.set('name', name);
            this.set('gravatar', gravatar);
            this.set('url', url);
        },
        getLink: function() {
            var rval = ['<a href="' + this.get('url') + '" title="' + this.get('name') + '">',
                        this.getIcon(),
                        this.get('name'),
                        '</a>'].join('');

            return rval;
        },
        getIcon: function() {
            var rval = ['<img src="http://www.gravatar.com/avatar/',
                        this.get('gravatar'),
                        '?s=40" title="' + this.get('name') + '">'].join('');

            return rval;
        }
    });

    var Issue = Backbone.Model.extend({
        getClosedTime: function() {
            var closed = this.get('closed_at') || 0;
            var date = new Date(closed);
            return date.getTime() / 1000;
        },
        getCreatedTime: function() {
            var created = this.get('created_at') || 0;
            var date = new Date(created);
            return date.getTime() / 1000;
        },
        getLabels: function() {
            var labels = this.get('labels') || [];
            return _.map(labels, function(label) {
                return label.name;
            });
        },
        getLabelString: function() {
            return _.reduce(this.getLabels(), function(memo, label) {
                return memo + label + ',';
            }, '');
        },
        getWeight: function() {
            var self = this;
            var weight = 1;
            var re = /Weight: (\d+)/;
            var label = _.find(self.getLabels(), function(label) {
                return re.test(label);
            });
            var myArray = re.exec(label);
            if (myArray != null) {
                weight = parseInt(myArray[1], 10);
            }
            return weight;
        },
        createLink: function() {
            var rval = '';

            var assignee = this.get('assignee') || null;
            var creator = this.get('user');
            var title = this.escape('title');
            var url = this.get('html_url');

            var created_at = new Date(this.get('created_at'));
            var month = created_at.getMonth() + 1;
            var issue_created = month + '/' + created_at.getDate() + '/' + created_at.getFullYear();

            var closed_at = this.get('closed_at');
            var work_duration_details = '';
            if (closed_at) {
                var closed_date = new Date(closed_at);
                var month = closed_date.getMonth() + 1;
                var issue_closed = month + '/' + closed_date.getDate() + '/' + closed_date.getFullYear();
                work_duration_details = issue_created + ' - ' + issue_closed;
            }
            else {
                var closed_date = new Date(Date.now());
                work_duration_details = issue_created + ' - ???';
            }

            var work_duration = moment(closed_date).from(created_at, true);

            if (creator && title && url) {
                var creator_user = new GithubUser(creator);
                var assigned_user = assignee ? new GithubUser(assignee) : null;
                rval = ['<ul>',
                        '<li class="title"><a target="_blank" href="' + url + '" title="' + title + '">',
                        title,
                        '<small>' +
                        'created by ' + creator_user.get('name'),
                        '</small>',
                        '</a></li>',
                        '<li class="whoswho">',
                        creator_user.getIcon(),
                        '<ins>&rArr;</ins>',
                        assigned_user ? assigned_user.getIcon() : '<ins class="annon">?</ins>',
                        '</li>',
                        '<li class="countdown">',
                        '<ins>' + work_duration + '</ins>',
                        '<ins>' + work_duration_details + '</ins>',
                        '</li>',
                        '</ul>'].join('');
            }

            return rval;
        }
    });
    var IssuesBase = Backbone.Collection.extend({
        model: Issue,
        comparator: function(issue) {
            return issue.get('closed_at');
        },
        url: function() {
            var token = session.get('token');
            var owner = session.get('owner');
            var repo = session.get('repo');

            // Build initial url string.
            var url = ['https://api.github.com',
                       '/repos/'+owner+'/'+repo+'/issues',
                       '?access_token='+token,
                       ''].join('');

            // If any parameter properties exist, append then to the URL string.
            if (this.state) {
                url += '&state='+this.state;
            }
            if (this.milestoneId) {
                url += '&milestone='+this.milestoneId;
            }
            if (this.since) {
                var _since = this.since();
                url += '&since='+_since;
            }
            if (this.direction) {
                url += '&direction='+this.direction;
            }

            return url;
        },
        parse: function(response) {
            return response;
        },
        getDateSince: function(days) {
            var d = new Date();
            d.setDate(d.getDate()-days);
            return d.toISOString();
        },
        getTotalWeight: function() {
            return _.reduce(this.models, function(memo, issue) {
                return memo + issue.getWeight();
            }, 0);
        },
        /*
         * parse_link_header()
         *
         * Parse the Github Link HTTP header used for pageination
         * http://developer.github.com/v3/#pagination
         */
        parseLinkHeader: function (header) {
          if (header.length == 0) {
              throw new Error("input must not be of zero length");
          }

          // Split parts by comma
          var parts = header.split(',');
          var links = {};
{'fraction_non_alphanumeric': 0.1464477896601049, 'fraction_numerical': 0.0029289557932020978, 'mean_word_length': 1.2911985018726593, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '7639623', 'n_tokens_mistral': 3963, 'n_tokens_neox': 3804, 'n_words': 847}