1
0

Sessions_Report.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.Text;
  31. using Mono.Data.SqliteClient;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. namespace OpenSim.Region.UserStatistics
  35. {
  36. public class Sessions_Report : IStatsController
  37. {
  38. #region IStatsController Members
  39. public string ReportName
  40. {
  41. get { return "Sessions"; }
  42. }
  43. public Hashtable ProcessModel(Hashtable pParams)
  44. {
  45. Hashtable modeldata = new Hashtable();
  46. modeldata.Add("Scenes", pParams["Scenes"]);
  47. modeldata.Add("Reports", pParams["Reports"]);
  48. SqliteConnection dbConn = (SqliteConnection)pParams["DatabaseConnection"];
  49. List<SessionList> lstSessions = new List<SessionList>();
  50. Hashtable requestvars = (Hashtable) pParams["RequestVars"];
  51. string puserUUID = string.Empty;
  52. string clientVersionString = string.Empty;
  53. int queryparams = 0;
  54. if (requestvars != null)
  55. {
  56. if (requestvars.ContainsKey("UserID"))
  57. {
  58. UUID testUUID = UUID.Zero;
  59. if (UUID.TryParse(requestvars["UserID"].ToString(), out testUUID))
  60. {
  61. puserUUID = requestvars["UserID"].ToString();
  62. }
  63. }
  64. if (requestvars.ContainsKey("VersionString"))
  65. {
  66. clientVersionString = requestvars["VersionString"].ToString();
  67. }
  68. }
  69. lock (dbConn)
  70. {
  71. string sql =
  72. "SELECT distinct a.name_f, a.name_l, a.Agent_ID, b.Session_ID, b.client_version, b.last_updated, b.start_time FROM stats_session_data a LEFT OUTER JOIN stats_session_data b ON a.Agent_ID = b.Agent_ID";
  73. if (puserUUID.Length > 0)
  74. {
  75. if (queryparams == 0)
  76. sql += " WHERE";
  77. else
  78. sql += " AND";
  79. sql += " b.agent_id=:agent_id";
  80. queryparams++;
  81. }
  82. if (clientVersionString.Length > 0)
  83. {
  84. if (queryparams == 0)
  85. sql += " WHERE";
  86. else
  87. sql += " AND";
  88. sql += " b.client_version=:client_version";
  89. queryparams++;
  90. }
  91. sql += " ORDER BY a.name_f, a.name_l, b.last_updated;";
  92. SqliteCommand cmd = new SqliteCommand(sql, dbConn);
  93. if (puserUUID.Length > 0)
  94. cmd.Parameters.Add(new SqliteParameter(":agent_id", puserUUID));
  95. if (clientVersionString.Length > 0)
  96. cmd.Parameters.Add(new SqliteParameter(":client_version", clientVersionString));
  97. SqliteDataReader sdr = cmd.ExecuteReader();
  98. if (sdr.HasRows)
  99. {
  100. UUID userUUID = UUID.Zero;
  101. SessionList activeSessionList = new SessionList();
  102. activeSessionList.user_id=UUID.Random();
  103. while (sdr.Read())
  104. {
  105. UUID readUUID = UUID.Parse(sdr["agent_id"].ToString());
  106. if (readUUID != userUUID)
  107. {
  108. activeSessionList = new SessionList();
  109. activeSessionList.user_id = readUUID;
  110. activeSessionList.firstname = sdr["name_f"].ToString();
  111. activeSessionList.lastname = sdr["name_l"].ToString();
  112. activeSessionList.sessions = new List<ShortSessionData>();
  113. lstSessions.Add(activeSessionList);
  114. }
  115. ShortSessionData ssd = new ShortSessionData();
  116. ssd.last_update = Utils.UnixTimeToDateTime((uint)Convert.ToInt32(sdr["last_updated"]));
  117. ssd.start_time = Utils.UnixTimeToDateTime((uint)Convert.ToInt32(sdr["start_time"]));
  118. ssd.session_id = UUID.Parse(sdr["session_id"].ToString());
  119. ssd.client_version = sdr["client_version"].ToString();
  120. activeSessionList.sessions.Add(ssd);
  121. userUUID = activeSessionList.user_id;
  122. }
  123. }
  124. sdr.Close();
  125. sdr.Dispose();
  126. }
  127. modeldata["SessionData"] = lstSessions;
  128. return modeldata;
  129. }
  130. public string RenderView(Hashtable pModelResult)
  131. {
  132. List<SessionList> lstSession = (List<SessionList>) pModelResult["SessionData"];
  133. Dictionary<string, IStatsController> reports = (Dictionary<string, IStatsController>)pModelResult["Reports"];
  134. const string STYLESHEET =
  135. @"
  136. <STYLE>
  137. body
  138. {
  139. font-size:15px; font-family:Helvetica, Verdana; color:Black;
  140. }
  141. TABLE.defaultr { }
  142. TR.defaultr { padding: 5px; }
  143. TD.header { font-weight:bold; padding:5px; }
  144. TD.content {}
  145. TD.contentright { text-align: right; }
  146. TD.contentcenter { text-align: center; }
  147. TD.align_top { vertical-align: top; }
  148. </STYLE>
  149. ";
  150. StringBuilder output = new StringBuilder();
  151. HTMLUtil.HtmlHeaders_O(ref output);
  152. output.Append(STYLESHEET);
  153. HTMLUtil.HtmlHeaders_C(ref output);
  154. HTMLUtil.AddReportLinks(ref output, reports, "");
  155. HTMLUtil.TABLE_O(ref output, "defaultr");
  156. HTMLUtil.TR_O(ref output, "defaultr");
  157. HTMLUtil.TD_O(ref output, "header");
  158. output.Append("FirstName");
  159. HTMLUtil.TD_C(ref output);
  160. HTMLUtil.TD_O(ref output, "header");
  161. output.Append("LastName");
  162. HTMLUtil.TD_C(ref output);
  163. HTMLUtil.TD_O(ref output, "header");
  164. output.Append("SessionEnd");
  165. HTMLUtil.TD_C(ref output);
  166. HTMLUtil.TD_O(ref output, "header");
  167. output.Append("SessionLength");
  168. HTMLUtil.TD_C(ref output);
  169. HTMLUtil.TD_O(ref output, "header");
  170. output.Append("Client");
  171. HTMLUtil.TD_C(ref output);
  172. HTMLUtil.TR_C(ref output);
  173. if (lstSession.Count == 0)
  174. {
  175. HTMLUtil.TR_O(ref output, "");
  176. HTMLUtil.TD_O(ref output, "align_top", 1, 5);
  177. output.Append("No results for that query");
  178. HTMLUtil.TD_C(ref output);
  179. HTMLUtil.TR_C(ref output);
  180. }
  181. foreach (SessionList ssnlst in lstSession)
  182. {
  183. int cnt = 0;
  184. foreach (ShortSessionData sesdata in ssnlst.sessions)
  185. {
  186. HTMLUtil.TR_O(ref output, "");
  187. if (cnt++ == 0)
  188. {
  189. HTMLUtil.TD_O(ref output, "align_top", ssnlst.sessions.Count, 1);
  190. output.Append(ssnlst.firstname);
  191. HTMLUtil.TD_C(ref output);
  192. HTMLUtil.TD_O(ref output, "align_top", ssnlst.sessions.Count, 1);
  193. output.Append(ssnlst.lastname);
  194. HTMLUtil.TD_C(ref output);
  195. }
  196. HTMLUtil.TD_O(ref output, "content");
  197. output.Append(sesdata.last_update.ToShortDateString());
  198. output.Append(" - ");
  199. output.Append(sesdata.last_update.ToShortTimeString());
  200. HTMLUtil.TD_C(ref output);
  201. HTMLUtil.TD_O(ref output, "content");
  202. TimeSpan dtlength = sesdata.last_update.Subtract(sesdata.start_time);
  203. if (dtlength.Days > 0)
  204. {
  205. output.Append(dtlength.Days);
  206. output.Append(" Days ");
  207. }
  208. if (dtlength.Hours > 0)
  209. {
  210. output.Append(dtlength.Hours);
  211. output.Append(" Hours ");
  212. }
  213. if (dtlength.Minutes > 0)
  214. {
  215. output.Append(dtlength.Minutes);
  216. output.Append(" Minutes");
  217. }
  218. HTMLUtil.TD_C(ref output);
  219. HTMLUtil.TD_O(ref output, "content");
  220. output.Append(sesdata.client_version);
  221. HTMLUtil.TD_C(ref output);
  222. HTMLUtil.TR_C(ref output);
  223. }
  224. HTMLUtil.TR_O(ref output, "");
  225. HTMLUtil.TD_O(ref output, "align_top", 1, 5);
  226. HTMLUtil.HR(ref output, "");
  227. HTMLUtil.TD_C(ref output);
  228. HTMLUtil.TR_C(ref output);
  229. }
  230. HTMLUtil.TABLE_C(ref output);
  231. output.Append("</BODY>\n</HTML>");
  232. return output.ToString();
  233. }
  234. public class SessionList
  235. {
  236. public string firstname;
  237. public string lastname;
  238. public UUID user_id;
  239. public List<ShortSessionData> sessions;
  240. }
  241. public struct ShortSessionData
  242. {
  243. public UUID session_id;
  244. public string client_version;
  245. public DateTime last_update;
  246. public DateTime start_time;
  247. }
  248. #endregion
  249. }
  250. }