Usage.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include <Configuration.h>
  5. #include <CoreMain.h>
  6. #include <Common.h>
  7. #include <Usage.h>
  8. //---------------------------------------------------------------------------
  9. #pragma package(smart_init)
  10. //---------------------------------------------------------------------------
  11. __fastcall TUsage::TUsage(TConfiguration * Configuration)
  12. {
  13. FCriticalSection = new TCriticalSection();
  14. FConfiguration = Configuration;
  15. FValues = new TStringList();
  16. FValues->Delimiter = L'&';
  17. FValues->StrictDelimiter = true;
  18. FCollect = true;
  19. Default();
  20. }
  21. //---------------------------------------------------------------------------
  22. __fastcall TUsage::~TUsage()
  23. {
  24. delete FValues;
  25. delete FCriticalSection;
  26. }
  27. //---------------------------------------------------------------------------
  28. void __fastcall TUsage::Default()
  29. {
  30. TGuard Guard(FCriticalSection);
  31. FPeriodCounters.clear();
  32. FLifetimeCounters.clear();
  33. FValues->Clear();
  34. if (Collect) // optimization
  35. {
  36. Set(L"FirstUse", StandardTimestamp());
  37. Set(L"FirstVersion", IntToStr(FConfiguration->CompoundVersion));
  38. UpdateLastReport();
  39. }
  40. }
  41. //---------------------------------------------------------------------------
  42. void __fastcall TUsage::Load(THierarchicalStorage * Storage)
  43. {
  44. TGuard Guard(FCriticalSection);
  45. Default();
  46. if (Storage->OpenSubKey(L"Values", false))
  47. {
  48. TStrings * Names = new TStringList();
  49. try
  50. {
  51. Storage->GetValueNames(Names);
  52. for (int Index = 0; Index < Names->Count; Index++)
  53. {
  54. UnicodeString Name = Names->Strings[Index];
  55. Set(Name, Storage->ReadString(Name, L""));
  56. }
  57. Storage->CloseSubKey();
  58. }
  59. __finally
  60. {
  61. delete Names;
  62. }
  63. }
  64. Load(Storage, L"PeriodCounters", FPeriodCounters);
  65. Load(Storage, L"LifetimeCounters", FLifetimeCounters);
  66. }
  67. //---------------------------------------------------------------------------
  68. void __fastcall TUsage::Load(THierarchicalStorage * Storage,
  69. const UnicodeString& Name, TCounters & Counters)
  70. {
  71. if (Storage->OpenSubKey(Name, false))
  72. {
  73. TStrings * Names = new TStringList();
  74. try
  75. {
  76. Storage->GetValueNames(Names);
  77. for (int Index = 0; Index < Names->Count; Index++)
  78. {
  79. UnicodeString Name = Names->Strings[Index];
  80. Counters.insert(
  81. std::make_pair(Name, Storage->ReadInteger(Name, 0)));
  82. }
  83. Storage->CloseSubKey();
  84. }
  85. __finally
  86. {
  87. delete Names;
  88. }
  89. }
  90. }
  91. //---------------------------------------------------------------------------
  92. void __fastcall TUsage::Save(THierarchicalStorage * Storage) const
  93. {
  94. TGuard Guard(FCriticalSection);
  95. if (Storage->OpenSubKey(L"Values", true))
  96. {
  97. Storage->ClearValues();
  98. Storage->WriteValues(FValues, true);
  99. Storage->CloseSubKey();
  100. }
  101. Save(Storage, L"PeriodCounters", FPeriodCounters);
  102. Save(Storage, L"LifetimeCounters", FLifetimeCounters);
  103. }
  104. //---------------------------------------------------------------------------
  105. void __fastcall TUsage::Save(THierarchicalStorage * Storage,
  106. const UnicodeString & Name, const TCounters & Counters) const
  107. {
  108. if (Storage->OpenSubKey(Name, true))
  109. {
  110. Storage->ClearValues();
  111. TCounters::const_iterator i = Counters.begin();
  112. while (i != Counters.end())
  113. {
  114. Storage->WriteInteger(i->first, i->second);
  115. i++;
  116. }
  117. Storage->CloseSubKey();
  118. }
  119. }
  120. //---------------------------------------------------------------------------
  121. void __fastcall TUsage::Set(const UnicodeString & Key, const UnicodeString & Value)
  122. {
  123. if (Collect)
  124. {
  125. TGuard Guard(FCriticalSection);
  126. FValues->Values[Key] = Value;
  127. }
  128. }
  129. //---------------------------------------------------------------------------
  130. void __fastcall TUsage::Set(const UnicodeString & Key, int Value)
  131. {
  132. Set(Key, IntToStr(Value));
  133. }
  134. //---------------------------------------------------------------------------
  135. void __fastcall TUsage::Set(const UnicodeString & Key, bool Value)
  136. {
  137. Set(Key, Value ? 1 : 0);
  138. }
  139. //---------------------------------------------------------------------------
  140. UnicodeString __fastcall TUsage::Get(const UnicodeString & Key)
  141. {
  142. TGuard Guard(FCriticalSection);
  143. UnicodeString Result = FValues->Values[Key];
  144. Result.Unique();
  145. return Result;
  146. }
  147. //---------------------------------------------------------------------------
  148. void __fastcall TUsage::UpdateLastReport()
  149. {
  150. Set(L"LastReport", StandardTimestamp());
  151. }
  152. //---------------------------------------------------------------------------
  153. void __fastcall TUsage::Reset()
  154. {
  155. TGuard Guard(FCriticalSection);
  156. UpdateLastReport();
  157. FPeriodCounters.clear();
  158. }
  159. //---------------------------------------------------------------------------
  160. void __fastcall TUsage::UpdateCurrentVersion()
  161. {
  162. TGuard Guard(FCriticalSection);
  163. int CompoundVersion = FConfiguration->CompoundVersion;
  164. int PrevVersion = StrToIntDef(Get(L"CurrentVersion"), 0);
  165. if (PrevVersion != CompoundVersion)
  166. {
  167. Set(L"Installed", StandardTimestamp());
  168. }
  169. if (PrevVersion != 0)
  170. {
  171. if (PrevVersion < CompoundVersion)
  172. {
  173. Inc(L"Upgrades");
  174. }
  175. else if (PrevVersion > CompoundVersion)
  176. {
  177. Inc(L"Downgrades");
  178. }
  179. }
  180. Set(L"CurrentVersion", CompoundVersion);
  181. }
  182. //---------------------------------------------------------------------------
  183. void __fastcall TUsage::Inc(const UnicodeString & Key)
  184. {
  185. if (Collect)
  186. {
  187. TGuard Guard(FCriticalSection);
  188. Inc(Key, FPeriodCounters);
  189. Inc(Key, FLifetimeCounters);
  190. }
  191. }
  192. //---------------------------------------------------------------------------
  193. void __fastcall TUsage::Inc(const UnicodeString & Key, TCounters & Counters)
  194. {
  195. TCounters::iterator i = Counters.find(Key);
  196. if (i != Counters.end())
  197. {
  198. i->second++;
  199. }
  200. else
  201. {
  202. Counters.insert(std::make_pair(Key, 1));
  203. }
  204. }
  205. //---------------------------------------------------------------------------
  206. void __fastcall TUsage::SetMax(const UnicodeString & Key, int Value)
  207. {
  208. if (Collect)
  209. {
  210. TGuard Guard(FCriticalSection);
  211. SetMax(Key, Value, FPeriodCounters);
  212. SetMax(Key, Value, FLifetimeCounters);
  213. }
  214. }
  215. //---------------------------------------------------------------------------
  216. void __fastcall TUsage::SetMax(const UnicodeString & Key, int Value,
  217. TCounters & Counters)
  218. {
  219. TCounters::iterator i = Counters.find(Key);
  220. if (i != Counters.end())
  221. {
  222. if (Value > i->second)
  223. {
  224. i->second = Value;
  225. }
  226. }
  227. else
  228. {
  229. Counters.insert(std::make_pair(Key, Value));
  230. }
  231. }
  232. //---------------------------------------------------------------------------
  233. void __fastcall TUsage::SetCollect(bool value)
  234. {
  235. TGuard Guard(FCriticalSection);
  236. if (Collect != value)
  237. {
  238. FCollect = value;
  239. if (!FCollect)
  240. {
  241. FPeriodCounters.clear();
  242. FLifetimeCounters.clear();
  243. FValues->Clear();
  244. }
  245. else
  246. {
  247. Default();
  248. }
  249. }
  250. }
  251. //---------------------------------------------------------------------------
  252. UnicodeString __fastcall TUsage::Serialize() const
  253. {
  254. TGuard Guard(FCriticalSection);
  255. UnicodeString Result;
  256. AddToList(Result, FValues->DelimitedText, L"&");
  257. Serialize(Result, L"Period", FPeriodCounters);
  258. Serialize(Result, L"Lifetime", FLifetimeCounters);
  259. return Result;
  260. }
  261. //---------------------------------------------------------------------------
  262. void __fastcall TUsage::Serialize(UnicodeString& List,
  263. const UnicodeString & Name, const TCounters & Counters) const
  264. {
  265. TCounters::const_iterator i = Counters.begin();
  266. while (i != Counters.end())
  267. {
  268. AddToList(List, FORMAT(L"%s%s=%d", (Name, i->first, i->second)), L"&");
  269. i++;
  270. }
  271. }