2010-07-25 23:13:27 -04:00
NEWSBLUR . ReaderStatistics = function ( feed _id , options ) {
var defaults = { } ;
this . options = $ . extend ( { } , defaults , options ) ;
this . model = NEWSBLUR . AssetModel . reader ( ) ;
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 ( ) ;
} ;
NEWSBLUR . ReaderStatistics . prototype = {
runner : function ( ) {
2010-08-03 09:19:38 -04:00
var self = this ;
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
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' } , [
2011-02-17 10:32:47 -05:00
$ . make ( 'img' , { className : 'NB-modal-feed-image feed_favicon' , src : $ . favicon ( this . feed . favicon ) } ) ,
2010-08-01 19:12:42 -04:00
$ . make ( 'span' , { className : 'NB-modal-feed-title' } , this . feed . feed _title )
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' : '' ,
'next_update' : ''
} ) ;
$ ( '.NB-modal-statistics-info' , this . $modal ) . replaceWith ( $stats ) ;
2010-07-25 23:13:27 -04:00
} ,
2010-07-30 17:12:20 -04:00
initialize _feed : function ( feed _id ) {
this . feed _id = feed _id ;
this . feed = this . model . get _feed ( feed _id ) ;
2011-02-17 10:32:47 -05:00
$ ( '.NB-modal-subtitle .NB-modal-feed-image' , this . $modal ) . attr ( 'src' , $ . favicon ( this . feed . favicon ) ) ;
2010-08-01 19:12:42 -04:00
$ ( '.NB-modal-subtitle .NB-modal-feed-title' , this . $modal ) . html ( this . feed [ 'feed_title' ] ) ;
2010-07-30 17:12:20 -04:00
} ,
2010-07-25 23:13:27 -04:00
open _modal : function ( ) {
var self = this ;
this . $modal . modal ( {
'minWidth' : 600 ,
2010-08-03 23:41:02 -04:00
'maxWidth' : 600 ,
2010-08-03 09:19:38 -04:00
'minHeight' : 425 ,
2010-07-25 23:13:27 -04:00
'overlayClose' : true ,
2010-08-01 23:47:40 -04:00
'autoResize' : true ,
2010-07-25 23:13:27 -04:00
'onOpen' : function ( dialog ) {
dialog . overlay . fadeIn ( 200 , function ( ) {
dialog . container . fadeIn ( 200 ) ;
dialog . data . fadeIn ( 200 ) ;
} ) ;
} ,
'onShow' : function ( dialog ) {
2010-07-26 22:21:58 -04:00
$ ( '#simplemodal-container' ) . corner ( '6px' ) ;
2010-07-25 23:13:27 -04:00
} ,
'onClose' : function ( dialog ) {
dialog . data . hide ( ) . empty ( ) . remove ( ) ;
dialog . container . hide ( ) . empty ( ) . remove ( ) ;
dialog . overlay . fadeOut ( 200 , function ( ) {
dialog . overlay . empty ( ) . remove ( ) ;
$ . modal . close ( ) ;
} ) ;
$ ( '.NB-modal-holder' ) . empty ( ) . remove ( ) ;
}
} ) ;
} ,
2010-07-30 17:12:20 -04:00
make _feed _chooser : function ( ) {
var $chooser = $ . make ( 'select' , { name : 'feed' , className : 'NB-modal-feed-chooser' } ) ;
for ( var f in this . feeds ) {
var feed = this . feeds [ f ] ;
var $option = $ . make ( 'option' , { value : feed . id } , feed . feed _title ) ;
$option . appendTo ( $chooser ) ;
if ( feed . id == this . feed _id ) {
$option . attr ( 'selected' , true ) ;
}
}
$ ( 'option' , $chooser ) . tsort ( ) ;
return $chooser ;
} ,
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' ) ;
2010-07-25 23:13:27 -04:00
this . model . get _feed _statistics ( this . feed _id , $ . rescope ( this . populate _stats , this ) ) ;
} ,
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' ) ;
2010-07-28 01:14:25 -04:00
var interval _start = data [ 'update_interval_minutes' ] ;
var interval _end = data [ 'update_interval_minutes' ] * 1.25 ;
var interval = '' ;
if ( interval _start < 60 ) {
interval = interval _start + ' to ' + interval _end + ' minutes' ;
} else {
var interval _start _hours = parseInt ( interval _start / 60 , 10 ) ;
var interval _end _hours = parseInt ( interval _end / 60 , 10 ) ;
var dec _start = interval _start % 60 ;
var dec _end = interval _end % 60 ;
interval = interval _start _hours + ( dec _start >= 30 ? '.5' : '' ) + ' to ' + interval _end _hours + ( dec _end >= 30 || interval _start _hours == interval _end _hours ? '.5' : '' ) + ' hours' ;
}
2010-07-30 23:50:49 -04:00
var $stats = this . make _stats ( data , interval ) ;
$ ( '.NB-modal-statistics-info' , this . $modal ) . replaceWith ( $stats ) ;
setTimeout ( function ( ) {
self . make _charts ( data ) ;
} , this . first _load ? 500 : 50 ) ;
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
} ,
make _stats : function ( data , interval ) {
2010-07-26 22:21:58 -04:00
var $stats = $ . make ( 'div' , { className : 'NB-modal-statistics-info' } , [
2010-07-28 01:14:25 -04:00
$ . make ( 'div' , { className : 'NB-statistics-stat NB-statistics-updates' } , [
$ . 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' ) ,
$ . make ( 'div' , { className : 'NB-statistics-count' } , 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' ) ,
2010-10-10 20:14:31 -04:00
$ . make ( 'div' , { className : 'NB-statistics-count' } , ' ' + ( data [ 'next_update' ] && ( 'in ' + data [ 'next_update' ] ) ) )
2010-07-28 01:14:25 -04: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
] ) ,
$ . make ( 'div' , { 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' ] )
] ) ) ,
2011-02-13 14:47:58 -05:00
$ . make ( 'div' , { className : 'NB-statistics-stat NB-statistics-fetches' } , [
$ . make ( 'div' , { className : 'NB-statistics-fetches-half' } , [
$ . make ( 'div' , { className : 'NB-statistics-label' } , 'Feed' ) ,
$ . make ( 'div' , this . make _history ( data , 'feed' ) )
] ) ,
$ . make ( 'div' , { className : 'NB-statistics-fetches-half' } , [
$ . make ( 'div' , { className : 'NB-statistics-label' } , 'Page' ) ,
$ . make ( 'div' , this . make _history ( data , 'page' ) )
] )
2010-07-26 22:21:58 -04:00
] )
] ) ;
2011-01-30 13:33:09 -05:00
var count = _ . isUndefined ( data [ 'subscriber_count' ] ) && 'Loading ' || data [ 'subscriber_count' ] ;
2011-04-07 10:36:25 -04:00
var $subscribers = $ . make ( 'div' , { className : 'NB-modal-subtitle-right' } , [
$ . make ( 'span' , { className : 'NB-modal-subtitle-right-count' } , '' + count ) ,
$ . make ( 'span' , { className : 'NB-modal-subtitle-right-label' } , 'subscriber' + ( data [ 'subscriber_count' ] == 1 ? '' : 's' ) )
2010-08-13 10:43:48 -04:00
] ) ;
2011-04-07 10:36:25 -04:00
$ ( '.NB-modal-subtitle-right' , this . $modal ) . remove ( ) ;
2010-08-13 10:43:48 -04:00
$ ( '.NB-modal-subtitle' , this . $modal ) . prepend ( $subscribers ) ;
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:47:37 -04:00
return Math . max ( 2 , 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' , [
$ . make ( 'img' , { className : 'NB-modal-feed-image feed_favicon' , src : $ . favicon ( self . feed . favicon ) } ) ,
$ . make ( 'span' , { className : 'NB-modal-feed-title' } , self . feed . feed _title )
] ) ] ;
}
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 ) ) ,
$ . make ( 'div' , { className : 'NB-statistics-facet-count' } , pos + Inflector . pluralize ( ' like' , pos ) ) . 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 ) ) ,
$ . make ( 'div' , { className : 'NB-statistics-facet-count' } , neg + Inflector . pluralize ( ' dislike' , neg ) ) . 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 ) {
var fetches = data [ fetch _type + '_fetch_history' ] ;
if ( ! fetches ) return ;
var $history = _ . map ( fetches , function ( fetch ) {
var feed _ok = _ . contains ( [ 200 , 304 ] , fetch . status _code ) ;
var status _class = feed _ok ? ' NB-ok ' : ' NB-error ' ;
return $ . make ( 'div' , { className : 'NB-statistics-history-fetch' + status _class , title : feed _ok ? '' : fetch . exception } , [
$ . make ( 'div' , { className : 'NB-statistics-history-fetch-date' } , fetch . fetch _date ) ,
$ . make ( 'div' , { className : 'NB-statistics-history-fetch-message' } , [
fetch . message ,
$ . make ( 'div' , { className : 'NB-statistics-history-fetch-code' } , ' (' + fetch . status _code + ')' )
] )
] ) ;
} ) ;
return $history ;
} ,
2010-07-30 23:50:49 -04:00
make _charts : function ( data ) {
2010-08-13 11:03:07 -04:00
data [ 'story_count_history' ] = _ . 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})/ ) ;
return [ ( new Date ( parseInt ( date _matched [ 1 ] , 10 ) , parseInt ( date _matched [ 2 ] , 10 ) - 1 ) ) . getTime ( ) ,
date [ 1 ] ] ;
2010-07-30 23:50:49 -04:00
} ) ;
2010-08-13 10:43:48 -04:00
var $plot = $ ( ".NB-statistics-history-chart" ) ;
var plot = $ . plot ( $plot ,
2010-08-13 11:03:07 -04:00
[ { data : data [ 'story_count_history' ] , label : "Stories" } ] , {
2010-08-13 10:43:48 -04:00
series : {
lines : { show : true } ,
points : { show : true }
} ,
average : data [ 'average_stories_per_month' ] ,
legend : { show : false } ,
grid : { hoverable : true , clickable : true } ,
yaxis : { tickDecimals : 0 , min : 0 } ,
2010-08-16 12:52:39 -04:00
xaxis : { mode : 'time' , minTickSize : [ 1 , 'month' ] , timeformat : '%b %y' }
2010-08-13 10:43:48 -04:00
} ) ;
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 ( ) ;
} ) ;
2010-07-25 23:13:27 -04:00
}
} ;