Permalink
Browse files

add more stats on the user pages under rankings

  • Loading branch information...
1 parent dc0f207 commit 3217e14806c63f8cffb9c44877fa47e7b3171d7b @tommyrot committed Oct 19, 2013
Showing with 81 additions and 37 deletions.
  1. +3 −0 empty_database_v7.sqlite
  2. +33 −9 maintenance.php
  3. +25 −14 www/sss.css
  4. +20 −14 www/user.php
View
3 empty_database_v7.sqlite
@@ -196,6 +196,9 @@ date TEXT NOT NULL,
rank INT NOT NULL,
l_total INT NOT NULL,
percentage REAL NOT NULL,
+l_avg REAL NOT NULL,
+activity REAL NOT NULL,
+l_max INT NOT NULL,
PRIMARY KEY (ruid, date)
);
View
42 maintenance.php
@@ -113,26 +113,46 @@ public function calculate_rankings($sqlite3)
}
/**
- * Retrieve all user activity.
+ * Retrieve and calculate the cumulative amount of days logged, by month.
*/
- $query = $sqlite3->query('SELECT ruid_activity_by_month.ruid AS ruid, date, l_total FROM ruid_activity_by_month JOIN uid_details ON ruid_activity_by_month.ruid = uid_details.uid WHERE status NOT IN (3,4)') or $this->output('critical', basename(__FILE__).':'.__LINE__.', sqlite3 says: '.$sqlite3->lastErrorMsg());
- $result = $query->fetchArray(SQLITE3_ASSOC);
+ $dayslogged_by_month = array_fill_keys($scope, 0);
+ $query = $sqlite3->query('SELECT SUBSTR(date, 1, 7) AS date, COUNT(*) AS dayslogged FROM parse_history GROUP BY SUBSTR(date, 1, 7)') or $this->output('critical', basename(__FILE__).':'.__LINE__.', sqlite3 says: '.$sqlite3->lastErrorMsg());
- if ($result === false) {
- return null;
+ while ($result = $query->fetchArray(SQLITE3_ASSOC)) {
+ $dayslogged_by_month[$result['date']] = $result['dayslogged'];
+ }
+
+ /**
+ * We don't have to worry about the possible gap between the date of first activity (as per $scope) and
+ * the date of first days logged. All dates prior to actual activity won't be used in calculations.
+ */
+ $cumulative_dayslogged = 0;
+ ksort($dayslogged_by_month);
+
+ foreach ($dayslogged_by_month as $date => $dayslogged) {
+ $cumulative_dayslogged += $dayslogged;
+ $dayslogged_by_month_cumulative[$date] = $cumulative_dayslogged;
}
+ /**
+ * Retrieve all user activity.
+ */
$channel_activity_by_month = array_fill_keys($scope, 0);
- $query->reset();
+ $query = $sqlite3->query('SELECT ruid, SUBSTR(date, 1, 7) AS date, SUM(l_total) AS l_total, COUNT(DISTINCT date) AS activedays, MAX(l_total) AS l_max FROM uid_activity JOIN uid_details ON uid_activity.uid = uid_details.uid WHERE ruid NOT IN (SELECT ruid FROM uid_details WHERE status IN (3,4)) GROUP BY ruid, SUBSTR(date, 1, 7)') or $this->output('critical', basename(__FILE__).':'.__LINE__.', sqlite3 says: '.$sqlite3->lastErrorMsg());
$ruid_activity_by_month = array();
while ($result = $query->fetchArray(SQLITE3_ASSOC)) {
if (!array_key_exists($result['ruid'], $ruid_activity_by_month)) {
- $ruid_activity_by_month[$result['ruid']] = array_fill_keys(array_slice($scope, array_search($result['date'], $scope)), 0);
+ $zeroed_scope = array_fill_keys(array_slice($scope, array_search($result['date'], $scope)), 0);
+ $ruid_activedays_by_month[$result['ruid']] = $zeroed_scope;
+ $ruid_activity_by_month[$result['ruid']] = $zeroed_scope;
+ $ruid_l_max_by_month[$result['ruid']] = $zeroed_scope;
}
$channel_activity_by_month[$result['date']] += $result['l_total'];
+ $ruid_activedays_by_month[$result['ruid']][$result['date']] = $result['activedays'];
$ruid_activity_by_month[$result['ruid']][$result['date']] = $result['l_total'];
+ $ruid_l_max_by_month[$result['ruid']][$result['date']] = $result['l_max'];
}
/**
@@ -149,14 +169,18 @@ public function calculate_rankings($sqlite3)
* Calculate cumulative user activity.
*/
foreach ($ruid_activity_by_month as $ruid => $dates) {
+ $cumulative_activedays = 0;
$cumulative_l_total = 0;
foreach ($dates as $date => $l_total) {
+ $cumulative_activedays += $ruid_activedays_by_month[$ruid][$date];
$cumulative_l_total += $l_total;
$ruid_activity_by_month_cumulative[] = array(
'ruid' => $ruid,
'date' => $date,
- 'l_total' => $cumulative_l_total);
+ 'l_total' => $cumulative_l_total,
+ 'activedays' => $cumulative_activedays,
+ 'l_max' => $ruid_l_max_by_month[$ruid][$date]);
$sort_dates[] = $date;
$sort_l_total[] = $cumulative_l_total;
$sort_ruids[] = $ruid;
@@ -175,7 +199,7 @@ public function calculate_rankings($sqlite3)
}
$prevdate = $values['date'];
- $sqlite3->exec('INSERT INTO ruid_rankings (ruid, date, rank, l_total, percentage) VALUES ('.$values['ruid'].', \''.$values['date'].'\', '.$rank.', '.$values['l_total'].', '.round(($values['l_total'] / $channel_activity_by_month_cumulative[$values['date']]) * 100, 2).')') or $this->output('critical', basename(__FILE__).':'.__LINE__.', sqlite3 says: '.$sqlite3->lastErrorMsg());
+ $sqlite3->exec('INSERT INTO ruid_rankings (ruid, date, rank, l_total, percentage, l_avg, activity, l_max) VALUES ('.$values['ruid'].', \''.$values['date'].'\', '.$rank.', '.$values['l_total'].', '.round(($values['l_total'] / $channel_activity_by_month_cumulative[$values['date']]) * 100, 2).', '.round($values['l_total'] / $values['activedays'], 1).', '.round(($values['activedays'] / $dayslogged_by_month_cumulative[$values['date']]) * 100, 2).', '.$values['l_max'].')') or $this->output('critical', basename(__FILE__).':'.__LINE__.', sqlite3 says: '.$sqlite3->lastErrorMsg());
$rank++;
}
}
View
39 www/sss.css
@@ -435,27 +435,33 @@ th {
* Specific table styling: rankings
*/
.rank {
- width:438px;
+ width:818px;
}
.rank col.c1,
.rank col.c2 {
- width:44px;
+ width:45px;
}
.rank col.c3 {
- width:70px;
+ width:78px;
}
.rank col.c4,
-.rank col.c6 {
- width:80px;
-}
.rank col.c5,
-.rank col.c7 {
- width:60px;
-}
-.rank td.k12,
+.rank col.c6,
+.rank col.c7,
+.rank col.c8,
+.rank col.c9,
+.rank col.c10,
+.rank col.c11,
+.rank col.c12 {
+ width:72px;
+}
+.rank td.k1-2,
.rank td.k3,
-.rank td.k45,
-.rank td.k67,
+.rank td.k4-5,
+.rank td.k6-7,
+.rank td.k8-9,
+.rank td.k10-11,
+.rank td.k12,
.rank td.v3 {
background-color:#E0E0E0;
color:#999;
@@ -464,12 +470,17 @@ th {
}
.rank td.v1,
.rank td.v4,
-.rank td.v6 {
+.rank td.v6,
+.rank td.v8,
+.rank td.v10,
+.rank td.v12 {
padding-right:2px;
text-align:right;
}
.rank td.v2,
.rank td.v5,
-.rank td.v7 {
+.rank td.v7,
+.rank td.v9,
+.rank td.v11 {
padding-left:2px;
}
View
34 www/user.php
@@ -139,7 +139,6 @@ private function make_html($sqlite3)
if (empty($result)) {
$mood = '';
} else {
- arsort($result);
$smileys = array(
's_01' => ':)',
's_02' => ';)',
@@ -191,6 +190,7 @@ private function make_html($sqlite3)
's_48' => ':[',
's_49' => '>:(',
's_50' => ';o');
+ arsort($result);
foreach ($result as $key => $value) {
if ($key != 'ruid') {
@@ -299,26 +299,26 @@ private function make_table_activity($sqlite3, $type)
if ($type == 'day') {
$class = 'act';
$columns = 24;
+ $head = 'Activity by Day';
+ $query = $sqlite3->query('SELECT date, l_total, l_night, l_morning, l_afternoon, l_evening FROM ruid_activity_by_day WHERE ruid = '.$this->ruid.' AND date > \''.date('Y-m-d', mktime(0, 0, 0, $this->datetime['month'], $this->datetime['dayofmonth'] - 24, $this->datetime['year'])).'\'') or $this->output($sqlite3->lastErrorCode(), basename(__FILE__).':'.__LINE__.', sqlite3 says: '.$sqlite3->lastErrorMsg());
for ($i = $columns - 1; $i >= 0; $i--) {
$dates[] = date('Y-m-d', mktime(0, 0, 0, $this->datetime['month'], $this->datetime['dayofmonth'] - $i, $this->datetime['year']));
}
-
- $head = 'Activity by Day';
- $query = $sqlite3->query('SELECT date, l_total, l_night, l_morning, l_afternoon, l_evening FROM ruid_activity_by_day WHERE ruid = '.$this->ruid.' AND date > \''.date('Y-m-d', mktime(0, 0, 0, $this->datetime['month'], $this->datetime['dayofmonth'] - 24, $this->datetime['year'])).'\'') or $this->output($sqlite3->lastErrorCode(), basename(__FILE__).':'.__LINE__.', sqlite3 says: '.$sqlite3->lastErrorMsg());
} elseif ($type == 'month') {
$class = 'act';
$columns = 24;
+ $head = 'Activity by Month';
+ $query = $sqlite3->query('SELECT date, l_total, l_night, l_morning, l_afternoon, l_evening FROM ruid_activity_by_month WHERE ruid = '.$this->ruid.' AND date > \''.date('Y-m', mktime(0, 0, 0, $this->datetime['month'] - 24, 1, $this->datetime['year'])).'\'') or $this->output($sqlite3->lastErrorCode(), basename(__FILE__).':'.__LINE__.', sqlite3 says: '.$sqlite3->lastErrorMsg());
for ($i = $columns - 1; $i >= 0; $i--) {
$dates[] = date('Y-m', mktime(0, 0, 0, $this->datetime['month'] - $i, 1, $this->datetime['year']));
}
-
- $head = 'Activity by Month';
- $query = $sqlite3->query('SELECT date, l_total, l_night, l_morning, l_afternoon, l_evening FROM ruid_activity_by_month WHERE ruid = '.$this->ruid.' AND date > \''.date('Y-m', mktime(0, 0, 0, $this->datetime['month'] - 24, 1, $this->datetime['year'])).'\'') or $this->output($sqlite3->lastErrorCode(), basename(__FILE__).':'.__LINE__.', sqlite3 says: '.$sqlite3->lastErrorMsg());
} elseif ($type == 'year') {
$class = 'act-year';
$columns = $this->datetime['years'];
+ $head = 'Activity by Year';
+ $query = $sqlite3->query('SELECT date, l_total, l_night, l_morning, l_afternoon, l_evening FROM ruid_activity_by_year WHERE ruid = '.$this->ruid) or $this->output($sqlite3->lastErrorCode(), basename(__FILE__).':'.__LINE__.', sqlite3 says: '.$sqlite3->lastErrorMsg());
for ($i = $columns - 1; $i >= 0; $i--) {
$dates[] = $this->datetime['year'] - $i;
@@ -328,9 +328,6 @@ private function make_table_activity($sqlite3, $type)
$columns++;
$dates[] = 'estimate';
}
-
- $head = 'Activity by Year';
- $query = $sqlite3->query('SELECT date, l_total, l_night, l_morning, l_afternoon, l_evening FROM ruid_activity_by_year WHERE ruid = '.$this->ruid) or $this->output($sqlite3->lastErrorCode(), basename(__FILE__).':'.__LINE__.', sqlite3 says: '.$sqlite3->lastErrorMsg());
}
$result = $query->fetchArray(SQLITE3_ASSOC);
@@ -571,7 +568,11 @@ private function make_table_activity_distribution_hour($sqlite3)
private function make_table_rankings($sqlite3)
{
- $query = $sqlite3->query('SELECT * FROM ruid_rankings WHERE ruid = '.$this->ruid.' ORDER BY date ASC') or $this->output($sqlite3->lastErrorCode(), basename(__FILE__).':'.__LINE__.', sqlite3 says: '.$sqlite3->lastErrorMsg());
+ if (($dayslogged = $sqlite3->querySingle('SELECT COUNT(*) FROM parse_history')) === false) {
+ $this->output($sqlite3->lastErrorCode(), basename(__FILE__).':'.__LINE__.', sqlite3 says: '.$sqlite3->lastErrorMsg());
+ }
+
+ $query = $sqlite3->query('SELECT date, rank, l_total, percentage, l_avg, activity, l_max FROM ruid_rankings WHERE ruid = '.$this->ruid.' ORDER BY date ASC') or $this->output($sqlite3->lastErrorCode(), basename(__FILE__).':'.__LINE__.', sqlite3 says: '.$sqlite3->lastErrorMsg());
$result = $query->fetchArray(SQLITE3_ASSOC);
if ($result === false) {
@@ -585,24 +586,29 @@ private function make_table_rankings($sqlite3)
$prevdate = date('Y-m', mktime(0, 0, 0, (int) substr($result['date'], 5, 2) - 1, 1, (int) substr($result['date'], 0, 4)));
if (array_key_exists($prevdate, $rankings)) {
+ $rankings[$result['date']]['activity_delta'] = $result['activity'] - $rankings[$prevdate]['activity'];
+ $rankings[$result['date']]['l_avg_delta'] = $result['l_avg'] - $rankings[$prevdate]['l_avg'];
$rankings[$result['date']]['l_total_delta'] = $result['l_total'] - $rankings[$prevdate]['l_total'];
$rankings[$result['date']]['percentage_delta'] = $result['percentage'] - $rankings[$prevdate]['percentage'];
$rankings[$result['date']]['rank_delta'] = $rankings[$prevdate]['rank'] - $result['rank'];
}
+ $rankings[$result['date']]['activity'] = $result['activity'];
+ $rankings[$result['date']]['l_avg'] = $result['l_avg'];
+ $rankings[$result['date']]['l_max'] = $result['l_max'];
$rankings[$result['date']]['l_total'] = $result['l_total'];
$rankings[$result['date']]['percentage'] = $result['percentage'];
$rankings[$result['date']]['rank'] = $result['rank'];
}
- $tr0 = '<colgroup><col class="c1"><col class="c2"><col class="c3"><col class="c4"><col class="c5"><col class="c6"><col class="c7">';
+ $tr0 = '<colgroup><col class="c1"><col class="c2"><col class="c3"><col class="c4"><col class="c5"><col class="c6"><col class="c7"><col class="c8"><col class="c9"><col class="c10"><col class="c11"><col class="c12">';
$tr1 = '<tr><th colspan="7">Rankings';
- $tr2 = '<tr><td class="k12" colspan="2">Rank<td class="k3"><td class="k45" colspan="2">Lines<td class="k67" colspan="2">Percentage';
+ $tr2 = '<tr><td class="k1-2" colspan="2">Rank<td class="k3"><td class="k4-5" colspan="2">Lines<td class="k6-7" colspan="2">Percentage<td class="k8-9" colspan="2">Lines/Day<td class="k10-11" colspan="2">Activity<td class="k12" colspan="2">Top Day';
$trx = '';
krsort($rankings);
foreach ($rankings as $date => $values) {
- $trx .= '<tr><td class="v1">'.$values['rank'].'<td class="v2">'.(empty($values['rank_delta']) ? '' : ($values['rank_delta'] < 0 ? '<span class="red">'.$values['rank_delta'].'</span>' : '<span class="green">+'.$values['rank_delta'].'</span>')).'<td class="v3">'.date('M Y', strtotime($date.'-01')).'<td class="v4">'.number_format($values['l_total']).'<td class="v5">'.(empty($values['l_total_delta']) ? '' : '<span class="green">+'.number_format($values['l_total_delta']).'</span>').'<td class="v6">'.number_format($values['percentage'], 2).'%<td class="v7">'.(empty($values['percentage_delta']) ? '' : ($values['percentage_delta'] < 0 ? '<span class="red">'.number_format($values['percentage_delta'], 2).'</span>' : '<span class="green">+'.number_format($values['percentage_delta'], 2).'</span>'));
+ $trx .= '<tr><td class="v1">'.$values['rank'].'<td class="v2">'.(empty($values['rank_delta']) ? '' : ($values['rank_delta'] < 0 ? '<span class="red">'.$values['rank_delta'].'</span>' : '<span class="green">+'.$values['rank_delta'].'</span>')).'<td class="v3">'.date('M Y', strtotime($date.'-01')).'<td class="v4">'.number_format($values['l_total']).'<td class="v5">'.(empty($values['l_total_delta']) ? '' : '<span class="green">+'.number_format($values['l_total_delta']).'</span>').'<td class="v6">'.number_format($values['percentage'], 2).'%<td class="v7">'.(empty($values['percentage_delta']) ? '' : ($values['percentage_delta'] < 0 ? '<span class="red">'.number_format($values['percentage_delta'], 2).'</span>' : '<span class="green">+'.number_format($values['percentage_delta'], 2).'</span>')).'<td class="v8">'.number_format($values['l_avg'], 1).'<td class="v9">'.(empty($values['l_avg_delta']) ? '' : ($values['l_avg_delta'] < 0 ? '<span class="red">'.number_format($values['l_avg_delta'], 1).'</span>' : '<span class="green">+'.number_format($values['l_avg_delta'], 1).'</span>')).'<td class="v10">'.number_format($values['activity'], 2).'%<td class="v11">'.(empty($values['activity_delta']) ? '' : ($values['activity_delta'] < 0 ? '<span class="red">'.number_format($values['activity_delta'], 2).'</span>' : '<span class="green">+'.number_format($values['activity_delta'], 2).'</span>')).'<td class="v12">'.($values['l_max'] == 0 ? '<span class="grey">n/a</span>' : number_format($values['l_max']));
}
return '<table class="rank">'.$tr0.$tr1.$tr2.$trx.'</table>'."\n";

0 comments on commit 3217e14

Please sign in to comment.