2010-07-25 23:13:27 -04:00
|
|
|
NEWSBLUR.ReaderStatistics = function(feed_id, options) {
|
|
|
|
var defaults = {};
|
|
|
|
|
|
|
|
this.options = $.extend({}, defaults, options);
|
2012-05-17 18:40:46 -07:00
|
|
|
this.model = NEWSBLUR.assets;
|
2010-07-25 23:13:27 -04:00
|
|
|
this.feed_id = feed_id;
|
|
|
|
this.feed = this.model.get_feed(feed_id);
|
2010-07-30 17:12:20 -04:00
|
|
|
this.feeds = this.model.get_feeds();
|
2010-07-30 23:50:49 -04:00
|
|
|
this.first_load = true;
|
2010-07-25 23:13:27 -04:00
|
|
|
this.runner();
|
|
|
|
};
|
|
|
|
|
2011-11-12 18:19:57 -08:00
|
|
|
NEWSBLUR.ReaderStatistics.prototype = new NEWSBLUR.Modal;
|
|
|
|
NEWSBLUR.ReaderStatistics.prototype.constructor = NEWSBLUR.ReaderStatistics;
|
|
|
|
|
|
|
|
_.extend(NEWSBLUR.ReaderStatistics.prototype, {
|
2010-07-25 23:13:27 -04:00
|
|
|
|
|
|
|
runner: function() {
|
2010-08-03 09:19:38 -04:00
|
|
|
var self = this;
|
|
|
|
|
2012-02-16 18:36:52 -08:00
|
|
|
this.initialize_feed(this.feed_id);
|
2010-07-25 23:13:27 -04:00
|
|
|
this.make_modal();
|
|
|
|
this.open_modal();
|
2010-08-03 09:19:38 -04:00
|
|
|
setTimeout(function() {
|
|
|
|
self.get_stats();
|
|
|
|
}, 50);
|
2010-07-30 17:12:20 -04:00
|
|
|
|
2012-01-13 17:54:17 -08:00
|
|
|
this.$modal.bind('click', $.rescope(this.handle_click, this));
|
2010-07-30 17:12:20 -04:00
|
|
|
this.$modal.bind('change', $.rescope(this.handle_change, this));
|
2010-07-25 23:13:27 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
make_modal: function() {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
this.$modal = $.make('div', { className: 'NB-modal-statistics NB-modal' }, [
|
2010-08-03 09:19:38 -04:00
|
|
|
$.make('div', { className: 'NB-modal-feed-chooser-container'}, [
|
|
|
|
this.make_feed_chooser()
|
|
|
|
]),
|
2010-07-30 17:12:20 -04:00
|
|
|
$.make('div', { className: 'NB-modal-loading' }),
|
2010-07-25 23:13:27 -04:00
|
|
|
$.make('h2', { className: 'NB-modal-title' }, 'Statistics & History'),
|
|
|
|
$.make('h2', { className: 'NB-modal-subtitle' }, [
|
2012-01-26 18:59:40 -08:00
|
|
|
$.make('img', { className: 'NB-modal-feed-image feed_favicon', src: $.favicon(this.feed) }),
|
2011-07-27 22:17:34 -07:00
|
|
|
$.make('div', { className: 'NB-modal-feed-heading' }, [
|
2012-05-21 20:08:27 -07:00
|
|
|
$.make('span', { className: 'NB-modal-feed-title' }, this.feed.get('feed_title')),
|
|
|
|
$.make('span', { className: 'NB-modal-feed-subscribers' }, Inflector.pluralize(' subscriber', this.feed.get('num_subscribers'), true))
|
2011-07-27 22:17:34 -07:00
|
|
|
])
|
2010-07-26 22:21:58 -04:00
|
|
|
]),
|
2010-08-03 09:19:38 -04:00
|
|
|
$.make('div', { className: 'NB-modal-statistics-info' })
|
2010-07-25 23:13:27 -04:00
|
|
|
]);
|
2010-10-10 20:14:31 -04:00
|
|
|
|
|
|
|
var $stats = this.make_stats({
|
|
|
|
'last_update': '',
|
2012-07-07 10:04:13 -07:00
|
|
|
'next_update': '',
|
|
|
|
'loading': true
|
2010-10-10 20:14:31 -04:00
|
|
|
});
|
|
|
|
$('.NB-modal-statistics-info', this.$modal).replaceWith($stats);
|
2010-07-25 23:13:27 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
get_stats: function() {
|
2010-07-30 17:12:20 -04:00
|
|
|
var $loading = $('.NB-modal-loading', this.$modal);
|
|
|
|
$loading.addClass('NB-active');
|
|
|
|
|
2012-02-16 18:36:52 -08:00
|
|
|
var statistics_fn = this.options.social_feed ? this.model.get_social_statistics : this.model.get_feed_statistics;
|
|
|
|
statistics_fn.call(this.model, this.feed_id, $.rescope(this.populate_stats, this));
|
2010-07-25 23:13:27 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
populate_stats: function(s, data) {
|
2010-07-30 23:50:49 -04:00
|
|
|
var self = this;
|
|
|
|
|
2010-07-25 23:13:27 -04:00
|
|
|
NEWSBLUR.log(['Stats', data]);
|
2010-07-30 23:50:49 -04:00
|
|
|
|
2010-07-30 17:12:20 -04:00
|
|
|
var $loading = $('.NB-modal-loading', this.$modal);
|
|
|
|
$loading.removeClass('NB-active');
|
|
|
|
|
2012-01-13 17:54:17 -08:00
|
|
|
var $stats = this.make_stats(data);
|
2010-07-30 23:50:49 -04:00
|
|
|
$('.NB-modal-statistics-info', this.$modal).replaceWith($stats);
|
|
|
|
|
|
|
|
setTimeout(function() {
|
|
|
|
self.make_charts(data);
|
2013-03-19 11:11:17 -07:00
|
|
|
}, this.first_load ? 200 : 50);
|
2010-07-30 23:50:49 -04:00
|
|
|
|
|
|
|
setTimeout(function() {
|
2010-08-01 23:47:40 -04:00
|
|
|
$.modal.impl.resize(self.$modal);
|
2010-08-13 18:18:46 -04:00
|
|
|
}, 100);
|
2010-07-30 23:50:49 -04:00
|
|
|
},
|
|
|
|
|
2012-01-13 18:00:33 -08:00
|
|
|
make_stats: function(data) {
|
2013-02-11 16:07:08 -08:00
|
|
|
var update_interval = NEWSBLUR.utils.calculate_update_interval(data['update_interval_minutes']);
|
|
|
|
var premium_update_interval = NEWSBLUR.utils.calculate_update_interval(data['premium_update_interval_minutes']);
|
2012-01-13 17:54:17 -08:00
|
|
|
|
2010-07-26 22:21:58 -04:00
|
|
|
var $stats = $.make('div', { className: 'NB-modal-statistics-info' }, [
|
2012-02-16 18:36:52 -08:00
|
|
|
(!this.options.social_feed && $.make('div', { className: 'NB-statistics-stat NB-statistics-updates'}, [
|
2010-07-28 01:14:25 -04:00
|
|
|
$.make('div', { className: 'NB-statistics-update'}, [
|
2010-07-30 17:12:20 -04:00
|
|
|
$.make('div', { className: 'NB-statistics-label' }, 'Last Update'),
|
2010-10-10 20:14:31 -04:00
|
|
|
$.make('div', { className: 'NB-statistics-count' }, ' ' + (data['last_update'] && (data['last_update'] + ' ago')))
|
2010-07-28 01:14:25 -04:00
|
|
|
]),
|
|
|
|
$.make('div', { className: 'NB-statistics-update'}, [
|
2010-07-30 17:12:20 -04:00
|
|
|
$.make('div', { className: 'NB-statistics-label' }, 'Every'),
|
2012-01-13 17:54:17 -08:00
|
|
|
$.make('div', { className: 'NB-statistics-count' }, update_interval)
|
2010-07-28 01:14:25 -04:00
|
|
|
]),
|
|
|
|
$.make('div', { className: 'NB-statistics-update'}, [
|
2010-07-30 17:12:20 -04:00
|
|
|
$.make('div', { className: 'NB-statistics-label' }, 'Next Update'),
|
2012-02-13 11:07:32 -08:00
|
|
|
(data.active && $.make('div', { className: 'NB-statistics-count' }, ' ' + (data['next_update'] && ('in ' + data['next_update'])))),
|
2012-07-07 10:04:13 -07:00
|
|
|
(!data.active && !data.loading && $.make('div', { className: 'NB-statistics-count' }, "Not active"))
|
2012-01-13 17:54:17 -08:00
|
|
|
]),
|
|
|
|
(!NEWSBLUR.Globals.is_premium && $.make('div', { className: 'NB-statistics-premium-stats' }, [
|
|
|
|
$.make('div', { className: 'NB-statistics-update'}, [
|
|
|
|
$.make('div', { className: 'NB-statistics-label' }, [
|
|
|
|
'If you went ',
|
|
|
|
$.make('a', { href: '#', className: 'NB-premium-link NB-splash-link' }, 'premium'),
|
|
|
|
', ',
|
|
|
|
$.make('br'),
|
|
|
|
'this site would update every'
|
|
|
|
]),
|
|
|
|
$.make('div', { className: 'NB-statistics-count' }, premium_update_interval)
|
|
|
|
])
|
|
|
|
]))
|
2012-02-16 18:36:52 -08:00
|
|
|
])),
|
2010-07-30 17:12:20 -04:00
|
|
|
$.make('div', { className: 'NB-statistics-stat NB-statistics-history'}, [
|
|
|
|
$.make('div', { className: 'NB-statistics-history-stat' }, [
|
2010-08-13 10:43:48 -04:00
|
|
|
$.make('div', { className: 'NB-statistics-label' }, 'Stories per month')
|
2010-07-30 23:50:49 -04:00
|
|
|
]),
|
2013-03-19 11:11:17 -07:00
|
|
|
$.make('canvas', { id: 'NB-statistics-history-chart', className: 'NB-statistics-history-chart' })
|
2011-02-13 14:47:58 -05:00
|
|
|
]),
|
2011-04-09 11:06:36 -04:00
|
|
|
(data.classifier_counts && $.make('div', { className: 'NB-statistics-state NB-statistics-classifiers' }, [
|
|
|
|
this.make_classifier_count('tag', data.classifier_counts['tag']),
|
|
|
|
this.make_classifier_count('author', data.classifier_counts['author']),
|
|
|
|
this.make_classifier_count('title', data.classifier_counts['title']),
|
|
|
|
this.make_classifier_count('feed', data.classifier_counts['feed'])
|
|
|
|
])),
|
2012-02-17 17:41:20 -08:00
|
|
|
(!this.options.social_feed && $.make('div', { className: 'NB-statistics-stat NB-statistics-fetches'}, [
|
2011-02-13 14:47:58 -05:00
|
|
|
$.make('div', { className: 'NB-statistics-fetches-half'}, [
|
2012-04-09 14:58:53 -07:00
|
|
|
$.make('div', { className: 'NB-statistics-label' }, 'Feed Fetch'),
|
|
|
|
$.make('div', this.make_history(data, 'feed_fetch'))
|
2011-02-13 14:47:58 -05:00
|
|
|
]),
|
|
|
|
$.make('div', { className: 'NB-statistics-fetches-half'}, [
|
2012-04-09 14:58:53 -07:00
|
|
|
$.make('div', { className: 'NB-statistics-label' }, 'Page Fetch'),
|
|
|
|
$.make('div', this.make_history(data, 'page_fetch'))
|
|
|
|
]),
|
2012-04-09 15:09:29 -07:00
|
|
|
$.make('div', { className: 'NB-statistics-fetches-half'}, [
|
2012-04-09 14:58:53 -07:00
|
|
|
$.make('div', { className: 'NB-statistics-label' }, 'Feed Push'),
|
|
|
|
$.make('div', this.make_history(data, 'feed_push'))
|
2011-02-13 14:47:58 -05:00
|
|
|
])
|
2012-02-17 17:41:20 -08:00
|
|
|
]))
|
2010-07-26 22:21:58 -04:00
|
|
|
]);
|
|
|
|
|
2010-07-30 23:50:49 -04:00
|
|
|
return $stats;
|
|
|
|
},
|
|
|
|
|
2011-04-09 11:06:36 -04:00
|
|
|
make_classifier_count: function(facet, data) {
|
2011-04-09 14:47:37 -04:00
|
|
|
var self = this;
|
2011-04-09 11:06:36 -04:00
|
|
|
if (!data) return;
|
|
|
|
|
|
|
|
var $facets = $.make('div', { className: 'NB-statistics-facets' }, [
|
2011-04-09 14:37:07 -04:00
|
|
|
$.make('div', { className: 'NB-statistics-facet-title' }, Inflector.pluralize(facet, data.length))
|
2011-04-09 11:06:36 -04:00
|
|
|
]);
|
|
|
|
|
2011-04-09 14:37:07 -04:00
|
|
|
var max = 10;
|
|
|
|
_.each(data, function(v) {
|
|
|
|
if (v.pos > max || v.neg > max) {
|
|
|
|
max = Math.max(v.pos, v.neg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var max_width = 100;
|
|
|
|
var multiplier = max_width / parseFloat(max, 10);
|
|
|
|
var calculate_width = function(count) {
|
2011-04-09 14:49:24 -04:00
|
|
|
return Math.max(1, multiplier * count);
|
2011-04-09 14:37:07 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
_.each(data, function(counts) {
|
2011-04-09 11:06:36 -04:00
|
|
|
var pos = counts.pos || 0;
|
|
|
|
var neg = counts.neg || 0;
|
2011-04-09 14:37:07 -04:00
|
|
|
var key = counts[facet];
|
2011-04-09 14:47:37 -04:00
|
|
|
if (facet == 'feed') {
|
|
|
|
key = [$.make('div', [
|
2012-01-26 18:59:40 -08:00
|
|
|
$.make('img', { className: 'NB-modal-feed-image feed_favicon', src: $.favicon(self.feed) }),
|
2012-05-21 20:08:27 -07:00
|
|
|
$.make('span', { className: 'NB-modal-feed-title' }, self.feed.get('feed_title'))
|
2011-04-09 14:47:37 -04:00
|
|
|
])];
|
|
|
|
}
|
|
|
|
if (!key || (!pos && !neg)) return;
|
2011-04-09 11:06:36 -04:00
|
|
|
var $facet = $.make('div', { className: 'NB-statistics-facet' }, [
|
2011-04-09 13:38:14 -04:00
|
|
|
(pos && $.make('div', { className: 'NB-statistics-facet-pos' }, [
|
2011-04-09 14:37:07 -04:00
|
|
|
$.make('div', { className: 'NB-statistics-facet-bar' }).css('width', calculate_width(pos)),
|
2012-05-21 20:08:27 -07:00
|
|
|
$.make('div', { className: 'NB-statistics-facet-count' }, Inflector.pluralize(' like', pos, true)).css('margin-left', calculate_width(pos)+5)
|
2011-04-09 13:38:14 -04:00
|
|
|
])),
|
|
|
|
(neg && $.make('div', { className: 'NB-statistics-facet-neg' }, [
|
2011-04-09 14:37:07 -04:00
|
|
|
$.make('div', { className: 'NB-statistics-facet-bar' }).css('width', calculate_width(neg)),
|
2012-05-21 20:08:27 -07:00
|
|
|
$.make('div', { className: 'NB-statistics-facet-count' }, Inflector.pluralize(' dislike', neg, true)).css('margin-right', calculate_width(neg)+5)
|
2011-04-09 13:38:14 -04:00
|
|
|
])),
|
|
|
|
$.make('div', { className: 'NB-statistics-facet-separator' }),
|
2011-04-09 11:06:36 -04:00
|
|
|
$.make('div', { className: 'NB-statistics-facet-name' }, key)
|
|
|
|
]);
|
|
|
|
$facets.append($facet);
|
|
|
|
});
|
|
|
|
|
|
|
|
return $facets;
|
|
|
|
},
|
|
|
|
|
2011-02-13 14:47:58 -05:00
|
|
|
make_history: function(data, fetch_type) {
|
2012-04-09 14:58:53 -07:00
|
|
|
var fetches = data[fetch_type+'_history'];
|
2012-04-09 15:09:29 -07:00
|
|
|
var $history;
|
|
|
|
|
|
|
|
if (!fetches || !fetches.length) {
|
2012-12-28 21:42:52 -08:00
|
|
|
$history = $.make('div', { className: 'NB-history-empty' }, "Nothing recorded.");
|
2012-04-09 15:09:29 -07:00
|
|
|
} else {
|
|
|
|
$history = _.map(fetches, function(fetch) {
|
|
|
|
var feed_ok = _.contains([200, 304], fetch.status_code) || !fetch.status_code;
|
|
|
|
var status_class = feed_ok ? ' NB-ok ' : ' NB-errorcode ';
|
2012-12-28 21:42:52 -08:00
|
|
|
return $.make('div', { className: 'NB-history-fetch' + status_class, title: feed_ok ? '' : fetch.exception }, [
|
|
|
|
$.make('div', { className: 'NB-history-fetch-date' }, fetch.fetch_date || fetch.push_date),
|
|
|
|
$.make('div', { className: 'NB-history-fetch-message' }, [
|
2012-04-09 15:09:29 -07:00
|
|
|
fetch.message,
|
2012-12-28 21:42:52 -08:00
|
|
|
(fetch.status_code && $.make('div', { className: 'NB-history-fetch-code' }, ' ('+fetch.status_code+')'))
|
2012-04-09 15:09:29 -07:00
|
|
|
])
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
}
|
2011-02-13 14:47:58 -05:00
|
|
|
|
|
|
|
return $history;
|
|
|
|
},
|
|
|
|
|
2010-07-30 23:50:49 -04:00
|
|
|
make_charts: function(data) {
|
2013-03-19 11:11:17 -07:00
|
|
|
var labels = _.map(data['story_count_history'], function(date) {
|
2010-08-13 10:43:48 -04:00
|
|
|
var date_matched = date[0].match(/(\d{4})-(\d{1,2})/);
|
2013-03-19 11:11:17 -07:00
|
|
|
var date = (new Date(parseInt(date_matched[1], 10), parseInt(date_matched[2],10)-1));
|
|
|
|
return NEWSBLUR.utils.shortMonthNames[date.getMonth()] + " " + date.getUTCFullYear();
|
2010-07-30 23:50:49 -04:00
|
|
|
});
|
2013-03-19 11:11:17 -07:00
|
|
|
var values = _.map(data['story_count_history'], function(date) {
|
|
|
|
return date[1];
|
|
|
|
});
|
|
|
|
var points = {
|
|
|
|
labels: labels,
|
|
|
|
datasets: [
|
|
|
|
{
|
|
|
|
fillColor : "rgba(151,187,205,0.5)",
|
|
|
|
strokeColor : "rgba(151,187,205,1)",
|
|
|
|
pointColor : "rgba(151,187,205,1)",
|
|
|
|
pointStrokeColor : "#fff",
|
|
|
|
data : values
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
2010-08-13 10:43:48 -04:00
|
|
|
var $plot = $(".NB-statistics-history-chart");
|
2013-03-19 11:11:17 -07:00
|
|
|
var width = $plot.width();
|
|
|
|
var height = $plot.height();
|
|
|
|
$plot.attr('width', width);
|
|
|
|
$plot.attr('height', height);
|
|
|
|
var myLine = new Chart($plot.get(0).getContext("2d")).Line(points, {
|
|
|
|
scaleLabel : "<%= Math.round(value) %>"
|
|
|
|
});
|
2010-07-30 17:12:20 -04:00
|
|
|
},
|
|
|
|
|
2012-01-13 17:54:17 -08:00
|
|
|
close_and_load_premium: function() {
|
|
|
|
this.close(function() {
|
|
|
|
NEWSBLUR.reader.open_feedchooser_modal();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// ===========
|
|
|
|
// = Actions =
|
|
|
|
// ===========
|
|
|
|
|
2010-07-30 17:12:20 -04:00
|
|
|
handle_change: function(elem, e) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
$.targetIs(e, { tagSelector: '.NB-modal-feed-chooser' }, function($t, $p){
|
|
|
|
var feed_id = $t.val();
|
2010-07-30 23:50:49 -04:00
|
|
|
self.first_load = false;
|
2010-07-30 17:12:20 -04:00
|
|
|
self.initialize_feed(feed_id);
|
|
|
|
self.get_stats();
|
|
|
|
});
|
2012-01-13 17:54:17 -08:00
|
|
|
},
|
|
|
|
|
|
|
|
handle_click: function(elem, e) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
$.targetIs(e, { tagSelector: '.NB-premium-link' }, function($t, $p) {
|
|
|
|
e.preventDefault();
|
|
|
|
self.close_and_load_premium();
|
|
|
|
});
|
2010-07-25 23:13:27 -04:00
|
|
|
}
|
|
|
|
|
2011-11-12 18:19:57 -08:00
|
|
|
});
|