check_stats.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is addons.mozilla.org site.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * The Mozilla Foundation.
  19. * Portions created by the Initial Developer are Copyright (C) 2008
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. * Justin Scott <fligtar@mozilla.com> (Original Author)
  24. *
  25. *
  26. * Alternatively, the contents of this file may be used under the terms of
  27. * either the GNU General Public License Version 2 or later (the "GPL"), or
  28. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29. * in which case the provisions of the GPL or the LGPL are applicable instead
  30. * of those above. If you wish to allow use of your version of this file only
  31. * under the terms of either the GPL or the LGPL, and not to allow others to
  32. * use your version of this file under the terms of the MPL, indicate your
  33. * decision by deleting the provisions above and replace them with the notice
  34. * and other provisions required by the GPL or the LGPL. If you do not delete
  35. * the provisions above, a recipient may use your version of this file under
  36. * the terms of any one of the MPL, the GPL or the LGPL.
  37. *
  38. * ***** END LICENSE BLOCK ***** */
  39. /**
  40. * This script checks the integrity of the add-on download and updateping counts
  41. * obtained from parsing logs.
  42. *
  43. * The script checks a number of popular add-ons for several things:
  44. * - Does the download count from 2 days ago exist?
  45. * - If so, is there a difference of greater or less than 50% from the
  46. * previous 2 counts?
  47. * - If it's Saturday, does the update ping count from Wednesday exist?
  48. * - If so, is there a difference of greater or less than 50% from the
  49. * previous 2 counts?
  50. *
  51. * Download counter runs every night and can go into the next morning.
  52. * Accounting for a potential 24 hour log delay from the CDN, this is checked
  53. * 2 days after.
  54. *
  55. * Update ping counter is only run on Thursday nights and can go into Friday
  56. * mornings. Accounting for a potential 24 hour log delay from the CDN, this
  57. * will be checked on Saturdays.
  58. */
  59. // Prevent running from the web
  60. if (isset($_SERVER['HTTP_HOST'])) {
  61. exit;
  62. }
  63. // Include class files
  64. require_once('database.class.php');
  65. $db = new Database();
  66. define('DAY_SUNDAY', 0);
  67. define('DAY_MONDAY', 1);
  68. define('DAY_TUESDAY', 2);
  69. define('DAY_WEDNESDAY', 3);
  70. define('DAY_THURSDAY', 4);
  71. define('DAY_FRIDAY', 5);
  72. define('DAY_SATURDAY', 6);
  73. // Some popular add-ons to analyze
  74. $addons = array(
  75. 1865 => 'Adblock Plus',
  76. 722 => 'NoScript',
  77. 201 => 'DownThemAll'
  78. );
  79. foreach ($addons as $addon_id => $addon_name) {
  80. // DOWNLOADS
  81. $date = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d')-2, date('Y')));
  82. // Does download count from 2 days ago exist?
  83. $qry_2day = $db->query("SELECT count FROM download_counts WHERE date='{$date}' AND addon_id={$addon_id}");
  84. if (mysql_num_rows($qry_2day) > 0) {
  85. $count_2day = mysql_fetch_array($qry_2day);
  86. output('PASS', "[Downloads] Count from 2 days ago ({$date}) exists: {$count_2day['count']}", $addon_name);
  87. // Does that count have a 50% + or - difference from previous 2 counts?
  88. $qry_last2 = $db->query("SELECT date, count FROM download_counts WHERE addon_id={$addon_id} AND date < '{$date}' ORDER BY date DESC LIMIT 2");
  89. if (mysql_num_rows($qry_last2) > 0) {
  90. while ($row = mysql_fetch_array($qry_last2)) {
  91. $change = ceil(abs(($count_2day['count'] - $row['count']) / $row['count']) * 100);
  92. if ($change >= 50) {
  93. output('FAIL', "[Downloads] Count from 2 days ago changed by {$change}% from {$row['date']} count of {$row['count']}", $addon_name);
  94. }
  95. else {
  96. output('PASS', "[Downloads] Count from 2 days ago changed by {$change}% from {$row['date']} count of {$row['count']}", $addon_name);
  97. }
  98. }
  99. }
  100. }
  101. else {
  102. output('FAIL', "[Downloads] Count from 2 days ago ({$date}) does not exist", $addon_name);
  103. }
  104. // UPDATE PINGS
  105. if (date('w') == DAY_SATURDAY) {
  106. $wed = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d')-3, date('Y')));
  107. // Does update ping count from wednesday exist?
  108. $qry_wed = $db->query("SELECT count FROM update_counts WHERE date='{$wed}' AND addon_id={$addon_id}");
  109. if (mysql_num_rows($qry_wed) > 0) {
  110. $count_wed = mysql_fetch_array($qry_wed);
  111. output('PASS', "[Update Pings] Count from Wednesday ({$wed}) exists: {$count_wed['count']}", $addon_name);
  112. // Does that count have a 50% + or - difference from previous 2 counts?
  113. $qry_last2 = $db->query("SELECT date, count FROM update_counts WHERE addon_id={$addon_id} AND date < '{$wed}' ORDER BY date DESC LIMIT 2");
  114. if (mysql_num_rows($qry_last2) > 0) {
  115. while ($row = mysql_fetch_array($qry_last2)) {
  116. $change = ceil(abs(($count_wed['count'] - $row['count'])/ $row['count']) * 100);
  117. if ($change >= 50) {
  118. output('FAIL', "[Update Pings] Count from Wednesday changed by {$change}% from {$row['date']} count of {$row['count']}", $addon_name);
  119. }
  120. else {
  121. output('PASS', "[Update Pings] Count from Wednesday changed by {$change}% from {$row['date']} count of {$row['count']}", $addon_name);
  122. }
  123. }
  124. }
  125. }
  126. else {
  127. output('FAIL', "[Update Pings] Count from Wednesday ({$wed}) does not exist", $addon_name);
  128. }
  129. }
  130. else {
  131. output('NOTICE', "[Update Pings] Update ping integrity not checked because today is ".date('l'), $addon_name);
  132. }
  133. }
  134. function output($result, $message, $addon_name = '') {
  135. echo "[{$result}]";
  136. if (!empty($addon_name))
  137. echo "[{$addon_name}] ";
  138. echo "{$message}\n";
  139. }
  140. ?>