PageRenderTime 64ms CodeModel.GetById 2ms RepoModel.GetById 2ms app.codeStats 0ms

/webapp/plugins/insightsgenerator/insights/likespike.php

https://github.com/SimonCoopey/ThinkUp
PHP | 264 lines | 190 code | 26 blank | 48 comment | 30 complexity | 5bc45da8721d542f6afe6f67504b6c64 MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: Video Like Spike
  4. Description: Like spikes and high insights for videos posted the past 7, 30, and 365 days.
  5. */
  6. /**
  7. *
  8. * ThinkUp/webapp/plugins/insightsgenerator/insights/likespike.php
  9. *
  10. * LICENSE:
  11. *
  12. * This file is part of ThinkUp (http://thinkup.com).
  13. *
  14. * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  15. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  16. * later version.
  17. *
  18. * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  19. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
  23. * <http://www.gnu.org/licenses/>.
  24. *
  25. *
  26. * LikeSpike Insight
  27. *
  28. * Highlights videos with spikes in likes or all time like highs
  29. *
  30. * Copyright (c) 2013 Aaron Kalair
  31. *
  32. * @author Aaron Kalair aaronkalair@gmail.com
  33. * @license http://www.gnu.org/licenses/gpl.html
  34. * @copyright 2013 Aaron Kalair
  35. */
  36. class LikeSpikeInsight extends InsightPluginParent implements InsightPlugin {
  37. public function generateInsight(Instance $instance, User $user, $last_week_of_posts, $number_days) {
  38. parent::generateInsight($instance, $user, $last_week_of_posts, $number_days);
  39. self::generateInsightBaselines($instance, $number_days);
  40. $this->logger->logInfo("Begin generating insight", __METHOD__.','.__LINE__);
  41. $insight_baseline_dao = DAOFactory::getDAO('InsightBaselineDAO');
  42. $filename = basename(__FILE__, ".php");
  43. $video_dao = DAOFactory::getDAO('VideoDAO');
  44. $insight_text = '';
  45. $simplified_post_date = "";
  46. foreach ($last_week_of_posts as $post) {
  47. if($post->network == 'youtube') {
  48. $video = $video_dao->getVideoByID($post->post_id, 'youtube');
  49. }
  50. else {
  51. break;
  52. }
  53. if ($video->likes > 2) { //Only show insight for more than 2 likes
  54. // First get spike/high 30/90/365 day baselines
  55. if ($simplified_post_date != date('Y-m-d', strtotime($post->pub_date))) {
  56. $simplified_post_date = date('Y-m-d', strtotime($post->pub_date));
  57. $average_like_count_30_days =
  58. $insight_baseline_dao->getInsightBaseline('avg_like_count_last_30_days', $instance->id,
  59. $simplified_post_date);
  60. $average_like_count_90_days =
  61. $insight_baseline_dao->getInsightBaseline('avg_like_count_last_90_days', $instance->id,
  62. $simplified_post_date);
  63. $high_like_count_30_days =
  64. $insight_baseline_dao->getInsightBaseline('high_like_count_last_30_days', $instance->id,
  65. $simplified_post_date);
  66. $high_like_count_90_days =
  67. $insight_baseline_dao->getInsightBaseline('high_like_count_last_90_days', $instance->id,
  68. $simplified_post_date);
  69. $high_like_count_365_days =
  70. $insight_baseline_dao->getInsightBaseline('high_like_count_last_365_days', $instance->id,
  71. $simplified_post_date);
  72. $hot_videos_data = $video_dao->getHotVideos($instance->network_username, 'youtube', 10, 'likes',
  73. 'Likes');
  74. $chart_data = VideoMySQLDAO::getHotVideosVisualizationData($hot_videos_data, 'Likes');
  75. }
  76. // Next compare post like counts to baselines and store insights where there's a spike or high
  77. if (isset($high_like_count_365_days->value)
  78. && $video->likes >= $high_like_count_365_days->value) {
  79. if (isset($chart_data)) {
  80. $headline = "<strong>".number_format($video->likes)." people</strong> liked ";
  81. $headline .= "<a href=http://plus.google.com/$instance->network_user_id>";
  82. $headline .= "$instance->network_username</a>'s video <a href=http://www.youtube.com/watch?v=";
  83. $headline .= "$video->post_id>$video->post_text</a>.";
  84. $insight_text = "That's a new 365-day record!";
  85. $this->insight_dao->insertInsightDeprecated('like_high_365_day_'.$video->id, $instance->id,
  86. $simplified_post_date, $headline, $insight_text, $filename, Insight::EMPHASIS_HIGH,
  87. serialize(array($video, $chart_data)));
  88. $this->insight_dao->deleteInsight('like_high_90_day_'.$post->id, $instance->id,
  89. $simplified_post_date);
  90. $this->insight_dao->deleteInsight('like_high_90_day_'.$post->id, $instance->id,
  91. $simplified_post_date);
  92. $this->insight_dao->deleteInsight('like_spike_30_day_'.$post->id, $instance->id,
  93. $simplified_post_date);
  94. $this->insight_dao->deleteInsight('like_spike_30_day_'.$post->id, $instance->id,
  95. $simplified_post_date);
  96. }
  97. } elseif (isset($high_like_count_90_days->value)
  98. && $video->likes >= $high_like_count_90_days->value) {
  99. if (isset($chart_data)) {
  100. $insight_text = "That's a new 90-day record!";
  101. $headline = "<strong>".number_format($video->likes)." people</strong> liked ";
  102. $headline .= "<a href=http://plus.google.com/$instance->network_user_id>";
  103. $headline .= "$instance->network_username</a>'s video <a href=http://www.youtube.com/watch?v=";
  104. $headline .= "$video->post_id>$video->post_text</a>.";
  105. $this->insight_dao->insertInsightDeprecated('like_high_90_day_'.$video->id, $instance->id,
  106. $simplified_post_date, $headline, $insight_text, $filename, Insight::EMPHASIS_HIGH,
  107. serialize(array($video, $chart_data)));
  108. $this->insight_dao->deleteInsight('like_high_30_day_'.$post->id, $instance->id,
  109. $simplified_post_date);
  110. $this->insight_dao->deleteInsight('like_spike_30_day_'.$post->id, $instance->id,
  111. $simplified_post_date);
  112. }
  113. } elseif (isset($high_like_count_30_days->value)
  114. && $video->likes >= $high_like_count_30_days->value) {
  115. if (isset($chart_data)) {
  116. $insight_text = "That's a new 30-day record.";
  117. $headline = "<strong>".number_format($video->likes)." people</strong> liked ";
  118. $headline .= "<a href=http://plus.google.com/$instance->network_user_id>";
  119. $headline .= "$instance->network_username</a>'s video <a href=http://www.youtube.com/watch?v=";
  120. $headline .= "$video->post_id>$video->post_text</a>.";
  121. $this->insight_dao->insertInsightDeprecated('like_high_30_day_'.$video->id, $instance->id,
  122. $simplified_post_date, $headline, $insight_text, $filename, Insight::EMPHASIS_HIGH,
  123. serialize(array($video, $chart_data)));
  124. }
  125. }
  126. if (isset($average_like_count_90_days->value)
  127. && $video->likes > ($average_like_count_90_days->value*2)) {
  128. if (isset($chart_data)) {
  129. $multiplier = floor($video->likes/$average_like_count_30_days->value);
  130. $multiplier = $this->terms->getMultiplierAdverb($multiplier);
  131. $headline = "<strong>".number_format($video->likes)." people</strong> liked ";
  132. $headline .= "<a href=http://plus.google.com/$instance->network_user_id>";
  133. $headline .= "$instance->network_username</a>'s video <a href=http://www.youtube.com/watch?v=";
  134. $headline .= "$video->post_id>$video->post_text</a>, more than <strong>".$multiplier."</strong> ";
  135. $headline .= "the 90-day average.";
  136. $this->insight_dao->insertInsightDeprecated('like_spike_90_day_'.$post->id, $instance->id,
  137. $simplified_post_date, $headline, $insight_text, $filename, Insight::EMPHASIS_LOW,
  138. serialize(array($video, $chart_data)));
  139. $this->insight_dao->deleteInsight('like_spike_30_day_'.$post->id, $instance->id,
  140. $simplified_post_date);
  141. }
  142. } elseif (isset($average_like_count_30_days->value)
  143. && $video->likes > ($average_like_count_30_days->value*2)) {
  144. if (isset($chart_data)) {
  145. $multiplier = floor($video->likes/$average_like_count_30_days->value);
  146. $multiplier = $this->terms->getMultiplierAdverb($multiplier);
  147. $headline = "<strong>".number_format($video->likes)." people</strong> liked ";
  148. $headline .= "<a href=http://plus.google.com/$instance->network_user_id>";
  149. $headline .= "$instance->network_username</a>'s video <a href=http://www.youtube.com/watch?v=";
  150. $headline .= "$video->post_id>$video->post_text</a>, more than <strong>".$multiplier."</strong> ";
  151. $headline .= "the 30-day average.";
  152. $this->insight_dao->insertInsightDeprecated('like_spike_30_day_'.$post->id, $instance->id,
  153. $simplified_post_date, $headline, $insight_text, $filename, Insight::EMPHASIS_LOW,
  154. serialize(array($video, $chart_data)));
  155. }
  156. }
  157. }
  158. }
  159. $this->logger->logInfo("Done generating insight", __METHOD__.','.__LINE__);
  160. }
  161. /**
  162. * Calculate and store insight baselines for a specified number of days.
  163. * @param Instance $instance
  164. * @param int $number_days Number of days to backfill
  165. * @return void
  166. */
  167. private function generateInsightBaselines($instance, $number_days=3) {
  168. $video_dao = DAOFactory::getDAO('VideoDAO');
  169. $insight_baseline_dao = DAOFactory::getDAO('InsightBaselineDAO');
  170. $days_ago = 0;
  171. // Generate baseline post insights for the last 7 days
  172. while ($days_ago < $number_days) {
  173. $since_date = date("Y-m-d", strtotime("-".$days_ago." day"));
  174. if ($video_dao->doesUserHaveVideosWithLikesSinceDate($instance->network_username, $instance->network, 30,
  175. $since_date)) {
  176. //Save average likes over past 30 days
  177. $average_like_count_30_days = null;
  178. $average_like_count_30_days = $video_dao->getAverageLikeCount($instance->network_username,
  179. $instance->network, 30, $since_date);
  180. if ($average_like_count_30_days != null ) {
  181. $insight_baseline_dao->insertInsightBaseline('avg_like_count_last_30_days', $instance->id,
  182. $average_like_count_30_days, $since_date);
  183. $this->logger->logSuccess("Averaged $average_like_count_30_days likes in the 30 days before ".
  184. $since_date, __METHOD__.','.__LINE__);
  185. }
  186. //Save like high for last 30 days
  187. $high_like_count_30_days = $video_dao->getHighestLikes($instance->network_username, $instance->network,
  188. 30, $since_date);
  189. if ($high_like_count_30_days != null ) {
  190. $insight_baseline_dao->insertInsightBaseline('high_like_count_last_30_days', $instance->id,
  191. $high_like_count_30_days, $since_date);
  192. $this->logger->logSuccess("High of $high_like_count_30_days likes in the 30 days before ".
  193. $since_date, __METHOD__.','.__LINE__);
  194. }
  195. }
  196. if ($video_dao->doesUserHaveVideosWithLikesSinceDate($instance->network_username, $instance->network, 90,
  197. $since_date)) {
  198. //Save average likes over past 90 days
  199. $average_like_count_90_days = null;
  200. $average_like_count_90_days = $video_dao->getAverageLikeCount($instance->network_username,
  201. $instance->network, 90, $since_date);
  202. if ($average_like_count_90_days != null ) {
  203. $insight_baseline_dao->insertInsightBaseline('avg_like_count_last_90_days', $instance->id,
  204. $average_like_count_90_days, $since_date);
  205. $this->logger->logSuccess("Averaged $average_like_count_90_days likes in the 90 days before ".
  206. $since_date, __METHOD__.','.__LINE__);
  207. }
  208. //Save like high for last 90 days
  209. $high_like_count_90_days = $video_dao->getHighestLikes($instance->network_username, $instance->network,
  210. 90, $since_date);
  211. if ($high_like_count_90_days != null ) {
  212. $insight_baseline_dao->insertInsightBaseline('high_like_count_last_90_days', $instance->id,
  213. $high_like_count_90_days, $since_date);
  214. $this->logger->logSuccess("High of $high_like_count_90_days likes in the 90 days before ".
  215. $since_date, __METHOD__.','.__LINE__);
  216. }
  217. }
  218. if ($video_dao->doesUserHaveVideosWithLikesSinceDate($instance->network_username, $instance->network, 365,
  219. $since_date)) {
  220. //Save like high for last 365 days
  221. $high_like_count_365_days = $video_dao->getHighestLikes($instance->network_username, $instance->network,
  222. 365, $since_date);
  223. if ($high_like_count_365_days != null ) {
  224. $insight_baseline_dao->insertInsightBaseline('high_like_count_last_365_days', $instance->id,
  225. $high_like_count_365_days, $since_date);
  226. $this->logger->logSuccess("High of $high_like_count_365_days likes in the 365 days before ".
  227. $since_date, __METHOD__.','.__LINE__);
  228. }
  229. }
  230. $days_ago++;
  231. }
  232. }
  233. }
  234. $insights_plugin_registrar = PluginRegistrarInsights::getInstance();
  235. $insights_plugin_registrar->registerInsightPlugin('LikeSpikeInsight');