Skip to content

Commit

Permalink
feat(active users): add more active user statistics intervals and imp…
Browse files Browse the repository at this point in the history
…rove presentation

Signed-off-by: Thomas Citharel <[email protected]>
  • Loading branch information
tcitworld authored and kesselb committed Oct 24, 2024
1 parent 03d27c4 commit 96de452
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 43 deletions.
32 changes: 32 additions & 0 deletions css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,35 @@
align-items: center;
height: 24px;
}

.active-users-wrapper {
flex: 1;
display: flex;
gap: 0 1rem;
flex-wrap: wrap;
align-items: center;
justify-content: center;
}

.active-users-box {
padding: 0.25rem 1rem;
flex: 1;
min-width: 150px;
border-left: 2px solid var(--color-border);
}

@media (width <= 1280px) {
.active-users-box {
padding: 1rem 0.5rem;
border: none;
text-align: center;
}
}

.active-users-box:first-child {
border: none;
}

.active-users-box .info {
font-size: 2rem;
}
10 changes: 10 additions & 0 deletions lib/SessionStatistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class SessionStatistics {
private const OFFSET_5MIN = 300;
private const OFFSET_1HOUR = 3600;
private const OFFSET_1DAY = 86400;
private const OFFSET_7DAYS = 604800;
private const OFFSET_1MONTH = 2592000;
private const OFFSET_3MONTHS = 7776000;
private const OFFSET_6MONTHS = 15552000;
private const OFFSET_1YEAR = 31536000;

private IDBConnection $connection;
private ITimeFactory $timeFactory;
Expand All @@ -39,6 +44,11 @@ public function getSessionStatistics(): array {
'last5minutes' => $this->getNumberOfActiveUsers(self::OFFSET_5MIN),
'last1hour' => $this->getNumberOfActiveUsers(self::OFFSET_1HOUR),
'last24hours' => $this->getNumberOfActiveUsers(self::OFFSET_1DAY),
'last7days' => $this->getNumberOfActiveUsers(self::OFFSET_7DAYS),
'last1month' => $this->getNumberOfActiveUsers(self::OFFSET_1MONTH),
'last3months' => $this->getNumberOfActiveUsers(self::OFFSET_3MONTHS),
'last6months' => $this->getNumberOfActiveUsers(self::OFFSET_6MONTHS),
'lastyear' => $this->getNumberOfActiveUsers(self::OFFSET_1YEAR),
];
}

Expand Down
38 changes: 25 additions & 13 deletions templates/settings-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,27 +227,39 @@ function FormatMegabytes(int $byte): string {

<div class="col col-12">
<div class="row">
<div class="col col-4 col-l-6 col-m-12">
<div class="col">
<div class="infobox">
<div class="interface-wrapper">
<?php if ($_['storage']['num_users'] > 0) : ?>
<?php p($l->t('Total users:')); ?>
<span class="info"><?php p($_['storage']['num_users']); ?></span><br>
<div class="interface-wrapper active-users-wrapper">
<?php if ($_['activeUsers']['last1hour'] > 0) : ?>
<div class="active-users-box">
<?php p($l->t('Last hour')); ?><br>
<span class="info"><?php p($_['activeUsers']['last1hour']) ?></span><br>
<em><?php p($l->t('%s%% of all users', [round($_['activeUsers']['last1hour'] * 100 / $_['storage']['num_users'], 1)])) ?></em>
</div>
<?php endif; ?>

<?php if ($_['activeUsers']['last24hours'] > 0) : ?>
<?php p($l->t('24 hours:')); ?>
<span class="info"><?php p($_['activeUsers']['last24hours']) ?></span><br>
<div class="active-users-box">
<?php p($l->t('Last 24 Hours')); ?><br>
<span class="info"><?php p($_['activeUsers']['last24hours']) ?></span><br>
<em><?php p($l->t('%s%% of all users', [round($_['activeUsers']['last24hours'] * 100 / $_['storage']['num_users'], 1)])) ?></em>
</div>
<?php endif; ?>

<?php if ($_['activeUsers']['last1hour'] > 0) : ?>
<?php p($l->t('1 hour:')); ?>
<span class="info"><?php p($_['activeUsers']['last1hour']) ?></span><br>
<?php if ($_['activeUsers']['last7days'] > 0) : ?>
<div class="active-users-box">
<?php p($l->t('Last 7 Days')); ?><br>
<span class="info"><?php p($_['activeUsers']['last7days']) ?></span><br>
<em><?php p($l->t('%s%% of all users', [round($_['activeUsers']['last7days'] * 100 / $_['storage']['num_users'], 1)])) ?></em>
</div>
<?php endif; ?>

<?php if ($_['activeUsers']['last5minutes'] > 0) : ?>
<?php p($l->t('5 mins:')); ?>
<span class="info"><?php p($_['activeUsers']['last5minutes']) ?></span><br>
<?php if ($_['activeUsers']['last1month'] > 0) : ?>
<div class="active-users-box">
<?php p($l->t('Last 30 Days')); ?><br>
<span class="info"><?php p($_['activeUsers']['last1month']) ?></span><br>
<em><?php p($l->t('%s%% of all users', [round($_['activeUsers']['last1month'] * 100 / $_['storage']['num_users'], 1)])) ?></em>
</div>
<?php endif; ?>
</div>
</div>
Expand Down
67 changes: 37 additions & 30 deletions tests/lib/SessionStatisticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use OCA\ServerInfo\SessionStatistics;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IDBConnection;
use OCP\Server;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;

/**
Expand All @@ -22,72 +24,77 @@
* @package OCA\ServerInfo\Tests
*/
class SessionStatisticsTest extends TestCase {
/** @var ITimeFactory | \PHPUnit_Framework_MockObject_MockObject */
private $timeFactory;
private ITimeFactory&MockObject $timeFactory;
private IDBConnection $connection;
private SessionStatistics $instance;
private const TABLE = 'authtoken';
private const OFFSET_5MIN = 300;
private const OFFSET_1HOUR = 3600;
private const OFFSET_1DAY = 86400;
private const OFFSET_7DAYS = 604800;
private const OFFSET_1MONTH = 2592000;
private const OFFSET_3MONTHS = 7776000;
private const OFFSET_6MONTHS = 15552000;
private const OFFSET_1YEAR = 31536000;
private const CURRENT_TIME = 100000000;

/** @var IDBConnection */
private $connection;

/** @var SessionStatistics */
private $instance;

private $table = 'authtoken';

private $offset5Minutes = 300;

private $offset1Hour = 3600;

private $offset1Day = 86400;

private $currentTime = 100000;

protected function setUp(): void {
parent::setUp();

$this->timeFactory = $this->getMockBuilder('OCP\AppFramework\Utility\ITimeFactory')
->disableOriginalConstructor()->getMock();
$this->timeFactory = $this->createMock(ITimeFactory::class);

$this->connection = \OC::$server->getDatabaseConnection();
$this->connection = Server::get(IDBConnection::class);

$this->instance = new SessionStatistics($this->connection, $this->timeFactory);
}

private function addDummyValues() {
$this->addDummyValuesWithLastLogin($this->currentTime - $this->offset5Minutes + 1, 10);
$this->addDummyValuesWithLastLogin($this->currentTime - $this->offset1Hour + 1, 20);
$this->addDummyValuesWithLastLogin($this->currentTime - $this->offset1Day + 1, 30);
private function addDummyValues(): void {
$this->addDummyValuesWithLastLogin(self::CURRENT_TIME - self::OFFSET_5MIN + 1, 10);
$this->addDummyValuesWithLastLogin(self::CURRENT_TIME - self::OFFSET_1HOUR + 1, 20);
$this->addDummyValuesWithLastLogin(self::CURRENT_TIME - self::OFFSET_1DAY + 1, 30);
$this->addDummyValuesWithLastLogin(self::CURRENT_TIME - self::OFFSET_7DAYS + 1, 40);
$this->addDummyValuesWithLastLogin(self::CURRENT_TIME - self::OFFSET_1MONTH + 1, 50);
$this->addDummyValuesWithLastLogin(self::CURRENT_TIME - self::OFFSET_3MONTHS + 1, 60);
$this->addDummyValuesWithLastLogin(self::CURRENT_TIME - self::OFFSET_6MONTHS + 1, 70);
$this->addDummyValuesWithLastLogin(self::CURRENT_TIME - self::OFFSET_1YEAR + 1, 80);
}

private function addDummyValuesWithLastLogin($lastActivity, $numOfEntries) {
private function addDummyValuesWithLastLogin($lastActivity, $numOfEntries): void {
for ($i = 0; $i < $numOfEntries; $i++) {
$query = $this->connection->getQueryBuilder();
$query->insert($this->table)
$query->insert(self::TABLE)
->values(
[
'uid' => $query->createNamedParameter('user-' . ($numOfEntries + $i % 2)),
'login_name' => $query->createNamedParameter('user-' . ($numOfEntries + $i % 2)),
'password' => $query->createNamedParameter('password'),
'name' => $query->createNamedParameter('user agent'),
'token' => $query->createNamedParameter('token-' . ($i + $numOfEntries * 10)),
'token' => $query->createNamedParameter('token-' . $this->getUniqueID()),
'type' => $query->createNamedParameter(0),
'last_activity' => $query->createNamedParameter($lastActivity),
'last_check' => $query->createNamedParameter($lastActivity),
]
);
$query->execute();
$query->executeStatement();
}
}

public function testGetSessionStatistics() {
$this->addDummyValues();
$this->timeFactory->expects($this->any())->method('getTime')
->willReturn($this->currentTime);
->willReturn(self::CURRENT_TIME);

$result = $this->instance->getSessionStatistics();

$this->assertSame(3, count($result));
$this->assertSame(8, count($result));
$this->assertSame(2, $result['last5minutes']);
$this->assertSame(4, $result['last1hour']);
$this->assertSame(6, $result['last24hours']);
$this->assertSame(8, $result['last7days']);
$this->assertSame(10, $result['last1month']);
$this->assertSame(12, $result['last3months']);
$this->assertSame(14, $result['last6months']);
$this->assertSame(16, $result['lastyear']);
}
}

0 comments on commit 96de452

Please sign in to comment.