From 659229c0777f381c66462fd7a4f2dfddbe3e1ef6 Mon Sep 17 00:00:00 2001
From: onefang
Date: Tue, 6 Aug 2019 01:08:51 +1000
Subject: Add actual grid stats to the web page.

Except HGers, still trying to sort that one out.
---
 .../Server/Handlers/Web/WebServerInConnector.cs    | 48 ++++++++++++++++++----
 example/web/loginpage.html                         | 10 ++---
 prebuild.xml                                       |  3 ++
 3 files changed, 48 insertions(+), 13 deletions(-)

diff --git a/OpenSim/Server/Handlers/Web/WebServerInConnector.cs b/OpenSim/Server/Handlers/Web/WebServerInConnector.cs
index e7fb2fc..01c74fe 100644
--- a/OpenSim/Server/Handlers/Web/WebServerInConnector.cs
+++ b/OpenSim/Server/Handlers/Web/WebServerInConnector.cs
@@ -38,6 +38,7 @@ using OpenMetaverse;
 using OpenMetaverse.StructuredData;
 using Nini.Config;
 using OpenSim.Framework;
+using OpenSim.Data.MySQL;
 using OpenSim.Framework.Servers.HttpServer;
 using OpenSim.Server.Handlers.Base;
 
@@ -48,12 +49,39 @@ namespace OpenSim.Server.Handlers.Web
 	// This is all slow and clunky, it's not a real web server, just something to use if you don't want a real one.
         private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
         private IConfigSource m_Config;
+        protected MySQLGenericHandler m_Database = null;
         private Hashtable mime = new Hashtable();
     	private Hashtable ssi = new Hashtable();
 
         public WebServerInConnector(IConfigSource config, IHttpServer server, string configName) : base(config, server, configName)
         {
             m_Config = config;
+
+            string dllName = String.Empty;
+            string connString = String.Empty;
+
+            //
+            // Try reading the [DatabaseService] section, if it exists
+            //
+            IConfig dbConfig = m_Config.Configs["DatabaseService"];
+            if (dbConfig != null)
+            {
+                if (dllName == String.Empty)
+                    dllName = dbConfig.GetString("StorageProvider", String.Empty);
+                if (connString == String.Empty)
+                    connString = dbConfig.GetString("ConnectionString", String.Empty);
+            }
+
+            if (dllName.Equals(String.Empty))
+                throw new Exception("No StorageProvider configured");
+
+//// TODO - Should do the plugin thing to pick between database backends.
+//            m_Database = LoadPlugin<SQLGenericHandler>(dllName, new Object[] { connString });
+            m_Database = new MySQLGenericHandler(connString);
+            if (m_Database == null)
+                throw new Exception("Could not find a storage interface in the given module " + dllName);
+
+
             mime.Add(".gz",	"application/gzip");
             mime.Add(".js",	"application/javascript");
             mime.Add(".json",	"application/json");
@@ -90,15 +118,19 @@ namespace OpenSim.Server.Handlers.Web
 
         private Hashtable WebRequestHandler(Hashtable request)
         {
+    	    long locIn  = m_Database.GetCount("Presence", "RegionID != '00000000-0000-0000-0000-000000000000'");	// Locals online but not HGing.
+//    	    long HGin   = m_Database.GetCount("hg_traveling_data", "GridExternalName = '" + ssi["uri"] + "'");		// HGers in world, and locals not HGing, sometimes more than once.
+    	    long locOut = m_Database.GetCount("hg_traveling_data", "GridExternalName != '" + ssi["uri"] + "'");		// Locals that are HGing.
             Hashtable reply = new Hashtable();
-            // Looks like we have to jump through hoops to do simple database queries.  B-(
-    	    ssi["members"]	= "?";	// SELECT COUNT(PrincipalID) FROM UserAccounts
-    	    ssi["inworld"]	= "?";	// SELECT COUNT(UserID) FROM presence_culled;  Includes locals and HGers in world, but not locals HGing.
-    					// SELECT COUNT(UserID) FROM users_online;    Includes local online only.
-    	    ssi["outworld"]	= "?";	// SELECT COUNT(UserID) FROM hg_traveling_data WHERE GridExternalName != ssi["uri"];	Includes locals that are HGing.
-//    	    ssi["hgers"]	= "?";	// This is complex.
-    	    ssi["sims"]		= "?";	// SELECT COUNT(uuid) FROM regions
-//    	    ssi["month"]	= "?";	// SELECT COUNT(UserID) FROM Presence WHERE LastSeen < one month ago 2017-11-28 09:01:51;  Includes external HGers, and locals on now.
+    	    ssi["members"]	= m_Database.GetCount("UserAccounts").ToString();
+    	    ssi["sims"]		= m_Database.GetCount("regions").ToString();
+    	    ssi["inworld"]	= locIn.ToString();
+    	    ssi["outworld"]	= locOut.ToString();
+//    	    ssi["hgers"]	= HGin.ToString();
+    	    ssi["hgers"]	= "?";
+    	    ssi["month"]	= m_Database.GetCount("GridUser", "Login > UNIX_TIMESTAMP(FROM_UNIXTIME(UNIX_TIMESTAMP(now()) - 2419200))").ToString();
+	    // SELECT COUNT(UserID) FROM Presence WHERE LastSeen < one month ago 2017-11-28 09:01:51;  This is not accurate, and never has been.
+	    // SELECT COUNT(UserID) FROM {GridUser} WHERE Login > UNIX_TIMESTAMP(FROM_UNIXTIME(UNIX_TIMESTAMP(now()) - 2419200)
 
             string reqpath	= (string)	request["uri"];
             string[] query	= (string[])	request["querystringkeys"];
diff --git a/example/web/loginpage.html b/example/web/loginpage.html
index ebce4aa..614c73d 100644
--- a/example/web/loginpage.html
+++ b/example/web/loginpage.html
@@ -70,11 +70,11 @@ a:active {color: blue;}
   <div class="top-right">
     <h1>Login URI - <!--#echo var="uri" --></h1>
     <p>&nbsp;</p>
-    <p><!--#echo var="grid" --> may or maynot be in good shape now.</p>
-    <p>There are likely some (<!--#echo var="members" -->) members of this grid.</p>
-    <p>There are maybe some (<!--#echo var="inworld" -->) people in world now (including hypergridders).</p>
-    <p>There are maybe some (<!--#echo var="outworld" -->) locals hypergridding now.</p>
-    <p>There is probably at least one (<!--#echo var="sims" -->) region, maybe more.</p>
+    <p>There are <!--#echo var="members" --> members of this grid.</p>
+    <p>There are <!--#echo var="inworld" --> locals and <!--#echo var="hgers" --> hypergrid visitors in world.</p>
+    <p>There are <!--#echo var="outworld" --> locals out on the hypergrid.</p>
+    <p>There have been <!--#echo var="month" --> people on this grid in the last month.</p>
+    <p>There are <!--#echo var="sims" --> regions, though some might not be online right now.</p>
   </div>
   <div class="centre">
     <p>Maybe add some news or events here, or something.</p>
diff --git a/prebuild.xml b/prebuild.xml
index 0241fb3..c4c5e4f 100644
--- a/prebuild.xml
+++ b/prebuild.xml
@@ -1208,6 +1208,9 @@
       <Reference name="OpenMetaverseTypes" path="../../../bin/"/>
       <Reference name="OpenMetaverse" path="../../../bin/"/>
       <Reference name="OpenMetaverse.StructuredData" path="../../../bin/"/>
+      <Reference name="OpenSim.Data"/>
+      <Reference name="OpenSim.Data.MySQL"/>
+      <Reference name="MySql.Data" path="../../../bin/"/>
       <Reference name="OpenSim.Framework"/>
       <Reference name="OpenSim.Framework.Console"/>
       <Reference name="OpenSim.Framework.Servers.HttpServer"/>
-- 
cgit v1.1