EventViewModel.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. using Aitex.Core.RT.Event;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.Utilities;
  4. using MECF.Framework.Common.DataCenter;
  5. using MECF.Framework.Common.Utilities;
  6. using MECF.Framework.UI.Client.ClientBase;
  7. using OpenSEMI.ClientBase.Command;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.ObjectModel;
  11. using System.Linq;
  12. using System.Threading;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. using System.Windows.Input;
  16. using System.Windows.Media.Imaging;
  17. namespace MECF.Framework.UI.Client.CenterViews.DataLogs.Event
  18. {
  19. public class SystemLogItem
  20. {
  21. /// <summary>
  22. /// 时间
  23. /// </summary>
  24. public string Time { get; set; }
  25. /// <summary>
  26. /// ICON
  27. /// </summary>
  28. public object Icon { get; set; }
  29. /// <summary>
  30. /// 类型:操作日志|事件|其他
  31. /// </summary>
  32. public string LogType { get; set; }
  33. /// <summary>
  34. /// 针对腔体
  35. /// </summary>
  36. public string TargetChamber { get; set; }
  37. /// <summary>
  38. /// 发起方
  39. /// </summary>
  40. public string Initiator { get; set; }
  41. /// <summary>
  42. /// 详情
  43. /// </summary>
  44. public string Detail { get; set; }
  45. }
  46. public class PerPageItem
  47. {
  48. public string Value { get; set; }
  49. }
  50. public class EventViewModel : BaseModel
  51. {
  52. private Dictionary<EventLevel, BitmapImage> _bitmapImageByEventLevel;
  53. public bool SearchAlarmEvent { get; set; }
  54. public bool SearchWarningEvent { get; set; }
  55. public bool SearchInfoEvent { get; set; }
  56. public bool SearchOpeLog { get; set; }
  57. public bool SearchPMA { get; set; }
  58. public bool SearchPMB { get; set; }
  59. public bool SearchPMC { get; set; }
  60. public bool SearchPMD { get; set; }
  61. //public bool SearchCoolDown { get; set; }
  62. public bool SearchTM { get; set; }
  63. public bool SearchLL { get; set; }
  64. //public bool SearchBuf1 { get; set; }
  65. public bool SearchSystem { get; set; }
  66. public string SearchKeyWords { get; set; }
  67. public string SearchDSKeyWords { get; set; }
  68. public DateTime SearchBeginTime { get; set; }
  69. public DateTime SearchEndTime { get; set; }
  70. public string Keywords { get; set; }
  71. public ObservableCollection<string> EventList { get; set; }
  72. public string SelectedEvent { get; set; }
  73. public ObservableCollection<string> UserList { get; set; }
  74. public string SelectedUser { get; set; }
  75. public ObservableCollection<SystemLogItem> SearchedResult { get; set; }
  76. public ObservableCollection<SystemLogItem> searchedResult { get; set; }
  77. public Func<string, List<EventItem>> QueryDBEventFunc { get; set; }
  78. public Func<List<string>> QueryEventList { get; set; }
  79. public bool IsPermission { get => this.Permission == 3; }
  80. private EventView view;
  81. public ObservableCollection<string> SourceLP { get; set; }
  82. public ObservableCollection<string> sourcelp { get; set; }
  83. //public ObservableCollection<string> sourceLP { get; set; }
  84. public string SelectedValueLP { get; set; }
  85. public ObservableCollection<string> ToolTipValueLP { get; set; }
  86. public ObservableCollection<string> SourceDS { get; set; }
  87. public ObservableCollection<string> sourceDS { get; set; }
  88. public string SelectedValueDS { get; set; }
  89. public ICommand tbLoadPort1SelectionChangedCommand { get; set; }
  90. //public ICommand tbLoadPort2SelectionChangedCommand { get; set; }
  91. public ICommand NavigateCommand { get; set; }
  92. public ObservableCollection<PerPageItem> PerPageItems { get; set; }
  93. public bool IsLoading { get; set; }
  94. private int _currentPage;
  95. private int _countPerPage;
  96. private int _totalPage;
  97. private int _total;
  98. private PerPageItem _selectedPerPage;
  99. public PerPageItem SelectedPerPage
  100. {
  101. get
  102. {
  103. return _selectedPerPage;
  104. }
  105. set
  106. {
  107. _selectedPerPage = value;
  108. Task.Run(() => DisplayResult(false));
  109. }
  110. }
  111. public string PageInfo { get; set; }
  112. private List<EventItem> _resultEvent;
  113. public bool EnableFirst { get; set; }
  114. public bool EnablePrevious { get; set; }
  115. public bool EnableNext { get; set; }
  116. public bool EnableLast { get; set; }
  117. public EventViewModel()
  118. {
  119. this.DisplayName = "Event";
  120. this.QueryDBEventFunc = (sql) => QueryDataClient.Instance.Service.QueryDBEvent(sql);
  121. this.QueryEventList = () =>
  122. {
  123. List<string> result = new List<string>();
  124. foreach (var eventName in Enum.GetNames(typeof(EventEnum)))
  125. result.Add(eventName);
  126. return result;
  127. };
  128. var now = DateTime.Today;
  129. SearchBeginTime = now;// -new TimeSpan(1, 0, 0, 0);
  130. SearchEndTime = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, 999);
  131. SelectedUser = "All";
  132. SearchKeyWords = string.Empty;
  133. SearchAlarmEvent = false;
  134. SearchWarningEvent = false;
  135. SearchInfoEvent = false;
  136. SearchOpeLog = false;
  137. SearchPMA = false;
  138. SearchPMB = false;
  139. SearchPMC = false;
  140. SearchPMD = false;
  141. //SearchCoolDown = false;
  142. SearchTM = false;
  143. SearchLL = false;
  144. //SearchBuf1 = false;
  145. SearchSystem = false;
  146. SourceLP = new ObservableCollection<string>();
  147. //sourceLP = new ObservableCollection<string>();
  148. //SourceLP = new ObservableCollection<string>(new[] { "LP1", "LP2", "LP3","lp4" });
  149. //SourceDS = new ObservableCollection<string>();
  150. //sourceDS = new ObservableCollection<string>();
  151. //ToolTipValueLP = new ObservableCollection<string>();
  152. tbLoadPort1SelectionChangedCommand = new BaseCommand<object>(tbLoadPort1Selection);
  153. searchedResult = new ObservableCollection<SystemLogItem>();
  154. sourcelp = new ObservableCollection<string>();
  155. //tbLoadPort2SelectionChangedCommand = new BaseCommand<object>(tbLoadPort2Selection);
  156. PerPageItems = new ObservableCollection<PerPageItem>();
  157. //PerPageItems.Add(new PerPageItem() { Value = "All" });
  158. PerPageItems.Add(new PerPageItem() { Value = "30" });
  159. //PerPageItems.Add(new PerPageItem() { Value = "300" });
  160. //PerPageItems.Add(new PerPageItem() { Value = "3000" });
  161. //PerPageItems.Add(new PerPageItem() { Value = "30000" });
  162. _selectedPerPage = PerPageItems[0];
  163. PageInfo = "0/0";
  164. NavigateCommand = new BaseCommand<string>((o) => Navigate(o), (o) => true);
  165. _bitmapImageByEventLevel = new Dictionary<EventLevel, BitmapImage>();
  166. EnumLoop<EventLevel>.ForEach(p=> {
  167. _bitmapImageByEventLevel[p] = new BitmapImage(new Uri(string.Format("pack://application:,,,/MECF.Framework.UI.Core;component/Resources/SystemLog/{0}.png", p.ToString()), UriKind.Absolute));
  168. });
  169. SearchedResult = new ObservableCollection<SystemLogItem>();
  170. _resultEvent = new List<EventItem>();
  171. }
  172. protected override void OnInitialize()
  173. {
  174. base.OnInitialize();
  175. }
  176. public void Navigate(string args)
  177. {
  178. LOG.Info($"DataLog->EventLog->{typeof(EventViewModel)}->Navigate ");
  179. if (args == "first")
  180. _currentPage = 1;
  181. if (args == "previous")
  182. _currentPage--;
  183. if (args == "next")
  184. _currentPage++;
  185. if (args == "last")
  186. _currentPage = _totalPage;
  187. DisplayResult(false);
  188. }
  189. protected override void OnViewLoaded(object _view)
  190. {
  191. base.OnViewLoaded(_view);
  192. this.view = (EventView)_view;
  193. this.view.wfTimeFrom.Value = this.SearchBeginTime;
  194. this.view.wfTimeTo.Value = this.SearchEndTime;
  195. //this.Preload();
  196. }
  197. protected override void OnDeactivate(bool close)
  198. {
  199. base.OnDeactivate(close);
  200. ClearData();
  201. }
  202. public void Preload()
  203. {
  204. EventList = new ObservableCollection<string>();
  205. EventList.Add("All");
  206. if (QueryEventList != null)
  207. {
  208. List<string> evList = QueryEventList();
  209. foreach (string ev in evList)
  210. EventList.Add(ev);
  211. }
  212. SelectedEvent = "All";
  213. if (SearchedResult == null)
  214. Search();
  215. }
  216. public void GetFormSearchedResult()
  217. {
  218. try
  219. {
  220. if (SearchedResult != null)
  221. {
  222. SourceLP.Clear();
  223. searchedResult.Clear();
  224. SearchedResult.ToList().ForEach(i => searchedResult.Add(i));
  225. SourceLP.Add("ALL");
  226. sourcelp.Add("ALL");
  227. foreach (var result in SearchedResult)
  228. {
  229. if (!SourceLP.Contains(result.TargetChamber))
  230. {
  231. SourceLP.Add(result.TargetChamber);
  232. sourcelp.Add(result.TargetChamber);
  233. }
  234. }
  235. }
  236. }
  237. catch (Exception ex)
  238. {
  239. LOG.Write(ex);
  240. }
  241. }
  242. /// <summary>
  243. /// 筛选信息
  244. /// </summary>
  245. public void Filter()
  246. {
  247. try
  248. {
  249. LOG.Info($"DataLog->EventLog->{typeof(EventViewModel)}->Filter ");
  250. if (SelectedValueLP != "")
  251. {
  252. SearchedResult.Clear();
  253. searchedResult.ToList().ForEach(i => SearchedResult.Add(i));
  254. string[] lsvp = SelectedValueLP.Split(',');
  255. SourceLP.ToList().ForEach(i => { if (!lsvp.Contains(i) && i != "ALL") SearchedResult.ToList().ForEach(m => { if (m.TargetChamber == i) SearchedResult.Remove(m); }); });// sourceLP.Add(i); });
  256. }
  257. else SearchedResult.Clear();
  258. if (SearchedResult != null && !string.IsNullOrWhiteSpace(SearchDSKeyWords))
  259. {
  260. SearchedResult.ToList().ForEach(m => { if (!m.Detail.Contains(SearchDSKeyWords)) SearchedResult.Remove(m); });
  261. }
  262. NotifyOfPropertyChange("SearchedResult");
  263. }
  264. catch (Exception ex)
  265. {
  266. LOG.Write(ex);
  267. MessageBox.Show("筛选信息失败", "筛选失败", MessageBoxButton.OK, MessageBoxImage.Warning);
  268. }
  269. }
  270. /// <summary>
  271. /// 命令
  272. /// </summary>
  273. /// <param name="o"></param>
  274. public void tbLoadPort1Selection(object o)
  275. {
  276. if (o != null)
  277. {
  278. var Item = o as ItemSelectionData;
  279. if (Item.SelectItem == "ALL")
  280. {
  281. if (Item.IsSelect)
  282. {
  283. SourceLP.ToList().ForEach(sp => { if (!sourcelp.Contains(sp)) sourcelp.Add(sp); });
  284. }
  285. else
  286. {
  287. sourcelp.Clear();
  288. }
  289. }
  290. }
  291. }
  292. /// <summary>
  293. /// 导出
  294. /// </summary>
  295. public void Export()
  296. {
  297. try
  298. {
  299. LOG.Info($"DataLog->EventLog->{typeof(EventViewModel)}->Export ");
  300. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  301. dlg.DefaultExt = ".xlsx"; // Default file extension
  302. dlg.FileName = $"Operation Log_{DateTime.Now:yyyyMMdd_HHmmss}";
  303. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  304. Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
  305. if (result == true) // Process open file dialog box results
  306. {
  307. System.Data.DataSet ds = new System.Data.DataSet();
  308. ds.Tables.Add(new System.Data.DataTable("系统运行日志"));
  309. ds.Tables[0].Columns.Add("Type");
  310. ds.Tables[0].Columns.Add("Time");
  311. ds.Tables[0].Columns.Add("System");
  312. ds.Tables[0].Columns.Add("Content");
  313. foreach (var item in SearchedResult)
  314. {
  315. var row = ds.Tables[0].NewRow();
  316. row[0] = item.LogType;
  317. row[1] = item.Time;
  318. row[2] = item.TargetChamber;
  319. row[3] = item.Detail;
  320. ds.Tables[0].Rows.Add(row);
  321. }
  322. if (!ExcelHelper.ExportToExcel(dlg.FileName, ds, out string reason))
  323. {
  324. MessageBox.Show($"Export failed, {reason}", "Export", MessageBoxButton.OK, MessageBoxImage.Warning);
  325. return;
  326. }
  327. MessageBox.Show($"Export succeed, file save as {dlg.FileName}", "Export", MessageBoxButton.OK, MessageBoxImage.Information);
  328. }
  329. }
  330. catch (Exception ex)
  331. {
  332. LOG.Write(ex);
  333. MessageBox.Show("导出系统日志发生错误", "导出失败", MessageBoxButton.OK, MessageBoxImage.Warning);
  334. }
  335. }
  336. private string GetSourceWhere()
  337. {
  338. return "";
  339. }
  340. public void Search()
  341. {
  342. LOG.Info($"DataLog->EventLog->{typeof(EventViewModel)}->Search ");
  343. if (IsLoading)
  344. return;
  345. this.SearchBeginTime = this.view.wfTimeFrom.Value;
  346. this.SearchEndTime = this.view.wfTimeTo.Value;
  347. if (SearchBeginTime > SearchEndTime || (SearchEndTime - SearchBeginTime).Days > 7)
  348. {
  349. MessageBox.Show("Time range invalid, start time should be early than end time and max interval 7 day");
  350. SearchedResult.Clear();
  351. _total = 0;
  352. UpdateUI(true);
  353. return;
  354. }
  355. UpdateUI(true);
  356. SetLoading(true);
  357. Task.Factory.StartNew(() =>
  358. {
  359. try
  360. {
  361. GetCount();
  362. UpdateUI(true);
  363. GetData();
  364. UpdateData();
  365. SetLoading(false);
  366. }
  367. catch (Exception ex)
  368. {
  369. SetLoading(false);
  370. LOG.Write(ex);
  371. }
  372. });
  373. }
  374. private void DisplayResult(bool firstTimeDisplay)
  375. {
  376. if (IsLoading || _total <= 0)
  377. return;
  378. UpdateUI(firstTimeDisplay);
  379. SetLoading(true);
  380. Task.Factory.StartNew(() =>
  381. {
  382. try
  383. {
  384. GetData();
  385. UpdateData();
  386. SetLoading(false);
  387. }
  388. catch (Exception ex)
  389. {
  390. SetLoading(false);
  391. LOG.Write(ex);
  392. }
  393. });
  394. }
  395. private void ClearData()
  396. {
  397. SearchAlarmEvent = false;
  398. SearchWarningEvent = false;
  399. SearchInfoEvent = false;
  400. SearchOpeLog = false;
  401. SearchKeyWords = string.Empty;
  402. SearchDSKeyWords = string.Empty;
  403. searchedResult?.Clear();
  404. SourceLP?.Clear();
  405. SearchedResult?.Clear();
  406. _total = 0;
  407. var now = DateTime.Today;
  408. SearchBeginTime = now;
  409. SearchEndTime = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, 999);
  410. this.view.wfTimeFrom.Value = this.SearchBeginTime;
  411. this.view.wfTimeTo.Value = this.SearchEndTime;
  412. NotifyOfPropertyChange(nameof(SearchedResult));
  413. NotifyOfPropertyChange(nameof(SourceLP));
  414. NotifyOfPropertyChange(nameof(SearchKeyWords));
  415. NotifyOfPropertyChange(nameof(SearchDSKeyWords));
  416. NotifyOfPropertyChange(nameof(SearchAlarmEvent));
  417. NotifyOfPropertyChange(nameof(SearchWarningEvent));
  418. NotifyOfPropertyChange(nameof(SearchInfoEvent));
  419. UpdateUI(true);
  420. }
  421. private void UpdateUI(bool firstTimeDisplay)
  422. {
  423. if (firstTimeDisplay)
  424. {
  425. _currentPage = 1;
  426. }
  427. _countPerPage = int.Parse(SelectedPerPage.Value);
  428. _totalPage = _total / _countPerPage + (_total % _countPerPage > 0 ? 1 : 0);
  429. if (_currentPage < 1)
  430. _currentPage = 1;
  431. if (_currentPage > _totalPage)
  432. _currentPage = _totalPage;
  433. EnableFirst = _currentPage > 1;
  434. EnablePrevious = _currentPage > 1;
  435. EnableNext = _currentPage < _totalPage;
  436. EnableLast = _currentPage < _totalPage;
  437. PageInfo = $"{_currentPage}/{_totalPage}";
  438. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  439. {
  440. NotifyOfPropertyChange(nameof(PageInfo));
  441. NotifyOfPropertyChange(nameof(EnableFirst));
  442. NotifyOfPropertyChange(nameof(EnableLast));
  443. NotifyOfPropertyChange(nameof(EnableNext));
  444. NotifyOfPropertyChange(nameof(EnablePrevious));
  445. }));
  446. }
  447. private void SetLoading(bool isLoading)
  448. {
  449. IsLoading = isLoading;
  450. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  451. {
  452. NotifyOfPropertyChange(nameof(IsLoading));
  453. }));
  454. }
  455. private void UpdateData()
  456. {
  457. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  458. {
  459. SearchedResult.Clear();
  460. if (_resultEvent != null && _resultEvent.Count > 0)
  461. {
  462. string logTypeStr;
  463. for (int j = 0; j < _resultEvent.Count; j++)
  464. {
  465. EventItem ev = _resultEvent[j];
  466. switch (ev.Level)
  467. {
  468. case EventLevel.Information: logTypeStr = "Info"; break;
  469. case EventLevel.Warning: logTypeStr = "Warning"; break;
  470. case EventLevel.Alarm: logTypeStr = "Alarm"; break;
  471. default: logTypeStr = "Undefine"; break;
  472. }
  473. SearchedResult.Add(new SystemLogItem()
  474. {
  475. Time = ((DateTime)ev.OccuringTime).ToString("yyyy/MM/dd HH:mm:ss.fff"),
  476. LogType = logTypeStr,
  477. Detail = ev.Description,
  478. TargetChamber = ev.Source,
  479. Initiator = "",
  480. Icon = _bitmapImageByEventLevel[ev.Level]
  481. });
  482. }
  483. NotifyOfPropertyChange("SearchedResult");
  484. }
  485. GetFormSearchedResult();
  486. }));
  487. }
  488. private void GetData()
  489. {
  490. if (SearchAlarmEvent || SearchWarningEvent || SearchInfoEvent)
  491. {
  492. _resultEvent.Clear();
  493. var 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"));
  494. sqlEvent += GetSourceWhere();
  495. sqlEvent += " and (FALSE ";
  496. if (SearchAlarmEvent) sqlEvent += " OR \"level\"='Alarm' ";
  497. if (SearchWarningEvent) sqlEvent += " OR \"level\"='Warning' ";
  498. if (SearchInfoEvent) sqlEvent += " OR \"level\"='Information' ";
  499. sqlEvent += " ) ";
  500. if (!string.IsNullOrWhiteSpace(SelectedEvent) && SelectedEvent != "All") sqlEvent += string.Format(" and lower(\"event_enum\")='{0}' ", SelectedEvent.ToLower());
  501. if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlEvent += string.Format(" and lower(\"description\") like '%{0}%' ", SearchKeyWords.ToLower());
  502. if (_currentPage < 1)
  503. _currentPage = 1;
  504. if (_currentPage > _totalPage)
  505. _currentPage = _totalPage;
  506. sqlEvent += $" order by \"occur_time\" ASC limit {_countPerPage} offset {(_currentPage - 1) * _countPerPage};";
  507. _resultEvent.AddRange(QueryDBEventFunc(sqlEvent));
  508. }
  509. }
  510. private void GetCount()
  511. {
  512. if (SearchAlarmEvent || SearchWarningEvent || SearchInfoEvent)
  513. {
  514. var sqlEvent = string.Format("SELECT COUNT(*) FROM \"event_data\" where \"occur_time\" >='{0}' and \"occur_time\" <='{1}' ", SearchBeginTime.ToString("yyyyMMdd HHmmss"), SearchEndTime.ToString("yyyyMMdd HHmmss"));
  515. sqlEvent += GetSourceWhere();
  516. sqlEvent += " and (FALSE ";
  517. if (SearchAlarmEvent) sqlEvent += " OR \"level\"='Alarm' ";
  518. if (SearchWarningEvent) sqlEvent += " OR \"level\"='Warning' ";
  519. if (SearchInfoEvent) sqlEvent += " OR \"level\"='Information' ";
  520. sqlEvent += " ) ";
  521. if (!string.IsNullOrWhiteSpace(SelectedEvent) && SelectedEvent != "All") sqlEvent += string.Format(" and lower(\"event_enum\")='{0}' ", SelectedEvent.ToLower());
  522. if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlEvent += string.Format(" and lower(\"description\") like '%{0}%' ", SearchKeyWords.ToLower());
  523. _total = QueryDataClient.Instance.Service.GetCount(sqlEvent);
  524. }
  525. }
  526. }
  527. }