use true values for get_grid_stats and in grid status card

This commit is contained in:
Olivier van Helden
2025-02-02 14:59:35 -04:00
parent f9a353e000
commit e9ce39c309
4 changed files with 153 additions and 128 deletions
+103 -118
View File
@@ -11,14 +11,29 @@ class OpenSim_Grid {
private $grid_info;
private $grid_info_card;
private $grid_stats_card;
private static $labels = array();
public function __construct() {
$this->constants();
// $this->grid_stats = $this->get_grid_stats();
// $this->grid_info = $this->get_grid_info();
// $this->grid_info_card = $this->get_grid_info_card();
// $this->grid_stats_card = $this->get_grid_stats_card();
}
public function constants() {
self::$labels = array(
'status' => _('Status'),
'members' => _('Members'),
'active_members' => _('Active members (30 days)'),
'members_in_world' => _('Members in world'),
'active_users' => _('Active users (30 days)'),
'total_users' => _('Total users in world'),
'regions' => _('Regions'),
'total_area' => _('Total area'),
);
}
public static function get_grid_info( $grid_uri = false, $args = array() ) {
$info = array();
$HomeURI = OpenSim::get_option( 'Hypergrid.HomeURI' );
@@ -140,7 +155,6 @@ class OpenSim_Grid {
$title = false;
if( ! empty( $args['title'])) {
error_log('using args title = ' . print_r($args['title'], true));
$title = $args['title'] === true ? _( 'Grid Information' ) : $args['title'];
} else {
$title = _( 'Grid Information' );
@@ -152,16 +166,14 @@ class OpenSim_Grid {
public static function grid_stats_card( $args = null ) {
$grid_stats = self::get_grid_stats( $args );
error_log( __METHOD__ . ' info = ' . print_r( $grid_stats, true ) );
if( ! $grid_stats || OpenSim::is_error( $info ) ) {
if( ! $grid_stats || OpenSim::is_error( $grid_stats ) ) {
error_log( __METHOD__ . ' grid stats empty or error' );
return false;
}
$title = false;
if( ! empty( $args['title'])) {
error_log('using args title = ' . print_r($args['title'], true));
$title = $args['title'] === true ? _( 'Grid Information' ) : $args['title'];
} else {
$title = _( 'Grid Status' );
@@ -172,130 +184,103 @@ class OpenSim_Grid {
) );
}
private static function array_to_xml($array, $xml) {
foreach($array as $key => $value) {
if(is_array($value)) {
$subnode = $xml->addChild($key);
self::array_to_xml($value, $subnode);
} else {
$xml->addChild($key, htmlspecialchars($value));
}
}
}
public static function get_grid_stats( $args = null ) {
$grid_info = self::get_grid_info();
$grid_uri = $grid_info['login'];
error_log('grid_uri = ' . print_r($grid_uri, true));
$args = array_merge(array(
'output' => 'array',
'title' => true,
));
// DEBUG - Fake data for debugging purpose
$labels = array(
'status' => _('Status'),
'members' => _('Members'),
'active_members' => _('Active Members (30 days)'),
'members_in_world' => _('Members in world'),
'active_users' => _('Active users (30 days)'),
'total_users' => _('Total users in world'),
'regions' => _('Regions'),
'total_area' => _('Total area'),
);
$values = array_map( function( $label ) {
return 'TBD';
}, $labels );
$values = array(
$stats = array(
'status' => $grid_info['online'] ? _('Online') : _('Offline'),
);
$labels = array_intersect_key( $labels, $values );
error_log('labels = ' . print_r($labels, true));
// $values['status'] = $grid_info['online'] ? _('Online') : _('Offline');
$stats = array_combine( $labels, $values );
global $OpenSimDB;
// If db is not yet configured, calls to $OpenSimDB would crash otherwise
if ( ! $OpenSimDB ) {
$stats['error'] = _('Database not configured.');
$robust_db = OpenSim::$robust_db;
if ( ! $robust_db || OpenSim::is_error($robust_db) ) {
$stats['error'] = _('Database not connected.');
} else {
error_log('querying robust db');
$lastmonth = time() - 30 * 86400;
$gridonline = $grid_info['online'] ? _('Yes') : _('No');
$filter = '';
// if ( get_option( 'w4os_exclude_models' ) ) {
// $filter .= "u.FirstName != '" . get_option( 'w4os_model_firstname' ) . "'
// AND u.LastName != '" . get_option( 'w4os_model_lastname' ) . "'";
// }
// if ( get_option( 'w4os_exclude_nomail' ) ) {
// $filter .= " AND u.Email != ''";
// }
if ( ! empty( $filter ) ) {
$filter = "$filter AND ";
}
$stats = array(
'status' => $grid_info['online'] ? _('Online') : _('Offline'),
'members' => $robust_db->get_var( "SELECT COUNT(*)
FROM UserAccounts as u WHERE $filter active=1"
),
'active_members' => $robust_db->get_var( "SELECT COUNT(*)
FROM GridUser as g, UserAccounts as u
WHERE $filter PrincipalID = UserID AND g.Login > :lastmonth",
array(
'lastmonth' => $lastmonth,
)
),
'members_in_world' => $robust_db->get_var( "SELECT COUNT(*)
FROM Presence AS p, UserAccounts AS u
WHERE $filter RegionID != '00000000-0000-0000-0000-000000000000'
AND p.UserID = u.PrincipalID;"
),
'active_users' => $robust_db->get_var( "SELECT COUNT(*)
FROM GridUser WHERE Login > :lastmonth",
array(
'lastmonth' => $lastmonth,
)
),
'total_users' => $robust_db->get_var( "SELECT COUNT(*)
FROM Presence WHERE RegionID != '00000000-0000-0000-0000-000000000000';"
),
'regions' => $robust_db->get_var( "SELECT COUNT(*)
FROM regions"
),
'total_area' => $robust_db->get_var( "SELECT round(sum(sizex * sizey / 1000000),2)
FROM regions"
) . ' km²',
);
// Replace keys with values of self::$labels
$labels = self::$labels;
$labels = array_intersect_key( self::$labels, $stats );
error_log( 'labels: ' . print_r( $labels, true ) . ' stats: ' . print_r( $stats, true ) );
$stats = array_combine( $labels, $stats );
}
switch( $args['output'] ) {
case 'xml':
$xml = new SimpleXMLElement('<gridstatus/>');
self::array_to_xml($stats, $xml);
return $xml->asXML();
case 'array':
return $stats;
default:
return $stats;
}
return $stats;
// End DEBUG
// $status = wp_cache_get( 'gridstatus', 'w4os' );
// if ( false === $status ) {
// // $cached="uncached";
// if ( $OpenSimDB->check_connection() ) {
// $lastmonth = time() - 30 * 86400;
// // $urlinfo = explode( ':', get_option( 'w4os_login_uri' ) );
// // $host = $urlinfo['0'];
// // $port = $urlinfo['1'];
// // $fp = @fsockopen( $host, $port, $errno, $errstr, 1.0 );
// $gridonline = w4os_grid_stats();
// // if ($fp) {
// // $gridonline = __("Yes", 'w4os' );
// // } else {
// // $gridonline = __("No", 'w4os' );
// // }
// $filter = '';
// if ( get_option( 'w4os_exclude_models' ) ) {
// $filter .= "u.FirstName != '" . get_option( 'w4os_model_firstname' ) . "'
// AND u.LastName != '" . get_option( 'w4os_model_lastname' ) . "'";
// }
// if ( get_option( 'w4os_exclude_nomail' ) ) {
// $filter .= " AND u.Email != ''";
// }
// if ( ! empty( $filter ) ) {
// $filter = "$filter AND ";
// }
// }
// $status = array(
// __( 'Status', 'w4os' ) => $gridonline,
// __( 'Members', 'w4os' ) => number_format_i18n(
// $OpenSimDB->get_var(
// "SELECT COUNT(*)
// FROM UserAccounts as u WHERE $filter active=1"
// )
// ),
// __( 'Active members (30 days)', 'w4os' ) => number_format_i18n(
// $OpenSimDB->get_var(
// "SELECT COUNT(*)
// FROM GridUser as g, UserAccounts as u WHERE $filter PrincipalID = UserID AND g.Login > $lastmonth"
// )
// ),
// );
// $status[ __( 'Members in world', 'w4os' ) ] = number_format_i18n(
// $OpenSimDB->get_var(
// "SELECT COUNT(*)
// FROM Presence AS p, UserAccounts AS u
// WHERE $filter RegionID != '00000000-0000-0000-0000-000000000000'
// AND p.UserID = u.PrincipalID;"
// )
// );
// // 'Active citizens (30 days)' => number_format_i18n($OpenSimDB->get_var("SELECT COUNT(*)
// // FROM GridUser as g, UserAccounts as u WHERE g.UserID = u.PrincipalID AND Login > $lastmonth" )),
// if ( ! get_option( 'w4os_exclude_hypergrid' ) ) {
// $status[ __( 'Active users (30 days)', 'w4os' ) ] = number_format_i18n(
// $OpenSimDB->get_var(
// "SELECT COUNT(*)
// FROM GridUser WHERE Login > $lastmonth"
// )
// );
// $status[ __( 'Total users in world', 'w4os' ) ] = number_format_i18n(
// $OpenSimDB->get_var(
// "SELECT COUNT(*)
// FROM Presence
// WHERE RegionID != '00000000-0000-0000-0000-000000000000'; "
// )
// );
// }
// $status[ __( 'Regions', 'w4os' ) ] = number_format_i18n(
// $OpenSimDB->get_var(
// 'SELECT COUNT(*)
// FROM regions'
// )
// );
// $status[ __( 'Total area', 'w4os' ) ] = number_format_i18n(
// $OpenSimDB->get_var(
// 'SELECT round(sum(sizex * sizey / 1000000),2)
// FROM regions'
// ),
// 2
// ) . '&nbsp;km²';
// wp_cache_add( 'gridstatus', $status, 'w4os' );
// }
// return $status;
}
}
+30 -3
View File
@@ -23,6 +23,8 @@ class OpenSim {
private static $styles;
private static $is_dev;
public static $robust_db;
public function __construct() {
// Check if domain name starts with "dev." or usual wp debug constants are set
self::$is_dev = ( strpos( $_SERVER['HTTP_HOST'], 'dev.' ) === 0 ) || ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG );
@@ -48,12 +50,37 @@ class OpenSim {
public function includes() {
require_once( OSHELPERS_DIR . 'classes/class-exception.php' );
require_once( OSHELPERS_DIR . 'includes/databases.php' );
require_once( OSHELPERS_DIR . 'includes/functions.php' );
$this->connect_db();
require_once( OSHELPERS_DIR . 'classes/class-locale.php' );
require_once( OSHELPERS_DIR . 'classes/class-ini.php' );
require_once( OSHELPERS_DIR . 'classes/class-grid.php' );
}
public function connect_db() {
$DatabaseService = self::get_option( 'DatabaseService', false );
$connectionstring = self::get_option( 'DatabaseService.ConnectionString', false);
if( $connectionstring ) {
$creds = self::connectionstring_to_array( $connectionstring );
$dsn = sprintf(
'mysql:host=%s;dbname=%s',
$creds['host'] . ( empty( $creds['port'] ) ? '' : ':' . $creds['port'] ),
$creds['name'],
);
$db = new OSPDO( $dsn, $creds['user'], $creds['pass'] );
if( $db->connected ) {
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$db->setAttribute( PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC );
self::$robust_db = $db;
}
} else {
error_log('No connection string found');
}
}
public function get_helpers_url() {
$helpers_path = dirname( __DIR__ );
$url_path = self::trailingslashit( str_replace( $_SERVER['DOCUMENT_ROOT'], '', $helpers_path ) );
@@ -600,9 +627,9 @@ class OpenSim {
// Give value of $config[$section][$key] if when given $option = 'section.key'
if( strpos( $option, '.' ) !== false ) {
$parts = explode( '.', $option );
$section = $parts[0];
$key = $parts[1];
return $config[$section][$key] ?? $default;
$section = $config[$parts[0]];
$key = trim($parts[1]);
return $section[$key] ?? $default;
}
// Otherwise return global option
+13
View File
@@ -54,6 +54,19 @@ class OSPDO extends PDO {
$statement = $this->prepare( $sql );
return $statement->execute( $values );
}
/**
* Prepare query and return single value (without using prepareAndExecute)
*/
public function get_var( $query, $options = array() ) {
$statement = $this->prepareAndExecute( $query, $options );
if ( $statement ) {
$result = $statement->fetchColumn();
$statement->closeCursor();
return $result;
}
return false;
}
}
function tableExists( $pdo, $tables ) {
+7 -7
View File
@@ -128,7 +128,7 @@ class OpenSim_Install extends OpenSim_Page {
// OpenSim::notify_error( __FUNCTION__ . '() ' . _('No configuration found.') );
// return false;
// }
$robust_db = OpenSim::connectionstring_to_array($config['DatabaseService']['ConnectionString']);
$robust_creds = OpenSim::connectionstring_to_array($config['DatabaseService']['ConnectionString']);
$registrars = array(
'DATA_SRV_W4OSDev' => "http://dev.w4os.org/helpers/register.php",
@@ -149,13 +149,13 @@ class OpenSim_Install extends OpenSim_Page {
'OPENSIM_GRID_NAME' => $config['Const']['BaseURL'],
'OPENSIM_LOGIN_URI' => $config['Const']['BaseURL'] . ':' . $config['Const']['PublicPort'],
'OPENSIM_MAIL_SENDER' => "no-reply@" . parse_url($config['Const']['BaseURL'], PHP_URL_HOST),
'ROBUST_DB' => $robust_db,
'ROBUST_DB' => $robust_creds,
'OPENSIM_DB' => true, // Changed from string to boolean
'OPENSIM_DB_HOST' => $robust_db['host'],
'OPENSIM_DB_PORT' => $robust_db['port'] ?? null,
'OPENSIM_DB_NAME' => $robust_db['name'],
'OPENSIM_DB_USER' => $robust_db['user'],
'OPENSIM_DB_PASS' => $robust_db['pass'],
'OPENSIM_DB_HOST' => $robust_creds['host'],
'OPENSIM_DB_PORT' => $robust_creds['port'] ?? null,
'OPENSIM_DB_NAME' => $robust_creds['name'],
'OPENSIM_DB_USER' => $robust_creds['user'],
'OPENSIM_DB_PASS' => $robust_creds['pass'],
'SEARCH_REGISTRARS' => $registrars,
'ROBUST_CONSOLE' => $console,
'CURRENCY_NAME' => $config['LoginService']['Currency'] ?? 'L$',