new approach for grid info and grid status cards

This commit is contained in:
Olivier van Helden
2025-02-02 12:52:55 -04:00
parent 97e1634b61
commit f9a353e000
4 changed files with 175 additions and 75 deletions
+107 -70
View File
@@ -7,44 +7,65 @@ if( ! defined( 'OSHELPERS' ) ) {
}
class OpenSim_Grid {
private $grid_status;
private static $grid_stats;
private $grid_info;
private $grid_info_card;
private $grid_status_card;
private $grid_stats_card;
public function __construct() {
// $this->grid_status = $this->get_grid_status();
// $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_status_card = $this->get_grid_status_card();
// $this->grid_stats_card = $this->get_grid_stats_card();
}
public static function get_grid_info( $grid_uri = false, $args = array() ) {
$info = array();
if( ! $grid_uri ) {
$info = array_filter( array_merge(
$info,
array(
OpenSim::get_option( 'GridInfoService.gridname' ),
'Login URI' => OpenSim::hop( OpenSim::get_option( 'Hypergrid.HomeURI' ) ),
)
) );
// $info = OpenSim::get_option( 'GridInfoService' );
// $login_uri = OpenSim::get_option( 'Hypergrid.HomeURI' );
$HomeURI = OpenSim::get_option( 'Hypergrid.HomeURI' );
if( ! $grid_uri || $grid_uri === $HomeURI ) {
$is_local_grid = true;
// Default, get login_uri from config, query grid for live grid_info
$grid_uri = OpenSim::get_option( 'Hypergrid.HomeURI' );
} else {
$is_local_grid = false;
// External grid lookup, not yet implemented
$info = array(
'Grid Name' => 'External Grid, not implemented.',
);
return false;
}
$info = array_filter( $info );
// Fetch live info from grid using $login_uri/get_grid_info and parse xml result in array
// Example xml result:
$xml_url = $grid_uri . '/get_grid_info';
try {
if( empty( $info ) ) {
throw new Exception( 'No grid info found.' );
}
$xml = simplexml_load_file( $xml_url );
} catch( Exception $e ) {
return $e;
$xml = false;
}
if( ! $xml ) {
$info = array(
'online' => false,
'login' => $grid_uri,
);
} else {
$info['online'] = true;
try {
$array = (array) $xml;
if( ! $array ) {
throw new Exception( 'Error parsing grid info.' );
}
$info = array_merge( $info, $array );
} catch( Exception $e ) {
return $e;
}
}
if( $is_local_grid ) {
$config_info = OpenSim::get_option( 'GridInfoService' );
if( is_array( $config_info ) ) {
$info = array_merge( $config_info, $info );
}
}
return $info;
@@ -59,7 +80,7 @@ class OpenSim_Grid {
$title = $args['title'];
} else {
$title = array_values( $info )[0];
if( is_numeric( array_keys( $info )[0] ) ) {
if( is_numeric( array_keys( $info )[0] ) || ( isset($args['hide_first']) && $args['hide_first'] === true ) ) {
$hide_first = 'hidden d-none';
}
}
@@ -107,73 +128,89 @@ class OpenSim_Grid {
* Get grid information as a card
*/
public static function grid_info_card( $grid_uri = false, $args = array() ) {
$info = self::get_grid_info( $grid_uri, $args );
if( ! $info || OpenSim::is_error( $info ) ) {
$grid_info = self::get_grid_info( $grid_uri, $args );
if( ! $grid_info || OpenSim::is_error( $grid_info ) ) {
return false;
}
$info = array(
_('Grid Name') => $grid_info['gridname'],
_('Login URI') => OpenSim::hop( $grid_info['login'] ),
);
$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' );
}
return self::array_to_card( 'grid-info', $info, $title );
return self::array_to_card( 'grid-info', $info, array(
'title' => $title,
) );
}
// public function get_grid_status() {
// $status = OpenSim::get_grid_status();
// return $status;
// }
public static function grid_stats_card( $args = null ) {
$grid_stats = self::get_grid_stats( $args );
error_log( __METHOD__ . ' info = ' . print_r( $grid_stats, true ) );
// public function get_grid_info() {
// $info = OpenSim::get_grid_info();
// return $info;
// }
// public function get_grid_info_card() {
// $info = $this->get_grid_info();
// $html = '';
// if( ! empty( $info ) ) {
// $html .= '<div class="card">';
// $html .= '<div class="card-header">';
// $html .= '<h5 class="card-title">' . _('Grid Info') . '</h5>';
// $html .= '</div>';
// $html .= '<div class="card-body">';
// $html .= OpenSim::array2table( $info, 'gridinfo' );
// $html .= '</div>';
// $html .= '</div>';
// }
// return $html;
// }
public static function grid_status_card( $grid_uri = null, $args = null ) {
$info = self::grid_status( $grid_uri, $args );
if( ! $info || OpenSim::is_error( $info ) ) {
if( ! $grid_stats || OpenSim::is_error( $info ) ) {
error_log( __METHOD__ . ' grid stats empty or error' );
return false;
}
return self::array_to_card( 'grid-status', $info );
$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' );
}
return self::array_to_card( 'grid-status', $grid_stats, array(
'title' => $title,
) );
}
public static function grid_status( $grid_url = null ) {
// DEBUG - Fake data for debugging purpose
$info = array(
_('Status') => _('Grid Online'),
_('Members') => 113,
_('Active Members (30 days)') => 6,
_('Members in world') => 0,
_('Active users (30 days)') => 33,
_('Total users in world') => 0,
_('Regions') => 22,
_('Total area') => '1.44 km²',
);
return $info;
// End DEBUG
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));
// 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(
'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 ) {
return false;
$stats['error'] = _('Database not configured.');
}
return $stats;
// End DEBUG
// $status = wp_cache_get( 'gridstatus', 'w4os' );
// if ( false === $status ) {
@@ -185,7 +222,7 @@ class OpenSim_Grid {
// // $host = $urlinfo['0'];
// // $port = $urlinfo['1'];
// // $fp = @fsockopen( $host, $port, $errno, $errstr, 1.0 );
// $gridonline = w4os_grid_status();
// $gridonline = w4os_grid_stats();
// // if ($fp) {
// // $gridonline = __("Yes", 'w4os' );
+2 -2
View File
@@ -27,11 +27,11 @@ class OpenSim_Page {
$html = '';
switch( $id ) {
case 'left':
// $html = OpenSim_Grid::grid_status_card();
// $html = OpenSim_Grid::grid_stats_card();
break;
case 'right':
$html .= OpenSim_Grid::grid_info_card();
$html .= OpenSim_Grid::grid_status_card();
$html .= OpenSim_Grid::grid_stats_card();
break;
default:
break;
+3 -3
View File
@@ -16,7 +16,7 @@ if ( __FILE__ !== $_SERVER['SCRIPT_FILENAME'] ) {
}
require_once( __DIR__ . '/classes/init.php' ); // Common to all main scripts
require_once( __DIR__ . '/classes/class-page.php' ); // Specific, because we generate the page
require_once( __DIR__ . '/classes/class-page.php' ); // Specific, because we generate a page
require_once( __DIR__ . '/classes/class-form.php' ); // Specific, because we use forms
class OpenSim_Install extends OpenSim_Page {
@@ -239,8 +239,8 @@ class OpenSim_Install extends OpenSim_Page {
// OpenSim::notify( _('Configuration file loaded successfully.'), 'success' );
// TODO: copy the temp file to the final location on success.
// return true;
throw new OpenSim_Error( _('The robust_test_config routine is not finished yet, but so far, so good.' ) );
return true;
// throw new OpenSim_Error( _('The robust_test_config routine is not finished yet, but so far, so good.' ) );
}
public function process_form_installation() {
+63
View File
@@ -0,0 +1,63 @@
<?php
/**
* Migration class. Should be used only once.
*
* This class is used to migrate the configuration from the old constants-based config.php to the new array-based settings.
*
* First it defines an map of the old constants to the new settings names.
*/
class OSHelpers_Migration {
private $map = array(
'Robust.HG.ini' => array(
'OPENSIM_GRID_NAME' => 'Const.BaseURL',
'OPENSIM_LOGIN_URI' => array ( 'Const.BaseURL', 'Const.PublicPort' ),
'OPENSIM_MAIL_SENDER' => array( $this, 'mail_sender' ),
'ROBUST_DB' => 'OpenSim.ini::DatabaseService.ConnectionString',
'OPENSIM_DB' => 'DatabaseService.ConnectionString',
// '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'],
'SEARCH_REGISTRARS' => array( $this, 'search_registrars' ),
// 'ROBUST_CONSOLE' => $console,
'CURRENCY_HELPER_URL' => 'GridInfoService.economy',
'CURRENCY_DB' => '',
'CURRENCY_DB_HOST' => 'MoneyServer.ini::MySql.hostname',
'CURRENCY_DB_NAME' => 'MoneyServer.ini::MySql.database',
'CURRENCY_DB_USER' => 'MoneyServer.ini::MySql.username',
'CURRENCY_DB_PASS' => 'MoneyServer.ini::MySql.password',
'CURRENCY_DB_PORT' => 'MoneyServer.ini::MySql.port',
// 'CURRENCY_HELPER_PATH' => '',
'CURRENCY_MONEY_TBL' => '',
'CURRENCY_PROVIDER' => '',
'CURRENCY_RATE' => '',
'CURRENCY_RATE_PER' => '',
'CURRENCY_SCRIPT_KEY' => '',
'CURRENCY_TRANSACTION_TBL' => '',
'CURRENCY_USE_MONEYSERVER' => '',
'HYPEVENTS_URL' => '',
'MUTE_DB_HOST' => '',
'MUTE_DB_NAME' => '',
'MUTE_DB_PASS' => '',
'MUTE_DB_USER' => '',
'MUTE_LIST_TBL' => '',
'OFFLINE_DB' => '',
'OFFLINE_DB_HOST' => '',
'OFFLINE_DB_NAME' => '',
'OFFLINE_DB_PASS' => '',
'OFFLINE_DB_PORT' => '',
'OFFLINE_DB_USER' => '',
'OFFLINE_MESSAGE_TBL' => '',
'OPENSIM_GRID_LOGO_URL' => '',
'OPENSIM_USE_UTC_TIME' => '',
'SEARCH_DB' => '',
'SEARCH_DB_HOST' => '',
'SEARCH_DB_NAME' => '',
'SEARCH_DB_PASS' => '',
'SEARCH_DB_PORT' => '',
'SEARCH_DB_USER' => '',
'SEARCH_TABLE_EVENTS' => '',
);
}