/**
  * Instantiate class
  *
  * @param Child $child
  */
 public function __construct(Child $child)
 {
     $this->child = $child;
     $this->startOfWeek = start_of_week();
     $this->endOfWeek = end_of_week();
     $this->generateWeekDays();
 }
Exemple #2
0
function grouped_stats($period_length, $period_count)
{
    $now = time();
    $week0 = start_of_week($now);
    $weeks = array();
    for ($i = $period_count; $i >= 0; $i--) {
        $week = new stdClass();
        $week->start = advance_period($period_length, $week0, -$i);
        $week->end = advance_period($period_length, $week->start, 1);
        // + $period_length * 24 * 60 * 60;
        $week->start_fmt = strftime('%b %d', $week->start);
        $week->period_fmt = strftime('%b %d', $week->start) . ' – ' . strftime('%b %d', $week->end);
        $week->stats = index_by_field('ip', stats_before(min($now, $week->end)));
        foreach ($week->stats as $ip => &$row) {
            if ($row->age < TRIAL_PERIOD) {
                $row->status = STATUS_TRIAL;
            } else {
                if ($row->inactive_period > INACTIVITY_CUTOFF || $row->inactive_period > $row->active_period) {
                    $row->status = STATUS_INACTIVE;
                } else {
                    $row->status = STATUS_ACTIVE;
                }
            }
        }
        $weeks[] = $week;
    }
    foreach ($weeks as &$week) {
        if (!empty($last_week)) {
            $week->active_user_count = 0;
            $week->new_users = array();
            $week->gone_users = array();
            $week->trial_users = array();
            $week->users_this_week = 0;
            foreach ($week->stats as $ip => $row) {
                if ($row->inactive_period < $period_length) {
                    $week->users_this_week++;
                }
            }
            foreach ($week->stats as $ip => $row) {
                $prev_row = $last_week->stats[$ip];
                if ($row->status == STATUS_ACTIVE) {
                    ++$week->active_user_count;
                    if (empty($prev_row) || $prev_row->status != STATUS_ACTIVE) {
                        $week->new_users[] = $ip;
                    }
                }
            }
            foreach ($last_week->stats as $ip => $prev_row) {
                $row = $week->stats[$ip];
                if ($prev_row->status == STATUS_ACTIVE) {
                    if (empty($row) || $row->status != STATUS_ACTIVE) {
                        $week->gone_users[] = $ip;
                    }
                }
            }
            $week->trial_fresh = 0;
            $week->trial_good = 0;
            $week->trial_bad = 0;
            foreach ($week->stats as $ip => $row) {
                if ($row->status == STATUS_TRIAL) {
                    $week->trial_users[] = $ip;
                    if ($row->age <= 3) {
                        $week->trial_fresh++;
                    } else {
                        if ($row->active_period > $row->inactive_period) {
                            $week->trial_good++;
                        } else {
                            $week->trial_bad++;
                        }
                    }
                }
            }
            $week->trial_count = count($week->trial_users);
            $week->new_user_count = count($week->new_users);
            $week->gone_user_count = count($week->gone_users);
            $week->growth = $last_week->active_user_count > 0 ? (double) $week->active_user_count / $last_week->active_user_count - 1 : 0;
            $week->growth_fmt = round($week->growth * 100) . '%';
            $week->delta = $week->active_user_count - $last_week->active_user_count;
            $week->new_user_growth = $last_week->new_user_count > 0 ? (double) $week->new_user_count / $last_week->new_user_count - 1 : 0;
            $week->new_user_growth_fmt = round($week->new_user_growth * 100) . '%';
            $week->churn = $last_week->active_user_count > 0 ? (double) $week->gone_user_count / $last_week->active_user_count : 0;
            $week->churn_fmt = round($week->churn * 100) . '%';
        }
        $last_week = $week;
    }
    array_shift($weeks);
    // remove the first week, it was only used to compute futher stats
    return $weeks;
}