-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathQuery.php
More file actions
775 lines (710 loc) · 26.3 KB
/
Query.php
File metadata and controls
775 lines (710 loc) · 26.3 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
<?php
namespace dokuwiki\plugin\statistics;
use dokuwiki\Logger;
use dokuwiki\plugin\sqlite\SQLiteDB;
use helper_plugin_statistics;
/**
* This class defines a bunch of SQL queries to fetch various statistics from the database
*/
class Query
{
protected helper_plugin_statistics $hlp;
protected SQLiteDB $db;
protected string $from;
protected string $to;
protected string $limit = '';
protected string $tz = 'localtime';
/**
* @param helper_plugin_statistics $hlp
*/
public function __construct(helper_plugin_statistics $hlp)
{
$this->hlp = $hlp;
$this->db = $hlp->getDB();
$today = date('Y-m-d');
$this->setTimeFrame($today, $today);
$this->setPagination(0, 20);
}
/**
* Set the time frame for all queries
*
* @param string $from The start date as YYYY-MM-DD
* @param string $to The end date as YYYY-MM-DD
*/
public function setTimeFrame(string $from, string $to): void
{
try {
$from = new \DateTime($from);
$to = new \DateTime($to);
} catch (\Exception $e) {
$from = new \DateTime();
$to = new \DateTime();
}
$from->setTime(0, 0);
$to->setTime(23, 59, 59);
$this->from = $from->format('Y-m-d H:i:s');
$this->to = $to->format('Y-m-d H:i:s');
$this->setTimezone();
}
/**
* Force configured timezone.
* This is useful if you cannot set localtime on the server.
*
* @return void
*/
public function setTimezone()
{
$timezoneId = $this->hlp->getConf('timezone');
if (!$timezoneId || !in_array($timezoneId, \DateTimeZone::listIdentifiers())) return;
try {
$dateTime = new \DateTime($this->from, new \DateTimeZone($timezoneId));
$this->tz = $dateTime->format('P');
} catch (\Exception $e) {
Logger::error($e->getMessage());
}
}
/**
* Set the pagination settings for some queries
*
* @param int $start The start offset
* @param int $limit The number of results. If one more is returned, there is another page
* @return void
*/
public function setPagination(int $start, int $limit)
{
// when a limit is set, one more is fetched to indicate when a next page exists
if ($limit) $limit += 1;
if ($limit) {
$this->limit = " LIMIT $start,$limit";
} elseif ($start) {
$this->limit = " OFFSET $start";
}
}
/**
* Return some aggregated statistics
*/
public function aggregate(): array
{
// init some values that might not be set
$data = [
'referers' => 0, // total number of (external) referrers
'external' => 0, // external referrers
'search' => 0, // search engine referrers
'direct' => 0, // direct referrers
'internal' => 0, // internal referrers
'bouncerate' => 0,
'newvisitors' => 0,
];
// Count referrer types by joining with referers table
$sql = "SELECT
CASE
WHEN R.engine IS NOT NULL THEN 'search'
WHEN R.url = '' THEN 'direct'
WHEN R.url IS NOT NULL THEN 'external'
ELSE 'internal'
END as ref_type,
COUNT(*) as cnt
FROM pageviews as P
LEFT JOIN referers as R ON P.ref_id = R.id
LEFT JOIN sessions as S ON P.session = S.session
WHERE DATETIME(P.dt, ?) >= ? AND DATETIME(P.dt, ?) <= ?
AND S.ua_type = 'browser'
GROUP BY ref_type";
$result = $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
foreach ($result as $row) {
if ($row['ref_type'] == 'search') {
$data['search'] = $row['cnt'];
$data['referers'] += $row['cnt'];
}
if ($row['ref_type'] == 'direct') {
$data['direct'] = $row['cnt'];
$data['referers'] += $row['cnt'];
}
if ($row['ref_type'] == 'external') {
$data['external'] = $row['cnt'];
$data['referers'] += $row['cnt'];
}
if ($row['ref_type'] == 'internal') {
$data['internal'] = $row['cnt'];
}
}
// general user and session info
$sql = "SELECT COUNT(DISTINCT P.session) as sessions,
COUNT(P.session) as views,
COUNT(DISTINCT S.user) as users,
COUNT(DISTINCT S.uid) as visitors,
DATETIME(MAX(P.dt), ?) as last
FROM pageviews as P
LEFT JOIN sessions as S ON P.session = S.session
WHERE DATETIME(P.dt, ?) >= ? AND DATETIME(P.dt, ?) <= ?
AND S.ua_type = 'browser'";
$result = $this->db->queryRecord($sql, [$this->tz, $this->tz, $this->from, $this->tz, $this->to]);
$data['users'] = $result['users'];
$data['sessions'] = $result['sessions'];
$data['pageviews'] = $result['views'];
$data['visitors'] = $result['visitors'];
$data['last'] = $result['last'];
// calculate bounce rate (sessions with only 1 page view)
if ($data['sessions']) {
$sql = "SELECT COUNT(*) as cnt
FROM (
SELECT P.session, COUNT(*) as views
FROM pageviews as P
LEFT JOIN sessions as S ON P.session = S.session
WHERE DATETIME(P.dt, ?) >= ? AND DATETIME(P.dt, ?) <= ?
AND S.ua_type = 'browser'
GROUP BY P.session
HAVING views = 1
)";
$count = $this->db->queryValue($sql, [$this->tz, $this->from, $this->tz, $this->to]);
$data['bouncerate'] = $count * 100 / $data['sessions'];
$data['newvisitors'] = $count * 100 / $data['sessions'];
}
// calculate avg. number of views per session
$sql = "SELECT AVG(views) as cnt
FROM (
SELECT P.session, COUNT(*) as views
FROM pageviews as P
LEFT JOIN sessions as S ON P.session = S.session
WHERE DATETIME(P.dt, ?) >= ? AND DATETIME(P.dt, ?) <= ?
AND S.ua_type = 'browser'
GROUP BY P.session
)";
$data['avgpages'] = $this->db->queryValue($sql, [$this->tz, $this->from, $this->tz, $this->to]);
// average time spent on the site
$sql = "SELECT AVG((unixepoch(end) - unixepoch(dt)) / 60) as time
FROM sessions as S
WHERE S.dt != S.end
AND DATETIME(S.dt, ?) >= ? AND DATETIME(S.dt, ?) <= ?
AND S.ua_type = 'browser'";
$data['timespent'] = $this->db->queryValue($sql, [$this->tz, $this->from, $this->tz, $this->to]);
// logins
$sql = "SELECT COUNT(*) as logins
FROM logins as A
WHERE DATETIME(A.dt, ?) >= ? AND DATETIME(A.dt, ?) <= ?
AND (type = 'l' OR type = 'p')";
$data['logins'] = $this->db->queryValue($sql, [$this->tz, $this->from, $this->tz, $this->to]);
// registrations
$sql = "SELECT COUNT(*) as registrations
FROM logins as A
WHERE DATETIME(A.dt, ?) >= ? AND DATETIME(A.dt, ?) <= ?
AND type = 'C'";
$data['registrations'] = $this->db->queryValue($sql, [$this->tz, $this->from, $this->tz, $this->to]);
// current users (based on recent sessions)
$sql = "SELECT COUNT(DISTINCT uid) as current
FROM sessions
WHERE end >= datetime('now', '-10 minutes')";
$data['current'] = $this->db->queryValue($sql);
return $data;
}
/**
* Return some trend data about visits and edits in the wiki
*
* @param bool $hours Use hour resolution rather than days
* @return array
*/
public function dashboardviews(bool $hours = false): array
{
if ($hours) {
$TIME = "strftime('%H', DATETIME(P.dt, '$this->tz'))";
} else {
$TIME = "DATE(DATETIME(P.dt, '$this->tz'))";
}
$data = [];
// access trends
$sql = "SELECT $TIME as time,
COUNT(DISTINCT P.session) as sessions,
COUNT(P.session) as pageviews,
COUNT(DISTINCT S.uid) as visitors
FROM pageviews as P
LEFT JOIN sessions as S ON P.session = S.session
WHERE DATETIME(P.dt, ?) >= ? AND DATETIME(P.dt, ?) <= ?
AND S.ua_type = 'browser'
GROUP BY $TIME
ORDER BY time";
$result = $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
foreach ($result as $row) {
$data[$row['time']]['sessions'] = $row['sessions'];
$data[$row['time']]['pageviews'] = $row['pageviews'];
$data[$row['time']]['visitors'] = $row['visitors'];
}
return $data;
}
/**
* @param bool $hours Use hour resolution rather than days
* @return array
*/
public function dashboardwiki(bool $hours = false): array
{
if ($hours) {
$TIME = "strftime('%H', DATETIME(dt, '$this->tz'))";
} else {
$TIME = "DATE(DATETIME(dt, '$this->tz'))";
}
$data = [];
// edit trends
foreach (['E', 'C', 'D'] as $type) {
$sql = "SELECT $TIME as time,
COUNT(*) as cnt
FROM edits as A
WHERE DATETIME(A.dt, ?) >= ? AND DATETIME(A.dt, ?) <= ?
AND type = '$type'
GROUP BY $TIME
ORDER BY time";
$result = $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
foreach ($result as $row) {
$data[$row['time']][$type] = $row['cnt'];
}
}
ksort($data);
return $data;
}
/**
* @param string $info Which type of history to select (FIXME which ones are there?)
* @param string $interval Group data by this interval (days, weeks, months)
* @return array
*/
public function history(string $info, string $interval = 'day'): array
{
if ($interval == 'weeks') {
$TIME = "strftime('%Y', DATETIME(dt, '$this->tz')), strftime('%W', DATETIME(dt, '$this->tz'))";
} elseif ($interval == 'months') {
$TIME = "strftime('%Y-%m', DATETIME(dt, '$this->tz'))";
} else {
$TIME = "strftime('%d-%m', DATETIME(dt, '$this->tz'))";
}
$mod = 1;
if ($info == 'media_size' || $info == 'page_size') {
$mod = 1024 * 1024;
}
$sql = "SELECT $TIME as time,
AVG(value)/$mod as cnt
FROM history as A
WHERE DATETIME(A.dt, ?) >= ? AND DATETIME(A.dt, ?) <= ?
AND info = ?
GROUP BY $TIME
ORDER BY $TIME";
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to, $info]);
}
/**
* @return array
*/
public function searchengines(): array
{
$sql = "SELECT COUNT(*) as cnt, R.engine
FROM pageviews as P,
referers as R
WHERE DATETIME(P.dt, ?) >= ? AND DATETIME(P.dt, ?) <= ?
AND P.ref_id = R.id
AND R.engine != ''
GROUP BY R.engine
ORDER BY cnt DESC, R.engine" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function internalsearchphrases(): array
{
$sql = "SELECT COUNT(*) as cnt, query, query as ilookup
FROM search
WHERE DATETIME(dt, ?) >= ? AND DATETIME(dt, ?) <= ?
GROUP BY query
ORDER BY cnt DESC, query" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function internalsearchwords(): array
{
$sql = "SELECT COUNT(*) as cnt, SW.word, SW.word as ilookup
FROM search as S,
searchwords as SW
WHERE DATETIME(S.dt, ?) >= ? AND DATETIME(S.dt, ?) <= ?
AND S.id = SW.sid
GROUP BY SW.word
ORDER BY cnt DESC, SW.word" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function outlinks(): array
{
$sql = "SELECT COUNT(*) as cnt, link as url
FROM outlinks as A
WHERE DATETIME(A.dt, ?) >= ? AND DATETIME(A.dt, ?) <= ?
GROUP BY link
ORDER BY cnt DESC, link" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function pages(): array
{
$sql = "SELECT COUNT(*) as cnt, P.page
FROM pageviews as P,
sessions as S
WHERE DATETIME(P.dt, ?) >= ? AND DATETIME(P.dt, ?) <= ?
AND P.session = S.session
AND S.ua_type = 'browser'
GROUP BY P.page
ORDER BY cnt DESC, P.page" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function edits(): array
{
$sql = "SELECT COUNT(*) as cnt, page
FROM edits as A
WHERE DATETIME(A.dt, ?) >= ? AND DATETIME(A.dt, ?) <= ?
GROUP BY page
ORDER BY cnt DESC, page" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function images(): array
{
$sql = "SELECT COUNT(*) as cnt, media, SUM(size) as filesize
FROM media as A
WHERE DATETIME(A.dt, ?) >= ? AND DATETIME(A.dt, ?) <= ?
AND mime1 = 'image'
GROUP BY media
ORDER BY cnt DESC, media" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function imagessum(): array
{
$sql = "SELECT COUNT(*) as cnt, SUM(size) as filesize
FROM media as A
WHERE DATETIME(A.dt, ?) >= ? AND DATETIME(A.dt, ?) <= ?
AND mime1 = 'image'";
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function downloads(): array
{
$sql = "SELECT COUNT(*) as cnt, media, SUM(size) as filesize
FROM media as A
WHERE DATETIME(A.dt, ?) >= ? AND DATETIME(A.dt, ?) <= ?
AND mime1 != 'image'
GROUP BY media
ORDER BY cnt DESC, media" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function downloadssum(): array
{
$sql = "SELECT COUNT(*) as cnt, SUM(size) as filesize
FROM media as A
WHERE DATETIME(A.dt, ?) >= ? AND DATETIME(A.dt, ?) <= ?
AND mime1 != 'image'";
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function referer(): array
{
$sql = "SELECT COUNT(*) as cnt, R.url
FROM pageviews as P
LEFT JOIN sessions as S ON P.session = S.session
LEFT JOIN referers as R ON P.ref_id = R.id
WHERE DATETIME(P.dt, ?) >= ? AND DATETIME(P.dt, ?) <= ?
AND S.ua_type = 'browser'
AND R.url IS NOT NULL
AND R.url != ''
AND R.engine IS NULL
GROUP BY R.url
ORDER BY cnt DESC, R.url" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function newreferer(): array
{
$sql = "SELECT COUNT(*) as cnt, R.url
FROM pageviews as P
LEFT JOIN sessions as S ON P.session = S.session
LEFT JOIN referers as R ON P.ref_id = R.id
WHERE DATETIME(P.dt, ?) >= ? AND DATETIME(P.dt, ?) <= ?
AND S.ua_type = 'browser'
AND R.url IS NOT NULL
AND R.url != ''
AND R.engine IS NULL
AND DATETIME(R.dt, ?) >= ?
GROUP BY R.url
ORDER BY cnt DESC, R.url" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to, $this->tz, $this->from]);
}
/**
* @return array
*/
public function campaigns(): array
{
$sql = "SELECT COUNT(*) as cnt, C.campaign
FROM campaigns as C,
sessions as S
WHERE DATETIME(S.dt, ?) >= ? AND DATETIME(S.dt, ?) <= ?
AND S.session = C.session
GROUP BY C.campaign
ORDER BY cnt DESC, C.campaign" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function source(): array
{
$sql = "SELECT COUNT(*) as cnt, C.campaign || ' ' || C.source AS campaign
FROM campaigns as C,
sessions as S
WHERE DATETIME(S.dt, ?) >= ? AND DATETIME(S.dt, ?) <= ?
AND S.session = C.session
AND C.source IS NOT NULL
GROUP BY C.campaign, C.source
ORDER BY cnt DESC, C.campaign" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function medium(): array
{
$sql = "SELECT COUNT(*) as cnt, C.campaign || ' ' || C.medium AS campaign
FROM campaigns as C,
sessions as S
WHERE DATETIME(S.dt, ?) >= ? AND DATETIME(S.dt, ?) <= ?
AND S.session = C.session
AND C.medium IS NOT NULL
GROUP BY C.campaign, C.medium
ORDER BY cnt DESC, C.campaign" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function countries(): array
{
$sql = "SELECT COUNT(DISTINCT P.session) as cnt, I.country
FROM pageviews as P,
iplocation as I
WHERE DATETIME(P.dt, ?) >= ? AND DATETIME(P.dt, ?) <= ?
AND P.ip = I.ip
AND I.country != ''
GROUP BY I.code
ORDER BY cnt DESC, I.country" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @param bool $ext return extended information
* @return array
*/
public function browsers(bool $ext = false): array
{
if ($ext) {
$sel = 'S.ua_info as browser, S.ua_ver';
$grp = 'S.ua_info, S.ua_ver';
} else {
$sel = 'S.ua_info as browser';
$grp = 'S.ua_info';
}
$sql = "SELECT COUNT(DISTINCT S.session) as cnt, $sel
FROM sessions as S
WHERE DATETIME(S.dt, ?) >= ? AND DATETIME(S.dt, ?) <= ?
AND S.ua_type = 'browser'
GROUP BY $grp
ORDER BY cnt DESC, S.ua_info" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function os(): array
{
$sql = "SELECT COUNT(DISTINCT S.session) as cnt, S.os
FROM sessions as S
WHERE DATETIME(S.dt, ?) >= ? AND DATETIME(S.dt, ?) <= ?
AND S.ua_type = 'browser'
GROUP BY S.os
ORDER BY cnt DESC, S.os" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function topdomain(): array
{
$sql = "SELECT COUNT(*) as cnt, U.domain
FROM pageviews as P,
sessions as S,
users as U
WHERE DATETIME(P.dt, ?) >= ? AND DATETIME(P.dt, ?) <= ?
AND P.session = S.session
AND S.user = U.user
AND S.ua_type = 'browser'
AND S.user IS NOT NULL
GROUP BY U.domain
ORDER BY cnt DESC, U.domain" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function topuser(): array
{
$sql = "SELECT COUNT(*) as cnt, S.user
FROM pageviews as P,
sessions as S
WHERE DATETIME(P.dt, ?) >= ? AND DATETIME(P.dt, ?) <= ?
AND P.session = S.session
AND S.ua_type = 'browser'
AND S.user IS NOT NULL
GROUP BY S.user
ORDER BY cnt DESC, S.user" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function topeditor(): array
{
$sql = "SELECT COUNT(*) as cnt, user
FROM edits as E,
sessions as S
WHERE DATETIME(E.dt, ?) >= ? AND DATETIME(E.dt, ?) <= ?
AND E.session = S.session
AND S.user IS NOT NULL
GROUP BY user
ORDER BY cnt DESC, user" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function topgroup(): array
{
$sql = "SELECT COUNT(*) as cnt, G.`group`
FROM pageviews as P,
sessions as S,
groups as G
WHERE DATETIME(P.dt, ?) >= ? AND DATETIME(P.dt, ?) <= ?
AND P.session = S.session
AND S.user = G.user
AND S.ua_type = 'browser'
GROUP BY G.`group`
ORDER BY cnt DESC, G.`group`" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function topgroupedit(): array
{
$sql = "SELECT COUNT(*) as cnt, G.`group`
FROM edits as E,
sessions as S,
groups as G
WHERE DATETIME(E.dt, ?) >= ? AND DATETIME(E.dt, ?) <= ?
AND E.session = S.session
AND S.user = G.user
GROUP BY G.`group`
ORDER BY cnt DESC, G.`group`" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function resolution(): array
{
$sql = "SELECT COUNT(DISTINCT S.uid) as cnt,
ROUND(P.screen_x/100)*100 as res_x,
ROUND(P.screen_y/100)*100 as res_y,
CAST(ROUND(P.screen_x/100)*100 AS int)
|| 'x' ||
CAST(ROUND(P.screen_y/100)*100 AS int) as resolution
FROM pageviews as P,
sessions as S
WHERE DATETIME(P.dt, ?) >= ? AND DATETIME(P.dt, ?) <= ?
AND P.session = S.session
AND S.ua_type = 'browser'
AND P.screen_x != 0
AND P.screen_y != 0
GROUP BY resolution
ORDER BY cnt DESC" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function viewport(): array
{
$sql = "SELECT COUNT(DISTINCT S.uid) as cnt,
ROUND(P.view_x/100)*100 as res_x,
ROUND(P.view_y/100)*100 as res_y,
CAST(ROUND(P.view_x/100)*100 AS int)
|| 'x' ||
CAST(ROUND(P.view_y/100)*100 AS int) as resolution
FROM pageviews as P,
sessions as S
WHERE DATETIME(P.dt, ?) >= ? AND DATETIME(P.dt, ?) <= ?
AND P.session = S.session
AND S.ua_type = 'browser'
AND P.view_x != 0
AND P.view_y != 0
GROUP BY resolution
ORDER BY cnt DESC" .
$this->limit;
return $this->db->queryAll($sql, [$this->tz, $this->from, $this->tz, $this->to]);
}
/**
* @return array
*/
public function seenusers(): array
{
$sql = "SELECT `user`, MAX(`dt`) as dt
FROM users
WHERE `user` IS NOT NULL
AND `user` != ''
GROUP BY `user`
ORDER BY `dt` DESC" .
$this->limit;
return $this->db->queryAll($sql);
}
}