Index: trunk/extensions/ArticleFeedback/populateAFStatistics.php |
— | — | @@ -17,7 +17,7 @@ |
18 | 18 | * The period (in seconds) before now for which to gather stats |
19 | 19 | * @var int |
20 | 20 | */ |
21 | | - public $polling_period = 86400; |
| 21 | + public $polling_period = 1000000;//86400; |
22 | 22 | |
23 | 23 | /** |
24 | 24 | * The formatted timestamp from which to determine stats |
— | — | @@ -62,6 +62,7 @@ |
63 | 63 | $ratings = array(); |
64 | 64 | |
65 | 65 | // fetch the ratings since the lower bound timestamp |
| 66 | + $this->output( 'Fetching page ratings between now and ' . date('Y-m-d H:i:s', strtotime( $this->getLowerBoundTimestamp())) . "...\n"); |
66 | 67 | $res = $this->dbr->select( |
67 | 68 | 'article_feedback', |
68 | 69 | array( |
— | — | @@ -73,14 +74,17 @@ |
74 | 75 | __METHOD__, |
75 | 76 | array() // better to do with limits and offsets? |
76 | 77 | ); |
77 | | - |
| 78 | + |
78 | 79 | // assign the rating data to our data structure |
79 | 80 | foreach ( $res as $row ) { |
80 | 81 | $ratings[ $row->aa_page_id ][ $row->aa_rating_id ][] = $row->aa_rating_value; |
81 | 82 | } |
| 83 | + $this->output( "Done\n" ); |
82 | 84 | |
83 | 85 | // determine the average ratings for a given page |
| 86 | + $this->output( "Determining average ratings for articles ...\n" ); |
84 | 87 | $highs_and_lows = array(); |
| 88 | + $averages = array(); |
85 | 89 | foreach ( $ratings as $page_id => $data ) { |
86 | 90 | foreach( $data as $rating_id => $rating ) { |
87 | 91 | $rating_sum = array_sum( $rating ); |
— | — | @@ -90,13 +94,31 @@ |
91 | 95 | $overall_rating_sum = array_sum( $highs_and_lows[ $page_id ][ 'avg_ratings' ] ); |
92 | 96 | $overall_rating_average = $overall_rating_sum / count( $highs_and_lows[ $page_id ][ 'avg_ratings' ] ); |
93 | 97 | $highs_and_lows[ $page_id ][ 'average' ] = $overall_rating_average; |
| 98 | + |
| 99 | + // store overall average rating seperately so we can easily sort |
| 100 | + $averages[ $page_id ] = $overall_rating_average; |
94 | 101 | } |
| 102 | + $this->output( "Done.\n" ); |
| 103 | + |
| 104 | + // determine highest 50 and lowest 50 |
| 105 | + $this->output( "Determining 50 highest and 50 lowest rated articles...\n" ); |
| 106 | + asort( $averages ); |
| 107 | + // take lowest 50 and highest 50 |
| 108 | + $highest_and_lowest_page_ids = array_slice( $averages, 0, 50, true ); |
| 109 | + if ( count( $averages ) > 50 ) { |
| 110 | + $highest_and_lowest_page_ids = array_merge( $highest_and_lowest_page_ids, array_slice( $averages, -50, 50, true )); |
| 111 | + } |
| 112 | + $this->output( "Done\n" ); |
95 | 113 | |
96 | 114 | // prepare data for insert into db |
97 | 115 | $this->output( "Preparing data for db insertion ...\n"); |
98 | 116 | $cur_ts = $this->dbw->timestamp(); |
99 | 117 | $rows = array(); |
100 | 118 | foreach( $highs_and_lows as $page_id => $data ) { |
| 119 | + // make sure this is one of the highest/lowest average ratings |
| 120 | + if ( !in_array( $page_id, array_keys( $highest_and_lowest_page_ids ))) { |
| 121 | + continue; |
| 122 | + } |
101 | 123 | $rows[] = array( |
102 | 124 | 'afshl_page_id' => $page_id, |
103 | 125 | 'afshl_avg_overall' => $data[ 'average' ], |
— | — | @@ -105,7 +127,7 @@ |
106 | 128 | ); |
107 | 129 | } |
108 | 130 | $this->output( "Done.\n" ); |
109 | | - |
| 131 | + |
110 | 132 | // insert data to db |
111 | 133 | $this->output( "Writing data to article_feedback_stats_highs_lows ...\n" ); |
112 | 134 | $rowsInserted = 0; |