3 declare(strict_types=1);
37 return (
bool)
$ilSetting->get(
'session_statistics',
"1");
43 public static function createRawEntry(
string $a_session_id,
int $a_session_type,
int $a_timestamp,
int $a_user_id): void
49 if (!$a_user_id || !$a_session_id || !self::isActive()) {
57 "usr_session_stats_raw",
59 "session_id" => array(
"text", $a_session_id)
62 "type" => array(
"integer", $a_session_type),
63 "start_time" => array(
"integer", $a_timestamp),
64 "user_id" => array(
"integer", $a_user_id)
76 public static function closeRawEntry($a_session_id, ?
int $a_context = null, $a_expired_at = null): void
82 if (!self::isActive()) {
87 if (!is_array($a_session_id)) {
89 $end_time = $a_expired_at;
93 $sql =
"UPDATE usr_session_stats_raw" .
94 " SET end_time = " .
$ilDB->quote($end_time,
"integer");
96 $sql .=
",end_context = " .
$ilDB->quote($a_context,
"integer");
98 $sql .=
" WHERE session_id = " .
$ilDB->quote($a_session_id,
"text") .
99 " AND end_time IS NULL";
100 $ilDB->manipulate($sql);
103 elseif (!$a_expired_at) {
104 $sql =
"UPDATE usr_session_stats_raw" .
105 " SET end_time = " .
$ilDB->quote(time(),
"integer");
107 $sql .=
",end_context = " .
$ilDB->quote($a_context,
"integer");
109 $sql .=
" WHERE " .
$ilDB->in(
"session_id", $a_session_id,
false,
"text") .
110 " AND end_time IS NULL";
111 $ilDB->manipulate($sql);
115 foreach ($a_session_id as
$id => $ts) {
116 $sql =
"UPDATE usr_session_stats_raw" .
117 " SET end_time = " .
$ilDB->quote($ts,
"integer");
119 $sql .=
",end_context = " .
$ilDB->quote($a_context,
"integer");
121 $sql .=
" WHERE session_id = " .
$ilDB->quote(
$id,
"text") .
122 " AND end_time IS NULL";
123 $ilDB->manipulate($sql);
137 $ilDB = $DIC[
'ilDB'];
140 $sql =
"SELECT MAX(slot_end) previous_slot_end" .
141 " FROM usr_session_stats";
144 $previous_slot_end = $row[
"previous_slot_end"];
148 if (!$previous_slot_end) {
149 $slot = (
int) (floor(date(
"i") / self::SLOT_SIZE));
152 $current_slot_begin = mktime((
int) date(
"H", $a_now) - 1, 60 - self::SLOT_SIZE, 0);
156 $current_slot_begin = mktime((
int) date(
"H", $a_now), ($slot - 1) * self::SLOT_SIZE, 0);
159 $current_slot_begin = $previous_slot_end + 1;
162 $current_slot_end = $current_slot_begin + (60 * self::SLOT_SIZE) - 1;
165 if ($current_slot_end < $a_now) {
166 return array($current_slot_begin, $current_slot_end);
181 $ilDB = $DIC[
'ilDB'];
183 $sql =
"SELECT COUNT(*) counter FROM usr_session_stats_raw" .
184 " WHERE (end_time IS NULL OR end_time >= " .
$ilDB->quote($a_time,
"integer") .
")" .
185 " AND start_time <= " .
$ilDB->quote($a_time,
"integer") .
189 return (
int) $row[
"counter"];
195 protected static function getRawData(
int $a_begin,
int $a_end): array
199 $ilDB = $DIC[
'ilDB'];
201 $sql =
"SELECT start_time,end_time,end_context FROM usr_session_stats_raw" .
202 " WHERE start_time <= " .
$ilDB->quote($a_end,
"integer") .
203 " AND (end_time IS NULL OR end_time >= " .
$ilDB->quote($a_begin,
"integer") .
")" .
205 " ORDER BY start_time";
223 $ilDB = $DIC[
'ilDB'];
225 $ilAtomQuery =
$ilDB->buildAtomQuery();
226 $ilAtomQuery->addTableLock(
"usr_session_stats");
230 $slot = self::getCurrentSlot($a_now);
231 if (!is_array($slot)) {
238 "slot_begin" => array(
"integer", $slot[0]),
239 "slot_end" => array(
"integer", $slot[1]),
241 $ilDB->
insert(
"usr_session_stats", $fields);
256 if (!self::isActive()) {
260 $slot = self::createNewAggregationSlot($a_now);
261 while (is_array($slot)) {
262 self::aggregateRawHelper($slot[0], $slot[1]);
263 $slot = self::createNewAggregationSlot($a_now);
267 self::deleteAggregatedRaw($a_now);
278 $ilDB = $DIC[
'ilDB'];
290 $closed_counter = $events = array();
292 foreach (self::getRawData($a_begin, $a_end) as $item) {
301 if ($item[
"start_time"] >= $a_begin) {
303 $events[$item[
"start_time"]][] = 1;
306 if ($item[
"end_time"] && $item[
"end_time"] <= $a_end) {
307 if (in_array($item[
"end_context"], $separate_closed,
true)) {
308 if (!isset($closed_counter[$item[
"end_context"]])) {
309 $closed_counter[$item[
"end_context"]] = 0;
312 $closed_counter[$item[
"end_context"]]++;
314 $closed_counter[0] = ($closed_counter[0] ?? 0) + 1;
316 $events[$item[
"end_time"]][] = -1;
321 $active_begin = self::getNumberOfActiveRawSessions($a_begin - 1);
322 $active_end = $active_min = $active_max = $active_avg = $active_begin;
325 if (count($events)) {
326 $last_update_avg = $a_begin - 1;
327 $slot_seconds = self::SLOT_SIZE * 60;
332 foreach ($events as $ts => $actions) {
334 foreach ($actions as $action) {
346 if ($active_end > $active_max) {
347 $active_max = $active_end;
351 if ($active_end < $active_min) {
352 $active_min = $active_end;
356 $diff = $ts - $last_update_avg;
357 $active_avg += $diff / $slot_seconds * $active_end;
358 $last_update_avg = $ts;
362 if ($last_update_avg < $a_end) {
363 $diff = $a_end - $last_update_avg;
364 $active_avg += $diff / $slot_seconds * $active_end;
367 $active_avg = round($active_avg);
374 $max_sessions = self::getLimitForSlot($a_begin);
378 "active_min" => array(
"integer", $active_min),
379 "active_max" => array(
"integer", $active_max),
380 "active_avg" => array(
"integer", $active_avg),
381 "active_end" => array(
"integer", $active_end),
382 "opened" => array(
"integer", $opened_counter),
389 "closed_misc" => array(
"integer", (
int) ($closed_counter[0] ?? 0)),
390 "max_sessions" => array(
"integer", $max_sessions)
395 array(
"slot_begin" => array(
"integer", $a_begin),
396 "slot_end" => array(
"integer", $a_end))
409 $ilDB = $DIC[
'ilDB'];
412 $cut = $a_now - (60 * 60 * 24 * 7);
414 $ilDB->manipulate(
"DELETE FROM usr_session_stats_raw" .
415 " WHERE start_time <= " .
$ilDB->quote($cut,
"integer"));
425 $ilDB = $DIC[
'ilDB'];
427 $sql =
"SELECT max(slot_end) latest FROM usr_session_stats" .
428 " WHERE active_max >= max_sessions" .
429 " AND max_sessions > " .
$ilDB->quote(0,
"integer");
432 if ($row[
"latest"]) {
433 return (
int) $row[
"latest"];
447 $ilDB = $DIC[
'ilDB'];
449 $sql =
"SELECT SUM(slot_end-slot_begin) dur FROM usr_session_stats" .
450 " WHERE active_max >= max_sessions" .
451 " AND max_sessions > " .
$ilDB->quote(0,
"integer") .
452 " AND slot_end > " .
$ilDB->quote($a_from,
"integer") .
453 " AND slot_begin < " .
$ilDB->quote($a_to,
"integer");
457 return (
int) $row[
"dur"];
470 $ilDB = $DIC[
'ilDB'];
472 $sql =
"SELECT SUM(opened) opened, SUM(closed_manual) closed_manual," .
473 " SUM(closed_expire) closed_expire, SUM(closed_idle) closed_idle," .
474 " SUM(closed_idle_first) closed_idle_first, SUM(closed_limit) closed_limit," .
475 " SUM(closed_login) closed_login, SUM(closed_misc) closed_misc" .
476 " FROM usr_session_stats" .
477 " WHERE slot_end > " .
$ilDB->quote($a_from,
"integer") .
478 " AND slot_begin < " .
$ilDB->quote($a_to,
"integer");
486 public static function getActiveSessions(
int $a_from,
int $a_to): array
491 $ilDB = $DIC[
'ilDB'];
493 $sql =
"SELECT slot_begin, slot_end, active_min, active_max, active_avg," .
495 " FROM usr_session_stats" .
496 " WHERE slot_end > " .
$ilDB->quote($a_from,
"integer") .
497 " AND slot_begin < " .
$ilDB->quote($a_to,
"integer") .
498 " ORDER BY slot_begin";
503 foreach ($row as
$key => $value) {
518 $ilDB = $DIC[
'ilDB'];
520 $sql =
"SELECT max(slot_end) latest FROM usr_session_stats";
523 if ($row[
"latest"]) {
524 return (
int) $row[
"latest"];
538 $ilDB = $DIC[
'ilDB'];
542 $sql =
"SELECT maxval FROM usr_session_log" .
543 " WHERE tstamp <= " .
$ilDB->quote($a_timestamp,
"integer") .
544 " ORDER BY tstamp DESC";
547 if (isset($val[
"maxval"]) && $val[
"maxval"]) {
548 return (
int) $val[
"maxval"];
561 $ilDB = $DIC[
'ilDB'];
563 $ilUser = $DIC[
'ilUser'];
565 $new_value = $a_new_value;
568 if ($new_value !== $old_value) {
570 "tstamp" => array(
"timestamp", time()),
571 "maxval" => array(
"integer", $new_value),
572 "user_id" => array(
"integer", $ilUser->getId())
574 $ilDB->insert(
"usr_session_log", $fields);
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const DEFAULT_MAX_COUNT
default value for settings that have not been defined in setup or administration yet ...
static updateLimitLog(int $a_new_value)
Log max session setting.
static getNumberOfActiveRawSessions(int $a_time)
Count number of active sessions at given time.
static getLimitForSlot(int $a_timestamp)
Get max session setting for given timestamp.
static getNumberOfSessionsByType(int $a_from, int $a_to)
Get session counters by type (opened, closed)
static deleteAggregatedRaw($a_now)
Remove already aggregated raw data.
insert(string $table_name, array $values)
static createRawEntry(string $a_session_id, int $a_session_type, int $a_timestamp, int $a_user_id)
Create raw data entry.
const SESSION_CLOSE_LOGIN
static aggretateRaw(int $a_now)
Aggregate raw session data (older than given time)
const SESSION_CLOSE_EXPIRE
static aggregateRawHelper(int $a_begin, int $a_end)
Aggregate statistics data for one slot.
static getLastAggregation()
Get timestamp of last aggregation.
static getCurrentSlot(int $a_now)
Get next slot to aggregate.
static closeRawEntry($a_session_id, ?int $a_context=null, $a_expired_at=null)
Close raw data entry.
const SESSION_CLOSE_LIMIT
static getRawData(int $a_begin, int $a_end)
Read raw data for timespan.
static getMaxedOutDuration(int $a_from, int $a_to)
Get maxed out duration in given timeframe.
static isActive()
Is session statistics active at all?
static array $session_types_controlled
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
static createNewAggregationSlot(int $a_now)
Create new slot (using table lock)
static getLastMaxedOut()
Get latest slot during which sessions were maxed out.
const SESSION_CLOSE_FIRST