2008-01-31 00:42:31 +00:00
|
|
|
/*
|
2008-03-18 05:16:43 +00:00
|
|
|
* Copyright (c) Contributors, http://opensimulator.org/
|
|
|
|
* See CONTRIBUTORS.TXT for a full list of copyright holders.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2009-06-01 06:37:14 +00:00
|
|
|
* * Neither the name of the OpenSimulator Project nor the
|
2008-03-18 05:16:43 +00:00
|
|
|
* names of its contributors may be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
|
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2008-01-31 00:42:31 +00:00
|
|
|
|
2012-10-03 23:32:42 +00:00
|
|
|
using System;
|
2013-08-06 15:21:16 +00:00
|
|
|
using System.Collections;
|
2012-10-03 23:32:42 +00:00
|
|
|
using System.Collections.Generic;
|
2013-06-19 23:45:56 +00:00
|
|
|
using System.Linq;
|
2012-11-16 04:36:22 +00:00
|
|
|
using System.Text;
|
2012-10-03 23:32:42 +00:00
|
|
|
|
2013-08-06 15:21:16 +00:00
|
|
|
using OpenSim.Framework;
|
2013-07-12 21:04:14 +00:00
|
|
|
using OpenMetaverse.StructuredData;
|
|
|
|
|
2012-07-25 22:11:50 +00:00
|
|
|
namespace OpenSim.Framework.Monitoring
|
2008-05-16 01:22:11 +00:00
|
|
|
{
|
2008-01-31 00:42:31 +00:00
|
|
|
/// <summary>
|
2013-08-05 18:22:47 +00:00
|
|
|
/// Static class used to register/deregister/fetch statistics
|
2008-01-31 00:42:31 +00:00
|
|
|
/// </summary>
|
2013-08-05 18:22:47 +00:00
|
|
|
public static class StatsManager
|
2008-01-31 00:42:31 +00:00
|
|
|
{
|
2012-10-11 22:28:53 +00:00
|
|
|
// Subcommand used to list other stats.
|
|
|
|
public const string AllSubCommand = "all";
|
|
|
|
|
|
|
|
// Subcommand used to list other stats.
|
|
|
|
public const string ListSubCommand = "list";
|
|
|
|
|
|
|
|
// All subcommands
|
|
|
|
public static HashSet<string> SubCommands = new HashSet<string> { AllSubCommand, ListSubCommand };
|
|
|
|
|
2012-10-03 23:32:42 +00:00
|
|
|
/// <summary>
|
2012-10-11 22:28:53 +00:00
|
|
|
/// Registered stats categorized by category/container/shortname
|
2012-10-03 23:32:42 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
2012-10-11 22:28:53 +00:00
|
|
|
/// Do not add or remove directly from this dictionary.
|
2012-10-03 23:32:42 +00:00
|
|
|
/// </remarks>
|
2013-02-15 04:05:42 +00:00
|
|
|
public static SortedDictionary<string, SortedDictionary<string, SortedDictionary<string, Stat>>> RegisteredStats
|
|
|
|
= new SortedDictionary<string, SortedDictionary<string, SortedDictionary<string, Stat>>>();
|
2012-10-03 23:32:42 +00:00
|
|
|
|
2013-06-17 21:39:00 +00:00
|
|
|
// private static AssetStatsCollector assetStats;
|
|
|
|
// private static UserStatsCollector userStats;
|
|
|
|
// private static SimExtraStatsCollector simExtraStats = new SimExtraStatsCollector();
|
2008-05-16 01:22:11 +00:00
|
|
|
|
2013-06-17 21:39:00 +00:00
|
|
|
// public static AssetStatsCollector AssetStats { get { return assetStats; } }
|
|
|
|
// public static UserStatsCollector UserStats { get { return userStats; } }
|
|
|
|
public static SimExtraStatsCollector SimExtraStats { get; set; }
|
2008-05-16 01:22:11 +00:00
|
|
|
|
2012-10-11 23:10:51 +00:00
|
|
|
public static void RegisterConsoleCommands(ICommandConsole console)
|
2012-10-11 22:28:53 +00:00
|
|
|
{
|
|
|
|
console.Commands.AddCommand(
|
|
|
|
"General",
|
|
|
|
false,
|
2014-03-19 00:35:49 +00:00
|
|
|
"stats show",
|
|
|
|
"stats show [list|all|(<category>[.<container>])+",
|
2012-10-11 22:28:53 +00:00
|
|
|
"Show statistical information for this server",
|
|
|
|
"If no final argument is specified then legacy statistics information is currently shown.\n"
|
2013-06-19 23:45:56 +00:00
|
|
|
+ "'list' argument will show statistic categories.\n"
|
|
|
|
+ "'all' will show all statistics.\n"
|
|
|
|
+ "A <category> name will show statistics from that category.\n"
|
|
|
|
+ "A <category>.<container> name will show statistics from that category in that container.\n"
|
2016-08-24 05:57:21 +00:00
|
|
|
+ "More than one name can be given separated by spaces.\n",
|
2014-03-19 00:35:49 +00:00
|
|
|
HandleShowStatsCommand);
|
|
|
|
|
2016-08-25 03:55:01 +00:00
|
|
|
console.Commands.AddCommand(
|
|
|
|
"General",
|
|
|
|
false,
|
|
|
|
"show stats",
|
|
|
|
"show stats [list|all|(<category>[.<container>])+",
|
|
|
|
"Alias for 'stats show' command",
|
|
|
|
HandleShowStatsCommand);
|
2013-08-06 17:29:33 +00:00
|
|
|
StatsLogger.RegisterConsoleCommands(console);
|
2012-10-11 22:28:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void HandleShowStatsCommand(string module, string[] cmd)
|
|
|
|
{
|
|
|
|
ICommandConsole con = MainConsole.Instance;
|
|
|
|
|
|
|
|
if (cmd.Length > 2)
|
|
|
|
{
|
2013-06-19 23:45:56 +00:00
|
|
|
foreach (string name in cmd.Skip(2))
|
|
|
|
{
|
|
|
|
string[] components = name.Split('.');
|
2013-06-19 23:00:39 +00:00
|
|
|
|
2013-06-19 23:45:56 +00:00
|
|
|
string categoryName = components[0];
|
|
|
|
string containerName = components.Length > 1 ? components[1] : null;
|
2014-03-18 20:21:47 +00:00
|
|
|
string statName = components.Length > 2 ? components[2] : null;
|
2012-10-11 22:28:53 +00:00
|
|
|
|
2013-06-19 23:45:56 +00:00
|
|
|
if (categoryName == AllSubCommand)
|
|
|
|
{
|
2014-07-25 00:56:41 +00:00
|
|
|
OutputAllStatsToConsole(con);
|
2013-06-19 23:45:56 +00:00
|
|
|
}
|
|
|
|
else if (categoryName == ListSubCommand)
|
2012-10-11 22:28:53 +00:00
|
|
|
{
|
2013-06-19 23:45:56 +00:00
|
|
|
con.Output("Statistic categories available are:");
|
|
|
|
foreach (string category in RegisteredStats.Keys)
|
|
|
|
con.OutputFormat(" {0}", category);
|
2012-10-11 22:28:53 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-19 23:45:56 +00:00
|
|
|
SortedDictionary<string, SortedDictionary<string, Stat>> category;
|
|
|
|
if (!RegisteredStats.TryGetValue(categoryName, out category))
|
2013-06-19 23:00:39 +00:00
|
|
|
{
|
2013-06-19 23:45:56 +00:00
|
|
|
con.OutputFormat("No such category as {0}", categoryName);
|
2013-06-19 23:00:39 +00:00
|
|
|
}
|
2013-02-24 01:04:19 +00:00
|
|
|
else
|
|
|
|
{
|
2013-06-19 23:45:56 +00:00
|
|
|
if (String.IsNullOrEmpty(containerName))
|
2013-02-24 01:04:19 +00:00
|
|
|
{
|
2014-07-25 00:56:41 +00:00
|
|
|
OutputCategoryStatsToConsole(con, category);
|
2013-02-24 01:04:19 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-19 23:45:56 +00:00
|
|
|
SortedDictionary<string, Stat> container;
|
|
|
|
if (category.TryGetValue(containerName, out container))
|
|
|
|
{
|
2014-03-18 20:21:47 +00:00
|
|
|
if (String.IsNullOrEmpty(statName))
|
|
|
|
{
|
2014-07-25 00:56:41 +00:00
|
|
|
OutputContainerStatsToConsole(con, container);
|
2014-03-18 20:21:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Stat stat;
|
|
|
|
if (container.TryGetValue(statName, out stat))
|
|
|
|
{
|
2014-07-25 00:56:41 +00:00
|
|
|
OutputStatToConsole(con, stat);
|
2014-03-18 20:21:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
con.OutputFormat(
|
|
|
|
"No such stat {0} in {1}.{2}", statName, categoryName, containerName);
|
|
|
|
}
|
|
|
|
}
|
2013-06-19 23:45:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
con.OutputFormat("No such container {0} in category {1}", containerName, categoryName);
|
|
|
|
}
|
2013-02-24 01:04:19 +00:00
|
|
|
}
|
|
|
|
}
|
2012-10-11 22:28:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Legacy
|
2013-06-17 21:39:00 +00:00
|
|
|
if (SimExtraStats != null)
|
2014-07-25 00:56:41 +00:00
|
|
|
con.Output(SimExtraStats.Report());
|
2013-06-17 21:39:00 +00:00
|
|
|
else
|
2014-07-25 00:56:41 +00:00
|
|
|
OutputAllStatsToConsole(con);
|
2013-06-17 21:39:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-06 17:29:33 +00:00
|
|
|
public static List<string> GetAllStatsReports()
|
2013-06-17 21:39:00 +00:00
|
|
|
{
|
2013-08-06 17:29:33 +00:00
|
|
|
List<string> reports = new List<string>();
|
|
|
|
|
2013-06-17 21:39:00 +00:00
|
|
|
foreach (var category in RegisteredStats.Values)
|
2013-08-06 17:29:33 +00:00
|
|
|
reports.AddRange(GetCategoryStatsReports(category));
|
|
|
|
|
|
|
|
return reports;
|
|
|
|
}
|
|
|
|
|
2014-07-25 00:56:41 +00:00
|
|
|
private static void OutputAllStatsToConsole(ICommandConsole con)
|
|
|
|
{
|
|
|
|
foreach (string report in GetAllStatsReports())
|
|
|
|
con.Output(report);
|
|
|
|
}
|
|
|
|
|
2013-08-06 17:29:33 +00:00
|
|
|
private static List<string> GetCategoryStatsReports(
|
|
|
|
SortedDictionary<string, SortedDictionary<string, Stat>> category)
|
|
|
|
{
|
|
|
|
List<string> reports = new List<string>();
|
|
|
|
|
|
|
|
foreach (var container in category.Values)
|
|
|
|
reports.AddRange(GetContainerStatsReports(container));
|
|
|
|
|
|
|
|
return reports;
|
2012-10-11 22:28:53 +00:00
|
|
|
}
|
|
|
|
|
2014-07-25 00:56:41 +00:00
|
|
|
private static void OutputCategoryStatsToConsole(
|
|
|
|
ICommandConsole con, SortedDictionary<string, SortedDictionary<string, Stat>> category)
|
|
|
|
{
|
|
|
|
foreach (string report in GetCategoryStatsReports(category))
|
|
|
|
con.Output(report);
|
|
|
|
}
|
|
|
|
|
2013-08-06 17:29:33 +00:00
|
|
|
private static List<string> GetContainerStatsReports(SortedDictionary<string, Stat> container)
|
2013-02-24 01:04:19 +00:00
|
|
|
{
|
2013-08-06 17:29:33 +00:00
|
|
|
List<string> reports = new List<string>();
|
|
|
|
|
2013-02-24 01:04:19 +00:00
|
|
|
foreach (Stat stat in container.Values)
|
2013-08-06 17:29:33 +00:00
|
|
|
reports.Add(stat.ToConsoleString());
|
|
|
|
|
|
|
|
return reports;
|
|
|
|
}
|
|
|
|
|
2014-07-25 00:56:41 +00:00
|
|
|
private static void OutputContainerStatsToConsole(
|
|
|
|
ICommandConsole con, SortedDictionary<string, Stat> container)
|
|
|
|
{
|
|
|
|
foreach (string report in GetContainerStatsReports(container))
|
|
|
|
con.Output(report);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void OutputStatToConsole(ICommandConsole con, Stat stat)
|
|
|
|
{
|
|
|
|
con.Output(stat.ToConsoleString());
|
|
|
|
}
|
|
|
|
|
2013-07-12 21:04:14 +00:00
|
|
|
// Creates an OSDMap of the format:
|
|
|
|
// { categoryName: {
|
|
|
|
// containerName: {
|
|
|
|
// statName: {
|
|
|
|
// "Name": name,
|
|
|
|
// "ShortName": shortName,
|
|
|
|
// ...
|
|
|
|
// },
|
|
|
|
// statName: {
|
|
|
|
// "Name": name,
|
|
|
|
// "ShortName": shortName,
|
|
|
|
// ...
|
|
|
|
// },
|
|
|
|
// ...
|
|
|
|
// },
|
|
|
|
// containerName: {
|
|
|
|
// ...
|
|
|
|
// },
|
|
|
|
// ...
|
|
|
|
// },
|
|
|
|
// categoryName: {
|
|
|
|
// ...
|
|
|
|
// },
|
|
|
|
// ...
|
|
|
|
// }
|
|
|
|
// The passed in parameters will filter the categories, containers and stats returned. If any of the
|
|
|
|
// parameters are either EmptyOrNull or the AllSubCommand value, all of that type will be returned.
|
|
|
|
// Case matters.
|
|
|
|
public static OSDMap GetStatsAsOSDMap(string pCategoryName, string pContainerName, string pStatName)
|
|
|
|
{
|
|
|
|
OSDMap map = new OSDMap();
|
|
|
|
|
2015-12-27 23:05:17 +00:00
|
|
|
lock (RegisteredStats)
|
2013-07-12 21:04:14 +00:00
|
|
|
{
|
2015-12-27 23:05:17 +00:00
|
|
|
foreach (string catName in RegisteredStats.Keys)
|
2013-07-12 21:04:14 +00:00
|
|
|
{
|
2015-12-27 23:05:17 +00:00
|
|
|
// Do this category if null spec, "all" subcommand or category name matches passed parameter.
|
|
|
|
// Skip category if none of the above.
|
|
|
|
if (!(String.IsNullOrEmpty(pCategoryName) || pCategoryName == AllSubCommand || pCategoryName == catName))
|
2013-07-12 21:04:14 +00:00
|
|
|
continue;
|
|
|
|
|
2015-12-27 23:05:17 +00:00
|
|
|
OSDMap contMap = new OSDMap();
|
|
|
|
foreach (string contName in RegisteredStats[catName].Keys)
|
2013-07-12 21:04:14 +00:00
|
|
|
{
|
2015-12-27 23:05:17 +00:00
|
|
|
if (!(string.IsNullOrEmpty(pContainerName) || pContainerName == AllSubCommand || pContainerName == contName))
|
2013-07-12 21:04:14 +00:00
|
|
|
continue;
|
2017-01-05 19:07:37 +00:00
|
|
|
|
2015-12-27 23:05:17 +00:00
|
|
|
OSDMap statMap = new OSDMap();
|
2013-07-12 21:04:14 +00:00
|
|
|
|
2015-12-27 23:05:17 +00:00
|
|
|
SortedDictionary<string, Stat> theStats = RegisteredStats[catName][contName];
|
|
|
|
foreach (string statName in theStats.Keys)
|
|
|
|
{
|
|
|
|
if (!(String.IsNullOrEmpty(pStatName) || pStatName == AllSubCommand || pStatName == statName))
|
|
|
|
continue;
|
2013-07-12 21:04:14 +00:00
|
|
|
|
2015-12-27 23:05:17 +00:00
|
|
|
statMap.Add(statName, theStats[statName].ToBriefOSDMap());
|
|
|
|
}
|
|
|
|
|
|
|
|
contMap.Add(contName, statMap);
|
|
|
|
}
|
|
|
|
map.Add(catName, contMap);
|
2013-07-12 21:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
2013-08-06 15:21:16 +00:00
|
|
|
public static Hashtable HandleStatsRequest(Hashtable request)
|
|
|
|
{
|
|
|
|
Hashtable responsedata = new Hashtable();
|
2013-08-16 23:51:21 +00:00
|
|
|
// string regpath = request["uri"].ToString();
|
2013-08-06 15:21:16 +00:00
|
|
|
int response_code = 200;
|
|
|
|
string contenttype = "text/json";
|
|
|
|
|
|
|
|
string pCategoryName = StatsManager.AllSubCommand;
|
|
|
|
string pContainerName = StatsManager.AllSubCommand;
|
|
|
|
string pStatName = StatsManager.AllSubCommand;
|
|
|
|
|
|
|
|
if (request.ContainsKey("cat")) pCategoryName = request["cat"].ToString();
|
|
|
|
if (request.ContainsKey("cont")) pContainerName = request["cat"].ToString();
|
2015-01-01 17:39:07 +00:00
|
|
|
if (request.ContainsKey("stat")) pStatName = request["stat"].ToString();
|
2013-08-06 15:21:16 +00:00
|
|
|
|
|
|
|
string strOut = StatsManager.GetStatsAsOSDMap(pCategoryName, pContainerName, pStatName).ToString();
|
|
|
|
|
2013-08-08 16:41:11 +00:00
|
|
|
// If requestor wants it as a callback function, build response as a function rather than just the JSON string.
|
|
|
|
if (request.ContainsKey("callback"))
|
|
|
|
{
|
|
|
|
strOut = request["callback"].ToString() + "(" + strOut + ");";
|
|
|
|
}
|
|
|
|
|
2013-08-06 15:21:16 +00:00
|
|
|
// m_log.DebugFormat("{0} StatFetch: uri={1}, cat={2}, cont={3}, stat={4}, resp={5}",
|
|
|
|
// LogHeader, regpath, pCategoryName, pContainerName, pStatName, strOut);
|
|
|
|
|
|
|
|
responsedata["int_response_code"] = response_code;
|
|
|
|
responsedata["content_type"] = contenttype;
|
|
|
|
responsedata["keepalive"] = false;
|
|
|
|
responsedata["str_response_string"] = strOut;
|
|
|
|
responsedata["access_control_allow_origin"] = "*";
|
|
|
|
|
|
|
|
return responsedata;
|
|
|
|
}
|
|
|
|
|
2013-06-17 21:39:00 +00:00
|
|
|
// /// <summary>
|
|
|
|
// /// Start collecting statistics related to assets.
|
|
|
|
// /// Should only be called once.
|
|
|
|
// /// </summary>
|
|
|
|
// public static AssetStatsCollector StartCollectingAssetStats()
|
|
|
|
// {
|
|
|
|
// assetStats = new AssetStatsCollector();
|
|
|
|
//
|
|
|
|
// return assetStats;
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// /// <summary>
|
|
|
|
// /// Start collecting statistics related to users.
|
|
|
|
// /// Should only be called once.
|
|
|
|
// /// </summary>
|
|
|
|
// public static UserStatsCollector StartCollectingUserStats()
|
|
|
|
// {
|
|
|
|
// userStats = new UserStatsCollector();
|
|
|
|
//
|
|
|
|
// return userStats;
|
|
|
|
// }
|
2012-10-03 23:32:42 +00:00
|
|
|
|
2012-10-11 22:28:53 +00:00
|
|
|
/// <summary>
|
2013-08-05 18:22:47 +00:00
|
|
|
/// Register a statistic.
|
2012-10-11 22:28:53 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name='stat'></param>
|
|
|
|
/// <returns></returns>
|
2012-10-03 23:32:42 +00:00
|
|
|
public static bool RegisterStat(Stat stat)
|
|
|
|
{
|
2016-08-24 05:26:31 +00:00
|
|
|
SortedDictionary<string, SortedDictionary<string, Stat>> category = null;
|
|
|
|
SortedDictionary<string, Stat> container = null;
|
2012-10-11 22:28:53 +00:00
|
|
|
|
2012-10-03 23:32:42 +00:00
|
|
|
lock (RegisteredStats)
|
|
|
|
{
|
2012-10-11 22:28:53 +00:00
|
|
|
// Stat name is not unique across category/container/shortname key.
|
|
|
|
// XXX: For now just return false. This is to avoid problems in regression tests where all tests
|
|
|
|
// in a class are run in the same instance of the VM.
|
2013-07-29 22:18:29 +00:00
|
|
|
if (TryGetStatParents(stat, out category, out container))
|
2012-10-03 23:32:42 +00:00
|
|
|
return false;
|
|
|
|
|
2016-08-24 05:26:31 +00:00
|
|
|
if (container == null)
|
|
|
|
container = new SortedDictionary<string, Stat>();
|
2012-10-11 22:28:53 +00:00
|
|
|
|
2016-08-24 05:26:31 +00:00
|
|
|
if (category == null)
|
|
|
|
category = new SortedDictionary<string, SortedDictionary<string, Stat>>();
|
2012-10-03 23:32:42 +00:00
|
|
|
|
2016-08-24 05:26:31 +00:00
|
|
|
container[stat.ShortName] = stat;
|
|
|
|
category[stat.Container] = container;
|
|
|
|
RegisteredStats[stat.Category] = category;
|
2012-10-03 23:32:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-10-11 22:28:53 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Deregister a statistic
|
|
|
|
/// </summary>>
|
|
|
|
/// <param name='stat'></param>
|
2013-02-15 04:05:42 +00:00
|
|
|
/// <returns></returns>
|
2012-10-03 23:32:42 +00:00
|
|
|
public static bool DeregisterStat(Stat stat)
|
|
|
|
{
|
2016-08-24 05:57:21 +00:00
|
|
|
SortedDictionary<string, SortedDictionary<string, Stat>> category = null;
|
|
|
|
SortedDictionary<string, Stat> container = null;
|
2012-10-11 22:28:53 +00:00
|
|
|
|
2012-10-03 23:32:42 +00:00
|
|
|
lock (RegisteredStats)
|
|
|
|
{
|
2013-07-29 22:18:29 +00:00
|
|
|
if (!TryGetStatParents(stat, out category, out container))
|
2012-10-03 23:32:42 +00:00
|
|
|
return false;
|
2017-01-05 19:07:37 +00:00
|
|
|
|
2016-08-24 05:57:21 +00:00
|
|
|
if(container != null)
|
|
|
|
{
|
|
|
|
container.Remove(stat.ShortName);
|
|
|
|
if(category != null && container.Count == 0)
|
|
|
|
{
|
|
|
|
category.Remove(stat.Container);
|
|
|
|
if(category.Count == 0)
|
|
|
|
RegisteredStats.Remove(stat.Category);
|
|
|
|
}
|
|
|
|
}
|
2012-10-03 23:32:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2012-10-11 22:28:53 +00:00
|
|
|
|
2013-07-29 22:18:29 +00:00
|
|
|
public static bool TryGetStat(string category, string container, string statShortName, out Stat stat)
|
2012-10-11 22:28:53 +00:00
|
|
|
{
|
2013-07-29 22:18:29 +00:00
|
|
|
stat = null;
|
|
|
|
SortedDictionary<string, SortedDictionary<string, Stat>> categoryStats;
|
|
|
|
|
|
|
|
lock (RegisteredStats)
|
|
|
|
{
|
|
|
|
if (!TryGetStatsForCategory(category, out categoryStats))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SortedDictionary<string, Stat> containerStats;
|
|
|
|
|
|
|
|
if (!categoryStats.TryGetValue(container, out containerStats))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return containerStats.TryGetValue(statShortName, out stat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool TryGetStatsForCategory(
|
|
|
|
string category, out SortedDictionary<string, SortedDictionary<string, Stat>> stats)
|
|
|
|
{
|
|
|
|
lock (RegisteredStats)
|
|
|
|
return RegisteredStats.TryGetValue(category, out stats);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Get the same stat for each container in a given category.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>
|
|
|
|
/// The stats if there were any to fetch. Otherwise null.
|
|
|
|
/// </returns>
|
|
|
|
/// <param name='category'></param>
|
|
|
|
/// <param name='statShortName'></param>
|
|
|
|
public static List<Stat> GetStatsFromEachContainer(string category, string statShortName)
|
|
|
|
{
|
|
|
|
SortedDictionary<string, SortedDictionary<string, Stat>> categoryStats;
|
|
|
|
|
|
|
|
lock (RegisteredStats)
|
|
|
|
{
|
|
|
|
if (!RegisteredStats.TryGetValue(category, out categoryStats))
|
|
|
|
return null;
|
|
|
|
|
|
|
|
List<Stat> stats = null;
|
|
|
|
|
|
|
|
foreach (SortedDictionary<string, Stat> containerStats in categoryStats.Values)
|
|
|
|
{
|
|
|
|
if (containerStats.ContainsKey(statShortName))
|
|
|
|
{
|
|
|
|
if (stats == null)
|
|
|
|
stats = new List<Stat>();
|
|
|
|
|
|
|
|
stats.Add(containerStats[statShortName]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stats;
|
|
|
|
}
|
2012-10-11 22:28:53 +00:00
|
|
|
}
|
|
|
|
|
2013-07-29 22:18:29 +00:00
|
|
|
public static bool TryGetStatParents(
|
2012-10-11 22:28:53 +00:00
|
|
|
Stat stat,
|
2013-02-15 04:05:42 +00:00
|
|
|
out SortedDictionary<string, SortedDictionary<string, Stat>> category,
|
|
|
|
out SortedDictionary<string, Stat> container)
|
2012-10-11 22:28:53 +00:00
|
|
|
{
|
|
|
|
category = null;
|
|
|
|
container = null;
|
|
|
|
|
|
|
|
lock (RegisteredStats)
|
|
|
|
{
|
|
|
|
if (RegisteredStats.TryGetValue(stat.Category, out category))
|
|
|
|
{
|
|
|
|
if (category.TryGetValue(stat.Container, out container))
|
|
|
|
{
|
|
|
|
if (container.ContainsKey(stat.ShortName))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-16 04:36:22 +00:00
|
|
|
|
|
|
|
public static void RecordStats()
|
|
|
|
{
|
|
|
|
lock (RegisteredStats)
|
|
|
|
{
|
2013-02-15 04:05:42 +00:00
|
|
|
foreach (SortedDictionary<string, SortedDictionary<string, Stat>> category in RegisteredStats.Values)
|
2012-11-16 04:36:22 +00:00
|
|
|
{
|
2013-02-15 04:05:42 +00:00
|
|
|
foreach (SortedDictionary<string, Stat> container in category.Values)
|
2012-11-16 04:36:22 +00:00
|
|
|
{
|
|
|
|
foreach (Stat stat in container.Values)
|
|
|
|
{
|
|
|
|
if (stat.MeasuresOfInterest != MeasuresOfInterest.None)
|
|
|
|
stat.RecordValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-03 23:32:42 +00:00
|
|
|
}
|
|
|
|
|
2012-10-23 00:50:05 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Stat type.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// A push stat is one which is continually updated and so it's value can simply by read.
|
|
|
|
/// A pull stat is one where reading the value triggers a collection method - the stat is not continually updated.
|
|
|
|
/// </remarks>
|
|
|
|
public enum StatType
|
|
|
|
{
|
|
|
|
Push,
|
|
|
|
Pull
|
|
|
|
}
|
|
|
|
|
2012-11-16 04:36:22 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Measures of interest for this stat.
|
|
|
|
/// </summary>
|
|
|
|
[Flags]
|
|
|
|
public enum MeasuresOfInterest
|
|
|
|
{
|
|
|
|
None,
|
|
|
|
AverageChangeOverTime
|
|
|
|
}
|
|
|
|
|
2012-10-03 23:32:42 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Verbosity of stat.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// Info will always be displayed.
|
|
|
|
/// </remarks>
|
|
|
|
public enum StatVerbosity
|
|
|
|
{
|
|
|
|
Debug,
|
|
|
|
Info
|
|
|
|
}
|
2015-02-18 00:14:08 +00:00
|
|
|
}
|