ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilSessionStatistics.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 
12 {
13  const SLOT_SIZE = 15;
14 
20  public static function isActive()
21  {
22  global $DIC;
23 
24  $ilSetting = $DIC['ilSetting'];
25 
26  return (bool) $ilSetting->get('session_statistics', 1);
27 
28  /* #13566 - includes somehow won't work this late in the request - doing it directly
29  include_once "Services/Tracking/classes/class.ilObjUserTracking.php";
30  return ilObjUserTracking::_enabledSessionStatistics();
31  */
32  }
33 
42  public static function createRawEntry($a_session_id, $a_session_type, $a_timestamp, $a_user_id)
43  {
44  global $DIC;
45 
46  $ilDB = $DIC['ilDB'];
47 
48  if (!$a_user_id || !$a_session_id || !self::isActive()) {
49  return;
50  }
51 
52  // #9669: if a session was destroyed and somehow the session id is still
53  // in use there will be a id-collision for the raw-entry
54 
55  $ilDB->replace(
56  "usr_session_stats_raw",
57  array(
58  "session_id" => array("text", $a_session_id)
59  ),
60  array(
61  "type" => array("integer", $a_session_type),
62  "start_time" => array("integer", $a_timestamp),
63  "user_id" => array("integer", $a_user_id)
64  )
65  );
66  }
67 
75  public static function closeRawEntry($a_session_id, $a_context = null, $a_expired_at = null)
76  {
77  global $DIC;
78 
79  $ilDB = $DIC['ilDB'];
80 
81  if (!self::isActive()) {
82  return;
83  }
84 
85  // single entry
86  if (!is_array($a_session_id)) {
87  if ($a_expired_at) {
88  $end_time = $a_expired_at;
89  } else {
90  $end_time = time();
91  }
92  $sql = "UPDATE usr_session_stats_raw" .
93  " SET end_time = " . $ilDB->quote($end_time, "integer");
94  if ($a_context) {
95  $sql .= ",end_context = " . $ilDB->quote($a_context, "integer");
96  }
97  $sql .= " WHERE session_id = " . $ilDB->quote($a_session_id, "text") .
98  " AND end_time IS NULL";
99  $ilDB->manipulate($sql);
100  }
101  // batch closing
102  elseif (!$a_expired_at) {
103  $sql = "UPDATE usr_session_stats_raw" .
104  " SET end_time = " . $ilDB->quote(time(), "integer");
105  if ($a_context) {
106  $sql .= ",end_context = " . $ilDB->quote($a_context, "integer");
107  }
108  $sql .= " WHERE " . $ilDB->in("session_id", $a_session_id, false, "text") .
109  " AND end_time IS NULL";
110  $ilDB->manipulate($sql);
111  }
112  // batch with individual timestamps
113  else {
114  foreach ($a_session_id as $id => $ts) {
115  $sql = "UPDATE usr_session_stats_raw" .
116  " SET end_time = " . $ilDB->quote($ts, "integer");
117  if ($a_context) {
118  $sql .= ",end_context = " . $ilDB->quote($a_context, "integer");
119  }
120  $sql .= " WHERE session_id = " . $ilDB->quote($id, "text") .
121  " AND end_time IS NULL";
122  $ilDB->manipulate($sql);
123  }
124  }
125  }
126 
133  protected static function getCurrentSlot($a_now)
134  {
135  global $DIC;
136 
137  $ilDB = $DIC['ilDB'];
138 
139  // get latest slot in db
140  $sql = "SELECT MAX(slot_end) previous_slot_end" .
141  " FROM usr_session_stats";
142  $res = $ilDB->query($sql);
143  $row = $ilDB->fetchAssoc($res);
144  $previous_slot_end = $row["previous_slot_end"];
145 
146  // no previous slot? calculate last complete slot
147  // should we use minimum session raw date instead? (problem: table lock)
148  if (!$previous_slot_end) {
149  $slot = floor(date("i") / self::SLOT_SIZE);
150  // last slot of previous hour
151  if (!$slot) {
152  $current_slot_begin = mktime(date("H", $a_now) - 1, 60 - self::SLOT_SIZE, 0);
153  }
154  // "normalize" to slot
155  else {
156  $current_slot_begin = mktime(date("H", $a_now), ($slot - 1) * self::SLOT_SIZE, 0);
157  }
158  } else {
159  $current_slot_begin = $previous_slot_end + 1;
160  }
161 
162  $current_slot_end = $current_slot_begin + (60 * self::SLOT_SIZE) - 1;
163 
164  // no complete slot: nothing to do yet
165  if ($current_slot_end < $a_now) {
166  return array($current_slot_begin, $current_slot_end);
167  }
168  }
169 
176  protected static function getNumberOfActiveRawSessions($a_time)
177  {
178  global $DIC;
179 
180  $ilDB = $DIC['ilDB'];
181 
182  $sql = "SELECT COUNT(*) counter FROM usr_session_stats_raw" .
183  " WHERE (end_time IS NULL OR end_time >= " . $ilDB->quote($a_time, "integer") . ")" .
184  " AND start_time <= " . $ilDB->quote($a_time, "integer") .
185  " AND " . $ilDB->in("type", ilSessionControl::$session_types_controlled, false, "integer");
186  $res = $ilDB->query($sql);
187  $row = $ilDB->fetchAssoc($res);
188  return $row["counter"];
189  }
190 
198  protected static function getRawData($a_begin, $a_end)
199  {
200  global $DIC;
201 
202  $ilDB = $DIC['ilDB'];
203 
204  $sql = "SELECT start_time,end_time,end_context FROM usr_session_stats_raw" .
205  " WHERE start_time <= " . $ilDB->quote($a_end, "integer") .
206  " AND (end_time IS NULL OR end_time >= " . $ilDB->quote($a_begin, "integer") . ")" .
207  " AND " . $ilDB->in("type", ilSessionControl::$session_types_controlled, false, "integer") .
208  " ORDER BY start_time";
209  $res = $ilDB->query($sql);
210  $all = array();
211  while ($row = $ilDB->fetchAssoc($res)) {
212  $all[] = $row;
213  }
214  return $all;
215  }
216 
223  protected static function createNewAggregationSlot($a_now)
224  {
225  global $DIC;
226 
227  $ilDB = $DIC['ilDB'];
228 
229  $ilAtomQuery = $ilDB->buildAtomQuery();
230  $ilAtomQuery->addTableLock("usr_session_stats");
231 
232  $ilAtomQuery->addQueryCallable(function (ilDBInterface $ilDB) use ($a_now, &$slot) {
233 
234  // if we had to wait for the lock, no current slot should be returned here
235  $slot = self::getCurrentSlot($a_now);
236  if (!is_array($slot)) {
237  $slot = false;
238  return;
239  }
240 
241  // save slot to mark as taken
242  $fields = array(
243  "slot_begin" => array("integer", $slot[0]),
244  "slot_end" => array("integer", $slot[1]),
245  );
246  $ilDB->insert("usr_session_stats", $fields);
247  });
248 
249  $ilAtomQuery->run();
250 
251  return $slot;
252  }
253 
259  public static function aggretateRaw($a_now)
260  {
261  if (!self::isActive()) {
262  return;
263  }
264 
265  $slot = self::createNewAggregationSlot($a_now);
266  while (is_array($slot)) {
267  self::aggregateRawHelper($slot[0], $slot[1]);
268  $slot = self::createNewAggregationSlot($a_now);
269  }
270 
271  // #12728
272  self::deleteAggregatedRaw($a_now);
273  }
274 
281  public static function aggregateRawHelper($a_begin, $a_end)
282  {
283  global $DIC;
284 
285  $ilDB = $DIC['ilDB'];
286  $ilSetting = $DIC['ilSetting'];
287 
288  // "relevant" closing types
289  $separate_closed = array(ilSession::SESSION_CLOSE_USER,
295 
296  // gather/process data (build event timeline)
297  $closed_counter = $events = array();
298  $opened_counter = 0;
299  foreach (self::getRawData($a_begin, $a_end) as $item) {
300  // open/close counters are _not_ time related
301 
302  // we could filter for undefined/invalid closing contexts
303  // and ignore those items, but this would make any debugging
304  // close to impossible
305  // "closed_other" would have been a good idea...
306 
307  // session opened
308  if ($item["start_time"] >= $a_begin) {
309  $opened_counter++;
310  $events[$item["start_time"]][] = 1;
311  }
312  // session closed
313  if ($item["end_time"] && $item["end_time"] <= $a_end) {
314  if (in_array($item["end_context"], $separate_closed)) {
315  $closed_counter[$item["end_context"]]++;
316  } else {
317  $closed_counter[0]++;
318  }
319  $events[$item["end_time"]][] = -1;
320  }
321  }
322 
323  // initialising active statistical values
324  $active_begin = self::getNumberOfActiveRawSessions($a_begin - 1);
325  $active_end = $active_min = $active_max = $active_avg = $active_begin;
326 
327  // parsing events / building avergages
328  if (sizeof($events)) {
329  $last_update_avg = $a_begin - 1;
330  $slot_seconds = self::SLOT_SIZE * 60;
331  $active_avg = 0;
332 
333  // parse all open/closing events
334  ksort($events);
335  foreach ($events as $ts => $actions) {
336  // actions which occur in the same second are "merged"
337  foreach ($actions as $action) {
338  // max
339  if ($action > 0) {
340  $active_end++;
341  }
342  // min
343  else {
344  $active_end--;
345  }
346  }
347 
348  // max
349  if ($active_end > $active_max) {
350  $active_max = $active_end;
351  }
352 
353  // min
354  if ($active_end < $active_min) {
355  $active_min = $active_end;
356  }
357 
358  // avg
359  $diff = $ts - $last_update_avg;
360  $active_avg += $diff / $slot_seconds * $active_end;
361  $last_update_avg = $ts;
362  }
363  unset($actions);
364 
365  // add up to end of slot if needed
366  if ($last_update_avg < $a_end) {
367  $diff = $a_end - $last_update_avg;
368  $active_avg += $diff / $slot_seconds * $active_end;
369  }
370 
371  $active_avg = round($active_avg);
372  }
373  unset($events);
374 
375 
376  // do we (really) need a log here?
377  // $max_sessions = (int)$ilSetting->get("session_max_count", ilSessionControl::DEFAULT_MAX_COUNT);
378  $max_sessions = self::getLimitForSlot($a_begin);
379 
380  // save aggregated data
381  $fields = array(
382  "active_min" => array("integer", $active_min),
383  "active_max" => array("integer", $active_max),
384  "active_avg" => array("integer", $active_avg),
385  "active_end" => array("integer", $active_end),
386  "opened" => array("integer", $opened_counter),
387  "closed_manual" => array("integer", (int) $closed_counter[ilSession::SESSION_CLOSE_USER]),
388  "closed_expire" => array("integer", (int) $closed_counter[ilSession::SESSION_CLOSE_EXPIRE]),
389  "closed_idle" => array("integer", (int) $closed_counter[ilSession::SESSION_CLOSE_IDLE]),
390  "closed_idle_first" => array("integer", (int) $closed_counter[ilSession::SESSION_CLOSE_FIRST]),
391  "closed_limit" => array("integer", (int) $closed_counter[ilSession::SESSION_CLOSE_LIMIT]),
392  "closed_login" => array("integer", (int) $closed_counter[ilSession::SESSION_CLOSE_LOGIN]),
393  "closed_misc" => array("integer", (int) $closed_counter[0]),
394  "max_sessions" => array("integer", (int) $max_sessions)
395  );
396  $ilDB->update(
397  "usr_session_stats",
398  $fields,
399  array("slot_begin" => array("integer", $a_begin),
400  "slot_end" => array("integer", $a_end))
401  );
402  }
403 
409  protected static function deleteAggregatedRaw($a_now)
410  {
411  global $DIC;
412 
413  $ilDB = $DIC['ilDB'];
414 
415  // we are rather defensive here - 7 days BEFORE current aggregation
416  $cut = $a_now - (60 * 60 * 24 * 7);
417 
418  $ilDB->manipulate("DELETE FROM usr_session_stats_raw" .
419  " WHERE start_time <= " . $ilDB->quote($cut, "integer"));
420  }
421 
427  public static function getLastMaxedOut()
428  {
429  global $DIC;
430 
431  $ilDB = $DIC['ilDB'];
432 
433  $sql = "SELECT max(slot_end) latest FROM usr_session_stats" .
434  " WHERE active_max >= max_sessions" .
435  " AND max_sessions > " . $ilDB->quote(0, "integer");
436  $res = $ilDB->query($sql);
437  $row = $ilDB->fetchAssoc($res);
438  if ($row["latest"]) {
439  return $row["latest"];
440  }
441  }
442 
450  public static function getMaxedOutDuration($a_from, $a_to)
451  {
452  global $DIC;
453 
454  $ilDB = $DIC['ilDB'];
455 
456  $sql = "SELECT SUM(slot_end-slot_begin) dur FROM usr_session_stats" .
457  " WHERE active_max >= max_sessions" .
458  " AND max_sessions > " . $ilDB->quote(0, "integer") .
459  " AND slot_end > " . $ilDB->quote($a_from, "integer") .
460  " AND slot_begin < " . $ilDB->quote($a_to, "integer");
461  $res = $ilDB->query($sql);
462  $row = $ilDB->fetchAssoc($res);
463  if ($row["dur"]) {
464  return $row["dur"];
465  }
466  }
467 
475  public static function getNumberOfSessionsByType($a_from, $a_to)
476  {
477  global $DIC;
478 
479  $ilDB = $DIC['ilDB'];
480 
481  $sql = "SELECT SUM(opened) opened, SUM(closed_manual) closed_manual," .
482  " SUM(closed_expire) closed_expire, SUM(closed_idle) closed_idle," .
483  " SUM(closed_idle_first) closed_idle_first, SUM(closed_limit) closed_limit," .
484  " SUM(closed_login) closed_login, SUM(closed_misc) closed_misc" .
485  " FROM usr_session_stats" .
486  " WHERE slot_end > " . $ilDB->quote($a_from, "integer") .
487  " AND slot_begin < " . $ilDB->quote($a_to, "integer");
488  $res = $ilDB->query($sql);
489  return $ilDB->fetchAssoc($res);
490  }
491 
499  public static function getActiveSessions($a_from, $a_to)
500  {
501  global $DIC;
502 
503  $ilDB = $DIC['ilDB'];
504 
505  $sql = "SELECT slot_begin, slot_end, active_min, active_max, active_avg," .
506  " max_sessions" .
507  " FROM usr_session_stats" .
508  " WHERE slot_end > " . $ilDB->quote($a_from, "integer") .
509  " AND slot_begin < " . $ilDB->quote($a_to, "integer") .
510  " ORDER BY slot_begin";
511  $res = $ilDB->query($sql);
512  $all = array();
513  while ($row = $ilDB->fetchAssoc($res)) {
514  $all[] = $row;
515  }
516  return $all;
517  }
518 
524  public static function getLastAggregation()
525  {
526  global $DIC;
527 
528  $ilDB = $DIC['ilDB'];
529 
530  $sql = "SELECT max(slot_end) latest FROM usr_session_stats";
531  $res = $ilDB->query($sql);
532  $row = $ilDB->fetchAssoc($res);
533  if ($row["latest"]) {
534  return $row["latest"];
535  }
536  }
537 
544  public static function getLimitForSlot($a_timestamp)
545  {
546  global $DIC;
547 
548  $ilDB = $DIC['ilDB'];
549  $ilSetting = $DIC['ilSetting'];
550 
551  $ilDB->setLimit(1);
552  $sql = "SELECT maxval FROM usr_session_log" .
553  " WHERE tstamp <= " . $ilDB->quote($a_timestamp, "integer") .
554  " ORDER BY tstamp DESC";
555  $res = $ilDB->query($sql);
556  $val = $ilDB->fetchAssoc($res);
557  if ($val["maxval"]) {
558  return (int) $val["maxval"];
559  } else {
560  return (int) $ilSetting->get("session_max_count", ilSessionControl::DEFAULT_MAX_COUNT);
561  }
562  }
563 
569  public static function updateLimitLog($a_new_value)
570  {
571  global $DIC;
572 
573  $ilDB = $DIC['ilDB'];
574  $ilSetting = $DIC['ilSetting'];
575  $ilUser = $DIC['ilUser'];
576 
577  $new_value = (int) $a_new_value;
578  $old_value = (int) $ilSetting->get("session_max_count", ilSessionControl::DEFAULT_MAX_COUNT);
579 
580  if ($new_value != $old_value) {
581  $fields = array(
582  "tstamp" => array("timestamp", time()),
583  "maxval" => array("integer", $new_value),
584  "user_id" => array("integer", $ilUser->getId())
585  );
586  $ilDB->insert("usr_session_log", $fields);
587  }
588  }
589 }
static getNumberOfActiveRawSessions($a_time)
Count number of active sessions at given time.
const SESSION_CLOSE_IDLE
const DEFAULT_MAX_COUNT
default value for settings that have not been defined in setup or administration yet ...
static getMaxedOutDuration($a_from, $a_to)
Get maxed out duration in given timeframe.
static deleteAggregatedRaw($a_now)
Remove already aggregated raw data.
static getCurrentSlot($a_now)
Get next slot to aggregate.
$action
global $DIC
Definition: saml.php:7
const SESSION_CLOSE_LOGIN
if(!array_key_exists('StateId', $_REQUEST)) $id
if($argc< 2) $events
static getNumberOfSessionsByType($a_from, $a_to)
Get session counters by type (opened, closed)
static createNewAggregationSlot($a_now)
Create new slot (using table lock)
const SESSION_CLOSE_EXPIRE
static updateLimitLog($a_new_value)
Log max session setting.
static getLastAggregation()
Get timestamp of last aggregation.
static aggregateRawHelper($a_begin, $a_end)
Aggregate statistics data for one slot.
static getLimitForSlot($a_timestamp)
Get max session setting for given timestamp.
foreach($_POST as $key=> $value) $res
const SESSION_CLOSE_USER
$ilUser
Definition: imgupload.php:18
static createRawEntry($a_session_id, $a_session_type, $a_timestamp, $a_user_id)
Create raw data entry.
const SESSION_CLOSE_LIMIT
$row
static getRawData($a_begin, $a_end)
Read raw data for timespan.
static isActive()
Is session statistics active at all?
global $ilSetting
Definition: privfeed.php:17
global $ilDB
insert($table_name, $values)
static getLastMaxedOut()
Get latest slot during which sessions were maxed out.
static aggretateRaw($a_now)
Aggregate raw session data (older than given time)
const SESSION_CLOSE_FIRST
static closeRawEntry($a_session_id, $a_context=null, $a_expired_at=null)
Close raw data entry.
static getActiveSessions($a_from, $a_to)
Get active sessions aggregated data.