Project

General

Profile

Scripts » generate_actions.php

Δημιουργία στατιστικών - Yannis Exidaridis, 24/03/2016 12:00

 
1
<?php
2

    
3
/**
4
 * @author Yannis Exidaridis <jexi@noc.uoa.gr>
5
 * @file generate_actions.php
6
 * @brief generate random statistics data. Data are entered in tables `actions_daily`, `logins`, `loginout`.
7
 * you can run it either from url or command line.
8
 * @param type $t how many data (e.g. rows) to be entered
9
 * @param type $from begin date (e.g. 2015-11-01)
10
 * @param type $to end date (e.g. 2016-05-01)
11
 */
12
require_once 'include/baseTheme.php';
13

    
14
if (PHP_SAPI == 'cli') {
15
    $t = $argv[1];
16
    $from = $argv[2];
17
    $to = $argv[3];
18
} else {
19
    $t = $_GET['t'];
20
    $from = $_GET['from'];
21
    $to = $_GET['to'];
22
}
23

    
24
$uid = 0;
25
$max_user_id = Database::get()->querySingle("SELECT MAX(id) AS maxuserid FROM user")->maxuserid;
26

    
27
$i = 0;
28
while ($i < $t) {
29
    //user id
30
    $sql = Database::get()->queryArray("SELECT id FROM user WHERE id > 1"); // we don't care about admin user
31
    foreach ($sql as $user) {
32
        $u = Database::get()->querySingle("SELECT FLOOR(2+RAND()*$max_user_id) AS u")->u;
33
        if ($user->id == $u) { // check if random generated user id is valid e.g. user exists in eclass
34
            $uid = $u;
35
        }		
36
        if (isset($uid) and $uid > 1) {
37
            // get random course_id from user courses
38
            $sql = Database::get()->querySingle("SELECT course_id FROM course_user WHERE user_id = $uid ORDER BY RAND() LIMIT 1");
39
            if ($sql) {
40
                $cid = $sql->course_id;
41
            }
42
        }
43
    }
44
    // module_id
45
    $sql = Database::get()->queryArray("SELECT DISTINCT(module_id) AS module_id FROM course_module");
46
    foreach ($sql as $module) {
47
        $m = Database::get()->querySingle("SELECT FLOOR(1+RAND()*37) AS m")->m;
48
        if ($module->module_id == $m) { // check if random generated module id is valid e.g. course module exists
49
            $mid = $m;
50
        }
51
    }
52
    // hits (let maximum hits = 20)
53
    $hits = Database::get()->querySingle("SELECT FLOOR(1+RAND()*20) AS hits")->hits;
54
    // duration (let maximum duration = 15000)
55
    $duration = Database::get()->querySingle("SELECT FLOOR(1+RAND()*15000) AS duration")->duration;
56
    // generatine random datetime values in given range
57
    $fromdate = Database::get()->querySingle("SELECT UNIX_TIMESTAMP('$from') AS fromdate")->fromdate;    
58
    $todate = Database::get()->querySingle("SELECT UNIX_TIMESTAMP('$to') AS todate")->todate;    
59
    $day = Database::get()->querySingle("SELECT FROM_UNIXTIME(UNIX_TIMESTAMP('$from')+FLOOR(RAND()*($todate-$fromdate+1))) AS day")->day;
60
    $last_update = $day;
61

    
62
    // update db tables
63
    if (isset($cid) and isset($mid)) {
64
        Database::get()->query("INSERT INTO actions_daily SET
65
                                    user_id = $uid,
66
                                    module_id = $mid,
67
                                    course_id = $cid,
68
                                    hits = $hits,
69
                                    duration = $duration,
70
                                    day = '$day',
71
                                    last_update = '$last_update'");        
72
        if ($i%25 == 0) { // fill entries in table `logins` and `loginout` more slowly         
73
            Database::get()->query("INSERT INTO logins SET
74
                                user_id = $uid,
75
                                ip = '127.0.0.1',
76
                                date_time = '$day',
77
                                course_id = $cid");
78
            Database::get()->query("INSERT INTO loginout SET
79
                                id_user = $uid,
80
                                ip = '127.0.0.1',
81
                                `when` = '$day',
82
                                action = 'LOGIN'");
83
        }
84
        if ($i%50 == 0) { // fill logout entries more slowly             
85
            Database::get()->query("INSERT INTO loginout SET
86
                            id_user = $uid,
87
                            ip = '127.0.0.1',
88
                            `when` = '$day',
89
                            action = 'LOGOUT'");
90
        }
91
    }
92
    $i++;
93
}
94
echo "done!";
(3-3/5)