EventViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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("系统运行日志"));
  149. ds.Tables[0].Columns.Add("类型");
  150. ds.Tables[0].Columns.Add("时间");
  151. ds.Tables[0].Columns.Add("腔体");
  152. ds.Tables[0].Columns.Add("发起源");
  153. ds.Tables[0].Columns.Add("内容描述");
  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. MessageBox.Show("导出系统日志发生错误", "导出失败", MessageBoxButton.OK, MessageBoxImage.Warning);
  171. }
  172. }
  173. private string GetSourceWhere()
  174. {
  175. //if (!SearchPMA) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorA);
  176. //if (!SearchPMB) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorB);
  177. //if (!SearchPMC) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorC);
  178. //if (!SearchPMD) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorD);
  179. //if (!SearchSystem) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.System);
  180. //if (!SearchLL) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.Loadlock);
  181. //if (!SearchTM) sqlEvent += string.Format(" and \"Source\"<>'{0}' and \"Source\"<>'{1}' and \"Source\"<>'{2}' ",
  182. // ChamberSet.Loadlock, ChamberSet.Buffer1, ChamberSet.Cooldown);
  183. return "";
  184. }
  185. /*
  186. *gid integer NOT NULL DEFAULT nextval('event_data_gid_seq'::regclass),
  187. event_id integer,
  188. event_enum text,
  189. type text,
  190. source text,
  191. description text,
  192. level text,
  193. occur_time timestamp without time zone,
  194. CONSTRAINT event_data_pkey PRIMARY KEY (gid)
  195. */
  196. /// <summary>
  197. /// 查询
  198. /// </summary>
  199. public void Search()
  200. {
  201. if (MenuPermission != 3) return;
  202. Task.Factory.StartNew(() =>
  203. {
  204. try
  205. {
  206. this.SearchBeginTime = this.view.wfTimeFrom.Value;
  207. this.SearchEndTime = this.view.wfTimeTo.Value;
  208. string sqlEvent = "";
  209. string sqlOperationLog = "";
  210. string sql = "";
  211. if (SearchAlarmEvent || SearchWarningEvent || SearchInfoEvent)
  212. {
  213. 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"));
  214. sqlEvent += GetSourceWhere();
  215. sqlEvent += " and (FALSE ";
  216. if (SearchAlarmEvent) sqlEvent += " OR \"level\"='Alarm' ";
  217. if (SearchWarningEvent) sqlEvent += " OR \"level\"='Warning' ";
  218. if (SearchInfoEvent) sqlEvent += " OR \"level\"='Information' ";
  219. sqlEvent += " ) ";
  220. if (!string.IsNullOrWhiteSpace(SelectedEvent) && SelectedEvent != "All") sqlEvent += string.Format(" and lower(\"event_enum\")='{0}' ", SelectedEvent.ToLower());
  221. //if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlEvent += string.Format(" and lower(\"description\") like '%{0}%' ", SearchKeyWords.ToLower());
  222. if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlEvent += string.Format(" and lower(\"description\") like '%{0}%' or lower(\"type\") like '%{1}%'", SearchKeyWords.ToLower(), SearchKeyWords.ToLower());
  223. }
  224. if (SearchOpeLog)
  225. {
  226. //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"));
  227. //if (!SearchPMA) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorA);
  228. //if (!SearchPMB) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorB);
  229. //if (!SearchPMC) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorC);
  230. //if (!SearchPMD) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorD);
  231. //if (!SearchSystem) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.System);
  232. //if (!SearchLL) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.Loadlock);
  233. //if (!SearchTM) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' and \"ChamberId\"<>'{1}' and \"ChamberId\"<>'{2}' ",
  234. // ChamberSet.Loadlock, ChamberSet.Buffer1, ChamberSet.Cooldown);
  235. //if (!string.IsNullOrWhiteSpace(SelectedUser) && SelectedUser != "不限") sqlOperationLog += string.Format(" and lower(\"UserName\")='{0}' ", SelectedUser.ToLower());
  236. //if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlOperationLog += string.Format(" and lower(\"Content\") like '%{0}%' ", SearchKeyWords.ToLower());
  237. }
  238. sql = sqlEvent;
  239. if (!string.IsNullOrEmpty(sqlOperationLog))
  240. {
  241. if (string.IsNullOrEmpty(sql))
  242. {
  243. sql = sqlOperationLog;
  244. }
  245. else
  246. {
  247. sql += " UNION ALL " + sqlOperationLog;
  248. }
  249. }
  250. if (!string.IsNullOrEmpty(sql) && QueryDBEventFunc != null)
  251. {
  252. sql += " order by \"occur_time\" DESC limit 2000;";
  253. List<EventItem> lstEvent = QueryDBEventFunc(sql);
  254. if (lstEvent == null)
  255. return;
  256. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  257. {
  258. SearchedResult = new ObservableCollection<Aitex.Core.UI.View.Common.SystemLogItem>();
  259. string logTypeStr;
  260. foreach (EventItem ev in lstEvent)
  261. {
  262. switch (ev.Level)
  263. {
  264. case EventLevel.Information: logTypeStr = "Info"; break;
  265. case EventLevel.Warning: logTypeStr = "Warning"; break;
  266. case EventLevel.Alarm: logTypeStr = "Alarm"; break;
  267. default: logTypeStr = "Undefine"; break;
  268. }
  269. SearchedResult.Add(new Aitex.Core.UI.View.Common.SystemLogItem()
  270. {
  271. Time = ((DateTime)ev.OccuringTime).ToString("yyyy/MM/dd HH:mm:ss.fff"),
  272. LogType = logTypeStr,
  273. Detail = ev.Description,
  274. TargetChamber = ev.Source,
  275. Initiator = "",
  276. Icon = new BitmapImage(new Uri(string.Format("pack://application:,,,/MECF.Framework.Common;component/Resources/SystemLog/{0}.png", ev.Level.ToString()), UriKind.Absolute))
  277. });
  278. }
  279. NotifyOfPropertyChange("SearchedResult");
  280. if (SearchedResult.Count >= 2000)
  281. {
  282. //MessageBox.Show("Only display max 2000 items,reset the query condition", "query too many result", MessageBoxButton.OK, MessageBoxImage.Warning);
  283. }
  284. }));
  285. }
  286. else
  287. {
  288. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  289. {
  290. SearchedResult = new ObservableCollection<Aitex.Core.UI.View.Common.SystemLogItem>();
  291. NotifyOfPropertyChange("SearchedResult");
  292. }));
  293. }
  294. }
  295. catch (Exception ex)
  296. {
  297. LOG.Write(ex);
  298. }
  299. });
  300. }
  301. private EventView view;
  302. }
  303. }