EventLogViewModel.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. using Aitex.Core.RT.Event;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.UI.MVVM;
  4. using MECF.Framework.Common.DataCenter;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Collections.ObjectModel;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Input;
  12. using System.Windows.Media.Imaging;
  13. namespace Aitex.Sorter.UI.ViewModel
  14. {
  15. /// <summary>
  16. /// 操作记录查询结果结构定义
  17. /// </summary>
  18. public class SystemLogItem
  19. {
  20. /// <summary>
  21. /// 时间
  22. /// </summary>
  23. public string Time { get; set; }
  24. /// <summary>
  25. /// ICON
  26. /// </summary>
  27. public object Icon { get; set; }
  28. /// <summary>
  29. /// CEID
  30. /// </summary>
  31. public object Ceid { get; set; }
  32. /// <summary>
  33. /// 类型:操作日志|事件|其他
  34. /// </summary>
  35. public string LogType { get; set; }
  36. /// <summary>
  37. /// 针对腔体
  38. /// </summary>
  39. public string TargetChamber { get; set; }
  40. /// <summary>
  41. /// 发起方
  42. /// </summary>
  43. public string Initiator { get; set; }
  44. /// <summary>
  45. /// 详情
  46. /// </summary>
  47. public string Detail { get; set; }
  48. }
  49. class PerPageItem
  50. {
  51. public string Value { get; set; }
  52. }
  53. class EventLogViewModel : UIViewModelBase
  54. {
  55. public bool SearchAlarmEvent { get; set; }
  56. public bool SearchWarningEvent { get; set; }
  57. public bool SearchInfoEvent { get; set; }
  58. public bool SearchOpeLog { get; set; }
  59. public bool SearchPMA { get; set; }
  60. public bool SearchPMB { get; set; }
  61. public bool SearchPMC { get; set; }
  62. public bool SearchPMD { get; set; }
  63. //public bool SearchCoolDown { get; set; }
  64. public bool SearchTM { get; set; }
  65. public bool SearchLL { get; set; }
  66. //public bool SearchBuf1 { get; set; }
  67. public bool SearchSystem { get; set; }
  68. public string SearchKeyWords { get; set; }
  69. public ICommand SearchCommand { get; set; }
  70. public ICommand ExportCommand { get; set; }
  71. public ICommand NavigateCommand { get; set; }
  72. public DateTime SearchBeginTime { get; set; }
  73. public DateTime SearchEndTime { get; set; }
  74. public string Keywords { get; set; }
  75. public ObservableCollection<string> EventList { get; set; }
  76. public string SelectedEvent { get; set; }
  77. public ObservableCollection<string> UserList { get; set; }
  78. public string SelectedUser { get; set; }
  79. public ObservableCollection<SystemLogItem> SearchedResult { get; set; }
  80. public Func<string, List<EventItem>> QueryDBEventFunc { get; set; }
  81. public Func<List<string>> QueryEventList { get; set; }
  82. public ObservableCollection<PerPageItem> PerPageItems { get; set; }
  83. public bool IsLoading { get; set; }
  84. private DateTime _pageStart;
  85. private DateTime _pageStop;
  86. private int _currentPage;
  87. private int _countPerPage;
  88. private int _totalPage;
  89. private PerPageItem _selectedPerPage;
  90. public PerPageItem SelectedPerPage
  91. {
  92. get
  93. {
  94. return _selectedPerPage;
  95. }
  96. set
  97. {
  98. _selectedPerPage = value;
  99. Task.Run(()=> DisplayResult(false));
  100. }
  101. }
  102. public string PageInfo { get; set; }
  103. private List<EventItem> _resultEvent;
  104. public bool EnableFirst { get; set; }
  105. public bool EnablePrevious { get; set; }
  106. public bool EnableNext { get; set; }
  107. public bool EnableLast { get; set; }
  108. public EventLogViewModel()
  109. : base("EventLogViewModel")
  110. {
  111. PerPageItems = new ObservableCollection<PerPageItem>();
  112. PerPageItems.Add(new PerPageItem(){Value = "All"});
  113. PerPageItems.Add(new PerPageItem() { Value = "30" });
  114. PerPageItems.Add(new PerPageItem() { Value = "300" });
  115. PerPageItems.Add(new PerPageItem() { Value = "3000" });
  116. PerPageItems.Add(new PerPageItem() { Value = "30000" });
  117. _selectedPerPage = PerPageItems[0];
  118. PageInfo = "0/0";
  119. QueryDBEventFunc = (sql) => QueryDataClient.Instance.Service.QueryDBEvent(sql);
  120. QueryEventList = () =>
  121. {
  122. List<string> result = new List<string>();
  123. foreach (var eventName in Enum.GetNames(typeof(EventEnum)))
  124. result.Add(eventName);
  125. return result;
  126. };
  127. var now = DateTime.Today;
  128. SearchBeginTime = now;// -new TimeSpan(1, 0, 0, 0);
  129. SearchEndTime = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, 999);
  130. SelectedUser = "All";
  131. SearchKeyWords = string.Empty;
  132. SearchAlarmEvent = true;
  133. SearchWarningEvent = true;
  134. SearchInfoEvent = true;
  135. SearchOpeLog = false;
  136. SearchPMA = false;
  137. SearchPMB = false;
  138. SearchPMC = false;
  139. SearchPMD = false;
  140. //SearchCoolDown = false;
  141. SearchTM = false;
  142. SearchLL = false;
  143. //SearchBuf1 = false;
  144. SearchSystem = false;
  145. SearchCommand = new DelegateCommand<object>((o) => Search(), (o) => true);
  146. ExportCommand = new DelegateCommand<object>((o) => Export(), (o) => true);
  147. NavigateCommand = new DelegateCommand<string>((o) => Navigate(o), (o) => true);
  148. }
  149. protected override void InvokeBeforeUpdateProperty(Dictionary<string, object> data)
  150. {
  151. }
  152. public void Navigate(string args)
  153. {
  154. if (args == "first")
  155. _currentPage = 1;
  156. if (args == "previous")
  157. _currentPage--;
  158. if (args == "next")
  159. _currentPage++;
  160. if (args == "last")
  161. _currentPage = _totalPage;
  162. DisplayResult(false);
  163. }
  164. /// <summary>
  165. /// 预先载入数据
  166. /// </summary>
  167. public void Preload()
  168. {
  169. //初始化所有的枚举事件类型
  170. EventList = new ObservableCollection<string>();
  171. EventList.Add("All");
  172. if (QueryEventList != null)
  173. {
  174. List<string> evList = QueryEventList();
  175. foreach (string ev in evList)
  176. EventList.Add(ev);
  177. }
  178. SelectedEvent = "All";
  179. //所有属性更新
  180. InvokePropertyChanged();
  181. //第一次默认查询
  182. if (SearchedResult == null)
  183. Search();
  184. }
  185. /// <summary>
  186. /// 导出
  187. /// </summary>
  188. void Export()
  189. {
  190. try
  191. {
  192. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  193. dlg.DefaultExt = ".xls"; // Default file extension
  194. dlg.Filter = "Excel数据表格文件(*.xls)|*.xls"; // Filter files by extension
  195. Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
  196. if (result == true) // Process open file dialog box results
  197. {
  198. System.Data.DataSet ds = new System.Data.DataSet();
  199. ds.Tables.Add(new System.Data.DataTable("系统运行日志"));
  200. ds.Tables[0].Columns.Add("类型");
  201. ds.Tables[0].Columns.Add("时间");
  202. ds.Tables[0].Columns.Add("腔体");
  203. ds.Tables[0].Columns.Add("发起源");
  204. ds.Tables[0].Columns.Add("内容描述");
  205. foreach (var item in SearchedResult)
  206. {
  207. var row = ds.Tables[0].NewRow();
  208. row[0] = item.LogType;
  209. row[1] = item.Time;
  210. row[2] = item.TargetChamber;
  211. row[3] = item.Initiator;
  212. row[4] = item.Detail;
  213. ds.Tables[0].Rows.Add(row);
  214. }
  215. ds.WriteXml(dlg.FileName);
  216. }
  217. }
  218. catch (Exception ex)
  219. {
  220. LOG.Write(ex);
  221. MessageBox.Show("导出系统日志发生错误", "导出失败", MessageBoxButton.OK, MessageBoxImage.Warning);
  222. }
  223. }
  224. private string GetSourceWhere()
  225. {
  226. return "";
  227. }
  228. /*
  229. *
  230. * gid integer NOT NULL DEFAULT nextval('event_data_gid_seq'::regclass),
  231. event_id integer,
  232. event_enum text,
  233. type text,
  234. source text,
  235. description text,
  236. level text,
  237. occur_time timestamp without time zone,
  238. CONSTRAINT event_data_pkey PRIMARY KEY (gid)
  239. */
  240. /// <summary>
  241. /// 查询
  242. /// </summary>
  243. void Search()
  244. {
  245. if (IsLoading)
  246. return;
  247. IsLoading = true;
  248. InvokePropertyChanged("IsLoading");
  249. Task.Factory.StartNew(() =>
  250. {
  251. try
  252. {
  253. string sqlEvent = "";
  254. string sqlOperationLog = "";
  255. string sql = "";
  256. if (SearchAlarmEvent || SearchWarningEvent || SearchInfoEvent)
  257. {
  258. sqlEvent = string.Format("SELECT \"event_id\", \"event_enum\", \"type\", \"occur_time\", \"level\",\"source\" , \"description\" FROM \"event_data\" where \"occur_time\" >='{0}' and \"occur_time\" <='{1}' ", SearchBeginTime.ToString("yyyyMMdd HHmmss"), SearchEndTime.ToString("yyyyMMdd HHmmss"));
  259. sqlEvent += GetSourceWhere();
  260. sqlEvent += " and (FALSE ";
  261. if (SearchAlarmEvent) sqlEvent += " OR \"level\"='Alarm' ";
  262. if (SearchWarningEvent) sqlEvent += " OR \"level\"='Warning' ";
  263. if (SearchInfoEvent) sqlEvent += " OR \"level\"='Information' ";
  264. sqlEvent += " ) ";
  265. if (!string.IsNullOrWhiteSpace(SelectedEvent) && SelectedEvent != "All") sqlEvent += string.Format(" and lower(\"event_enum\")='{0}' ", SelectedEvent.ToLower());
  266. if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlEvent += string.Format(" and lower(\"description\") like '%{0}%' ", SearchKeyWords.ToLower());
  267. }
  268. sql = sqlEvent;
  269. if (!string.IsNullOrEmpty(sqlOperationLog))
  270. {
  271. if (string.IsNullOrEmpty(sql))
  272. {
  273. sql = sqlOperationLog;
  274. }
  275. else
  276. {
  277. sql += " UNION ALL " + sqlOperationLog;
  278. }
  279. }
  280. if (!string.IsNullOrEmpty(sql) && QueryDBEventFunc != null)
  281. {
  282. sql += " order by \"occur_time\" asc ;";
  283. _resultEvent = QueryDBEventFunc(sql);
  284. IsLoading = false;
  285. DisplayResult(true);
  286. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  287. {
  288. InvokePropertyChanged("IsLoading");
  289. }));
  290. }
  291. else
  292. {
  293. IsLoading = false;
  294. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  295. {
  296. InvokePropertyChanged("IsLoading");
  297. }));
  298. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  299. {
  300. SearchedResult = new ObservableCollection<SystemLogItem>();
  301. InvokePropertyChanged("SearchedResult");
  302. }));
  303. }
  304. }
  305. catch (Exception ex)
  306. {
  307. LOG.Write(ex);
  308. }
  309. });
  310. }
  311. private void DisplayResult(bool firstTimeDisplay)
  312. {
  313. if (IsLoading)
  314. return;
  315. SearchedResult = new ObservableCollection<SystemLogItem>();
  316. if (_resultEvent == null || _resultEvent.Count == 0)
  317. {
  318. PageInfo = "0/0";
  319. EnableFirst = EnableLast = EnableNext = EnablePrevious = false;
  320. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  321. {
  322. InvokePropertyChanged(nameof(SearchedResult));
  323. InvokePropertyChanged(nameof(PageInfo));
  324. InvokePropertyChanged(nameof(EnableFirst));
  325. InvokePropertyChanged(nameof(EnableLast));
  326. InvokePropertyChanged(nameof(EnableNext));
  327. InvokePropertyChanged(nameof(EnablePrevious));
  328. }));
  329. return;
  330. }
  331. if (SelectedPerPage==null || SelectedPerPage.Value == "All" || string.IsNullOrEmpty(SelectedPerPage.Value))
  332. {
  333. _countPerPage = _resultEvent.Count;
  334. }
  335. else
  336. {
  337. _countPerPage = int.Parse(SelectedPerPage.Value);
  338. }
  339. if (firstTimeDisplay)
  340. {
  341. _currentPage = 1;
  342. }
  343. _totalPage = _resultEvent.Count / _countPerPage + (_resultEvent.Count % _countPerPage > 0 ? 1 : 0);
  344. if (_currentPage < 1)
  345. _currentPage = 1;
  346. if (_currentPage > _totalPage)
  347. _currentPage = _totalPage;
  348. EnableFirst = _currentPage > 1;
  349. EnablePrevious = _currentPage > 1;
  350. EnableNext = _currentPage < _totalPage;
  351. EnableLast = _currentPage < _totalPage;
  352. PageInfo = $"{_currentPage}/{_totalPage}";
  353. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  354. {
  355. InvokePropertyChanged(nameof(SearchedResult));
  356. InvokePropertyChanged(nameof(PageInfo));
  357. InvokePropertyChanged(nameof(EnableFirst));
  358. InvokePropertyChanged(nameof(EnableLast));
  359. InvokePropertyChanged(nameof(EnableNext));
  360. InvokePropertyChanged(nameof(EnablePrevious));
  361. }));
  362. //2000一次,显示全部页面
  363. int from = (_currentPage - 1) * _countPerPage;
  364. for (int i = 0; i < _countPerPage; i = i + 2000)
  365. {
  366. Application.Current.Dispatcher.BeginInvoke(new Action<int>((index) =>
  367. {
  368. string logTypeStr;
  369. for (int j = from+index; j - index-from < Math.Min(2000,_countPerPage) && j < _resultEvent.Count; j++)
  370. {
  371. EventItem ev = _resultEvent[j];
  372. switch (ev.Level)
  373. {
  374. case EventLevel.Information: logTypeStr = "Info"; break;
  375. case EventLevel.Warning: logTypeStr = "Warning"; break;
  376. case EventLevel.Alarm: logTypeStr = "Alarm"; break;
  377. default: logTypeStr = "Undefine"; break;
  378. }
  379. SearchedResult.Add(new SystemLogItem()
  380. {
  381. Time = ((DateTime)ev.OccuringTime).ToString("yyyy/MM/dd HH:mm:ss.fff"),
  382. LogType = logTypeStr,
  383. Detail = ev.Description,
  384. Ceid = ev.Id,
  385. TargetChamber = ev.Source,
  386. Initiator = "",
  387. Icon = new BitmapImage(new Uri(string.Format("pack://application:,,,/MECF.Framework.UI.Core;component/Resources/SystemLog/{0}.png", ev.Level.ToString()), UriKind.Absolute))
  388. });
  389. }
  390. InvokePropertyChanged("SearchedResult");
  391. }), i);
  392. Thread.Sleep(300);
  393. }
  394. }
  395. private void OnPerPageChanged()
  396. {
  397. }
  398. }
  399. }