Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Create insight baselines and insight database tables, model objects. …

…Create InsightBaseline data access object interface and MySQL implementation and tests
  • Loading branch information...
commit 44e4a4a919b27e96aa50953e5dfe67eded0dd29f 1 parent 24440cd
@ginatrapani ginatrapani authored
View
87 tests/TestOfInsightBaselineMySQLDAO.php
@@ -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);
+ }
+}
View
1  tests/all_model_tests.php
@@ -43,6 +43,7 @@
$model_tests->add(new TestOfGroupMySQLDAO());
$model_tests->add(new TestOfGroupMemberMySQLDAO());
$model_tests->add(new TestOfGroupMembershipCountMySQLDAO());
+$model_tests->add(new TestOfInsightBaselineMySQLDAO());
$model_tests->add(new TestOfInstanceMySQLDAO());
$model_tests->add(new TestOfInstaller());
$model_tests->add(new TestOfInstallerMySQLDAO());
View
64 webapp/_lib/model/class.Insight.php
@@ -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'];
+ }
+ }
+}
View
54 webapp/_lib/model/class.InsightBaseline.php
@@ -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'];
+ }
+ }
+}
View
80 webapp/_lib/model/class.InsightBaselineMySQLDAO.php
@@ -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);
+ }
+}
View
54 webapp/_lib/model/interface.InsightBaselineDAO.php
@@ -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);
+}
View
32 webapp/install/sql/build-db_mysql.sql
@@ -1,6 +1,6 @@
--
-- ThinkUp Database Creation Script
--- Auto-generated by thinkup/extras/scripts/migratedb script on 2012-05-07
+-- Auto-generated by thinkup/extras/scripts/migratedb script on 2012-05-08
--
ALTER DATABASE DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
@@ -132,6 +132,34 @@ CREATE TABLE tu_hashtags_posts (
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Hashtags captured per post.';
--
+-- Table structure for table tu_insight_baselines
+--
+
+CREATE TABLE tu_insight_baselines (
+ date date NOT NULL COMMENT 'Date of baseline statistic.',
+ instance_id int(11) NOT NULL COMMENT 'Instance ID.',
+ slug varchar(100) NOT NULL COMMENT 'Unique identifier for a type of statistic.',
+ value int(11) NOT NULL COMMENT 'The numeric value of this stat/total/average.',
+ UNIQUE KEY unique_base (date,instance_id,slug),
+ KEY date (date,instance_id)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Insight baseline statistics.';
+
+--
+-- Table structure for table tu_insights
+--
+
+CREATE TABLE tu_insights (
+ id int(11) NOT NULL AUTO_INCREMENT COMMENT 'Internal unique ID.',
+ instance_id int(11) NOT NULL COMMENT 'Instance ID.',
+ slug varchar(100) NOT NULL COMMENT 'Identifier for a type of statistic.',
+ text varchar(255) NOT NULL COMMENT 'Text content of the alert.',
+ post_key int(11) DEFAULT NULL COMMENT 'Internal post ID.',
+ date date NOT NULL COMMENT 'Date of insight.',
+ PRIMARY KEY (id),
+ KEY instance_id (instance_id,slug,post_key,date)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Insights for a given service user.';
+
+--
-- Table structure for table tu_instances
--
@@ -481,7 +509,7 @@ CREATE TABLE tu_users (
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Service user details.';
--- Dump completed on 2012-05-07 13:34:53
+-- Dump completed on 2012-05-08 10:49:52
--
-- Insert DB Version
View
19 webapp/install/sql/mysql_migrations/2012-05-08_insights_feed.sql
@@ -0,0 +1,19 @@
+CREATE TABLE tu_insight_baselines (
+ date date NOT NULL COMMENT 'Date of baseline statistic.',
+ instance_id int(11) NOT NULL COMMENT 'Instance ID.',
+ slug varchar(100) NOT NULL COMMENT 'Unique identifier for a type of statistic.',
+ value int(11) NOT NULL COMMENT 'The numeric value of this stat/total/average.',
+ UNIQUE KEY unique_base (date,instance_id,slug),
+ KEY date (date,instance_id)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE utf8_general_ci COMMENT='Insight baseline statistics.';
+
+CREATE TABLE tu_insights (
+id INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Internal unique ID.',
+instance_id INT( 11 ) NOT NULL COMMENT 'Instance ID.',
+slug VARCHAR( 100 ) NOT NULL COMMENT 'Identifier for a type of statistic.',
+text VARCHAR( 255 ) NOT NULL COMMENT 'Text content of the alert.',
+post_key INT( 11 ) NULL COMMENT 'Internal post ID.',
+date DATE NOT NULL COMMENT 'Date of insight.',
+INDEX ( instance_id , slug , post_key , date )
+) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci COMMENT = 'Insights for a given service user.';
+
Please sign in to comment.
Something went wrong with that request. Please try again.