diff --git a/include/library/Crunchbutton/Chart.php b/include/library/Crunchbutton/Chart.php index 11ad5c1db..c7c2d1cb9 100644 --- a/include/library/Crunchbutton/Chart.php +++ b/include/library/Crunchbutton/Chart.php @@ -3,9 +3,9 @@ class Crunchbutton_Chart extends Cana_Model { public $activeUsersInterval = 45; // Days - public $queryOnlyCommunties = 'AND c.id_community IN (1, 4)'; - public $queryExcludeCommunties = "AND c.name != 'Testing' AND c.name IS NOT NULL"; - public $queryExcludeUsers = "AND o.name NOT LIKE '%test%' and o.name != 'Judd' and o.name != 'dave' and o.name != 'Nick' and o.name != 'Devin' AND ( o.id_community != 6 OR o.id_community IS NULL ) "; + public $queryOnlyCommunties = ''; // 'AND c.id_community IN (1, 4)'; + public $queryExcludeCommunties = ''; //"AND c.name != 'Testing' AND c.name IS NOT NULL"; + public $queryExcludeUsers = "AND o.name NOT LIKE '%test%' and o.name != 'Judd' and o.name != 'dave' and o.name != 'Nick' and o.name != 'Devin'"; public $minDate = '2012-09-02'; #Issue 1523 - Fist week of September/12 public $from_month; @@ -238,6 +238,22 @@ class Crunchbutton_Chart extends Cana_Model { return $communities; } + public function activeFromLastDays( $lastDays = 60 ){ + $query = "SELECT COUNT(*) as Total + FROM + (SELECT u.phone, + o.date, + u.id_user + FROM `order` o + INNER JOIN user u ON u.id_user = o.id_user + WHERE o.date >= NOW() - INTERVAL {$lastDays} DAY + {$this->queryExcludeUsers} + GROUP BY u.phone) active"; + + $results = c::db()->get( $query ); + return $results->get(0)->Total; + } + public function allDays(){ $query = "SELECT DISTINCT( DATE_FORMAT( o.date ,'%Y-%m-%d') ) day FROM `order` o WHERE o.date IS NOT NULL AND o.date > '$this->minDate' ORDER BY day ASC"; $results = c::db()->get( $query ); diff --git a/include/library/Crunchbutton/Chart/Churn.php b/include/library/Crunchbutton/Chart/Churn.php index 605f875a7..c4a6e728d 100644 --- a/include/library/Crunchbutton/Chart/Churn.php +++ b/include/library/Crunchbutton/Chart/Churn.php @@ -25,12 +25,45 @@ class Crunchbutton_Chart_Churn extends Crunchbutton_Chart { // 'churn-rate-per-active-user-per-month' => array( 'title' => 'Month', 'interval' => 'month', 'type' => 'column', 'method' => 'activeByMonth', 'filters' => array( array( 'title' => 'Community', 'type' => 'community', 'method' => 'activeByMonthByCommunity' ) ) ), ) ), + 'group-historical-churn-rate-per-active-user' => array( + 'title' => 'Historical Churn Rate per Active User', + 'activeDays' => 60, + 'tags' => array( 'detailed-analytics' ), + 'charts' => array( + 'historial-churn-rate-per-active-user-per-day' => array( 'title' => 'Day', 'interval' => 'day', 'type' => 'column', 'method' => 'historicalActiveByDay'), + ) + ), ); public function __construct() { parent::__construct(); } + + public function historicalActiveByDay( $render = false ){ + $user = new Crunchbutton_Chart_User(); + $daysForward = $this->activeUsersInterval; + $activeUsers = $user->activeByDay(); + $newUsers = $user->newByDay(); + $activeToday = $this->activeFromLastDays(); + + $data = []; + for( $i = 0; $i < sizeof( $activeUsers ); $i++ ){ + $activeForwardDays = $activeUsers[ ( $i + $daysForward ) ]->Total; + $activeForwardDaysPlusOne = $activeUsers[ ( $i + $daysForward + 1 ) ]->Total; + $newForwardDays = $newUsers[ ( $i + $daysForward ) ]->Total; + $newForwardDaysPlusOne = $newUsers[ ( $i + $daysForward + 1 ) ]->Total; + $churn = ( ( $activeForwardDaysPlusOne + $newForwardDaysPlusOne ) - $activeForwardDaysPlusOne ) / $activeToday; + // Do not show the negatives + // $churn = ( $churn < 0 ) ? 0 : $churn; + $data[] = ( object ) array( 'Label' => $activeUsers[ $i ]->Label, 'Total' => $churn, 'Type' => 'Users' ); + } + if( $render ){ + return array( 'data' => $data, 'unit' => '%', 'interval' => 'day' ); + } + return $data; + } + public function activeByDay( $render = false ){ /* OLD FORMULA $user = new Crunchbutton_Chart_User(); diff --git a/include/library/Crunchbutton/Chart/User.php b/include/library/Crunchbutton/Chart/User.php index bd1fea10d..195095986 100644 --- a/include/library/Crunchbutton/Chart/User.php +++ b/include/library/Crunchbutton/Chart/User.php @@ -15,6 +15,15 @@ class Crunchbutton_Chart_User extends Crunchbutton_Chart { 'users-new-per-month' => array( 'title' => 'Month', 'interval' => 'month', 'type' => 'column', 'method' => 'newByMonth', 'filters' => array( array( 'title' => 'Community', 'type' => 'community', 'method' => 'newByMonthByCommunityGrouped' ), array( 'title' => 'Cohort', 'type' => 'cohort', 'method' => 'newByMonthCohort' ) ) ), ) ), + 'group-historical-new-users' => array( + 'title' => 'Historical New Users', + 'tags' => array( 'detailed-analytics' ), + 'charts' => array( + 'users-new-per-day-historical' => array( 'title' => 'Day', 'interval' => 'day', 'type' => 'column', 'method' => 'newByDayHistorical' ), + 'users-new-per-week-historical' => array( 'title' => 'Week', 'interval' => 'week', 'type' => 'column', 'method' => 'newByWeekHistorical' ), + 'users-new-per-month-historical' => array( 'title' => 'Month', 'interval' => 'month', 'type' => 'column', 'method' => 'newByMonthHistorical' ), + ) + ), 'group-new-users-community' => array( 'title' => 'New Users', 'tags' => array( 'reps' ), @@ -136,6 +145,7 @@ class Crunchbutton_Chart_User extends Crunchbutton_Chart { {$this->queryExcludeUsers} {$this->queryOnlyCommunties} GROUP BY u.phone ) ActiveUsers"; + $union = ' UNION '; } @@ -1359,6 +1369,58 @@ class Crunchbutton_Chart_User extends Crunchbutton_Chart { return $parsedData; } + public function newByDayHistorical( $render = false ){ + $newUsers = $this->newByDay(); + $usersReclaimed = $this->reclaimedByDay(); + $data = []; + for( $i = 0; $i < count( $newUsers ); $i++ ){ + $data[] = ( object ) array( + 'Label' => $newUsers[ $i ]->Label, + 'Total' => ( $newUsers[ $i ]->Total + $usersReclaimed[ $i ]->Total ), + 'Type' => $newUsers[ $i ]->Type, + ); + } + // echo '
'; var_dump( $data ); exit;
+ if( $render ){
+ return array( 'data' => $data, 'unit' => 'Users', 'interval' => 'day' );
+ }
+ return $data;
+ }
+
+ public function newByWeekHistorical( $render = false ){
+ $newUsers = $this->newByWeek();
+ $usersReclaimed = $this->reclaimedByWeek();
+ $data = [];
+ for( $i = 0; $i < count( $newUsers ); $i++ ){
+ $data[] = ( object ) array(
+ 'Label' => $newUsers[ $i ]->Label,
+ 'Total' => ( $newUsers[ $i ]->Total + $usersReclaimed[ $i ]->Total ),
+ 'Type' => $newUsers[ $i ]->Type,
+ );
+ }
+ // echo ''; var_dump( $data ); exit;
+ if( $render ){
+ return array( 'data' => $data, 'unit' => 'Users', 'interval' => 'week' );
+ }
+ return $data;
+ }
+
+ public function newByMonthHistorical( $render = false ){
+ $newUsers = $this->newByMonth();
+ $usersReclaimed = $this->reclaimedByMonth();
+ $data = [];
+ for( $i = 0; $i < count( $newUsers ); $i++ ){
+ $data[] = ( object ) array(
+ 'Label' => $newUsers[ $i ]->Label,
+ 'Total' => ( $newUsers[ $i ]->Total + $usersReclaimed[ $i ]->Total ),
+ 'Type' => $newUsers[ $i ]->Type,
+ );
+ }
+ if( $render ){
+ return array( 'data' => $data, 'unit' => 'Users', 'interval' => 'month' );
+ }
+ return $data;
+ }
public function newByDay( $render = false ){
$query = "SELECT SUM(1) AS Total,