Skip to content

Commit 83f7802

Browse files
hamza221kesselb
authored andcommitted
fix: get actual number of active users
Signed-off-by: Hamza Mahjoubi <hamzamahjoubi221@gmail.com>
1 parent f12c594 commit 83f7802

2 files changed

Lines changed: 72 additions & 49 deletions

File tree

lib/SessionStatistics.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
namespace OCA\ServerInfo;
1212

1313
use OCP\AppFramework\Utility\ITimeFactory;
14+
use OCP\DB\QueryBuilder\IQueryBuilder;
1415
use OCP\IDBConnection;
1516

1617
/**
@@ -58,18 +59,21 @@ public function getSessionStatistics(): array {
5859
* @param int $offset seconds
5960
*/
6061
private function getNumberOfActiveUsers(int $offset): int {
61-
$query = $this->connection->getQueryBuilder();
62-
$query->select('uid')
63-
->from('authtoken')
64-
->where($query->expr()->gte(
65-
'last_activity',
66-
$query->createNamedParameter($this->timeFactory->getTime() - $offset)
67-
))->groupBy('uid');
62+
$queryBuilder = $this->connection->getQueryBuilder();
63+
$queryBuilder->select($queryBuilder->func()->count('userid'))
64+
->from('preferences')
65+
->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('login')))
66+
->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('lastLogin')))
67+
->andwhere($queryBuilder->expr()->gte(
68+
$queryBuilder->expr()->castColumn('configvalue', IQueryBuilder::PARAM_INT),
69+
$queryBuilder->createNamedParameter($this->timeFactory->getTime() - $offset, IQueryBuilder::PARAM_INT),
70+
IQueryBuilder::PARAM_INT,
71+
));
6872

69-
$result = $query->executeQuery();
70-
$activeUsers = $result->fetchAll();
73+
$result = $queryBuilder->executeQuery();
74+
$activeUsers = (int)$result->fetchOne();
7175
$result->closeCursor();
7276

73-
return count($activeUsers);
77+
return $activeUsers;
7478
}
7579
}

tests/lib/SessionStatisticsTest.php

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,15 @@ class SessionStatisticsTest extends TestCase {
2727
private ITimeFactory&MockObject $timeFactory;
2828
private IDBConnection $connection;
2929
private SessionStatistics $instance;
30-
private const TABLE = 'authtoken';
30+
private int $currentTime;
31+
private const TABLE = 'preferences';
3132
private const OFFSET_5MIN = 300;
3233
private const OFFSET_1HOUR = 3600;
3334
private const OFFSET_1DAY = 86400;
34-
private const OFFSET_7DAYS = 604800;
3535
private const OFFSET_1MONTH = 2592000;
3636
private const OFFSET_3MONTHS = 7776000;
3737
private const OFFSET_6MONTHS = 15552000;
3838
private const OFFSET_1YEAR = 31536000;
39-
private const CURRENT_TIME = 100000000;
4039

4140

4241
protected function setUp(): void {
@@ -47,54 +46,74 @@ protected function setUp(): void {
4746
$this->connection = Server::get(IDBConnection::class);
4847

4948
$this->instance = new SessionStatistics($this->connection, $this->timeFactory);
49+
50+
// when running the tests locally, you may have other lastLogin values in the database.
51+
// using a timestamp in the future to workaround.
52+
$this->currentTime = time() + (400 * 24 * 60 * 60);
53+
54+
$this->removeDummyValues();
55+
$this->addDummyValues();
56+
}
57+
58+
protected function tearDown(): void {
59+
$this->removeDummyValues();
60+
}
61+
62+
protected function removeDummyValues(): void {
63+
$qb = $this->connection->getQueryBuilder();
64+
$qb->delete('preferences')
65+
->where($qb->expr()->eq('appid', $qb->createNamedParameter('login')))
66+
->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lastLogin')))
67+
->andWhere($qb->expr()->like('userid', $qb->createNamedParameter('session-statistics-test%')));
68+
$qb->executeStatement();
5069
}
5170

5271
private function addDummyValues(): void {
53-
$this->addDummyValuesWithLastLogin(self::CURRENT_TIME - self::OFFSET_5MIN + 1, 10);
54-
$this->addDummyValuesWithLastLogin(self::CURRENT_TIME - self::OFFSET_1HOUR + 1, 20);
55-
$this->addDummyValuesWithLastLogin(self::CURRENT_TIME - self::OFFSET_1DAY + 1, 30);
56-
$this->addDummyValuesWithLastLogin(self::CURRENT_TIME - self::OFFSET_7DAYS + 1, 40);
57-
$this->addDummyValuesWithLastLogin(self::CURRENT_TIME - self::OFFSET_1MONTH + 1, 50);
58-
$this->addDummyValuesWithLastLogin(self::CURRENT_TIME - self::OFFSET_3MONTHS + 1, 60);
59-
$this->addDummyValuesWithLastLogin(self::CURRENT_TIME - self::OFFSET_6MONTHS + 1, 70);
60-
$this->addDummyValuesWithLastLogin(self::CURRENT_TIME - self::OFFSET_1YEAR + 1, 80);
72+
$this->addDummyValuesWithLastLogin(self::OFFSET_5MIN, 10);
73+
$this->addDummyValuesWithLastLogin(self::OFFSET_5MIN, 11);
74+
$this->addDummyValuesWithLastLogin(self::OFFSET_1HOUR, 20);
75+
$this->addDummyValuesWithLastLogin(self::OFFSET_1HOUR, 21);
76+
$this->addDummyValuesWithLastLogin(self::OFFSET_1HOUR, 22);
77+
$this->addDummyValuesWithLastLogin(self::OFFSET_1DAY, 30);
78+
$this->addDummyValuesWithLastLogin(self::OFFSET_1MONTH, 50);
79+
$this->addDummyValuesWithLastLogin(self::OFFSET_3MONTHS, 60);
80+
$this->addDummyValuesWithLastLogin(self::OFFSET_6MONTHS, 70);
81+
$this->addDummyValuesWithLastLogin(self::OFFSET_1YEAR, 80);
82+
$this->addDummyValuesWithLastLogin(self::OFFSET_1YEAR, 81);
83+
$this->addDummyValuesWithLastLogin(self::OFFSET_1YEAR, 82);
6184
}
6285

63-
private function addDummyValuesWithLastLogin($lastActivity, $numOfEntries): void {
64-
for ($i = 0; $i < $numOfEntries; $i++) {
65-
$query = $this->connection->getQueryBuilder();
66-
$query->insert(self::TABLE)
67-
->values(
68-
[
69-
'uid' => $query->createNamedParameter('user-' . ($numOfEntries + $i % 2)),
70-
'login_name' => $query->createNamedParameter('user-' . ($numOfEntries + $i % 2)),
71-
'password' => $query->createNamedParameter('password'),
72-
'name' => $query->createNamedParameter('user agent'),
73-
'token' => $query->createNamedParameter('token-' . $this->getUniqueID()),
74-
'type' => $query->createNamedParameter(0),
75-
'last_activity' => $query->createNamedParameter($lastActivity),
76-
'last_check' => $query->createNamedParameter($lastActivity),
77-
]
78-
);
79-
$query->executeStatement();
80-
}
86+
private function addDummyValuesWithLastLogin(int $offset, int $id): void {
87+
$query = $this->connection->getQueryBuilder();
88+
$query->insert(self::TABLE)
89+
->values(
90+
[
91+
'userid' => $query->createNamedParameter("session-statistics-test$id"),
92+
'appid' => $query->createNamedParameter('login'),
93+
'configkey' => $query->createNamedParameter('lastLogin'),
94+
'configvalue' => $query->createNamedParameter((string)($this->currentTime - $offset + 1)),
95+
'lazy' => $query->createNamedParameter(0),
96+
'type' => $query->createNamedParameter(0),
97+
'flags' => $query->createNamedParameter(0),
98+
]
99+
);
100+
$query->executeStatement();
81101
}
82102

83103
public function testGetSessionStatistics() {
84-
$this->addDummyValues();
85104
$this->timeFactory->expects($this->any())->method('getTime')
86-
->willReturn(self::CURRENT_TIME);
105+
->willReturn($this->currentTime);
87106

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

90109
$this->assertSame(8, count($result));
91-
$this->assertSame(2, $result['last5minutes']);
92-
$this->assertSame(4, $result['last1hour']);
93-
$this->assertSame(6, $result['last24hours']);
94-
$this->assertSame(8, $result['last7days']);
95-
$this->assertSame(10, $result['last1month']);
96-
$this->assertSame(12, $result['last3months']);
97-
$this->assertSame(14, $result['last6months']);
98-
$this->assertSame(16, $result['lastyear']);
110+
$this->assertSame(2, $result['last5minutes']); // 2 users in last 5 minutes
111+
$this->assertSame(5, $result['last1hour']); // 2 + 3 users in last hour
112+
$this->assertSame(6, $result['last24hours']); // 2 + 3 + 1 users in last day
113+
$this->assertSame(6, $result['last7days']); // 2 + 3 + 1 + 0 users in last week
114+
$this->assertSame(7, $result['last1month']); // 2 + 3 + 1 + 0 + 1 users in last month
115+
$this->assertSame(8, $result['last3months']); // 2 + 3 + 1 + 0 + 1 + 1 users in last 3 months
116+
$this->assertSame(9, $result['last6months']); // 2 + 3 + 1 + 0 + 1 + 1 + 1 users in last 6 months
117+
$this->assertSame(12, $result['lastyear']); // 2 + 3 + 1 + 0 + 1 + 1 + 1 + 3 users in last year
99118
}
100119
}

0 commit comments

Comments
 (0)