public function testShouldGenerateInsight()
 {
     $instance = new Instance();
     $instance->id = 10;
     $instance->network_username = '******';
     $instance->network = 'twitter';
     $time_now = date("Y-m-d H:i:s");
     $today = date('Y-m-d', strtotime('today'));
     $yesterday = date('Y-m-d', strtotime('-1 day'));
     $builders = array();
     $builders[] = FixtureBuilder::build('insights', array('id' => 76, 'instance_id' => 10, 'slug' => 'some_slug', 'date' => $today, 'time_generated' => $time_now));
     $builders[] = FixtureBuilder::build('insights', array('id' => 77, 'instance_id' => 10, 'slug' => 'some_other_slug', 'date' => $yesterday, 'time_generated' => $time_now));
     $insight_plugin_parent = new InsightPluginParent();
     $insight_plugin_parent->insight_dao = DAOFactory::getDAO('InsightDAO');
     // Test default values
     $this->assertTrue($insight_plugin_parent->shouldGenerateInsight('a_slug', $instance));
     $this->assertFalse($insight_plugin_parent->shouldGenerateInsight('some_slug', $instance));
     // Test regeneration on a given date
     $this->assertTrue($insight_plugin_parent->shouldGenerateInsight('a_slug', $instance, $insight_date = $today));
     $this->assertFalse($insight_plugin_parent->shouldGenerateInsight('some_other_slug', $instance, $insight_date = $yesterday));
     $this->assertTrue($insight_plugin_parent->shouldGenerateInsight('some_other_slug', $instance, $insight_date = $yesterday, $regenerate_existing_insight = true));
     // Test with last week of posts
     $this->assertTrue($insight_plugin_parent->shouldGenerateInsight('a_slug', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $count_related_posts = 13));
     $this->assertFalse($insight_plugin_parent->shouldGenerateInsight('a_slug', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $count_related_posts = 0));
     // Test excluded networks
     $this->assertTrue($insight_plugin_parent->shouldGenerateInsight('a_slug', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $count_related_posts = null, $excluded_networks = array('facebook')));
     $this->assertFalse($insight_plugin_parent->shouldGenerateInsight('a_slug', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $count_related_posts = null, $excluded_networks = array('twitter', 'facebook')));
 }
Exemple #2
0
 /**
  * Determine whether an insight should be generated or not.
  * @param str $slug slug of the insight to be generated
  * @param Instance $instance user and network details for which the insight has to be generated
  * @param date $insight_date date for which the insight has to be generated
  * @param bool $regenerate_existing_insight whether the insight should be regenerated over a day
  * @param int $day_of_week the day of week (0 for Sunday through 6 for Saturday) on which the insight should run
  * @param int $count_last_week_of_posts if set, wouldn't run insight if there are no posts from last week
  * @param arr $excluded_networks array of networks for which the insight shouldn't be run
  * @param bool $alternate_day whether today is an alternate day or not
  * @return bool $run whether the insight should be generated or not
  */
 public function shouldGenerateInsight($slug, Instance $instance, $insight_date = null, $regenerate_existing_insight = false, $day_of_week = null, $count_last_week_of_posts = null, $excluded_networks = null, $alternate_day = true)
 {
     if (Utils::isTest()) {
         return true;
     } else {
         return $alternate_day && parent::shouldGenerateInsight($slug, $instance, $insight_date, $regenerate_existing_insight, $day_of_week, $count_last_week_of_posts, $excluded_networks);
     }
 }
Exemple #3
0
 /**
  * Determine whether an insight should be generated or not.
  * @param str $slug slug of the insight to be generated
  * @param Instance $instance user and network details for which the insight has to be generated
  * @param date $insight_date date for which the insight has to be generated
  * @param bool $regenerate_existing_insight whether the insight should be regenerated over a day
  * @param int $day_of_week the day of week (0 for Sunday through 6 for Saturday) on which the insight should run
  * @param int $count_last_week_of_posts if set, wouldn't run insight if there are no posts from last week
  * @param arr $excluded_networks array of networks for which the insight shouldn't be run
  * @param bool $alternate_day whether today is an alternate day or not
  * @return bool $run whether the insight should be generated or not
  */
 public function shouldGenerateInsight($slug, Instance $instance, $insight_date = null, $regenerate_existing_insight = false, $day_of_week = null, $count_last_week_of_posts = null, $excluded_networks = null, $alternate_day = true)
 {
     $in_test_mode = isset($_SESSION["MODE"]) && $_SESSION["MODE"] == "TESTS" || getenv("MODE") == "TESTS";
     if ($in_test_mode) {
         return true;
     } else {
         return $alternate_day && parent::shouldGenerateInsight($slug, $instance, $insight_date, $regenerate_existing_insight, $day_of_week, $count_last_week_of_posts, $excluded_networks);
     }
 }
Exemple #4
0
 public function generateInsight(Instance $instance, $last_week_of_posts, $number_days)
 {
     parent::generateInsight($instance, $last_week_of_posts, $number_days);
     $this->logger->logInfo("Begin generating insight", __METHOD__ . ',' . __LINE__);
     if (parent::shouldGenerateInsight('outreach_punchcard', $instance, $insight_date = 'today', $regenerate_existing_insight = false, $day_of_week = 6, count($last_week_of_posts))) {
         $owner_instance_dao = DAOFactory::getDAO('OwnerInstanceDAO');
         $owner_dao = DAOFactory::getDAO('OwnerDAO');
         $owner_instance = $owner_instance_dao->getByInstance($instance->id);
         $owner_id = $owner_instance[0]->owner_id;
         $owner = $owner_dao->getById($owner_instance[0]->owner_id);
         try {
             $owner_timezone = new DateTimeZone($owner->timezone);
         } catch (Exception $e) {
             // In the odd case the owner has no or a malformed timezone
             $cfg = Config::getInstance();
             $owner_timezone = new DateTimeZone($cfg->getValue('timezone'));
         }
         $now = new DateTime();
         $offset = timezone_offset_get($owner_timezone, $now);
         $post_dao = DAOFactory::getDAO('PostDAO');
         $punchcard = array();
         $responses_chron = array();
         $response_avg_timediffs = array();
         for ($hotd = 0; $hotd < 24; $hotd++) {
             for ($dotw = 1; $dotw <= 7; $dotw++) {
                 $punchcard['posts'][$dotw][$hotd] = 0;
                 $punchcard['responses'][$dotw][$hotd] = 0;
             }
             $responses_chron[$hotd] = 0;
         }
         foreach ($last_week_of_posts as $post) {
             $responses = array();
             $responses = array_merge((array) $post_dao->getRepliesToPost($post->post_id, $post->network), (array) $post_dao->getRetweetsOfPost($post->post_id, $post->network));
             foreach ($responses as $response) {
                 $response_pub_date = new DateTime($response->pub_date);
                 $response_dotw = date('N', date('U', strtotime($response->pub_date) + $offset));
                 // Day of week
                 $response_hotd = date('G', date('U', strtotime($response->pub_date) + $offset));
                 // Hour of day
                 $punchcard['responses'][$response_dotw][$response_hotd]++;
                 $responses_chron[$response_hotd]++;
             }
             $post_pub_date = new DateTime($post->pub_date);
             $post_dotw = date('N', date('U', strtotime($post->pub_date) + $offset));
             // Day of the week
             $post_hotd = date('G', date('U', strtotime($post->pub_date) + $offset));
             // Hour of the day
             $punchcard['posts'][$post_dotw][$post_hotd]++;
         }
         arsort($responses_chron);
         $most_responses = each($responses_chron);
         $insight_text = '';
         if ($most_responses['value'] > 0) {
             $time1_low_hotd = $most_responses['key'];
             $time1_high_hotd = $time1_low_hotd + 1;
             $time1_low = ($time1_low_hotd % 12 ? $time1_low_hotd % 12 : 12) . (floor($time1_low_hotd / 12) == 1 ? 'pm' : 'am');
             $time1_high = ($time1_high_hotd % 12 ? $time1_high_hotd % 12 : 12) . (floor($time1_high_hotd / 12) == 1 ? 'pm' : 'am');
             $insight_text = $this->username . "'s " . $this->terms->getNoun('post', InsightTerms::PLURAL) . " from last week got <strong>" . $most_responses['value'] . " " . ($most_responses['value'] > 1 ? 'responses' : 'response') . "</strong>" . " between <strong>" . $time1_low . " and " . $time1_high . "</strong>";
             $insight_comparison_text = '';
             foreach ($responses_chron as $key => $value) {
                 if ($value > 0 && $value < $most_responses['value']) {
                     $time2_low_hotd = $key;
                     $time2_high_hotd = $time2_low_hotd + 1;
                     $time2_low = ($time2_low_hotd % 12 ? $time2_low_hotd % 12 : 12) . (floor($time2_low_hotd / 12) == 1 ? 'pm' : 'am');
                     $time2_high = ($time2_high_hotd % 12 ? $time2_high_hotd % 12 : 12) . (floor($time2_high_hotd / 12) == 1 ? 'pm' : 'am');
                     $insight_comparison_text = ", as compared to <strong>" . $value . " " . ($value > 1 ? 'responses' : 'response') . "</strong>" . " between <strong>" . $time2_low . " and " . $time2_high . "</strong>";
                 }
             }
             $insight_text .= $insight_comparison_text . ".";
             $this->insight_dao->insertInsightDeprecated("outreach_punchcard", $instance->id, $this->insight_date, "Time of day:", $insight_text, basename(__FILE__, ".php"), Insight::EMPHASIS_LOW, serialize($punchcard));
         }
     }
     $this->logger->logInfo("Done generating insight", __METHOD__ . ',' . __LINE__);
 }