-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathtrends.php
131 lines (125 loc) · 4.39 KB
/
trends.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
<?
function logTodayTrend() {
$uid = reqUserId();
$r = redisLink();
$t = time()-(time()%(3600*24));
$pageviews = $visitors = 0;
$n = $r->get("day:pv:$uid:$t");
if ($n) $pageviews = $n;
$n = $r->get("day:uv:$uid:$t");
if ($n) $visitors = $n;
return Array("pageviews"=>$pageviews,"visitors"=>$visitors);
}
function logTodayVisitors() {
$trend = logTodayTrend();
return $trend['visitors'];
}
function logTodayPageviews() {
$trend = logTodayTrend();
return $trend['pageviews'];
}
function logGraphMaxValue($data) {
if (count($data) == 0) return 0;
$max = $data[0]['val'];
foreach ($data as $v) {
$v = $v['val'];
$max = ($v > $max) ? $v : $max;
}
return $max;
}
function logGraphBarHeight($logscale,$max,$value) {
if ($max == 0 || $value == 0) return "0%";
if ($logscale) {
$logrange=10;
$value = ($logrange*$value)/$max;
$value = log(1+$value);
$max = log(1+$logrange);
}
$h = (int)floor((80*$value)/$max);
return "$h%";
}
function logGraph($id,$data,$conf = Array()) {
$logscale = isset($conf['logscale']) ? $conf['logscale'] : 0;
$width = isset($conf['width']) ? $conf['width'] : 600;
$height = isset($conf['height']) ? $conf['height'] : 140;
# Compute some value
$slots = max(count($data),12);
$w = floor((($slots > 15) ? 90 : 80)/$slots);
$lstep = floor(90/$slots);
# Our graph lives inside a main div having the given id
$html = '<div id="'.$id.'" class="graph" style="width: '.$width.'px; ';
$html .= 'height: '.$height.'px;">';
# Output the div for the title if any
if (isset($conf['title'])) {
$html .= '<div class="graphtitle">'.utf8entities($conf['title'])."</div>\n";
}
$max = logGraphMaxValue($data);
# Output the div for the max line
if ($max > 0) {
$h = logGraphBarHeight($logscale,$max,$max);
$html .= '<div class="barmax" style="bottom: '.$h.'">'.$max."</div>\n";
}
# Output a div for every bar
$pos = 0;
for($i = 0; $i < $slots-count($data); $i++) {
$l = $pos*$lstep;
$html .= '<div class="bar" style="width:'.$w.'%; bottom:0px; height:1px; left:'.$l.'%; background-color:#dddddd; border:none;"><!-- --></div>'."\n";
$pos++;
}
$tot = 0;
for($i = 0; $i < count($data); $i++) {
$tot += $data[$i]['val'];
$h = logGraphBarHeight($logscale,$max,$data[$i]['val']);
$l = $pos*$lstep;
if ($i==count($data)-1) {
$class = "bar lastbar";
} else if ($data[$i]['lab'] == 'S') {
$class = "bar webar";
} else {
$class = "bar stdbar";
}
$tip = isset($data[$i]['tip']) ? $data[$i]['tip'] : $data[$i]['val'];
$html .= '<div class="'.$class.'" clicktip="'.$tip.'" style="bottom:0px; height:'.$h.'; left:'.$l.'%; width:'.$w.'%;">'.($data[$i]['lab']).'</div>'."\n";
$pos++;
}
$avg = count($data) ? $tot/count($data) : 0;
if ($avg > 10) {
$avg = floor($avg);
} else {
$avg = round($avg,2);
}
# Output the div for the average line
if ($avg > 0 && count($data) > 1) {
if ($logscale) {
if ($max < 10) $gridstep = .01;
else if ($max < 100) $gridstep = 1;
else if ($max < 5000) $gridstep = 100;
else if ($max < 10000) $gridstep = 1000;
else if ($max < 100000) $gridstep = 10000;
if ($gridstep) {
$step = 0;
$lasth = "0%";
while(1) {
$step += $gridstep;
if ($step > $max) break;
$h = logGraphBarHeight($logscale,$max,$step);
$hmax = logGraphBarHeight($logscale,$max,$max);
if (trim($h,"%")-trim($lasth,"%") > 10) {
if (trim($h,"%") < 70) $html .= '<div class="baravg" style="bottom: '.$h.'">'.round($step,2)."</div>\n";
$lasth = $h;
}
}
}
} else {
$h = logGraphBarHeight($logscale,$max,$avg);
$hmax = logGraphBarHeight($logscale,$max,$max);
if (trim($hmax,"%")/trim($h,"%") <= 1.15) $avg="";
else $avg = "avg ".$avg;
$html .= '<div class="baravg" style="bottom: '.$h.'">'.$avg."</div>\n";
}
}
# Done, almost.
$html .= '</div>';
return $html;
}
?>