mcp_activity_statistics()
Description
Aggregate retained installation-wide events using indexed years and a strictly bounded date range.Parameters
| Column | Type | Default | Description |
|---|---|---|---|
| $args: | array | ||
| $args | array | Validated tool arguments. |
Return
| array | Structured MCP result or permission/availability error. |
Location
include/mcp_management_functions.php lines 213 to 282
Definition
function mcp_activity_statistics(array $args): array
{
if (!checkperm('a') || !checkperm('t')) {
return mcp_error_result('Installation-wide reporting permission is required.', 'permission_denied');
}
$range = mcp_event_dates($args, "STR_TO_DATE(CONCAT_WS('-', year, month, day), '%Y-%c-%e')");
$from = new DateTimeImmutable($args['date_from']);
$to = new DateTimeImmutable($args['date_to']);
if ($to > $from->modify('+4 years')) {
throw new DomainException('Select a period of four years or less.');
}
$metric = $args['metric'] ?? 'uploads';
if ($metric === 'logins') {
$group = match ($args['group_by'] ?? 'month') {
'day' => 'DATE(a.logged)', 'week' => "DATE_FORMAT(a.logged, '%x-W%v')",
'month' => "DATE_FORMAT(a.logged, '%Y-%m')", 'year' => 'YEAR(a.logged)', 'account' => 'a.user',
default => throw new DomainException('Group recorded logins by account or time period.'),
};
if (isset($args['group'])) {
throw new DomainException('Historical account groups are unavailable for login events.');
}
$dates = mcp_event_dates($args, 'a.logged');
return mcp_query_page(
"SELECT {$group} AS bucket, COUNT(*) AS events, MAX(a.logged) AS last_login
FROM activity_log a WHERE a.remote_table = 'user' AND a.remote_column = 'last_ip'
AND BINARY a.log_code = ? AND a.user > 0 AND {$dates->sql} GROUP BY bucket ORDER BY bucket",
['s', LOG_CODE_LOGGED_IN, ...$dates->parameters],
$args,
['unit' => 'recorded_login_events', 'metric' => $metric]
);
}
if (($args['group_by'] ?? null) === 'account' && $metric !== 'sessions') {
throw new DomainException('Group by account with recorded sessions or logins.');
}
$events = ['uploads' => 'Resource upload', 'downloads' => 'Resource download', 'views' => 'Resource view',
'sessions' => 'User session', 'active_users' => 'User session', 'searches' => 'Search'];
if (in_array($metric, ['keyword_usage', 'no_results'], true)) {
$event = $metric === 'keyword_usage' ? 'Keyword usage' : 'Keyword usage - no results found';
return mcp_query_page("SELECT k.keyword, SUM(ds.count) AS events FROM daily_stat ds JOIN keyword k ON k.ref = ds.object_ref
WHERE ds.year BETWEEN ? AND ? AND ds.activity_type = ? AND {$range->sql}
GROUP BY k.ref, k.keyword ORDER BY events DESC, k.ref", ['i', $from->format('Y'), 'i', $to->format('Y'), 's', $event,
...$range->parameters], $args, ['metric' => $metric, 'unit' => 'recorded_keyword_events']);
}
$group = match ($args['group_by'] ?? 'month') {
'day' => "CONCAT(year, '-', LPAD(month, 2, '0'), '-', LPAD(day, 2, '0'))",
'week' => "DATE_FORMAT(STR_TO_DATE(CONCAT_WS('-', year, month, day), '%Y-%c-%e'), '%x-W%v')",
'account' => 'object_ref',
'month' => "CONCAT(year, '-', LPAD(month, 2, '0'))",
'year' => 'year',
'group' => 'usergroup',
};
$unique_users = $metric === 'active_users';
$measure = $unique_users ? 'COUNT(DISTINCT object_ref) AS accounts' : 'SUM(count) AS events';
$sql = "SELECT {$group} AS bucket, {$measure} FROM daily_stat WHERE year BETWEEN ? AND ?
AND activity_type = ? AND {$range->sql}";
if ($unique_users) {
$sql .= ' AND object_ref > 0 AND daily_stat.external = 0';
}
$params = ['i', $from->format('Y'), 'i', $to->format('Y'), 's', $events[$metric], ...$range->parameters];
if (isset($args['group'])) {
$sql .= ' AND usergroup = ?';
array_push($params, 'i', $args['group']);
}
return mcp_query_page(
$sql . ' GROUP BY bucket ORDER BY bucket',
$params,
$args,
['unit' => $unique_users ? 'distinct_session_accounts' : 'recorded_events', 'metric' => $metric, 'date_from' => $args['date_from'], 'date_to' => $args['date_to']]
);
}
This article was last updated 4th December 2026 11:35 Europe/London time based on the source file dated 4th December 2026 09:40 Europe/London time.