From 92a71f8c27de8c1abd182be9f8bef441e1e60166 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 6 Feb 2022 20:03:19 +0000 Subject: [PATCH] Store wekk/month&halfyear usage / number of posts --- database.sql | 7 ++- doc/database/db_gserver.md | 55 +++++++++-------- src/Model/GServer.php | 105 ++++++++++++++++++++++++++++++++ src/Module/Admin/Federation.php | 1 + static/dbstructure.config.php | 7 ++- 5 files changed, 148 insertions(+), 27 deletions(-) diff --git a/database.sql b/database.sql index 692b5deb7..ed136e620 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ -- ------------------------------------------ -- Friendica 2021.12-rc (Siberian Iris) --- DB_UPDATE_VERSION 1449 +-- DB_UPDATE_VERSION 1450 -- ------------------------------------------ @@ -16,6 +16,11 @@ CREATE TABLE IF NOT EXISTS `gserver` ( `info` text COMMENT '', `register_policy` tinyint NOT NULL DEFAULT 0 COMMENT '', `registered-users` int unsigned NOT NULL DEFAULT 0 COMMENT 'Number of registered users', + `active-week-users` int unsigned COMMENT 'Number of active users in the last week', + `active-month-users` int unsigned COMMENT 'Number of active users in the last month', + `active-halfyear-users` int unsigned COMMENT 'Number of active users in the last six month', + `local-posts` int unsigned COMMENT 'Number of local posts', + `local-comments` int unsigned COMMENT 'Number of local comments', `directory-type` tinyint DEFAULT 0 COMMENT 'Type of directory service (Poco, Mastodon)', `poco` varchar(255) NOT NULL DEFAULT '' COMMENT '', `noscrape` varchar(255) NOT NULL DEFAULT '' COMMENT '', diff --git a/doc/database/db_gserver.md b/doc/database/db_gserver.md index 905602b3b..d75c7335b 100644 --- a/doc/database/db_gserver.md +++ b/doc/database/db_gserver.md @@ -6,31 +6,36 @@ Global servers Fields ------ -| Field | Description | Type | Null | Key | Default | Extra | -| ---------------- | -------------------------------------------------- | ---------------- | ---- | --- | ------------------- | -------------- | -| id | sequential ID | int unsigned | NO | PRI | NULL | auto_increment | -| url | | varchar(255) | NO | | | | -| nurl | | varchar(255) | NO | | | | -| version | | varchar(255) | NO | | | | -| site_name | | varchar(255) | NO | | | | -| info | | text | YES | | NULL | | -| register_policy | | tinyint | NO | | 0 | | -| registered-users | Number of registered users | int unsigned | NO | | 0 | | -| directory-type | Type of directory service (Poco, Mastodon) | tinyint | YES | | 0 | | -| poco | | varchar(255) | NO | | | | -| noscrape | | varchar(255) | NO | | | | -| network | | char(4) | NO | | | | -| protocol | The protocol of the server | tinyint unsigned | YES | | NULL | | -| platform | | varchar(255) | NO | | | | -| relay-subscribe | Has the server subscribed to the relay system | boolean | NO | | 0 | | -| relay-scope | The scope of messages that the server wants to get | varchar(10) | NO | | | | -| detection-method | Method that had been used to detect that server | tinyint unsigned | YES | | NULL | | -| created | | datetime | NO | | 0001-01-01 00:00:00 | | -| last_poco_query | | datetime | YES | | 0001-01-01 00:00:00 | | -| last_contact | Last successful connection request | datetime | YES | | 0001-01-01 00:00:00 | | -| last_failure | Last failed connection request | datetime | YES | | 0001-01-01 00:00:00 | | -| failed | Connection failed | boolean | YES | | NULL | | -| next_contact | Next connection request | datetime | YES | | 0001-01-01 00:00:00 | | +| Field | Description | Type | Null | Key | Default | Extra | +| --------------------- | -------------------------------------------------- | ---------------- | ---- | --- | ------------------- | -------------- | +| id | sequential ID | int unsigned | NO | PRI | NULL | auto_increment | +| url | | varchar(255) | NO | | | | +| nurl | | varchar(255) | NO | | | | +| version | | varchar(255) | NO | | | | +| site_name | | varchar(255) | NO | | | | +| info | | text | YES | | NULL | | +| register_policy | | tinyint | NO | | 0 | | +| registered-users | Number of registered users | int unsigned | NO | | 0 | | +| active-week-users | Number of active users in the last week | int unsigned | YES | | NULL | | +| active-month-users | Number of active users in the last month | int unsigned | YES | | NULL | | +| active-halfyear-users | Number of active users in the last six month | int unsigned | YES | | NULL | | +| local-posts | Number of local posts | int unsigned | YES | | NULL | | +| local-comments | Number of local comments | int unsigned | YES | | NULL | | +| directory-type | Type of directory service (Poco, Mastodon) | tinyint | YES | | 0 | | +| poco | | varchar(255) | NO | | | | +| noscrape | | varchar(255) | NO | | | | +| network | | char(4) | NO | | | | +| protocol | The protocol of the server | tinyint unsigned | YES | | NULL | | +| platform | | varchar(255) | NO | | | | +| relay-subscribe | Has the server subscribed to the relay system | boolean | NO | | 0 | | +| relay-scope | The scope of messages that the server wants to get | varchar(10) | NO | | | | +| detection-method | Method that had been used to detect that server | tinyint unsigned | YES | | NULL | | +| created | | datetime | NO | | 0001-01-01 00:00:00 | | +| last_poco_query | | datetime | YES | | 0001-01-01 00:00:00 | | +| last_contact | Last successful connection request | datetime | YES | | 0001-01-01 00:00:00 | | +| last_failure | Last failed connection request | datetime | YES | | 0001-01-01 00:00:00 | | +| failed | Connection failed | boolean | YES | | NULL | | +| next_contact | Next connection request | datetime | YES | | 0001-01-01 00:00:00 | | Indexes ------------ diff --git a/src/Model/GServer.php b/src/Model/GServer.php index 90b023f6f..c4138cc2d 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -377,6 +377,12 @@ class GServer if (empty($nodeinfo['network']) || in_array($nodeinfo['network'], [Protocol::DFRN, Protocol::ZOT])) { if (!empty($nodeinfo['detection-method'])) { $serverdata['detection-method'] = $nodeinfo['detection-method']; + + foreach (['registered-users', 'active_users_monthly', 'active-halfyear-users', 'local-posts'] as $field) { + if (!empty($nodeinfo[$field])) { + $serverdata[$field] = $nodeinfo[$field]; + } + } } // Fetch the landing page, possibly it reveals some data @@ -500,6 +506,10 @@ class GServer $serverdata = self::detectNetworkViaContacts($url, $serverdata); } + if ($serverdata['network'] == Protocol::ACTIVITYPUB) { + $serverdata = self::fetchWeeklyUsage($url, $serverdata); + } + $serverdata['registered-users'] = $serverdata['registered-users'] ?? 0; // On an active server there has to be at least a single user @@ -687,6 +697,21 @@ class GServer } } + if (!empty($data['total_users'])) { + $serverdata['registered-users'] = max($data['total_users'], 1); + } + + if (!empty($data['active_users_monthly'])) { + $serverdata['active-month-users'] = max($data['active_users_monthly'], 0); + } + + if (!empty($data['active_users_halfyear'])) { + $serverdata['active-halfyear-users'] = max($data['active_users_halfyear'], 0); + } + + if (!empty($data['local_posts'])) { + $serverdata['local-posts'] = max($data['local_posts'], 0); + } if (!empty($data['registrations_open'])) { $serverdata['register_policy'] = Register::OPEN; @@ -801,6 +826,22 @@ class GServer $server['registered-users'] = max($nodeinfo['usage']['users']['total'], 1); } + if (!empty($nodeinfo['usage']['users']['activeMonth'])) { + $server['active-month-users'] = max($nodeinfo['usage']['users']['activeMonth'], 0); + } + + if (!empty($nodeinfo['usage']['users']['activeHalfyear'])) { + $server['active-halfyear-users'] = max($nodeinfo['usage']['users']['activeHalfyear'], 0); + } + + if (!empty($nodeinfo['usage']['localPosts'])) { + $server['local-posts'] = max($nodeinfo['usage']['localPosts'], 0); + } + + if (!empty($nodeinfo['usage']['localComments'])) { + $server['local-comments'] = max($nodeinfo['usage']['localComments'], 0); + } + if (!empty($nodeinfo['protocols']['inbound']) && is_array($nodeinfo['protocols']['inbound'])) { $protocols = []; foreach ($nodeinfo['protocols']['inbound'] as $protocol) { @@ -878,6 +919,22 @@ class GServer $server['registered-users'] = max($nodeinfo['usage']['users']['total'], 1); } + if (!empty($nodeinfo['usage']['users']['activeMonth'])) { + $server['active-month-users'] = max($nodeinfo['usage']['users']['activeMonth'], 0); + } + + if (!empty($nodeinfo['usage']['users']['activeHalfyear'])) { + $server['active-halfyear-users'] = max($nodeinfo['usage']['users']['activeHalfyear'], 0); + } + + if (!empty($nodeinfo['usage']['localPosts'])) { + $server['local-posts'] = max($nodeinfo['usage']['localPosts'], 0); + } + + if (!empty($nodeinfo['usage']['localComments'])) { + $server['local-comments'] = max($nodeinfo['usage']['localComments'], 0); + } + if (!empty($nodeinfo['protocols'])) { $protocols = []; foreach ($nodeinfo['protocols'] as $protocol) { @@ -957,6 +1014,22 @@ class GServer $serverdata['registered-users'] = max($data['channels_total'], 1); } + if (!empty($data['channels_active_monthly'])) { + $serverdata['active-month-users'] = max($data['channels_active_monthly'], 0); + } + + if (!empty($data['channels_active_halfyear'])) { + $serverdata['active-halfyear-users'] = max($data['channels_active_halfyear'], 0); + } + + if (!empty($data['local_posts'])) { + $serverdata['local-posts'] = max($data['local_posts'], 0); + } + + if (!empty($data['local_comments'])) { + $serverdata['local-comments'] = max($data['local_comments'], 0); + } + if (!empty($data['register_policy'])) { switch ($data['register_policy']) { case 'REGISTER_OPEN': @@ -1208,6 +1281,38 @@ class GServer return $serverdata; } + private static function fetchWeeklyUsage(string $url, array $serverdata) { + $curlResult = DI::httpClient()->get($url . '/api/v1/instance/activity'); + + if (!$curlResult->isSuccess() || ($curlResult->getBody() == '')) { + return $serverdata; + } + + $data = json_decode($curlResult->getBody(), true); + if (empty($data)) { + return $serverdata; + } + + $current_week = []; + foreach ($data as $week) { + // Use only data from a full week + if (empty($week['week']) || (time() - $week['week']) < 7 * 24 * 60 * 60) { + continue; + } + + // Most likely the data is sorted correctly. But we better are safe than sorry + if (empty($current_week['week']) || ($current_week['week'] < $week['week'])) { + $current_week = $week; + } + } + + if (!empty($current_week['logins'])) { + $serverdata['active-week-users'] = max($current_week['logins'], 0); + } + + return $serverdata; + } + /** * Detects data from a given server url if it was a mastodon alike system * diff --git a/src/Module/Admin/Federation.php b/src/Module/Admin/Federation.php index 119196189..fbd782ad5 100644 --- a/src/Module/Admin/Federation.php +++ b/src/Module/Admin/Federation.php @@ -43,6 +43,7 @@ class Federation extends BaseAdmin 'diaspora' => ['name' => 'Diaspora', 'color' => '#a1a1a1'], // logo is black and white, makes a gray 'funkwhale' => ['name' => 'Funkwhale', 'color' => '#4082B4'], // From the homepage 'gnusocial' => ['name' => 'GNU Social/Statusnet', 'color' => '#a22430'], // dark red from the logo + 'hometown' => ['name' => 'Hometown', 'color' => '#1f70c1'], // Color from the Patreon page 'hubzilla' => ['name' => 'Hubzilla/Red Matrix', 'color' => '#43488a'], // blue from the logo 'lemmy' => ['name' => 'Lemmy', 'color' => '#00c853'], // Green from the page 'mastodon' => ['name' => 'Mastodon', 'color' => '#1a9df9'], // blue from the Mastodon logo diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index 39481348e..2e1a2191c 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -55,7 +55,7 @@ use Friendica\Database\DBA; if (!defined('DB_UPDATE_VERSION')) { - define('DB_UPDATE_VERSION', 1449); + define('DB_UPDATE_VERSION', 1450); } return [ @@ -71,6 +71,11 @@ return [ "info" => ["type" => "text", "comment" => ""], "register_policy" => ["type" => "tinyint", "not null" => "1", "default" => "0", "comment" => ""], "registered-users" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "comment" => "Number of registered users"], + "active-week-users" => ["type" => "int unsigned", "comment" => "Number of active users in the last week"], + "active-month-users" => ["type" => "int unsigned", "comment" => "Number of active users in the last month"], + "active-halfyear-users" => ["type" => "int unsigned", "comment" => "Number of active users in the last six month"], + "local-posts" => ["type" => "int unsigned", "comment" => "Number of local posts"], + "local-comments" => ["type" => "int unsigned", "comment" => "Number of local comments"], "directory-type" => ["type" => "tinyint", "default" => "0", "comment" => "Type of directory service (Poco, Mastodon)"], "poco" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""], "noscrape" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""],