EventViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Threading.Tasks;
  5. using System.Windows;
  6. using System.Windows.Media.Imaging;
  7. using Aitex.Core.RT.Event;
  8. using Aitex.Core.RT.Log;
  9. using MECF.Framework.Common.DataCenter;
  10. using OpenSEMI.ClientBase;
  11. namespace VirgoUI.Client.Models.DataLog.Event
  12. {
  13. public class SystemLogItem
  14. {
  15. /// <summary>
  16. /// 时间
  17. /// </summary>
  18. public string Time { get; set; }
  19. /// <summary>
  20. /// ICON
  21. /// </summary>
  22. public object Icon { get; set; }
  23. /// <summary>
  24. /// 类型:操作日志|事件|其他
  25. /// </summary>
  26. public string LogType { get; set; }
  27. /// <summary>
  28. /// 针对腔体
  29. /// </summary>
  30. public string TargetChamber { get; set; }
  31. /// <summary>
  32. /// 发起方
  33. /// </summary>
  34. public string Initiator { get; set; }
  35. /// <summary>
  36. /// 详情
  37. /// </summary>
  38. public string Detail { get; set; }
  39. }
  40. public class EventViewModel : BaseModel
  41. {
  42. public EventViewModel()
  43. {
  44. this.DisplayName = "Event";
  45. this.QueryDBEventFunc = (sql) => QueryDataClient.Instance.Service.QueryDBEvent(sql);
  46. this.QueryEventList = () =>
  47. {
  48. List<string> result = new List<string>();
  49. foreach (var eventName in Enum.GetNames(typeof(EventEnum)))
  50. result.Add(eventName);
  51. return result;
  52. };
  53. var now = DateTime.Today;
  54. SearchBeginTime = now;// -new TimeSpan(1, 0, 0, 0);
  55. SearchEndTime = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, 999);
  56. SelectedUser = "All";
  57. SearchKeyWords = string.Empty;
  58. SearchAlarmEvent = true;
  59. SearchWarningEvent = true;
  60. SearchInfoEvent = true;
  61. SearchOpeLog = false;
  62. SearchPMA = false;
  63. SearchPMB = false;
  64. SearchPMC = false;
  65. SearchPMD = false;
  66. //SearchCoolDown = false;
  67. SearchTM = false;
  68. SearchLL = false;
  69. //SearchBuf1 = false;
  70. SearchSystem = false;
  71. }
  72. protected override void OnInitialize()
  73. {
  74. base.OnInitialize();
  75. }
  76. protected override void OnViewLoaded(object _view)
  77. {
  78. base.OnViewLoaded(_view);
  79. this.view = (EventView)_view;
  80. this.view.wfTimeFrom.Value = this.SearchBeginTime;
  81. this.view.wfTimeTo.Value = this.SearchEndTime;
  82. this.Preload();
  83. }
  84. public bool SearchAlarmEvent { get; set; }
  85. public bool SearchWarningEvent { get; set; }
  86. public bool SearchInfoEvent { get; set; }
  87. public bool SearchOpeLog { get; set; }
  88. public bool SearchPMA { get; set; }
  89. public bool SearchPMB { get; set; }
  90. public bool SearchPMC { get; set; }
  91. public bool SearchPMD { get; set; }
  92. //public bool SearchCoolDown { get; set; }
  93. public bool SearchTM { get; set; }
  94. public bool SearchLL { get; set; }
  95. //public bool SearchBuf1 { get; set; }
  96. public bool SearchSystem { get; set; }
  97. public string SearchKeyWords { get; set; }
  98. public DateTime SearchBeginTime { get; set; }
  99. public DateTime SearchEndTime { get; set; }
  100. public string Keywords { get; set; }
  101. public ObservableCollection<string> EventList { get; set; }
  102. public string SelectedEvent { get; set; }
  103. public ObservableCollection<string> UserList { get; set; }
  104. public string SelectedUser { get; set; }
  105. public ObservableCollection<Aitex.Core.UI.View.Common.SystemLogItem> SearchedResult { get; set; }
  106. public Func<string, List<EventItem>> QueryDBEventFunc { get; set; }
  107. public Func<List<string>> QueryEventList { get; set; }
  108. /// <summary>
  109. /// 预先载入数据
  110. /// </summary>
  111. public void Preload()
  112. {
  113. //初始化所有的枚举事件类型
  114. EventList = new ObservableCollection<string>();
  115. EventList.Add("All");
  116. if (QueryEventList != null)
  117. {
  118. List<string> evList = QueryEventList();
  119. foreach (string ev in evList)
  120. EventList.Add(ev);
  121. }
  122. SelectedEvent = "All";
  123. //初始化所有的用户
  124. //PreloadUsers();
  125. //所有属性更新
  126. //NotifyOfPropertyChange();
  127. //第一次默认查询
  128. if (SearchedResult == null)
  129. Search();
  130. }
  131. /// <summary>
  132. /// 导出
  133. /// </summary>
  134. public void Export()
  135. {
  136. try
  137. {
  138. Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
  139. dlg.DefaultExt = ".xls"; // Default file extension
  140. dlg.Filter = "Excel数据表格文件(*.xls)|*.xls"; // Filter files by extension
  141. Nullable<bool> result = dlg.ShowDialog();// Show open file dialog box
  142. if (result == true) // Process open file dialog box results
  143. {
  144. System.Data.DataSet ds = new System.Data.DataSet();
  145. ds.Tables.Add(new System.Data.DataTable("系统运行日志"));
  146. ds.Tables[0].Columns.Add("类型");
  147. ds.Tables[0].Columns.Add("时间");
  148. ds.Tables[0].Columns.Add("腔体");
  149. ds.Tables[0].Columns.Add("发起源");
  150. ds.Tables[0].Columns.Add("内容描述");
  151. foreach (var item in SearchedResult)
  152. {
  153. var row = ds.Tables[0].NewRow();
  154. row[0] = item.LogType;
  155. row[1] = item.Time;
  156. row[2] = item.TargetChamber;
  157. row[3] = item.Initiator;
  158. row[4] = item.Detail;
  159. ds.Tables[0].Rows.Add(row);
  160. }
  161. ds.WriteXml(dlg.FileName);
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. LOG.Write(ex);
  167. MessageBox.Show("导出系统日志发生错误", "导出失败", MessageBoxButton.OK, MessageBoxImage.Warning);
  168. }
  169. }
  170. private string GetSourceWhere()
  171. {
  172. //if (!SearchPMA) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorA);
  173. //if (!SearchPMB) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorB);
  174. //if (!SearchPMC) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorC);
  175. //if (!SearchPMD) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorD);
  176. //if (!SearchSystem) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.System);
  177. //if (!SearchLL) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.Loadlock);
  178. //if (!SearchTM) sqlEvent += string.Format(" and \"Source\"<>'{0}' and \"Source\"<>'{1}' and \"Source\"<>'{2}' ",
  179. // ChamberSet.Loadlock, ChamberSet.Buffer1, ChamberSet.Cooldown);
  180. return "";
  181. }
  182. /*
  183. *gid integer NOT NULL DEFAULT nextval('event_data_gid_seq'::regclass),
  184. event_id integer,
  185. event_enum text,
  186. type text,
  187. source text,
  188. description text,
  189. level text,
  190. occur_time timestamp without time zone,
  191. CONSTRAINT event_data_pkey PRIMARY KEY (gid)
  192. */
  193. /// <summary>
  194. /// 查询
  195. /// </summary>
  196. public void Search()
  197. {
  198. Task.Factory.StartNew(() =>
  199. {
  200. try
  201. {
  202. this.SearchBeginTime = this.view.wfTimeFrom.Value;
  203. this.SearchEndTime = this.view.wfTimeTo.Value;
  204. string sqlEvent = "";
  205. string sqlOperationLog = "";
  206. string sql = "";
  207. if (SearchAlarmEvent || SearchWarningEvent || SearchInfoEvent)
  208. {
  209. 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"));
  210. sqlEvent += GetSourceWhere();
  211. sqlEvent += " and (FALSE ";
  212. if (SearchAlarmEvent) sqlEvent += " OR \"level\"='Alarm' ";
  213. if (SearchWarningEvent) sqlEvent += " OR \"level\"='Warning' ";
  214. if (SearchInfoEvent) sqlEvent += " OR \"level\"='Information' ";
  215. sqlEvent += " ) ";
  216. if (!string.IsNullOrWhiteSpace(SelectedEvent) && SelectedEvent != "All") sqlEvent += string.Format(" and lower(\"event_enum\")='{0}' ", SelectedEvent.ToLower());
  217. //if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlEvent += string.Format(" and lower(\"description\") like '%{0}%' ", SearchKeyWords.ToLower());
  218. if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlEvent += string.Format(" and lower(\"description\") like '%{0}%' or lower(\"type\") like '%{1}%'", SearchKeyWords.ToLower(), SearchKeyWords.ToLower());
  219. }
  220. if (SearchOpeLog)
  221. {
  222. //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"));
  223. //if (!SearchPMA) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorA);
  224. //if (!SearchPMB) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorB);
  225. //if (!SearchPMC) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorC);
  226. //if (!SearchPMD) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorD);
  227. //if (!SearchSystem) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.System);
  228. //if (!SearchLL) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.Loadlock);
  229. //if (!SearchTM) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' and \"ChamberId\"<>'{1}' and \"ChamberId\"<>'{2}' ",
  230. // ChamberSet.Loadlock, ChamberSet.Buffer1, ChamberSet.Cooldown);
  231. //if (!string.IsNullOrWhiteSpace(SelectedUser) && SelectedUser != "不限") sqlOperationLog += string.Format(" and lower(\"UserName\")='{0}' ", SelectedUser.ToLower());
  232. //if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlOperationLog += string.Format(" and lower(\"Content\") like '%{0}%' ", SearchKeyWords.ToLower());
  233. }
  234. sql = sqlEvent;
  235. if (!string.IsNullOrEmpty(sqlOperationLog))
  236. {
  237. if (string.IsNullOrEmpty(sql))
  238. {
  239. sql = sqlOperationLog;
  240. }
  241. else
  242. {
  243. sql += " UNION ALL " + sqlOperationLog;
  244. }
  245. }
  246. if (!string.IsNullOrEmpty(sql) && QueryDBEventFunc != null)
  247. {
  248. sql += " order by \"occur_time\" DESC limit 2000;";
  249. List<EventItem> lstEvent = QueryDBEventFunc(sql);
  250. if (lstEvent == null)
  251. return;
  252. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  253. {
  254. SearchedResult = new ObservableCollection<Aitex.Core.UI.View.Common.SystemLogItem>();
  255. string logTypeStr;
  256. foreach (EventItem ev in lstEvent)
  257. {
  258. switch (ev.Level)
  259. {
  260. case EventLevel.Information: logTypeStr = "Info"; break;
  261. case EventLevel.Warning: logTypeStr = "Warning"; break;
  262. case EventLevel.Alarm: logTypeStr = "Alarm"; break;
  263. default: logTypeStr = "Undefine"; break;
  264. }
  265. SearchedResult.Add(new Aitex.Core.UI.View.Common.SystemLogItem()
  266. {
  267. Time = ((DateTime)ev.OccuringTime).ToString("yyyy/MM/dd HH:mm:ss.fff"),
  268. LogType = logTypeStr,
  269. Detail = ev.Description,
  270. TargetChamber = ev.Source,
  271. Initiator = "",
  272. Icon = new BitmapImage(new Uri(string.Format("pack://application:,,,/MECF.Framework.Common;component/Resources/SystemLog/{0}.png", ev.Level.ToString()), UriKind.Absolute))
  273. });
  274. }
  275. NotifyOfPropertyChange("SearchedResult");
  276. if (SearchedResult.Count >= 2000)
  277. {
  278. //MessageBox.Show("Only display max 2000 items,reset the query condition", "query too many result", MessageBoxButton.OK, MessageBoxImage.Warning);
  279. }
  280. }));
  281. }
  282. else
  283. {
  284. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  285. {
  286. SearchedResult = new ObservableCollection<Aitex.Core.UI.View.Common.SystemLogItem>();
  287. NotifyOfPropertyChange("SearchedResult");
  288. }));
  289. }
  290. }
  291. catch (Exception ex)
  292. {
  293. LOG.Write(ex);
  294. }
  295. });
  296. }
  297. private EventView view;
  298. }
  299. }