1
1
Fork 0

Store wekk/month&halfyear usage / number of posts

This commit is contained in:
Michael 2022-02-06 20:03:19 +00:00
parent 8b585c6ccc
commit 92a71f8c27
5 changed files with 148 additions and 27 deletions

View file

@ -1,6 +1,6 @@
-- ------------------------------------------ -- ------------------------------------------
-- Friendica 2021.12-rc (Siberian Iris) -- 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 '', `info` text COMMENT '',
`register_policy` tinyint NOT NULL DEFAULT 0 COMMENT '', `register_policy` tinyint NOT NULL DEFAULT 0 COMMENT '',
`registered-users` int unsigned NOT NULL DEFAULT 0 COMMENT 'Number of registered users', `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)', `directory-type` tinyint DEFAULT 0 COMMENT 'Type of directory service (Poco, Mastodon)',
`poco` varchar(255) NOT NULL DEFAULT '' COMMENT '', `poco` varchar(255) NOT NULL DEFAULT '' COMMENT '',
`noscrape` varchar(255) NOT NULL DEFAULT '' COMMENT '', `noscrape` varchar(255) NOT NULL DEFAULT '' COMMENT '',

View file

@ -7,7 +7,7 @@ Fields
------ ------
| Field | Description | Type | Null | Key | Default | Extra | | Field | Description | Type | Null | Key | Default | Extra |
| ---------------- | -------------------------------------------------- | ---------------- | ---- | --- | ------------------- | -------------- | | --------------------- | -------------------------------------------------- | ---------------- | ---- | --- | ------------------- | -------------- |
| id | sequential ID | int unsigned | NO | PRI | NULL | auto_increment | | id | sequential ID | int unsigned | NO | PRI | NULL | auto_increment |
| url | | varchar(255) | NO | | | | | url | | varchar(255) | NO | | | |
| nurl | | varchar(255) | NO | | | | | nurl | | varchar(255) | NO | | | |
@ -16,6 +16,11 @@ Fields
| info | | text | YES | | NULL | | | info | | text | YES | | NULL | |
| register_policy | | tinyint | NO | | 0 | | | register_policy | | tinyint | NO | | 0 | |
| registered-users | Number of registered users | int unsigned | 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 | | | directory-type | Type of directory service (Poco, Mastodon) | tinyint | YES | | 0 | |
| poco | | varchar(255) | NO | | | | | poco | | varchar(255) | NO | | | |
| noscrape | | varchar(255) | NO | | | | | noscrape | | varchar(255) | NO | | | |

View file

@ -377,6 +377,12 @@ class GServer
if (empty($nodeinfo['network']) || in_array($nodeinfo['network'], [Protocol::DFRN, Protocol::ZOT])) { if (empty($nodeinfo['network']) || in_array($nodeinfo['network'], [Protocol::DFRN, Protocol::ZOT])) {
if (!empty($nodeinfo['detection-method'])) { if (!empty($nodeinfo['detection-method'])) {
$serverdata['detection-method'] = $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 // Fetch the landing page, possibly it reveals some data
@ -500,6 +506,10 @@ class GServer
$serverdata = self::detectNetworkViaContacts($url, $serverdata); $serverdata = self::detectNetworkViaContacts($url, $serverdata);
} }
if ($serverdata['network'] == Protocol::ACTIVITYPUB) {
$serverdata = self::fetchWeeklyUsage($url, $serverdata);
}
$serverdata['registered-users'] = $serverdata['registered-users'] ?? 0; $serverdata['registered-users'] = $serverdata['registered-users'] ?? 0;
// On an active server there has to be at least a single user // 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'])) { if (!empty($data['registrations_open'])) {
$serverdata['register_policy'] = Register::OPEN; $serverdata['register_policy'] = Register::OPEN;
@ -801,6 +826,22 @@ class GServer
$server['registered-users'] = max($nodeinfo['usage']['users']['total'], 1); $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'])) { if (!empty($nodeinfo['protocols']['inbound']) && is_array($nodeinfo['protocols']['inbound'])) {
$protocols = []; $protocols = [];
foreach ($nodeinfo['protocols']['inbound'] as $protocol) { foreach ($nodeinfo['protocols']['inbound'] as $protocol) {
@ -878,6 +919,22 @@ class GServer
$server['registered-users'] = max($nodeinfo['usage']['users']['total'], 1); $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'])) { if (!empty($nodeinfo['protocols'])) {
$protocols = []; $protocols = [];
foreach ($nodeinfo['protocols'] as $protocol) { foreach ($nodeinfo['protocols'] as $protocol) {
@ -957,6 +1014,22 @@ class GServer
$serverdata['registered-users'] = max($data['channels_total'], 1); $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'])) { if (!empty($data['register_policy'])) {
switch ($data['register_policy']) { switch ($data['register_policy']) {
case 'REGISTER_OPEN': case 'REGISTER_OPEN':
@ -1208,6 +1281,38 @@ class GServer
return $serverdata; 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 * Detects data from a given server url if it was a mastodon alike system
* *

View file

@ -43,6 +43,7 @@ class Federation extends BaseAdmin
'diaspora' => ['name' => 'Diaspora', 'color' => '#a1a1a1'], // logo is black and white, makes a gray 'diaspora' => ['name' => 'Diaspora', 'color' => '#a1a1a1'], // logo is black and white, makes a gray
'funkwhale' => ['name' => 'Funkwhale', 'color' => '#4082B4'], // From the homepage 'funkwhale' => ['name' => 'Funkwhale', 'color' => '#4082B4'], // From the homepage
'gnusocial' => ['name' => 'GNU Social/Statusnet', 'color' => '#a22430'], // dark red from the logo '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 'hubzilla' => ['name' => 'Hubzilla/Red Matrix', 'color' => '#43488a'], // blue from the logo
'lemmy' => ['name' => 'Lemmy', 'color' => '#00c853'], // Green from the page 'lemmy' => ['name' => 'Lemmy', 'color' => '#00c853'], // Green from the page
'mastodon' => ['name' => 'Mastodon', 'color' => '#1a9df9'], // blue from the Mastodon logo 'mastodon' => ['name' => 'Mastodon', 'color' => '#1a9df9'], // blue from the Mastodon logo

View file

@ -55,7 +55,7 @@
use Friendica\Database\DBA; use Friendica\Database\DBA;
if (!defined('DB_UPDATE_VERSION')) { if (!defined('DB_UPDATE_VERSION')) {
define('DB_UPDATE_VERSION', 1449); define('DB_UPDATE_VERSION', 1450);
} }
return [ return [
@ -71,6 +71,11 @@ return [
"info" => ["type" => "text", "comment" => ""], "info" => ["type" => "text", "comment" => ""],
"register_policy" => ["type" => "tinyint", "not null" => "1", "default" => "0", "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"], "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)"], "directory-type" => ["type" => "tinyint", "default" => "0", "comment" => "Type of directory service (Poco, Mastodon)"],
"poco" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""], "poco" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""],
"noscrape" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""], "noscrape" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""],