EventViewModel.cs 14 KB

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