DataCollectionManager.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Aitex.Core.Util;
  6. using Npgsql;
  7. using System.Threading;
  8. using Aitex.Core.RT.Log;
  9. using Aitex.Core.RT.Event;
  10. using Aitex.Core.RT.DBCore;
  11. using System.IO;
  12. using Aitex.Common.Util;
  13. using Aitex.Core.RT.DataCenter;
  14. using System.Collections.Concurrent;
  15. using MECF.Framework.Common.Communications;
  16. using Aitex.Core.RT.SCCore;
  17. namespace Aitex.Core.RT.DataCollection
  18. {
  19. public class DataCollectionManager : Singleton<DataCollectionManager>, IConnection
  20. {
  21. public string Address
  22. {
  23. get { return "DB:DataCollection"; }
  24. }
  25. public bool IsConnected { get; set; }
  26. NpgsqlConnection _conn;
  27. bool _bAlive = true;
  28. Dictionary<string, Func<object>> _preSubscribedRecordedData; //for other thread to subscribe data
  29. Dictionary<string, Func<object>> _subscribedRecordedData; //internal use
  30. object _lock = new object();
  31. F_TRIG _connFailTrig = new F_TRIG();
  32. private int dataCollectionInterval;
  33. IDataCollectionCallback _callback;
  34. private string[] _modules = new string[] {"Data"};
  35. public DataCollectionManager()
  36. {
  37. _preSubscribedRecordedData = new Dictionary<string, Func<object>>();
  38. _subscribedRecordedData = new Dictionary<string, Func<object>>();
  39. dataCollectionInterval = SC.GetValue<int>("System.DataCollectionInterval");
  40. }
  41. public void Initialize(IDataCollectionCallback callback)
  42. {
  43. _callback = callback == null ? new DefaultDataCollectionCallback() : callback;
  44. Connect();
  45. System.Threading.Tasks.Task.Factory.StartNew(DataRecorderThread);
  46. }
  47. public void Initialize(string[] modules, string dbName)
  48. {
  49. _callback = new DefaultDataCollectionCallback(dbName);
  50. if (modules!=null && modules.Length>0 && !string.IsNullOrEmpty(modules[0]))
  51. _modules = modules;
  52. Connect();
  53. System.Threading.Tasks.Task.Factory.StartNew(DataRecorderThread);
  54. }
  55. public void Initialize(string dbName)
  56. {
  57. _callback = new DefaultDataCollectionCallback(dbName) ;
  58. Connect();
  59. System.Threading.Tasks.Task.Factory.StartNew(DataRecorderThread);
  60. }
  61. public void Terminate()
  62. {
  63. _bAlive = false;
  64. }
  65. /// <summary>
  66. /// initialization
  67. /// </summary>
  68. public void SubscribeData(string dataName, string alias, Func<object> dataValue)
  69. {
  70. lock (_lock)
  71. {
  72. if (!_preSubscribedRecordedData.Keys.Contains(dataName))
  73. {
  74. _preSubscribedRecordedData[dataName] = dataValue;
  75. }
  76. }
  77. }
  78. private void MonitorDataCenter()
  79. {
  80. lock (_lock)
  81. {
  82. SortedDictionary<string, Func<object>> data = DATA.GetDBRecorderList();
  83. foreach (var item in data)
  84. {
  85. if (!_subscribedRecordedData.ContainsKey(item.Key))
  86. {
  87. object o = item.Value.Invoke();
  88. if (o == null)
  89. continue;
  90. Type dataType = o.GetType();
  91. if (dataType == typeof(Boolean) || dataType == typeof(double) || dataType == typeof(float) ||
  92. dataType == typeof(int) || dataType == typeof(ushort) || dataType == typeof(short))
  93. {
  94. _subscribedRecordedData[item.Key] = item.Value;
  95. _preSubscribedRecordedData[item.Key] = item.Value;
  96. }
  97. }
  98. }
  99. }
  100. }
  101. public bool Connect()
  102. {
  103. bool result = true;
  104. try
  105. {
  106. if (_conn != null)
  107. _conn.Close();
  108. _conn = new NpgsqlConnection(PostgresqlHelper.ConnectionString);
  109. _conn.Open();
  110. _conn.ChangeDatabase(_callback.GetDBName());
  111. EV.PostInfoLog("DataCollection", "Connected with database");
  112. //Invoke operation
  113. //EV.PostMessage("DataCollection", EventEnum.da, "Connected with database");
  114. }
  115. catch (Exception ex)
  116. {
  117. if (_conn != null)
  118. {
  119. _conn.Close();
  120. _conn = null;
  121. }
  122. result = false;
  123. EV.PostInfoLog("DataCollection", "Can not connect database");
  124. LOG.WriteExeption(ex);
  125. }
  126. IsConnected = result;
  127. return result;
  128. }
  129. public bool Disconnect()
  130. {
  131. try
  132. {
  133. if (_conn != null)
  134. {
  135. _conn.Close();
  136. _conn = null;
  137. EV.PostInfoLog("DataCollection", "Disconnected with database");
  138. }
  139. }
  140. catch (Exception ex)
  141. {
  142. LOG.WriteExeption(ex);
  143. }
  144. IsConnected = false;
  145. return true;
  146. }
  147. /// <summary>
  148. /// 重置数据库连接出错事件
  149. /// </summary>
  150. public void Reset()
  151. {
  152. _connFailTrig.CLK = false;
  153. }
  154. /// <summary>
  155. /// data recorder thread
  156. /// </summary>
  157. void DataRecorderThread()
  158. {
  159. while (_bAlive)
  160. {
  161. try
  162. {
  163. Thread.Sleep(dataCollectionInterval);
  164. _connFailTrig.CLK = IsConnected;// Connect();
  165. if (_connFailTrig.Q)
  166. {
  167. EV.PostWarningLog("DataCollection", "Can not connect with database");
  168. }
  169. if (!_connFailTrig.M)
  170. continue;
  171. MonitorDataCenter();
  172. var moduleDataItem = new Dictionary<string, Dictionary<string, Func<object>>>();
  173. var moduleInsertSql = new Dictionary<string, string>();
  174. foreach (var module in _modules)
  175. {
  176. moduleDataItem[module] = new Dictionary<string, Func<object>>();
  177. }
  178. string defaultModule = _modules.Contains("System") ? "System" : (_modules.Contains("Data") ? "Data" : (_modules[0]));
  179. if (_subscribedRecordedData.Count > 0)
  180. {
  181. lock (_lock)
  182. {
  183. bool foundModule;
  184. foreach (var dataName in _subscribedRecordedData.Keys)
  185. {
  186. foundModule = false;
  187. foreach (var module in _modules)
  188. {
  189. if (dataName.StartsWith(module+"."))
  190. {
  191. moduleDataItem[module][dataName] = _subscribedRecordedData[dataName];
  192. foundModule = true;
  193. break;
  194. }
  195. }
  196. if (!foundModule)
  197. {
  198. moduleDataItem[defaultModule][dataName] = _subscribedRecordedData[dataName];
  199. }
  200. }
  201. _preSubscribedRecordedData.Clear();
  202. }
  203. }
  204. DateTime dtToday = DateTime.Now.Date;
  205. int count = 0;
  206. foreach (var module in _modules)
  207. {
  208. string tableName = $"{dtToday:yyyyMMdd}.{module}";
  209. UpdateTableSchema(tableName, moduleDataItem[module]);
  210. if (moduleDataItem[module].Keys.Count != 0)
  211. {
  212. string preCreatedInsertSQL = string.Format("INSERT INTO \"{0}\"(\"time\" ", tableName);
  213. foreach (var dataName in moduleDataItem[module].Keys)
  214. {
  215. preCreatedInsertSQL += string.Format(",\"{0}\"", dataName);
  216. }
  217. preCreatedInsertSQL += ")";
  218. moduleInsertSql[module] = preCreatedInsertSQL;
  219. count++;
  220. }
  221. }
  222. if (count == 0)
  223. {
  224. return;
  225. }
  226. //insert data into database
  227. StringBuilder sb = new StringBuilder(10000);
  228. while (_bAlive)
  229. {
  230. //Thread.Sleep(990);
  231. Thread.Sleep((int)(dataCollectionInterval * 0.99)); //for time delay in sleep function
  232. foreach (var module in _modules)
  233. {
  234. if (moduleDataItem[module].Keys.Count == 0)
  235. {
  236. continue;
  237. }
  238. sb.Remove(0, sb.Length);
  239. sb.Append("Values(");
  240. sb.Append(DateTime.Now.Ticks.ToString());
  241. foreach (var dataName in moduleDataItem[module].Keys)
  242. {
  243. sb.Append(",");
  244. var v1 = moduleDataItem[module][dataName].Invoke();
  245. if (v1 == null)
  246. {
  247. sb.Append("0");
  248. }
  249. else
  250. {
  251. if (v1 is double || v1 is float)
  252. {
  253. double v2 = Convert.ToDouble(v1);
  254. if (double.IsNaN(v2))
  255. v2 = 0;
  256. sb.Append(v2.ToString());
  257. }
  258. else
  259. {
  260. sb.Append(v1.ToString());
  261. }
  262. }
  263. }
  264. sb.Append(");");
  265. NpgsqlCommand cmd = new NpgsqlCommand(moduleInsertSql[module] + sb.ToString(), _conn);
  266. try
  267. {
  268. cmd.ExecuteNonQuery();
  269. }
  270. catch (Exception ex)
  271. {
  272. LOG.WriteExeption("数据记录发生异常" + moduleInsertSql[module], ex);
  273. }
  274. }
  275. //if alert to another day, create a new table
  276. if (DateTime.Now.Date != dtToday)
  277. break;
  278. //if preSubscribed data not empty, alert current table's structure
  279. MonitorDataCenter();
  280. if (_preSubscribedRecordedData.Count > 0)
  281. break;
  282. }//end while
  283. }//end while
  284. catch (Exception ex)
  285. {
  286. LOG.WriteExeption("数据库操作记录发生异常", ex);
  287. }
  288. }
  289. }
  290. private string UpdateTableSchema(string tblName, Dictionary<string, Func<object>> dataItem)
  291. {
  292. //query if table already exist?
  293. string sqlTblDefine = string.Format("select column_name from information_schema.columns where table_name = '{0}';", tblName);
  294. NpgsqlCommand cmdTblDefine = new NpgsqlCommand(sqlTblDefine, _conn);
  295. var tblDefineData = cmdTblDefine.ExecuteReader();
  296. string tblAlertString = string.Empty;
  297. List<string> colNameList = new List<string>();
  298. while (tblDefineData.Read())
  299. {
  300. for (int i = 0; i < tblDefineData.FieldCount; i++)
  301. colNameList.Add(tblDefineData[i].ToString());
  302. }
  303. tblDefineData.Close();
  304. if (colNameList.Count > 0)
  305. {
  306. //table exist
  307. foreach (var dataName in dataItem.Keys)
  308. {
  309. if (!colNameList.Contains(dataName) )
  310. {
  311. var dataType = dataItem[dataName].Invoke().GetType();
  312. if (dataType == typeof(Boolean))
  313. {
  314. tblAlertString += string.Format("ALTER TABLE \"{0}\" ADD COLUMN \"{1}\" {2};", tblName, dataName, "Boolean");
  315. }
  316. else if (dataType == typeof(double) || dataType == typeof(float) || dataType == typeof(int) || dataType == typeof(ushort) || dataType==typeof(short))
  317. {
  318. tblAlertString += string.Format("ALTER TABLE \"{0}\" ADD COLUMN \"{1}\" {2};", tblName, dataName, "Real");
  319. }
  320. else
  321. {
  322. }
  323. }
  324. }
  325. if (!string.IsNullOrEmpty(tblAlertString))
  326. {
  327. try
  328. {
  329. NpgsqlCommand alertTblCmd = new NpgsqlCommand(tblAlertString, _conn);
  330. alertTblCmd.ExecuteNonQuery();
  331. }
  332. catch (Exception ex)
  333. {
  334. LOG.WriteExeption(ex);
  335. }
  336. }
  337. }
  338. else
  339. {
  340. //table not exist, auto generate a table
  341. string createTble = string.Format("CREATE TABLE \"{0}\"(Time bigint NOT NULL,", tblName);
  342. if (dataItem.Keys.Count == 0)
  343. {
  344. return "";
  345. }
  346. string commentStr = "";
  347. foreach (var dataName in dataItem.Keys)
  348. {
  349. Type dataType = dataItem[dataName].Invoke().GetType();
  350. if (dataType == typeof(Boolean))
  351. {
  352. createTble += string.Format("\"{0}\" Boolean,\n", dataName);
  353. }
  354. else if (dataType == typeof(double) || dataType == typeof(float) ||
  355. dataType == typeof(int) || dataType == typeof(ushort) || dataType == typeof(short))
  356. {
  357. createTble += string.Format("\"{0}\" Real,\n", dataName);
  358. }
  359. //if (!string.IsNullOrEmpty(alias) && ((dataType == typeof(Boolean)) || (dataType == typeof(double) || dataType == typeof(float) ||
  360. // dataType == typeof(int) || dataType == typeof(ushort) || dataType == typeof(short))))
  361. // commentStr += string.Format("COMMENT ON COLUMN \"{0}\".\"{1}\" IS '{2}';\n", tblName, dataName, alias);
  362. }
  363. createTble += string.Format("CONSTRAINT \"{0}_pkey\" PRIMARY KEY (Time));", tblName);
  364. createTble += string.Format("GRANT SELECT ON TABLE \"{0}\" TO postgres;", tblName);
  365. // createTble += string.Format("CREATE INDEX \"{0}_time_idx\" ON \"{0}\" USING btree (\"time\" );", tblName);
  366. createTble += commentStr;
  367. try
  368. {
  369. NpgsqlCommand cmd = new NpgsqlCommand(createTble.ToString(), _conn);
  370. cmd.ExecuteNonQuery();
  371. }
  372. catch (Exception ex1)
  373. {
  374. LOG.WriteExeption("创建数据库表格失败", ex1);
  375. throw;
  376. }
  377. }
  378. return tblName;
  379. }
  380. }
  381. }