EventViewModel.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. using Aitex.Core.RT.Event;
  2. using Aitex.Core.RT.Log;
  3. using MECF.Framework.Common.DataCenter;
  4. using MECF.Framework.Common.Utilities;
  5. using MECF.Framework.UI.Client.CenterViews.DataLogs.Event;
  6. using OpenSEMI.ClientBase.Command;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using System.Windows.Input;
  15. using System.Windows.Media.Imaging;
  16. using FurnaceUI.Models;
  17. namespace FurnaceUI.Views.DataLog
  18. {
  19. public class EventViewModel : FurnaceUIViewModelBase
  20. {
  21. public int AlarmCount { get; set; }
  22. public int WarningCount { get; set; }
  23. public int InformationCount { get; set; }
  24. public bool IsPermission { get => this.Permission == 3; }
  25. private EventView view;
  26. public ObservableCollection<string> SourceLP { get; set; }
  27. public ObservableCollection<string> sourcelp { get; set; }
  28. //public ObservableCollection<string> sourceLP { get; set; }
  29. private string _selectedValueLP;
  30. public string SelectedValueLP
  31. {
  32. get
  33. {
  34. return _selectedValueLP;
  35. }
  36. set
  37. {
  38. _selectedValueLP = value;
  39. NotifyOfPropertyChange("SelectedValueLP");
  40. }
  41. }
  42. public ObservableCollection<string> ToolTipValueLP { get; set; }
  43. public ObservableCollection<string> SourceDS { get; set; }
  44. public ObservableCollection<string> sourceDS { get; set; }
  45. public string SelectedValueDS { get; set; }
  46. public ICommand tbLoadPort1SelectionChangedCommand { get; set; }
  47. //public ICommand tbLoadPort2SelectionChangedCommand { get; set; }
  48. public EventViewModel()
  49. {
  50. this.DisplayName = "Event";
  51. this.QueryDBEventFunc = (sql) => QueryDataClient.Instance.Service.QueryDBEvent(sql);
  52. this.QueryEventList = () =>
  53. {
  54. List<string> result = new List<string>();
  55. foreach (var eventName in Enum.GetNames(typeof(EventEnum)))
  56. result.Add(eventName);
  57. return result;
  58. };
  59. var now = DateTime.Today;
  60. SearchBeginTime = DateTime.Now.AddHours(-1);// -new TimeSpan(1, 0, 0, 0);
  61. SearchEndTime = DateTime.Now;
  62. SelectedUser = "All";
  63. SearchKeyWords = string.Empty;
  64. SearchAlarmEvent = true;
  65. SearchWarningEvent = true;
  66. SearchInfoEvent = true;
  67. SearchOpeLog = false;
  68. SearchPMA = false;
  69. SearchPMB = false;
  70. SearchPMC = false;
  71. SearchPMD = false;
  72. //SearchCoolDown = false;
  73. SearchTM = false;
  74. SearchLL = false;
  75. //SearchBuf1 = false;
  76. SearchSystem = false;
  77. SourceLP = new ObservableCollection<string>();
  78. //sourceLP = new ObservableCollection<string>();
  79. //SourceLP = new ObservableCollection<string>(new[] { "LP1", "LP2", "LP3","lp4" });
  80. //SourceDS = new ObservableCollection<string>();
  81. //sourceDS = new ObservableCollection<string>();
  82. //ToolTipValueLP = new ObservableCollection<string>();
  83. tbLoadPort1SelectionChangedCommand = new BaseCommand<object>(tbLoadPort1Selection);
  84. searchedResult = new ObservableCollection<SystemLogItem>();
  85. sourcelp = new ObservableCollection<string>();
  86. //tbLoadPort2SelectionChangedCommand = new BaseCommand<object>(tbLoadPort2Selection);
  87. }
  88. protected override void OnInitialize()
  89. {
  90. base.OnInitialize();
  91. }
  92. protected override void OnViewLoaded(object _view)
  93. {
  94. base.OnViewLoaded(_view);
  95. this.view = (EventView)_view;
  96. this.view.wfTimeFrom.Content = SearchBeginTime.ToString("yyyy-MM-dd HH:mm:ss");
  97. this.view.wfTimeTo.Content = SearchEndTime.ToString("yyyy-MM-dd HH:mm:ss");
  98. this.Preload();
  99. }
  100. private bool _SearchAlarmEvent;
  101. public bool SearchAlarmEvent
  102. {
  103. get => _SearchAlarmEvent;
  104. set
  105. {
  106. _SearchAlarmEvent = value;
  107. NotifyOfPropertyChange("SearchAlarmEvent");
  108. }
  109. }
  110. private bool _SearchWarningEvent;
  111. public bool SearchWarningEvent
  112. {
  113. get => _SearchWarningEvent;
  114. set
  115. {
  116. _SearchWarningEvent = value;
  117. NotifyOfPropertyChange("SearchWarningEvent");
  118. }
  119. }
  120. private bool _SearchInfoEvent;
  121. public bool SearchInfoEvent
  122. {
  123. get => _SearchInfoEvent;
  124. set
  125. {
  126. _SearchInfoEvent = value;
  127. NotifyOfPropertyChange("SearchInfoEvent");
  128. }
  129. }
  130. public bool SearchOpeLog { get; set; }
  131. public bool SearchPMA { get; set; }
  132. public bool SearchPMB { get; set; }
  133. public bool SearchPMC { get; set; }
  134. public bool SearchPMD { get; set; }
  135. //public bool SearchCoolDown { get; set; }
  136. public bool SearchTM { get; set; }
  137. public bool SearchLL { get; set; }
  138. //public bool SearchBuf1 { get; set; }
  139. public bool SearchSystem { get; set; }
  140. public string SearchKeyWords { get; set; }
  141. public string SearchDSKeyWords { get; set; }
  142. public DateTime SearchBeginTime { get; set; }
  143. public DateTime SearchEndTime { get; set; }
  144. public string Keywords { get; set; }
  145. public ObservableCollection<string> EventList { get; set; }
  146. public string SelectedEvent { get; set; }
  147. public ObservableCollection<string> UserList { get; set; }
  148. public string SelectedUser { get; set; }
  149. public ObservableCollection<SystemLogItem> SearchedResult { get; set; }
  150. public ObservableCollection<SystemLogItem> searchedResult { get; set; }
  151. public Func<string, List<EventItem>> QueryDBEventFunc { get; set; }
  152. public Func<List<string>> QueryEventList { get; set; }
  153. /// <summary>
  154. /// 预先载入数据
  155. /// </summary>
  156. public void Preload()
  157. {
  158. //初始化所有的枚举事件类型
  159. EventList = new ObservableCollection<string>();
  160. EventList.Add("All");
  161. if (QueryEventList != null)
  162. {
  163. List<string> evList = QueryEventList();
  164. foreach (string ev in evList)
  165. EventList.Add(ev);
  166. }
  167. SelectedEvent = "All";
  168. //初始化所有的用户
  169. //PreloadUsers();
  170. //所有属性更新
  171. //NotifyOfPropertyChange();
  172. //第一次默认查询
  173. if (SearchedResult == null)
  174. Search();
  175. }
  176. public void GetFormSearchedResult()
  177. {
  178. try
  179. {
  180. if (SearchedResult != null)
  181. {
  182. SourceLP.Clear();
  183. searchedResult.Clear();
  184. SearchedResult.ToList().ForEach(i => searchedResult.Add(i));
  185. SourceLP.Add("ALL");
  186. sourcelp.Add("ALL");
  187. foreach (var result in SearchedResult)
  188. {
  189. if (!SourceLP.Contains(result.TargetChamber))
  190. {
  191. SourceLP.Add(result.TargetChamber);
  192. sourcelp.Add(result.TargetChamber);
  193. }
  194. }
  195. }
  196. }
  197. catch (Exception ex)
  198. {
  199. LOG.Write(ex);
  200. }
  201. }
  202. public void SelectDate(string SelectType)
  203. {
  204. var windowManager = Caliburn.Micro.Core.IoC.Get<Caliburn.Micro.IWindowManager>();
  205. if (SelectType == "Start")
  206. {
  207. this.SearchBeginTime = Convert.ToDateTime(this.view.wfTimeFrom.Content);
  208. SelectDateViewModel selectdateViewModel = new SelectDateViewModel(SearchBeginTime);
  209. var result = (windowManager as Caliburn.Micro.WindowManager)?.ShowDialogWithTitle(selectdateViewModel, null, "Start Time");
  210. if (result == true)
  211. {
  212. this.view.wfTimeFrom.Content = selectdateViewModel.SearchDate;
  213. }
  214. }
  215. else
  216. {
  217. this.SearchEndTime = Convert.ToDateTime(this.view.wfTimeTo.Content);
  218. SelectDateViewModel selectdateViewModel = new SelectDateViewModel(SearchEndTime);
  219. var result = (windowManager as Caliburn.Micro.WindowManager)?.ShowDialogWithTitle(selectdateViewModel, null, "End Time");
  220. if (result == true)
  221. {
  222. this.view.wfTimeTo.Content = selectdateViewModel.SearchDate;
  223. }
  224. }
  225. }
  226. /// <summary>
  227. /// 筛选信息
  228. /// </summary>
  229. public void Filter()
  230. {
  231. try
  232. {
  233. if (SelectedValueLP != "" && SearchedResult != null && SearchedResult.Count > 0)
  234. {
  235. SearchedResult.Clear();
  236. searchedResult.ToList().ForEach(i => SearchedResult.Add(i));
  237. string[] lsvp = SelectedValueLP.Split(',');
  238. SourceLP.ToList().ForEach(i => { if (!lsvp.Contains(i) && i != "ALL") SearchedResult.ToList().ForEach(m => { if (m.TargetChamber == i) SearchedResult.Remove(m); }); });// sourceLP.Add(i); });
  239. }
  240. else SearchedResult.Clear();
  241. if (SearchedResult != null && !string.IsNullOrWhiteSpace(SearchDSKeyWords))
  242. {
  243. SearchedResult.ToList().ForEach(m => { if (!m.Detail.Contains(SearchDSKeyWords)) SearchedResult.Remove(m); });
  244. }
  245. NotifyOfPropertyChange("SearchedResult");
  246. }
  247. catch (Exception ex)
  248. {
  249. LOG.Write(ex);
  250. MessageBox.Show("筛选信息失败", "筛选失败", MessageBoxButton.OK, MessageBoxImage.Warning);
  251. }
  252. }
  253. /// <summary>
  254. /// 命令
  255. /// </summary>
  256. /// <param name="o"></param>
  257. public void tbLoadPort1Selection(object o)
  258. {
  259. if (o != null)
  260. {
  261. var Item = o as ItemSelectionData;
  262. if (Item.SelectItem == "ALL")
  263. {
  264. if (Item.IsSelect)
  265. {
  266. SourceLP.ToList().ForEach(sp => { if (!sourcelp.Contains(sp)) sourcelp.Add(sp); });
  267. }
  268. else
  269. {
  270. sourcelp.Clear();
  271. }
  272. }
  273. }
  274. }
  275. public void SelectValueLP()
  276. {
  277. var windowManager = Caliburn.Micro.Core.IoC.Get<Caliburn.Micro.IWindowManager>();
  278. SelectFilterConditionViewModel selectFilterViewModel = new SelectFilterConditionViewModel();
  279. (windowManager as Caliburn.Micro.WindowManager)?.ShowDialogWithTitle(selectFilterViewModel, null, "Select Condition");
  280. SelectedValueLP = selectFilterViewModel.FilterConditions;
  281. }
  282. /// <summary>
  283. /// 导出
  284. /// </summary>
  285. public void Export()
  286. {
  287. try
  288. {
  289. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  290. dlg.DefaultExt = ".xlsx"; // Default file extension
  291. dlg.FileName = $"Operation Log_{DateTime.Now:yyyyMMdd_HHmmss}";
  292. dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension
  293. Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
  294. if (result == true) // Process open file dialog box results
  295. {
  296. System.Data.DataSet ds = new System.Data.DataSet();
  297. ds.Tables.Add(new System.Data.DataTable("系统运行日志"));
  298. ds.Tables[0].Columns.Add("Type");
  299. ds.Tables[0].Columns.Add("Time");
  300. ds.Tables[0].Columns.Add("System");
  301. ds.Tables[0].Columns.Add("Content");
  302. foreach (var item in SearchedResult)
  303. {
  304. var row = ds.Tables[0].NewRow();
  305. row[0] = item.LogType;
  306. row[1] = item.Time;
  307. row[2] = item.TargetChamber;
  308. row[3] = item.Detail;
  309. ds.Tables[0].Rows.Add(row);
  310. }
  311. if (!ExcelHelper.ExportToExcel(dlg.FileName, ds, out string reason))
  312. {
  313. MessageBox.Show($"Export failed, {reason}", "Export", MessageBoxButton.OK, MessageBoxImage.Warning);
  314. return;
  315. }
  316. MessageBox.Show($"Export succeed, file save as {dlg.FileName}", "Export", MessageBoxButton.OK, MessageBoxImage.Information);
  317. }
  318. }
  319. catch (Exception ex)
  320. {
  321. LOG.Write(ex);
  322. MessageBox.Show("导出系统日志发生错误", "导出失败", MessageBoxButton.OK, MessageBoxImage.Warning);
  323. }
  324. }
  325. private string GetSourceWhere()
  326. {
  327. //if (!SearchPMA) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorA);
  328. //if (!SearchPMB) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorB);
  329. //if (!SearchPMC) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorC);
  330. //if (!SearchPMD) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorD);
  331. //if (!SearchSystem) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.System);
  332. //if (!SearchLL) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.Loadlock);
  333. //if (!SearchTM) sqlEvent += string.Format(" and \"Source\"<>'{0}' and \"Source\"<>'{1}' and \"Source\"<>'{2}' ",
  334. // ChamberSet.Loadlock, ChamberSet.Buffer1, ChamberSet.Cooldown);
  335. return "";
  336. }
  337. /*
  338. *gid integer NOT NULL DEFAULT nextval('event_data_gid_seq'::regclass),
  339. event_id integer,
  340. event_enum text,
  341. type text,
  342. source text,
  343. description text,
  344. level text,
  345. occur_time timestamp without time zone,
  346. CONSTRAINT event_data_pkey PRIMARY KEY (gid)
  347. */
  348. /// <summary>
  349. /// 查询
  350. /// </summary>
  351. public void Search()
  352. {
  353. this.SearchBeginTime = Convert.ToDateTime(this.view.wfTimeFrom.Content);
  354. this.SearchEndTime = Convert.ToDateTime(this.view.wfTimeTo.Content);
  355. Task.Factory.StartNew(() =>
  356. {
  357. try
  358. {
  359. if (SearchBeginTime > SearchEndTime)
  360. {
  361. MessageBox.Show("Time range invalid, start time should be early than end time");
  362. return;
  363. }
  364. string sqlEvent = "";
  365. string sqlOperationLog = "";
  366. string sql = "";
  367. int logCount = 2000;
  368. if (SearchAlarmEvent || SearchWarningEvent || SearchInfoEvent)
  369. {
  370. sqlEvent = string.Format("SELECT \"event_id\", \"role_id\", \"user_name\", \"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"));
  371. sqlEvent += GetSourceWhere();
  372. sqlEvent += " and (FALSE ";
  373. if (SearchAlarmEvent) sqlEvent += " OR \"level\"='Alarm' ";
  374. if (SearchWarningEvent) sqlEvent += " OR \"level\"='Warning' ";
  375. if (SearchInfoEvent) sqlEvent += " OR \"level\"='Information' ";
  376. sqlEvent += " ) ";
  377. if (!string.IsNullOrWhiteSpace(SelectedEvent) && SelectedEvent != "All") sqlEvent += string.Format(" and lower(\"event_enum\")='{0}' ", SelectedEvent.ToLower());
  378. if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlEvent += string.Format(" and lower(\"description\") like '%{0}%' ", SearchKeyWords.ToLower());
  379. }
  380. if (SearchOpeLog)
  381. {
  382. //sqlOperationLog = string.Format(" SELECT \"UserName\" as \"Initiator\", 'UserOperation' as \"LogType\", \"Time\", \"ChamberId\" as \"TargetChamber\", \"Content\" as \"Description\" FROM \"OperationLog\" where \"Time\" >='{0}' and \"Time\" <='{1}' ", SearchBeginTime.ToString("yyyy/MM/dd HH:mm:ss"), SearchEndTime.ToString("yyyy/MM/dd HH:mm:ss"));
  383. //if (!SearchPMA) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorA);
  384. //if (!SearchPMB) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorB);
  385. //if (!SearchPMC) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorC);
  386. //if (!SearchPMD) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorD);
  387. //if (!SearchSystem) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.System);
  388. //if (!SearchLL) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.Loadlock);
  389. //if (!SearchTM) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' and \"ChamberId\"<>'{1}' and \"ChamberId\"<>'{2}' ",
  390. // ChamberSet.Loadlock, ChamberSet.Buffer1, ChamberSet.Cooldown);
  391. //if (!string.IsNullOrWhiteSpace(SelectedUser) && SelectedUser != "不限") sqlOperationLog += string.Format(" and lower(\"UserName\")='{0}' ", SelectedUser.ToLower());
  392. //if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlOperationLog += string.Format(" and lower(\"Content\") like '%{0}%' ", SearchKeyWords.ToLower());
  393. }
  394. sql = sqlEvent;
  395. if (!string.IsNullOrEmpty(sqlOperationLog))
  396. {
  397. if (string.IsNullOrEmpty(sql))
  398. {
  399. sql = sqlOperationLog;
  400. }
  401. else
  402. {
  403. sql += " UNION ALL " + sqlOperationLog;
  404. }
  405. }
  406. var config = QueryDataClient.Instance.Service.GetConfig("System.LogCount");
  407. if (config != null)
  408. {
  409. logCount = (int)config;
  410. }
  411. if (!string.IsNullOrEmpty(sql) && QueryDBEventFunc != null)
  412. {
  413. sql += $" order by \"occur_time\" desc limit {logCount};";
  414. List<EventItem> lstEvent = QueryDBEventFunc(sql);
  415. if (lstEvent == null)
  416. return;
  417. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  418. {
  419. SearchedResult = new ObservableCollection<SystemLogItem>();
  420. string logTypeStr;
  421. foreach (EventItem ev in lstEvent)
  422. {
  423. switch (ev.Level)
  424. {
  425. case EventLevel.Information: logTypeStr = "Info"; break;
  426. case EventLevel.Warning: logTypeStr = "Warning"; break;
  427. case EventLevel.Alarm: logTypeStr = "Alarm"; break;
  428. default: logTypeStr = "Undefine"; break;
  429. }
  430. SearchedResult.Add(new SystemLogItem()
  431. {
  432. Time = ((DateTime)ev.OccuringTime).ToString("yyyy/MM/dd HH:mm:ss.fff"),
  433. LogType = logTypeStr,
  434. Detail = ev.Description,
  435. TargetChamber = ev.Source,
  436. Initiator = "",
  437. Icon = new BitmapImage(new Uri(string.Format("pack://application:,,,/MECF.Framework.Common;component/Resources/SystemLog/{0}.png", ev.Level.ToString()), UriKind.Absolute))
  438. });
  439. }
  440. NotifyOfPropertyChange("SearchedResult");
  441. if (SearchedResult.Count >= 2000)
  442. {
  443. //MessageBox.Show("Only display max 2000 items,reset the query condition", "query too many result", MessageBoxButton.OK, MessageBoxImage.Warning);
  444. }
  445. GetFormSearchedResult();
  446. }));
  447. }
  448. else
  449. {
  450. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  451. {
  452. SearchedResult = new ObservableCollection<SystemLogItem>();
  453. NotifyOfPropertyChange("SearchedResult");
  454. }));
  455. }
  456. }
  457. catch (Exception ex)
  458. {
  459. LOG.Write(ex);
  460. }
  461. });
  462. }
  463. public void QueryOptionCmd(string value)
  464. {
  465. switch (value)
  466. {
  467. case "Alarm":
  468. AlarmCount++;
  469. if (AlarmCount % 2 == 0)
  470. SearchAlarmEvent = true;
  471. else
  472. SearchAlarmEvent = false;
  473. break;
  474. case "Warning":
  475. WarningCount++;
  476. if (WarningCount % 2 == 0)
  477. SearchWarningEvent = true;
  478. else
  479. SearchWarningEvent = false;
  480. break;
  481. case "Information":
  482. InformationCount++;
  483. if (InformationCount % 2 == 0)
  484. SearchInfoEvent = true;
  485. else
  486. SearchInfoEvent = false;
  487. break;
  488. default:
  489. break;
  490. }
  491. }
  492. }
  493. }