EventViewModel.cs 15 KB

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