Managed instances: report usage counts each day by the instance's own date (tickets opened, resolved and by email, agent replies, invoices, payments, agents and client portal sign-ins, email sent and failed, API calls by key name), shown in a Usage part of the History card and a sortable Last 30 days column on the list, with Managed By saying only counts are sent, database 4.2.0

This commit is contained in:
johnnyq
2026-11-25 16:06:39 -05:00
parent 95c44f6c73
commit bfc91512c9
7 changed files with 330 additions and 11 deletions

View File

@@ -0,0 +1,40 @@
<?php
/*
* ITFlow - Database update to version 4.2.0 (from 4.1.9)
* Included by admin/database_updates.php - do not access directly
*
* How much a managed instance is used, on its history (functions/managed.php) - each day as
* the instance counts it, by its own date; NULL for a day it didn't report it:
* managed_instance_stats._tickets_opened, _tickets_email, _tickets_resolved, _replies
* (agents'), _invoices, _payments, _agent_signins (agents who signed in), _portal_signins,
* _emails_sent, _emails_failed, _api_calls and _api_keys (JSON, calls under each key's
* name), and _agents_30d (agents who signed in over the 30 days to that day)
*
* Re-runnable: each column is made only where it isn't there yet.
*/
defined('FROM_DB_UPDATER') || die("Direct file access is not allowed");
$itflow_usage_columns = [
'managed_stat_tickets_opened' => "int(11) DEFAULT NULL AFTER `managed_stat_outages`",
'managed_stat_tickets_email' => "int(11) DEFAULT NULL AFTER `managed_stat_tickets_opened`",
'managed_stat_tickets_resolved' => "int(11) DEFAULT NULL AFTER `managed_stat_tickets_email`",
'managed_stat_replies' => "int(11) DEFAULT NULL AFTER `managed_stat_tickets_resolved`",
'managed_stat_invoices' => "int(11) DEFAULT NULL AFTER `managed_stat_replies`",
'managed_stat_payments' => "int(11) DEFAULT NULL AFTER `managed_stat_invoices`",
'managed_stat_agent_signins' => "int(11) DEFAULT NULL AFTER `managed_stat_payments`",
'managed_stat_portal_signins' => "int(11) DEFAULT NULL AFTER `managed_stat_agent_signins`",
'managed_stat_emails_sent' => "int(11) DEFAULT NULL AFTER `managed_stat_portal_signins`",
'managed_stat_emails_failed' => "int(11) DEFAULT NULL AFTER `managed_stat_emails_sent`",
'managed_stat_api_calls' => "int(11) DEFAULT NULL AFTER `managed_stat_emails_failed`",
'managed_stat_api_keys' => "text DEFAULT NULL AFTER `managed_stat_api_calls`",
'managed_stat_agents_30d' => "int(11) DEFAULT NULL AFTER `managed_stat_api_keys`",
];
foreach ($itflow_usage_columns as $itflow_usage_column => $itflow_usage_definition) {
$itflow_column_exists = mysqli_query($mysqli, "SELECT 1 FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'managed_instance_stats' AND COLUMN_NAME = '$itflow_usage_column' LIMIT 1");
if (!$itflow_column_exists || mysqli_num_rows($itflow_column_exists) === 0) {
mysqli_query($mysqli, "ALTER TABLE `managed_instance_stats` ADD `$itflow_usage_column` $itflow_usage_definition");
}
}

View File

@@ -112,7 +112,7 @@ $lock = '<i class="fas fa-fw fa-lock text-body-secondary ms-1" title="Set by ' .
<h3 class="card-title"><i class="fas fa-fw fa-info-circle me-2"></i>What a manager can do</h3>
</div>
<ul class="list-group list-group-flush small">
<li class="list-group-item d-flex gap-2"><i class="fas fa-fw fa-heartbeat text-body-secondary mt-1"></i><span>See how it's doing - versions, updates, cron, backups, errors, and who holds its seats. No client data leaves here.</span></li>
<li class="list-group-item d-flex gap-2"><i class="fas fa-fw fa-heartbeat text-body-secondary mt-1"></i><span>See how it's doing - versions, updates, cron, backups, errors, who holds its seats and how much it's used. No client data leaves here.</span></li>
<li class="list-group-item d-flex gap-2"><i class="fas fa-fw fa-download text-body-secondary mt-1"></i><span>Update it, by hand or automatically - and then updates come only from there.</span></li>
<li class="list-group-item d-flex gap-2"><i class="fas fa-fw fa-users text-body-secondary mt-1"></i><span>Set how many agents it can have, and its white-label key.</span></li>
<li class="list-group-item d-flex gap-2"><i class="fas fa-fw fa-headset text-body-secondary mt-1"></i><span>Sign in as support, for two hours at a time - each time in the audit log, and you're notified.</span></li>
@@ -197,6 +197,7 @@ $lock = '<i class="fas fa-fw fa-lock text-body-secondary ms-1" title="Set by ' .
<li>how the last update went, whether cron is running, and when the last backup was</li>
<li>how many errors were logged today and the categories they were logged under - never what they say - how many clients it has, and its storage</li>
<li>its agents and the invites waiting - name, email, role, whether they sign in with MFA and when they last signed in</li>
<li>how much it's used each day - how many tickets were opened and resolved, replies, invoices, payments, sign-ins and emails sent and failed, and API calls under each key's name - never what any of them say</li>
</ul>
<p class="mb-0 text-body-secondary">No tickets, documents, credentials, invoices or contacts.</p>
</div>

View File

@@ -411,7 +411,7 @@ if ($error_count > 0) {
return $values ? array_sum($values) : null;
};
$backups = array_filter(array_column($week, 'backup'), function ($v) { return $v !== null; });
$history_bars[] = [
$bucket = [
'label' => $week[0]['label'],
'title' => $week[0]['label'] . ' - ' . end($week)['label'],
'errors' => $sum('errors'),
@@ -420,6 +420,12 @@ if ($error_count > 0) {
'made' => count(array_keys($backups, 'ok', true)),
'heard' => count($backups),
];
// Usage adds up over the week - but agents signed in is the most on any one day of it
foreach (MANAGED_USAGE_FIGURES as $figure) {
$values = array_filter(array_column($week, $figure), function ($v) { return $v !== null; });
$bucket[$figure] = !$values ? null : ($figure === 'agent_signins' ? max($values) : array_sum($values));
}
$history_bars[] = $bucket;
}
}
$bar_labels = array_column($history_bars, 'label');
@@ -556,6 +562,70 @@ if ($error_count > 0) {
echo $history_block('Backups', $figure, $strip);
?>
</div>
<?php
// How much it's used - counts only, as it reports them, by its own date
$usage = $history['usage'];
$total = function ($figure) use ($usage) {
return number_format(intval($usage[$figure] ?? 0));
};
$bars = function ($figure) use ($history_bars) {
return array_column($history_bars, $figure);
};
?>
<div class="d-flex flex-wrap align-items-baseline gap-2 border-top pt-3 mt-4 mb-3">
<h4 class="h6 mb-0">Usage</h4>
<span class="small text-body-secondary">How much it's used - counts only, by its own date</span>
</div>
<?php if ($usage === null) { ?>
<div class="small text-body-secondary" data-managed-usage-none>Its usage shows once it runs a version that counts it.</div>
<?php } else { ?>
<div class="row g-4" data-managed-usage>
<?php
// Tickets opened and resolved
$figure = $total('tickets_opened') . ' opened, ' . $total('tickets_resolved') . ' resolved' . (intval($usage['tickets_email'] ?? 0) ? ' - ' . $total('tickets_email') . ' came in by email' : '');
echo $history_block('Tickets', $figure, reportChart(['type' => 'bar', 'labels' => $bar_labels, 'format' => 'count', 'series' => [
['label' => "Opened$bar_period", 'data' => $bars('tickets_opened'), 'tone' => 'accent'],
['label' => "Resolved$bar_period", 'data' => $bars('tickets_resolved'), 'tone' => 'muted'],
]], 'sm', 'Tickets opened and resolved'));
// Agents' replies
echo $history_block('Replies', $total('replies') . ' replies and notes by agents', reportChart(['type' => 'bar', 'labels' => $bar_labels, 'format' => 'count', 'legend' => false,
'series' => [['label' => "Replies and notes$bar_period", 'data' => $bars('replies'), 'tone' => 'accent']]], 'sm', 'Replies by agents'));
// Who signs in - agents each day, against the agents it has
$figure = ($history['agents_30d'] !== null ? $history['agents_30d'] . ($history_now ? ' of ' . intval($history_now['agents']) : '') . ' agents signed in over the last 30 days' : 'Agents who signed in')
. ' - ' . $total('portal_signins') . ' client portal sign-ins';
echo $history_block('Signing in', $figure, reportChart(['type' => 'bar', 'labels' => $bar_labels, 'format' => 'count', 'legend' => false,
'series' => [['label' => $history_weekly ? 'Most agents signed in on a day that week' : 'Agents signed in', 'data' => $bars('agent_signins'), 'tone' => 'accent']]], 'sm', 'Agents signed in'));
// Invoices made and payments recorded
echo $history_block('Billing', $total('invoices') . ' invoices made, ' . $total('payments') . ' payments recorded', reportChart(['type' => 'bar', 'labels' => $bar_labels, 'format' => 'count', 'series' => [
['label' => "Invoices$bar_period", 'data' => $bars('invoices'), 'tone' => 'accent'],
['label' => "Payments$bar_period", 'data' => $bars('payments'), 'tone' => 'muted'],
]], 'sm', 'Invoices and payments'));
// Email sent, and what failed
echo $history_block('Email', $total('emails_sent') . ' sent - ' . $total('emails_failed') . ' failed', reportChart(['type' => 'bar', 'labels' => $bar_labels, 'format' => 'count', 'series' => [
['label' => "Sent$bar_period", 'data' => $bars('emails_sent'), 'tone' => 'accent'],
['label' => "Failed$bar_period", 'data' => $bars('emails_failed'), 'tone' => 'danger'],
]], 'sm', 'Email sent and failed'));
// API calls, and the keys making them
$keys_html = '';
if ($history['api_keys']) {
$named = [];
foreach (array_slice($history['api_keys'], 0, 5, true) as $name => $calls) {
$named[] = escapeHtml($name) . ' <span class="text-body-secondary">' . number_format($calls) . '</span>';
}
$more = count($history['api_keys']) - count($named);
$keys_html = '<div class="small mt-2">' . implode(' &middot; ', $named) . ($more > 0 ? " <span class=\"text-body-secondary\">and $more more</span>" : '') . '</div>';
}
echo $history_block('API calls', $total('api_calls') . ' calls', reportChart(['type' => 'bar', 'labels' => $bar_labels, 'format' => 'count', 'legend' => false,
'series' => [['label' => "Calls$bar_period", 'data' => $bars('api_calls'), 'tone' => 'accent']]], 'sm', 'API calls') . $keys_html);
?>
</div>
<?php } ?>
</div>
<?php } ?>
</div>

View File

@@ -40,6 +40,12 @@ $sortable_columns = [
'managed_instance_last_checkin_at' => "managed_instance_last_checkin_at IS NULL, managed_instance_last_checkin_at $order, managed_instance_name ASC",
'client_name' => "client_name IS NULL, client_name $order, managed_instance_name ASC",
];
// Its use over the last 30 days - tickets opened, from its history (managedStatsRecord())
$usage_ready = managedUsageReady();
if ($usage_ready) {
$sortable_columns['usage_tickets'] = "(SELECT COALESCE(SUM(managed_stat_tickets_opened), 0) FROM managed_instance_stats
WHERE managed_stat_instance_id = managed_instance_id AND managed_stat_date > CURDATE() - INTERVAL 30 DAY) $order, managed_instance_name ASC";
}
if (!isset($sortable_columns[$sort])) {
$sort = 'managed_instance_name';
}
@@ -101,12 +107,23 @@ $csrf = $_SESSION['csrf_token'];
<th class="d-none d-md-table-cell"><?= sortLink('managed_instance_version', 'Version') ?></th>
<th class="d-none d-lg-table-cell">Health</th>
<th class="d-none d-xl-table-cell">Agents</th>
<?php if ($usage_ready) { ?><th class="d-none d-xl-table-cell"><?= sortLink('usage_tickets', 'Last 30 days') ?></th><?php } ?>
<th class="d-none d-sm-table-cell"><?= sortLink('managed_instance_last_checkin_at', 'Last check-in') ?></th>
<th></th>
</tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_assoc($sql)) {
<?php
// Each instance's last 30 days - tickets opened and API calls, and whether it counted any
$usage_30d = [];
if ($usage_ready) {
$result = mysqli_query($mysqli, "SELECT managed_stat_instance_id, SUM(managed_stat_tickets_opened), SUM(managed_stat_api_calls), COUNT(managed_stat_tickets_opened)
FROM managed_instance_stats WHERE managed_stat_date > CURDATE() - INTERVAL 30 DAY GROUP BY managed_stat_instance_id");
while ($usage_row = mysqli_fetch_row($result)) {
$usage_30d[intval($usage_row[0])] = ['tickets' => intval($usage_row[1]), 'api_calls' => intval($usage_row[2]), 'counted' => intval($usage_row[3]) > 0];
}
}
while ($row = mysqli_fetch_assoc($sql)) {
$instance_id = intval($row['managed_instance_id']);
$name = escapeHtml($row['managed_instance_name']);
$health = managedInstanceHealth($row);
@@ -155,7 +172,7 @@ $csrf = $_SESSION['csrf_token'];
<span class="text-body-secondary">-</span>
<?php } ?>
</td>
<td class="d-none d-lg-table-cell small text-nowrap">
<td class="d-none d-lg-table-cell small" style="min-width: 14rem">
<?php if ($health['state'] === 'pending') { ?>
<span class="text-body-secondary">-</span>
<?php } elseif (!$health['issues']) { ?>
@@ -175,6 +192,26 @@ $csrf = $_SESSION['csrf_token'];
<span class="text-body-secondary">-</span>
<?php } ?>
</td>
<?php if ($usage_ready) {
$usage = $usage_30d[$instance_id] ?? null;
// The last time any of its agents signed in, from its agents list
$signed_in = max(array_map(function ($agent) {
return is_array($agent) && empty($agent['invited']) && !empty($agent['last_login']) ? strtotime((string) $agent['last_login']) : 0;
}, (array) ($agents['list'] ?? [])) ?: [0]);
$agents_30d = isset($report['usage']['agents_30d']) ? intval($report['usage']['agents_30d']) : null; ?>
<td class="d-none d-xl-table-cell small text-nowrap" data-managed-usage-30d>
<?php if (!$usage || !$usage['counted']) { ?>
<span class="text-body-secondary" title="It shows once it runs a version that counts it">-</span>
<?php } else { ?>
<div class="fw-semibold"><?= number_format($usage['tickets']) ?> ticket<?= $usage['tickets'] === 1 ? '' : 's' ?></div>
<?php if ($agents_30d !== null) { ?>
<div class="text-body-secondary"><?= $agents_30d ?><?= !empty($agents['active']) ? ' of ' . intval($agents['active']) : '' ?> agent<?= $agents_30d === 1 && empty($agents['active']) ? '' : 's' ?> signed in</div>
<?php } ?>
<?php if ($signed_in) { ?><div class="text-body-secondary" title="<?= date('M j, Y g:i A', $signed_in) ?>">Last sign-in <?= escapeHtml(timeAgo(date('Y-m-d H:i:s', $signed_in))) ?></div><?php } ?>
<?php if ($usage['api_calls']) { ?><div class="text-body-secondary"><?= number_format($usage['api_calls']) ?> API calls</div><?php } ?>
<?php } ?>
</td>
<?php } ?>
<td class="d-none d-sm-table-cell small text-nowrap"><?= $last_html ?></td>
<td class="text-end">
<div class="dropdown dropstart">