Example #1
0
 public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days)
 {
     parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
     $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__);
     $year = date('Y');
     $regenerate = false;
     //testing
     //$regenerate = true;
     $should_generate_insight = self::shouldGenerateEndOfYearAnnualInsight($this->slug, $instance, $insight_date = "{$year}-{$this->run_date}", $regenerate, $day_of_year = $this->run_date);
     if ($should_generate_insight) {
         $this->logger->logInfo("Should generate", __METHOD__ . ',' . __LINE__);
         $insight = new Insight();
         $insight->instance_id = $instance->id;
         $insight->slug = $this->slug;
         $insight->date = "{$year}-{$this->run_date}";
         $network = $instance->network;
         $count = 0;
         $post_dao = DAOFactory::getDAO('PostDAO');
         $last_year_of_posts = $post_dao->getThisYearOfPostsIterator($author_id = $instance->network_user_id, $network = $network);
         $total_posts = 0;
         foreach ($last_year_of_posts as $post) {
             $count += AllAboutYouInsight::hasFirstPersonReferences($post->post_text) ? 1 : 0;
             $total_posts++;
         }
         $earliest_pub_date = $post_dao->getEarliestCapturedPostPubDate($instance);
         $qualified_year = "";
         if (date('Y', strtotime($earliest_pub_date)) == date('Y')) {
             if (date('n', strtotime($earliest_pub_date)) > 1) {
                 //not January
                 //Earliest post was this year; figure out what month we have data since this year
                 $since = date('F', strtotime($earliest_pub_date));
                 $qualified_year = " (at least since " . $since . ")";
             }
         }
         if ($instance->network == 'twitter') {
             $headline = $this->getVariableCopy(array("A year's worth of %username"));
         } elseif ($instance->network == 'instagram') {
             $headline = $this->getVariableCopy(array("A year of %username photos"));
         } else {
             $headline = $this->getVariableCopy(array("A year about %username"));
         }
         $percent = round($count / $total_posts * 100);
         if ($count > 0) {
             if ($instance->network == 'twitter' || $instance->network == 'facebook') {
                 $insight_text = $this->getVariableCopy(array("In %year, <strong>{$percent}%</strong> of %username's %posts " . "&mdash; a grand total of %total &mdash; contained " . "the words &ldquo;I&rdquo;, &ldquo;me&rdquo;, &ldquo;my&rdquo;, " . "&ldquo;mine&rdquo;, or &ldquo;myself&rdquo;%qualified_year. " . ($instance->network == 'facebook' ? "Go ahead: tell your story" : "Sometimes, you've just got to get personal") . "."), array('total' => number_format($count), 'year' => $year, 'qualified_year' => $qualified_year));
             } else {
                 $insight_text = $this->getVariableCopy(array("In %year, <strong>{$percent}%</strong> of %username's Instagram captions " . "&mdash; a grand total of %total &mdash; contained " . "the words &ldquo;I&rdquo;, &ldquo;me&rdquo;, &ldquo;my&rdquo;, " . "&ldquo;mine&rdquo;, or &ldquo;myself&rdquo;%qualified_year. " . "What a life in pictures!"), array('total' => number_format($count), 'year' => $year, 'qualified_year' => $qualified_year));
             }
         } else {
             $insight_text = $this->getVariableCopy(array("In %year, none of %username's %posts contained " . "the words &ldquo;I&rdquo;, &ldquo;me&rdquo;, &ldquo;my&rdquo;, " . "&ldquo;mine&rdquo;, or &ldquo;myself&rdquo;%qualified_year. Sometimes, " . "you've just got to get personal &mdash; unless you're " . "%username, apparently!"), array('total' => number_format($count), 'year' => $year, 'qualified_year' => $qualified_year));
         }
         $insight->headline = $headline;
         $insight->text = $insight_text;
         $insight->filename = basename(__FILE__, ".php");
         $insight->emphasis = Insight::EMPHASIS_HIGH;
         $insight->header_image = $user->avatar;
         $this->insight_dao->insertInsight($insight);
     }
     $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__);
 }
 public function testAllAboutYouInsightPriorEqualBaseline()
 {
     // Get data ready that insight requires
     $posts = self::getTestPostObjects();
     $instance = new Instance();
     $instance->id = 10;
     $instance->network_username = '******';
     $instance->network = 'twitter';
     $insight_plugin = new AllAboutYouInsight();
     // Add a baseline from prior week
     $last_week = date('Y-m-d', strtotime('-7 day'));
     $builder = FixtureBuilder::build('insight_baselines', array('date' => $last_week, 'slug' => 'all_about_you', 'instance_id' => 10, 'value' => 9));
     $insight_plugin->generateInsight($instance, $posts, 3);
     // Assert that week-over-week comparison is correct
     $insight_dao = new InsightMySQLDAO();
     $today = date('Y-m-d');
     $result = $insight_dao->getInsight('all_about_you', 10, $today);
     $this->debug(Utils::varDumpToString($result));
     $this->assertNotNull($result);
     $this->assertIsA($result, "Insight");
     $this->assertPattern('/\\@testeriffic\'s posts contained the words/', $result->text);
     $this->assertPattern('/9 times/', $result->text);
     //assert no comparison to prior week
     $this->assertNoPattern('/prior week/', $result->text);
     $this->assertNoPattern('/prior week/', $result->text);
 }
 public function testHeadlines()
 {
     $posts = self::getTestPostObjects();
     $insight_plugin = new AllAboutYouInsight();
     $today = date('Y-m-d');
     $insight_plugin = new AllAboutYouInsight();
     $insight_dao = DAOFactory::getDAO('InsightDAO');
     $good_headlines = array(null, 'Sometimes it\'s all about @testeriffic', 'It\'s getting personal', 'A moment of self-reflection', 'Sometimes Twitter is a first-person story', 'A story about @testeriffic');
     for ($i = 1; $i <= 5; $i++) {
         TimeHelper::setTime($i);
         $insight_plugin->generateInsight($this->instance, null, $posts, 3);
         $result = $insight_dao->getInsight('all_about_you', 10, $today);
         $this->assertEqual($result->headline, $good_headlines[$i]);
     }
 }