Skip to content

Commit

Permalink
Issue #158: Crawler run notifier alerts you if the crawler hasn't upd…
Browse files Browse the repository at this point in the history
…ated an instance in more than 3 hours
  • Loading branch information
ginatrapani committed Aug 2, 2010
1 parent 715ab81 commit ef24456
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 0 deletions.
63 changes: 63 additions & 0 deletions tests/TestOfCheckCrawlerController.php
@@ -0,0 +1,63 @@
<?php
require_once dirname(__FILE__).'/config.tests.inc.php';
require_once $SOURCE_ROOT_PATH.'extlib/simpletest/autorun.php';
ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.$INCLUDE_PATH);

require_once $SOURCE_ROOT_PATH.'tests/classes/class.ThinkUpUnitTestCase.php';
require_once $SOURCE_ROOT_PATH.'webapp/controller/class.ThinkUpController.php';
require_once $SOURCE_ROOT_PATH.'tests/fixtures/class.FixtureBuilder.php';
require_once $SOURCE_ROOT_PATH.'tests/fixtures/class.FixtureBuilderException.php';
require_once $SOURCE_ROOT_PATH.'webapp/controller/class.CheckCrawlerController.php';
require_once $SOURCE_ROOT_PATH.'extlib/Smarty-2.6.26/libs/Smarty.class.php';
require_once $SOURCE_ROOT_PATH.'webapp/model/class.SmartyThinkUp.php';
require_once $SOURCE_ROOT_PATH.'webapp/model/class.Instance.php';
require_once $SOURCE_ROOT_PATH.'webapp/model/class.DAOFactory.php';
require_once $SOURCE_ROOT_PATH.'webapp/model/class.Profiler.php';
require_once $SOURCE_ROOT_PATH.'webapp/model/class.Session.php';

/**
* Test of CheckCrawlerController
*
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
*
*/
class TestOfCheckCrawlerController extends ThinkUpUnitTestCase {

public function __construct() {
$this->UnitTestCase('CheckCrawlerController class test');
}

public function setUp(){
parent::setUp();
}

public function tearDown(){
parent::tearDown();
}

public function testConstructor() {
$controller = new CheckCrawlerController(true);
$this->assertTrue(isset($controller));
}

public function testNoInstances() {
$controller = new CheckCrawlerController(true);
$results = $controller->go();
$this->assertEqual('', $results);
}

public function testInstanceLessThan3Hours() {
$instance_builder = FixtureBuilder::build('instances', array('crawler_last_run'=>'-1h'));
$controller = new CheckCrawlerController(true);
$results = $controller->go();
$this->assertEqual('', $results);
}

public function testInstanceMoreThan3Hours() {
$instance_builder = FixtureBuilder::build('instances', array('crawler_last_run'=>'-4h'));
$controller = new CheckCrawlerController(true);
$results = $controller->go();
$this->assertEqual("Crawler hasn't run in 4 hours", $results);
}

}
13 changes: 13 additions & 0 deletions tests/TestOfInstanceMySQLDAO.php
Expand Up @@ -5,6 +5,8 @@
ini_set("include_path", ini_get("include_path").PATH_SEPARATOR.$INCLUDE_PATH);

require_once $SOURCE_ROOT_PATH.'tests/classes/class.ThinkUpUnitTestCase.php';
require_once $SOURCE_ROOT_PATH.'tests/fixtures/class.FixtureBuilder.php';
require_once $SOURCE_ROOT_PATH.'tests/fixtures/class.FixtureBuilderException.php';
require_once $SOURCE_ROOT_PATH.'webapp/model/class.InstanceMySQLDAO.php';
require_once $SOURCE_ROOT_PATH.'webapp/model/class.Instance.php';
require_once $SOURCE_ROOT_PATH.'webapp/model/class.Owner.php';
Expand Down Expand Up @@ -41,6 +43,17 @@ public function tearDown() {
parent::tearDown();
}

public function testGetHoursSinceLastCrawlerRun() {
$dao = new InstanceMySQLDAO();
$instance_builder = FixtureBuilder::build('instances', array('crawler_last_run'=>'-3h'));
$hours = $dao->getHoursSinceLastCrawlerRun();
$this->assertEqual($hours, 3);

$instance1_builder = FixtureBuilder::build('instances', array('crawler_last_run'=>'-2h'));
$hours = $dao->getHoursSinceLastCrawlerRun();
$this->assertEqual($hours, 2);
}

public function testInsert() {
$result = $this->DAO->insert(11, 'ev');
$this->assertEqual($result, 6);
Expand Down
2 changes: 2 additions & 0 deletions tests/all_controller_tests.php
Expand Up @@ -7,6 +7,7 @@
/* CONTROLLER TESTS */
require_once $SOURCE_ROOT_PATH.'tests/TestOfAccountConfigurationController.php';
require_once $SOURCE_ROOT_PATH.'tests/TestOfActivateAccountController.php';
require_once $SOURCE_ROOT_PATH.'tests/TestOfCheckCrawlerController.php';
require_once $SOURCE_ROOT_PATH.'tests/TestOfExportController.php';
require_once $SOURCE_ROOT_PATH.'tests/TestOfInlineViewController.php';
require_once $SOURCE_ROOT_PATH.'tests/TestOfLoginController.php';
Expand All @@ -23,6 +24,7 @@
$controller_test = & new GroupTest('Controller tests');
$controller_test->addTestCase(new TestOfAccountConfigurationController());
$controller_test->addTestCase(new TestOfActivateAccountController());
$controller_test->addTestCase(new TestOfCheckCrawlerController());
$controller_test->addTestCase(new TestOfExportController());
$controller_test->addTestCase(new TestOfInlineViewController());
$controller_test->addTestCase(new TestOfLoginController());
Expand Down
29 changes: 29 additions & 0 deletions webapp/controller/class.CheckCrawlerController.php
@@ -0,0 +1,29 @@
<?php
/**
* CheckCrawler Controller
* Outputs a message if crawler hasn't run in a certain number of hours
* @author Gina Trapani <ginatrapani[at]gmail[dot]com>
*
*/
class CheckCrawlerController extends ThinkUpController {
var $THRESHOLD = 3;

/**
* Constructor
*
* @param boolean $session_started
*/
public function __construct($session_started=false) {
parent::__construct($session_started);
$this->setViewTemplate('crawler.checkcrawler.tpl');
}

public function control() {
$instance_dao = DAOFactory::getDAO('InstanceDAO');
$hours_since_last_crawl = $instance_dao->getHoursSinceLastCrawlerRun();
if (isset($hours_since_last_crawl) && $hours_since_last_crawl > $this->THRESHOLD) {
$this->addToView('message', "Crawler hasn't run in ".round($hours_since_last_crawl)." hours");
}
return $this->generateView();
}
}
6 changes: 6 additions & 0 deletions webapp/crawler/checkcrawler.php
@@ -0,0 +1,6 @@
<?php
require_once 'init.php';
require_once 'controller/class.CheckCrawlerController.php';

$controller = new CheckCrawlerController();
echo $controller->go();
12 changes: 12 additions & 0 deletions webapp/model/class.InstanceMySQLDAO.php
Expand Up @@ -314,4 +314,16 @@ public function getByViewerId($viewer_id, $network = 'facebook') {
$ps = $this->execute($q, $vars);
return $this->getDataRowsAsObjects($ps, "Instance");
}

public function getHoursSinceLastCrawlerRun() {
$q = "SELECT (unix_timestamp( NOW() ) - unix_timestamp(crawler_last_run )) / 3600 as hours_since_last_run ";
$q .= "FROM #prefix#instances ORDER BY crawler_last_run DESC LIMIT 1";
$ps = $this->execute($q);
$result = $this->getDataRowsAsArrays($ps);
if ($this->getDataIsReturned($ps)) {
return $result[0]['hours_since_last_run'];
} else {
return null;
}
}
}
6 changes: 6 additions & 0 deletions webapp/model/interface.InstanceDAO.php
Expand Up @@ -153,4 +153,10 @@ public function getByUserAndViewerId($network_user_id, $viewer_id, $network = "f
* @return Instance
*/
public function getByViewerId($viewer_id, $network = "facebook");

/**
* Get the number of hours since the freshest instance was updated
* @return int hours
*/
public function getHoursSinceLastCrawlerRun();
}
1 change: 1 addition & 0 deletions webapp/view/crawler.checkcrawler.tpl
@@ -0,0 +1 @@
{$message}

0 comments on commit ef24456

Please sign in to comment.