Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

412 lines
13 KiB

  1. //
  2. // MODULE: COUNTER.H
  3. //
  4. // PURPOSE: interface for the counter classes:
  5. // CPeriodicTotals (utility class)
  6. // CAbstractCounter (abstract base class).
  7. // CCounter (simple counter)
  8. // CHourlyCounter (counter with "bins" for each hour of the day)
  9. // CDailyCounter (counter with "bins" for day of the week)
  10. // CHourlyDailyCounter (counter with "bins" for each hour of the day and each day of the week)
  11. //
  12. // PROJECT: Generic Troubleshooter DLL for Microsoft AnswerPoint
  13. //
  14. // COMPANY: Saltmine Creative, Inc. (206)-284-7511 [email protected]
  15. //
  16. // AUTHOR: Joe Mabel
  17. // MODIFIED: Oleg Kalosha 10-20-98
  18. //
  19. // ORIGINAL DATE: 7-20-1998
  20. //
  21. // NOTES:
  22. // 1. CPeriodicTotals might better be implemented using STL vectors. We wrote this
  23. // before we really started bringing STL into this application. JM 10/98
  24. //
  25. // Version Date By Comments
  26. //--------------------------------------------------------------------
  27. // V3.0 7-20-98 JM Original
  28. //
  29. //////////////////////////////////////////////////////////////////////
  30. #if !defined(AFX_COUNTER_H__07B5ABBD_2005_11D2_95D0_00C04FC22ADD__INCLUDED_)
  31. #define AFX_COUNTER_H__07B5ABBD_2005_11D2_95D0_00C04FC22ADD__INCLUDED_
  32. #if _MSC_VER >= 1000
  33. #pragma once
  34. #endif // _MSC_VER >= 1000
  35. #include <time.h>
  36. #include "apgtsstr.h"
  37. ////////////////////////////////////////////////////////////////////////////////////
  38. // a utility class to allow the expression of a series of time periods & associated counts
  39. ////////////////////////////////////////////////////////////////////////////////////
  40. class CPeriodicTotals
  41. {
  42. public:
  43. CPeriodicTotals(long nPeriods);
  44. virtual ~CPeriodicTotals();
  45. void Reset();
  46. bool SetNext(time_t time, long Count);
  47. private:
  48. void ReleaseMem();
  49. protected:
  50. CString & DisplayPeriod(long i, CString & str) const;
  51. public:
  52. virtual CString HTMLDisplay() const = 0;
  53. protected:
  54. long m_nPeriods; // number of periods, based on initialization in constructor.
  55. // typically 25 (hours in a day + 1) or 8 (days in a week + 1)
  56. long m_nPeriodsSet; // number of periods for which we have data filled in.
  57. long m_iPeriod; // index. 0 <= m_iPeriod < m_nPeriodsSet. Zeroed by InitEnum,
  58. // incremented by GetNext or SetNext.
  59. time_t *m_ptime; // points to array of times: start time of relevant time period
  60. // (typically beginning of a clock hour or calendar day).
  61. long *m_pCount; // points to array totals, each the total for correspondingly
  62. // indexed time period
  63. };
  64. ////////////////////////////////////////////////////////////////////////////////////
  65. // CHourlyTotals class declaration
  66. // CHourlyTotals intended for displaying hourly totals
  67. ////////////////////////////////////////////////////////////////////////////////////
  68. class CHourlyTotals : public CPeriodicTotals
  69. {
  70. public:
  71. CHourlyTotals();
  72. ~CHourlyTotals();
  73. virtual CString HTMLDisplay() const;
  74. };
  75. ////////////////////////////////////////////////////////////////////////////////////
  76. // CDailyTotals class declaration
  77. // CDailyTotals intended for displaying dayly totals
  78. ////////////////////////////////////////////////////////////////////////////////////
  79. class CDailyTotals : public CPeriodicTotals
  80. {
  81. public:
  82. CDailyTotals();
  83. ~CDailyTotals();
  84. virtual CString HTMLDisplay() const;
  85. };
  86. ////////////////////////////////////////////////////////////////////////////////////
  87. // CCounterLocation class declaration
  88. // CCounterLocation is a mean to identify counter within the global counter pool
  89. ////////////////////////////////////////////////////////////////////////////////////
  90. class CCounterLocation
  91. {
  92. public:
  93. // prefixes for scope names
  94. static LPCTSTR m_GlobalStr;
  95. static LPCTSTR m_TopicStr;
  96. static LPCTSTR m_ThreadStr;
  97. // counter IDs
  98. enum EId {
  99. eIdGeneric,
  100. // counters that are realized
  101. eIdProgramContemporary,
  102. eIdStatusAccess,
  103. eIdActionAccess,
  104. eIdTotalAccessStart,
  105. eIdTotalAccessFinish,
  106. eIdRequestUnknown,
  107. eIdRequestRejected,
  108. eIdErrorLogged,
  109. // status information that I see as counters.
  110. // Oleg 10-20-98
  111. eIdWorkingThread,
  112. eIdQueueItem,
  113. eIdProgressItem,
  114. // status information that I think can be emulated as counters
  115. // Oleg 10-21-98
  116. eIdKnownTopic,
  117. eIdTopicNotTriedLoad,
  118. eIdTopicFailedLoad,
  119. // topic - bound counters
  120. eIdTopicLoad,
  121. eIdTopicLoadOK,
  122. eIdTopicEvent,
  123. eIdTopicHit,
  124. eIdTopicHitNewCookie,
  125. eIdTopicHitOldCookie,
  126. };
  127. private:
  128. const CString m_Scope; // scope where the counter is used (i.e. "Topic start", "Thread 1" and so on)
  129. EId m_Id; // identifier of the counter within the scope
  130. public:
  131. CCounterLocation(EId id, LPCTSTR scope =m_GlobalStr);
  132. virtual ~CCounterLocation();
  133. public:
  134. bool operator == (const CCounterLocation& sib) {return m_Scope == sib.m_Scope && m_Id == sib.m_Id;}
  135. public:
  136. CString GetScope() {return m_Scope;}
  137. EId GetId() {return m_Id;}
  138. };
  139. ////////////////////////////////////////////////////////////////////////////////////
  140. // CAbstractCounter class declaration
  141. // CAbstractCounter* are saved in counter pool
  142. ////////////////////////////////////////////////////////////////////////////////////
  143. // >>>(probably ignore for V3.0) There has been some disagreement over whether it is
  144. // appropriate for CAbstractCounter to inherit from CCounterLocation.
  145. // JM says (10/29/98):
  146. // This seems to me to be the same type of thinking as when pointers to the previous and
  147. // next item are made part of a class that someone intends to put in a doubly linked list.
  148. // They are NOT inherent to the class. They ought instead to be part of some other class
  149. // that manages CAbstractCounters.
  150. // Oleg replies (11/2/98)
  151. // Since all counters are elements of a global pool, we have somehow to identify them
  152. // in this pool. If the way, we are identifying counters, changes (from name - id to
  153. // name1 - name2 - id for example), we change only CCounterLocation part of counter classes.
  154. // I see no reasons to make CCounterLocation an instance in counter class:
  155. // 1. we definitely have only one CCounterLocation per counter
  156. // 2. in case if inheritance we do not need additional interface for accessing CCounterLocation
  157. // JM follows this up (11/5/98):
  158. // There is nothing wrong with this approach as such. There is, however, an issue of design
  159. // philosophy that at some point we should address. For the most part, we follow the style
  160. // of STL. Classes are normally designed with no regard to the fact that they will be
  161. // contained in a collection. Here, the counter class knows about the enumeration type
  162. // used to identify counters. This is sort of as if the values in an STL map had to know
  163. // about the keys mapped to those values.
  164. // I would have designed CCounterMgr as an "object factory", providing a means to
  165. // manufacture named counters much as Win32 manufactures named synchronization primitives
  166. // (e.g. a named Mutex or Semaphore). To indicate that an event has occurred, you would
  167. // increment a named counter; for status reporting, you would ask for values of that
  168. // named counter. (In theory, the "names" might either be text or numbers. The scheme
  169. // would have to allow for some means of manufacturing several distinct counters for
  170. // each topic in the catalog.)
  171. class CAbstractCounter : public CCounterLocation
  172. {
  173. protected: // we do not get instances of this class (evermore this is an abstract class)
  174. CAbstractCounter(EId id =eIdGeneric, CString scope =m_GlobalStr);
  175. virtual ~CAbstractCounter();
  176. public:
  177. virtual void Increment() = 0;
  178. virtual void Clear() = 0;
  179. virtual void Init(long count) = 0; // init counter with a number - in order to emulate
  180. // counting process
  181. };
  182. ////////////////////////////////////////////////////////////////////////////////////
  183. // CCounter class declaration
  184. // A simple counter
  185. ////////////////////////////////////////////////////////////////////////////////////
  186. class CCounter : public CAbstractCounter
  187. {
  188. public:
  189. CCounter(EId id =eIdGeneric, CString scope =m_GlobalStr);
  190. ~CCounter();
  191. // overrides
  192. void Increment();
  193. void Clear();
  194. void Init(long count);
  195. // specific to this class
  196. long Get() const;
  197. private:
  198. long m_Count;
  199. };
  200. ////////////////////////////////////////////////////////////////////////////////////
  201. // CHourlyCounter class declaration
  202. // CHourlyCounter is not supposed to be instantiated by user
  203. ////////////////////////////////////////////////////////////////////////////////////
  204. class CHourlyCounter : public CAbstractCounter
  205. {
  206. friend class CHourlyDailyCounter;
  207. protected:
  208. CHourlyCounter();
  209. public:
  210. ~CHourlyCounter();
  211. // overrides
  212. void Increment();
  213. void Clear();
  214. void Init(long count);
  215. // specific to this class
  216. long GetDayCount();
  217. void GetHourlies(CHourlyTotals & totals);
  218. private:
  219. void SetHour();
  220. private:
  221. long m_ThisHour; // hour of the day, 0-24. -1 means uninitialized.
  222. time_t m_ThisTime; // time corresponding to START of hour.
  223. CCounter m_arrCount[24]; // 24 "bins", one for each hour of the day.
  224. long m_nThisHourYesterday; // maintains a whole hour count for the hour 24 hours ago.
  225. HANDLE m_hMutex;
  226. };
  227. ////////////////////////////////////////////////////////////////////////////////////
  228. // CDailyCounter class declaration
  229. // CDailyCounter is not supposed to be instantiated by user
  230. ////////////////////////////////////////////////////////////////////////////////////
  231. class CDailyCounter : public CAbstractCounter
  232. {
  233. friend class CHourlyDailyCounter;
  234. protected:
  235. CDailyCounter();
  236. public:
  237. ~CDailyCounter();
  238. // overrides
  239. void Increment();
  240. void Clear();
  241. void Init(long count);
  242. // specific to this class
  243. long GetWeekCount();
  244. void GetDailies(CDailyTotals & totals);
  245. private:
  246. void SetDay();
  247. private:
  248. long m_ThisDay; // day of the week, 0(Sunday)-6(Saturday). -1 means uninitialized.
  249. time_t m_ThisTime; // time corresponding to START of day.
  250. CCounter m_arrCount[7]; // 7 "bins", one for each day of the week.
  251. long m_nThisDayLastWeek; // maintains a whole day count for the same day last week.
  252. HANDLE m_hMutex;
  253. };
  254. ////////////////////////////////////////////////////////////////////////////////////
  255. // CHourlyDailyCounter class declaration
  256. // CHourlyDailyCounter is an ONLY class used for counting events
  257. ////////////////////////////////////////////////////////////////////////////////////
  258. class CHourlyDailyCounter : public CAbstractCounter
  259. {
  260. public:
  261. CHourlyDailyCounter(EId id =eIdGeneric, CString scope =m_GlobalStr);
  262. ~CHourlyDailyCounter();
  263. // overrides
  264. void Increment();
  265. void Clear();
  266. void Init(long count);
  267. // specific to this class
  268. long GetDayCount();
  269. void GetHourlies(CHourlyTotals & totals);
  270. long GetWeekCount();
  271. void GetDailies(CDailyTotals & totals);
  272. long GetTotal() const;
  273. time_t GetTimeFirst() const;
  274. time_t GetTimeLast() const;
  275. time_t GetTimeCleared() const;
  276. time_t GetTimeCreated() const;
  277. time_t GetTimeNow() const; // time the object is being questioned
  278. private:
  279. CHourlyCounter m_hourly;
  280. CDailyCounter m_daily;
  281. long m_Total; // total since system startup or count cleared
  282. time_t m_timeFirst; // chronologically first time count was incremented
  283. time_t m_timeLast; // chronologically last time count was incremented
  284. time_t m_timeCleared; // last time init'd or cleared
  285. time_t m_timeCreated; // time the object was instantiated
  286. HANDLE m_hMutex;
  287. };
  288. ////////////////////////////////////////////////////////////////////////////////////
  289. // DisplayCounter classes
  290. ////////////////////////////////////////////////////////////////////////////////////
  291. class CAbstractDisplayCounter
  292. {
  293. protected:
  294. CAbstractCounter* m_pAbstractCounter;
  295. public:
  296. CAbstractDisplayCounter(CAbstractCounter* counter) : m_pAbstractCounter(counter) {}
  297. virtual ~CAbstractDisplayCounter() {}
  298. public:
  299. virtual CString Display() = 0;
  300. };
  301. class CDisplayCounterTotal : public CAbstractDisplayCounter
  302. {
  303. public:
  304. CDisplayCounterTotal(CHourlyDailyCounter* counter) : CAbstractDisplayCounter(counter) {}
  305. ~CDisplayCounterTotal() {}
  306. public:
  307. virtual CString Display();
  308. };
  309. class CDisplayCounterCurrentDateTime : public CAbstractDisplayCounter
  310. {
  311. public:
  312. CDisplayCounterCurrentDateTime(CHourlyDailyCounter* counter) : CAbstractDisplayCounter(counter) {}
  313. ~CDisplayCounterCurrentDateTime() {}
  314. public:
  315. virtual CString Display();
  316. };
  317. class CDisplayCounterCreateDateTime : public CAbstractDisplayCounter
  318. {
  319. public:
  320. CDisplayCounterCreateDateTime(CHourlyDailyCounter* counter) : CAbstractDisplayCounter(counter) {}
  321. ~CDisplayCounterCreateDateTime() {}
  322. public:
  323. virtual CString Display();
  324. };
  325. class CDisplayCounterFirstDateTime : public CAbstractDisplayCounter
  326. {
  327. public:
  328. CDisplayCounterFirstDateTime(CHourlyDailyCounter* counter) : CAbstractDisplayCounter(counter) {}
  329. ~CDisplayCounterFirstDateTime() {}
  330. public:
  331. virtual CString Display();
  332. };
  333. class CDisplayCounterLastDateTime : public CAbstractDisplayCounter
  334. {
  335. public:
  336. CDisplayCounterLastDateTime(CHourlyDailyCounter* counter) : CAbstractDisplayCounter(counter) {}
  337. ~CDisplayCounterLastDateTime() {}
  338. public:
  339. virtual CString Display();
  340. };
  341. class CDisplayCounterDailyHourly : public CAbstractDisplayCounter
  342. {
  343. protected:
  344. CDailyTotals* m_pDailyTotals;
  345. CHourlyTotals* m_pHourlyTotals;
  346. public:
  347. CDisplayCounterDailyHourly(CHourlyDailyCounter* counter,
  348. CDailyTotals* daily,
  349. CHourlyTotals* hourly)
  350. : CAbstractDisplayCounter(counter),
  351. m_pDailyTotals(daily),
  352. m_pHourlyTotals(hourly)
  353. {}
  354. ~CDisplayCounterDailyHourly() {}
  355. public:
  356. virtual CString Display();
  357. };
  358. #endif // !defined(AFX_COUNTER_H__07B5ABBD_2005_11D2_95D0_00C04FC22ADD__INCLUDED_)