StatisticsHistory.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /// \file StatisticsHistory.h
  11. /// \brief Input numerical values over time. Get sum, average, highest, lowest, standard deviation on recent or all-time values
  12. #include "NativeFeatureIncludes.h"
  13. #if _RAKNET_SUPPORT_StatisticsHistory==1
  14. #ifndef __STATISTICS_HISTORY_H
  15. #define __STATISTICS_HISTORY_H
  16. #include "PluginInterface2.h"
  17. #include "RakMemoryOverride.h"
  18. #include "NativeTypes.h"
  19. #include "DS_List.h"
  20. #include "RakNetTypes.h"
  21. #include "DS_OrderedList.h"
  22. #include "RakString.h"
  23. #include "DS_Queue.h"
  24. #include "DS_Hash.h"
  25. #include <float.h>
  26. namespace RakNet
  27. {
  28. /// Forward declarations
  29. class RakPeerInterface;
  30. // Type used to track values. If needed, change to double and recompile
  31. typedef double SHValueType;
  32. #define SH_TYPE_MAX DBL_MAX
  33. /// \brief Input numerical values over time. Get sum, average, highest, lowest, standard deviation on recent or all-time values
  34. class RAK_DLL_EXPORT StatisticsHistory
  35. {
  36. public:
  37. // GetInstance() and DestroyInstance(instance*)
  38. STATIC_FACTORY_DECLARATIONS(StatisticsHistory)
  39. enum SHErrorCode
  40. {
  41. SH_OK,
  42. SH_UKNOWN_OBJECT,
  43. SH_UKNOWN_KEY,
  44. SH_INVALID_PARAMETER,
  45. };
  46. enum SHSortOperation
  47. {
  48. SH_DO_NOT_SORT,
  49. SH_SORT_BY_RECENT_SUM_ASCENDING,
  50. SH_SORT_BY_RECENT_SUM_DESCENDING,
  51. SH_SORT_BY_LONG_TERM_SUM_ASCENDING,
  52. SH_SORT_BY_LONG_TERM_SUM_DESCENDING,
  53. SH_SORT_BY_RECENT_SUM_OF_SQUARES_ASCENDING,
  54. SH_SORT_BY_RECENT_SUM_OF_SQUARES_DESCENDING,
  55. SH_SORT_BY_RECENT_AVERAGE_ASCENDING,
  56. SH_SORT_BY_RECENT_AVERAGE_DESCENDING,
  57. SH_SORT_BY_LONG_TERM_AVERAGE_ASCENDING,
  58. SH_SORT_BY_LONG_TERM_AVERAGE_DESCENDING,
  59. SH_SORT_BY_RECENT_HIGHEST_ASCENDING,
  60. SH_SORT_BY_RECENT_HIGHEST_DESCENDING,
  61. SH_SORT_BY_RECENT_LOWEST_ASCENDING,
  62. SH_SORT_BY_RECENT_LOWEST_DESCENDING,
  63. SH_SORT_BY_LONG_TERM_HIGHEST_ASCENDING,
  64. SH_SORT_BY_LONG_TERM_HIGHEST_DESCENDING,
  65. SH_SORT_BY_LONG_TERM_LOWEST_ASCENDING,
  66. SH_SORT_BY_LONG_TERM_LOWEST_DESCENDING,
  67. };
  68. enum SHDataCategory
  69. {
  70. /// Insert values from one set into the other set, in time order
  71. /// Values at the same time end up in the final set twice
  72. /// Use when you have additional data points to add to a graph
  73. DC_DISCRETE,
  74. /// Add values from one set to values from the other set, at corresponding times
  75. /// If value at time t does not exist in the other set, linearly extrapolate value for other set based on nearest two data points
  76. /// longTerm* values are unknown using this method
  77. /// Use to add two graphs together
  78. DC_CONTINUOUS
  79. };
  80. struct TimeAndValue;
  81. struct TimeAndValueQueue;
  82. struct TrackedObjectData
  83. {
  84. TrackedObjectData();
  85. TrackedObjectData(uint64_t _objectId, int _objectType, void *_userData);
  86. uint64_t objectId;
  87. int objectType;
  88. void *userData;
  89. };
  90. StatisticsHistory();
  91. virtual ~StatisticsHistory();
  92. void SetDefaultTimeToTrack(Time defaultTimeToTrack);
  93. Time GetDefaultTimeToTrack(void) const;
  94. bool AddObject(TrackedObjectData tod);
  95. bool RemoveObject(uint64_t objectId, void **userData);
  96. void RemoveObjectAtIndex(unsigned int index);
  97. void Clear(void);
  98. unsigned int GetObjectCount(void) const;
  99. StatisticsHistory::TrackedObjectData * GetObjectAtIndex(unsigned int index) const;
  100. unsigned int GetObjectIndex(uint64_t objectId) const;
  101. bool AddValueByObjectID(uint64_t objectId, RakString key, SHValueType val, Time curTime, bool combineEqualTimes);
  102. void AddValueByIndex(unsigned int index, RakString key, SHValueType val, Time curTime, bool combineEqualTimes);
  103. SHErrorCode GetHistoryForKey(uint64_t objectId, RakString key, TimeAndValueQueue **values, Time curTime) const;
  104. bool GetHistorySorted(uint64_t objectId, SHSortOperation sortType, DataStructures::List<TimeAndValueQueue *> &values) const;
  105. void MergeAllObjectsOnKey(RakString key, TimeAndValueQueue *tavqOutput, SHDataCategory dataCategory) const;
  106. void GetUniqueKeyList(DataStructures::List<RakString> &keys);
  107. struct TimeAndValue
  108. {
  109. Time time;
  110. SHValueType val;
  111. };
  112. struct TimeAndValueQueue
  113. {
  114. TimeAndValueQueue();
  115. ~TimeAndValueQueue();
  116. DataStructures::Queue<TimeAndValue> values;
  117. Time timeToTrackValues;
  118. RakString key;
  119. SHValueType recentSum;
  120. SHValueType recentSumOfSquares;
  121. SHValueType longTermSum;
  122. SHValueType longTermCount;
  123. SHValueType longTermLowest;
  124. SHValueType longTermHighest;
  125. void SetTimeToTrackValues(Time t);
  126. Time GetTimeToTrackValues(void) const;
  127. SHValueType GetRecentSum(void) const;
  128. SHValueType GetRecentSumOfSquares(void) const;
  129. SHValueType GetLongTermSum(void) const;
  130. SHValueType GetRecentAverage(void) const;
  131. SHValueType GetRecentLowest(void) const;
  132. SHValueType GetRecentHighest(void) const;
  133. SHValueType GetRecentStandardDeviation(void) const;
  134. SHValueType GetLongTermAverage(void) const;
  135. SHValueType GetLongTermLowest(void) const;
  136. SHValueType GetLongTermHighest(void) const;
  137. SHValueType GetSumSinceTime(Time t) const;
  138. Time GetTimeRange(void) const;
  139. // Merge two sets to output
  140. static void MergeSets( const TimeAndValueQueue *lhs, SHDataCategory lhsDataCategory, const TimeAndValueQueue *rhs, SHDataCategory rhsDataCategory, TimeAndValueQueue *output );
  141. // Shrink or expand a sample set to the approximate number given
  142. // DC_DISCRETE will produce a histogram (sum) while DC_CONTINUOUS will produce an average
  143. void ResizeSampleSet( int approximateSamples, DataStructures::Queue<StatisticsHistory::TimeAndValue> &blendedSamples, SHDataCategory dataCategory, Time timeClipStart=0, Time timeClipEnd=0 );
  144. // Clear out all values
  145. void Clear(void);
  146. TimeAndValueQueue& operator = ( const TimeAndValueQueue& input );
  147. /// \internal
  148. void CullExpiredValues(Time curTime);
  149. /// \internal
  150. static SHValueType Interpolate(TimeAndValue t1, TimeAndValue t2, Time time);
  151. /// \internal
  152. SHValueType sortValue;
  153. };
  154. protected:
  155. struct TrackedObject;
  156. public:
  157. static int TrackedObjectComp( const uint64_t &key, TrackedObject* const &data );
  158. protected:
  159. struct TrackedObject
  160. {
  161. TrackedObject();
  162. ~TrackedObject();
  163. TrackedObjectData trackedObjectData;
  164. DataStructures::Hash<RakNet::RakString, TimeAndValueQueue*, 32, RakNet::RakString::ToInteger> dataQueues;
  165. };
  166. DataStructures::OrderedList<uint64_t, TrackedObject*,TrackedObjectComp> objects;
  167. Time timeToTrack;
  168. };
  169. /// \brief Input numerical values over time. Get sum, average, highest, lowest, standard deviation on recent or all-time values
  170. /// \ingroup PLUGINS_GROUP
  171. class RAK_DLL_EXPORT StatisticsHistoryPlugin : public PluginInterface2
  172. {
  173. public:
  174. // GetInstance() and DestroyInstance(instance*)
  175. STATIC_FACTORY_DECLARATIONS(StatisticsHistoryPlugin)
  176. StatisticsHistory statistics;
  177. StatisticsHistoryPlugin();
  178. virtual ~StatisticsHistoryPlugin();
  179. void SetTrackConnections(bool _addNewConnections, int newConnectionsObjectType, bool _removeLostConnections);
  180. protected:
  181. virtual void Update(void);
  182. virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
  183. virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming);
  184. // Too slow
  185. // virtual bool UsesReliabilityLayer(void) const {return true;}
  186. // virtual void OnDirectSocketSend(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress);
  187. // virtual void OnDirectSocketReceive(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress);
  188. bool addNewConnections;
  189. bool removeLostConnections;
  190. int newConnectionsObjectType;
  191. };
  192. } // namespace RakNet
  193. #endif // __STATISTICS_HISTORY_H
  194. #endif // _RAKNET_SUPPORT_*
粤ICP备19079148号