Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create insight baselines and insight database tables, model objects. …
…Create InsightBaseline data access object interface and MySQL implementation and tests
- Loading branch information
1 parent
24440cd
commit 44e4a4a
Showing
8 changed files
with
389 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
/** | ||
* | ||
* ThinkUp/tests/TestOfInsightBaselineMySQLDAO.php | ||
* | ||
* Copyright (c) 2012 Gina Trapani | ||
* | ||
* LICENSE: | ||
* | ||
* This file is part of ThinkUp (http://thinkupapp.com). | ||
* | ||
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public | ||
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any | ||
* later version. | ||
* | ||
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see | ||
* <http://www.gnu.org/licenses/>. | ||
* | ||
* @license http://www.gnu.org/licenses/gpl.html | ||
* @copyright 2012 Gina Trapani | ||
* @author Gina Trapani | ||
*/ | ||
require_once dirname(__FILE__).'/init.tests.php'; | ||
require_once THINKUP_WEBAPP_PATH.'_lib/extlib/simpletest/autorun.php'; | ||
require_once THINKUP_WEBAPP_PATH.'config.inc.php'; | ||
|
||
class TestOfInsightBaselineMySQLDAO extends ThinkUpUnitTestCase { | ||
public function setUp() { | ||
parent::setUp(); | ||
$this->builders = self::buildData(); | ||
} | ||
|
||
protected function buildData() { | ||
$builders = array(); | ||
$builders[] = FixtureBuilder::build('insight_baselines', array('date'=>'2012-05-01', | ||
'slug'=>'avg_replies_per_week', 'instance_id'=>1, 'value'=>51)); | ||
return $builders; | ||
} | ||
|
||
public function tearDown() { | ||
$this->builders = null; | ||
parent::tearDown(); | ||
} | ||
|
||
public function testInsertInsightBaseline() { | ||
$dao = new InsightBaselineMySQLDAO(); | ||
$result = $dao->insertInsightBaseline('avg_replies_per_week', '2012-05-05', 1, 51); | ||
$this->assertTrue($result); | ||
|
||
$result = $dao->insertInsightBaseline('avg_replies_per_week', '2012-05-05', 1, 50); | ||
$this->assertTrue($result); | ||
$result = $dao->getInsightBaseline('avg_replies_per_week', '2012-05-05', 1); | ||
$this->assertEqual($result->value, 50); | ||
} | ||
|
||
public function testGetInsightBaseline() { | ||
$dao = new InsightBaselineMySQLDAO(); | ||
$result = $dao->getInsightBaseline('avg_replies_per_week', '2012-05-01', 1); | ||
$this->assertIsA($result, 'InsightBaseline'); | ||
$this->assertEqual($result->slug, 'avg_replies_per_week'); | ||
$this->assertEqual($result->date, '2012-05-01'); | ||
$this->assertEqual($result->instance_id, 1); | ||
$this->assertEqual($result->value, 51); | ||
|
||
$result = $dao->getInsightBaseline('avg_replies_per_week', '2012-05-02', 1); | ||
$this->assertNull($result); | ||
} | ||
|
||
public function testUpdateInsightBaseline() { | ||
$dao = new InsightBaselineMySQLDAO(); | ||
|
||
//update existing baseline | ||
$result = $dao->updateInsightBaseline('avg_replies_per_week', '2012-05-01', 1, 101); | ||
$this->assertTrue($result); | ||
//check that value was updated | ||
$result = $dao->getInsightBaseline('avg_replies_per_week', '2012-05-01', 1); | ||
$this->assertEqual($result->value, 101); | ||
|
||
//update nonexistent baseline | ||
$result = $dao->updateInsightBaseline('avg_replies_per_week', '2012-05-10', 1, 101); | ||
$this->assertFalse($result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
/** | ||
* | ||
* ThinkUp/webapp/_lib/model/class.Insight.php | ||
* | ||
* Copyright (c) 2012 Gina Trapani | ||
* | ||
* LICENSE: | ||
* | ||
* This file is part of ThinkUp (http://thinkupapp.com). | ||
* | ||
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public | ||
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any | ||
* later version. | ||
* | ||
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see | ||
* <http://www.gnu.org/licenses/>. | ||
* | ||
* Insight | ||
* | ||
* @license http://www.gnu.org/licenses/gpl.html | ||
* @copyright 2012 Gina Trapani | ||
* @author Gina Trapani <ginatrapani[at]gmail[dot]com> | ||
*/ | ||
class Insight { | ||
/** | ||
* @var int Internal unique ID. | ||
*/ | ||
var $id; | ||
/** | ||
* @var int Instance ID. | ||
*/ | ||
var $instance_id; | ||
/** | ||
* @var str Identifier for a type of statistic. | ||
*/ | ||
var $slug; | ||
/** | ||
* @var str Text content of the alert. | ||
*/ | ||
var $text; | ||
/** | ||
* @var int Internal post ID. | ||
*/ | ||
var $post_key; | ||
/** | ||
* @var date Date of insight. | ||
*/ | ||
var $date; | ||
public function __construct($row = false) { | ||
if ($row) { | ||
$this->id = $row['id']; | ||
$this->instance_id = $row['instance_id']; | ||
$this->slug = $row['slug']; | ||
$this->text = $row['text']; | ||
$this->post_key = $row['post_key']; | ||
$this->date = $row['date']; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* | ||
* ThinkUp/webapp/_lib/model/class.InsightBaseline.php | ||
* | ||
* Copyright (c) 2012 Gina Trapani | ||
* | ||
* LICENSE: | ||
* | ||
* This file is part of ThinkUp (http://thinkupapp.com). | ||
* | ||
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public | ||
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any | ||
* later version. | ||
* | ||
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see | ||
* <http://www.gnu.org/licenses/>. | ||
* | ||
* InsightBaseline | ||
* | ||
* @license http://www.gnu.org/licenses/gpl.html | ||
* @copyright 2012 Gina Trapani | ||
* @author Gina Trapani <ginatrapani[at]gmail[dot]com> | ||
*/ | ||
class InsightBaseline { | ||
/** | ||
* @var date Date of baseline statistic. | ||
*/ | ||
var $date; | ||
/** | ||
* @var int Instance ID. | ||
*/ | ||
var $instance_id; | ||
/** | ||
* @var str Unique identifier for a type of statistic. | ||
*/ | ||
var $slug; | ||
/** | ||
* @var int The numeric value of this stat/total/average. | ||
*/ | ||
var $value; | ||
public function __construct($row = false) { | ||
if ($row) { | ||
$this->date = $row['date']; | ||
$this->instance_id = $row['instance_id']; | ||
$this->slug = $row['slug']; | ||
$this->value = $row['value']; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
/** | ||
* | ||
* ThinkUp/webapp/_lib/model/class.InsightBaselineMySQLDAO.php | ||
* | ||
* Copyright (c) 2012 Gina Trapani | ||
* | ||
* LICENSE: | ||
* | ||
* This file is part of ThinkUp (http://thinkupapp.com). | ||
* | ||
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public | ||
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any | ||
* later version. | ||
* | ||
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see | ||
* <http://www.gnu.org/licenses/>. | ||
* | ||
* Insight Baseline Data Access Object MySQL Implementation | ||
* | ||
* @license http://www.gnu.org/licenses/gpl.html | ||
* @copyright 2012 Gina Trapani | ||
* @author Gina Trapani <ginatrapani[at]gmail[dot]com> | ||
*/ | ||
class InsightBaselineMySQLDAO extends PDODAO implements InsightBaselineDAO { | ||
|
||
public function insertInsightBaseline($slug, $date, $instance_id, $value) { | ||
$baseline = self::getInsightBaseline($slug, $date, $instance_id); | ||
if ($baseline == null) { | ||
$q = "INSERT INTO #prefix#insight_baselines SET slug=:slug, date=:date, instance_id=:instance_id, "; | ||
$q .= "value=:value"; | ||
$vars = array( | ||
':slug'=>$slug, | ||
':date'=>$date, | ||
':instance_id'=>$instance_id, | ||
':value'=>$value | ||
); | ||
$ps = $this->execute($q, $vars); | ||
$result = $this->getUpdateCount($ps); | ||
return ($result > 0); | ||
} else { | ||
return self::updateInsightBaseline($slug, $date, $instance_id, $value); | ||
} | ||
} | ||
|
||
public function getInsightBaseline($slug, $date, $instance_id) { | ||
$q = "SELECT date, instance_id, slug, value FROM #prefix#insight_baselines WHERE slug=:slug AND date=:date "; | ||
$q .= "AND instance_id=:instance_id"; | ||
$vars = array( | ||
':slug'=>$slug, | ||
':date'=>$date, | ||
':instance_id'=>$instance_id | ||
); | ||
$ps = $this->execute($q, $vars); | ||
$row = $this->getDataRowAsArray($ps); | ||
if ($row) { | ||
return new InsightBaseline($row); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
public function updateInsightBaseline($slug, $date, $instance_id, $value) { | ||
$q = "UPDATE #prefix#insight_baselines SET value=:value "; | ||
$q .= "WHERE slug=:slug AND date=:date AND instance_id=:instance_id"; | ||
$vars = array( | ||
':slug'=>$slug, | ||
':date'=>$date, | ||
':instance_id'=>$instance_id, | ||
':value'=>$value | ||
); | ||
$ps = $this->execute($q, $vars); | ||
$result = $this->getUpdateCount($ps); | ||
return ($result > 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* | ||
* ThinkUp/webapp/_lib/model/interface.InsightBaselineDAO.php | ||
* | ||
* Copyright (c) 2012 Gina Trapani | ||
* | ||
* LICENSE: | ||
* | ||
* This file is part of ThinkUp (http://thinkupapp.com). | ||
* | ||
* ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public | ||
* License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any | ||
* later version. | ||
* | ||
* ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with ThinkUp. If not, see | ||
* <http://www.gnu.org/licenses/>. | ||
* | ||
* Insight Baseline Data Access Object | ||
* | ||
* @license http://www.gnu.org/licenses/gpl.html | ||
* @copyright 2012 Gina Trapani | ||
* @author Gina Trapani <ginatrapani[at]gmail[dot]com> | ||
*/ | ||
interface InsightBaselineDAO { | ||
/** | ||
* Insert insight baseline into storage. | ||
* @param str $slug | ||
* @param str $date | ||
* @param int $instance_id | ||
* @param int $value | ||
* @return bool | ||
*/ | ||
public function insertInsightBaseline($slug, $date, $instance_id, $value); | ||
/** | ||
* Retrieve insight baseline from storage. | ||
* @param str $slug | ||
* @param str $date | ||
* @param int $instance_id | ||
* @return InsightBaseline | ||
*/ | ||
public function getInsightBaseline($slug, $date, $instance_id); | ||
/** | ||
* Update insight baseline in storage. | ||
* @param str $slug | ||
* @param str $date | ||
* @return bool | ||
*/ | ||
public function updateInsightBaseline($slug, $date, $instance_id, $value); | ||
} |
Oops, something went wrong.