forked from tylerhall/Shine
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstats.php
203 lines (178 loc) · 5.89 KB
/
stats.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?PHP
require 'includes/master.inc.php';
$Auth->requireAdmin('login.php');
$nav = 'stats';
$db = Database::getDatabase();
$applications = DBObject::glob('Application', 'SELECT * FROM shine_applications ORDER BY name');
$chart_app_activity = new Chart();
$chart_app_activity->id = 'chart_app_activity';
$chart_app_activity->type = 'column';
$chart_app_activity->title = 'App Activity by Week';
$chart_app_activity->yAxisTitle = '# Sparkle Updates';
$chart_app_activity->xColumnName = 'ywdt';
$chart_app_activity->yColumnName = 'cnt';
$where = (isset($_GET['id'])) ? " AND app_id='" . $_GET['id'] . "' " : "";
$chart_app_activity->query = 'SELECT COUNT(*) as cnt, YEARWEEK(dt, 3) as ywdt FROM `shine_sparkle_reports` rep WHERE DATE_ADD(dt, INTERVAL 16 WEEK) >= NOW() '.$where.' GROUP BY ywdt ORDER BY ywdt ASC';
$chart_app_activity->query_unique = 'SELECT COUNT(DISTINCT CONCAT(DATE_FORMAT(dt,"%Y%m%d"), ip)) as cnt, YEARWEEK(dt, 3) as ywdt FROM `shine_sparkle_reports` rep WHERE DATE_ADD(dt, INTERVAL 16 WEEK) >= NOW() '.$where.' GROUP BY ywdt ORDER BY ywdt ASC';
Class Chart
{
public $id;
public $type;
public $title;
public $yearColumn;
public $weekColumn;
public $xColumnName;
public $yColumnName;
public $query;
public $appID;
public $yAxisTitle;
private $data;
private function getWeekStart($week, $year=""){
$first_date = strtotime("1 january ".($year ? $year : date("Y")));
if(date("D", $first_date)=="Mon") {
$weekStart = $first_date;
} else {
$weekStart = strtotime("next Monday", $first_date)-604800;
}
$plus_week = "+".($week-1)." week";
return strtotime($plus_week, $weekStart);
}
public function run()
{
$db = Database::getDatabase();
$stats = array(
'all' => array(),
'unique' => array()
);
$rows = $db->getRows($this->query);
foreach($rows as $row)
{
$x = $row[$this->xColumnName];
$y = $row[$this->yColumnName];
$stats['all'][$x] = $y;
}
$rows = $db->getRows($this->query_unique);
foreach($rows as $row)
{
$x = $row[$this->xColumnName];
$y = $row[$this->yColumnName];
$stats['unique'][$x] = $y;
}
$this->data = array(
'all' => array(),
'unique' => array()
);
for ($i=15; $i>=0; $i--) {
$nextWeek = date('oW', strtotime('-'.$i.' week', time()));
$year = date('o', strtotime('-'.$i.' week', time()));
$week = date('W', strtotime('-'.$i.' week', time()));
$weekStart = $this->getWeekStart($week, $year);
$key = date("M d", $weekStart) . " - " . date("M d", strtotime("+6 days", $weekStart));
$this->data['all'][$key] = (isset($stats['all'][$nextWeek])) ? $stats['all'][$nextWeek] : 0;
$this->data['unique'][$key] = (isset($stats['unique'][$nextWeek])) ? $stats['unique'][$nextWeek] : 0;
}
}
public function render()
{
$this->run();
$categories = array_keys($this->data['all']);
$categories = "'" . implode("','", $categories) . "'";
$data = implode(',', $this->data['all']);
$data_unique = implode(',', $this->data['unique']);
$out = "
{$this->id} = new Highcharts.Chart({";
$out .= "
chart: {
renderTo: '{$this->id}',
type: 'column'
},
title: {
text: '{$this->title}'
},
xAxis: [{
categories: [$categories],
//reversed: false
},
/*
{ // mirror axis on right side
opposite: true,
reversed: false,
categories: [$categories],
linkedTo: 0
}
*/],
yAxis: {
title: {
text: '{$this->yAxisTitle}'
},
/*
labels: {
formatter: function(){
return (Math.abs(this.value) / 1000) + 'k';
}
},
*/
//min: -400000,
//max: 400000
},
tooltip: {
formatter: function(){
return '<b>'+ this.series.name +', '+ this.point.category +'</b><br/>'+
'Updates: '+ Highcharts.numberFormat(Math.abs(this.point.y), 0);
}
},
/*
plotOptions: {
series: {
stacking: 'normal'
}
},
*/
series: [{
name: 'App updates',
data: [$data]
}, {
name: 'Unique queries',
data: [$data_unique]
}]
";
$out .= "});";
echo $out;
}
}
?>
<?PHP include('inc/header.inc.php'); ?>
<div id="bd">
<div id="yui-main">
<div class="yui-b"><div class="yui-g">
<div class="block tabs spaces">
<div class="hd">
<h2>Sparkle Stats</h2>
<ul>
<li class="<?PHP if(!isset($_GET['id'])) echo 'active'; ?>"><a href="stats.php">All Apps</a></li>
<?PHP foreach($applications as $a) : ?>
<li class="<?PHP if(@$_GET['id'] == $a->id) echo 'active'; ?>"><a href="stats.php?id=<?PHP echo $a->id; ?>"><?PHP echo $a->name; ?></a></li>
<?PHP endforeach; ?>
</ul>
<div class="clear"></div>
</div>
</div>
<div class="block" style="float:left;margin-right:2em;width:100%;">
<div class="hd">
<h2>App activity chart</h2>
</div>
<div class="bd">
<div id="chart_app_activity" class="chart"></div>
</div>
</div>
</div></div>
</div>
<div id="sidebar" class="yui-b">
</div>
</div>
<?PHP include('inc/footer.inc.php'); ?>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
<?PHP $chart_app_activity->render(); ?>
});
</script>