EventViewModel.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. namespace Aitex.Core.UI.View.Common
  15. {
  16. /// <summary>
  17. /// 操作记录查询结果结构定义
  18. /// </summary>
  19. public class SystemLogItem
  20. {
  21. public string ID { get; set; }
  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 = ".xls"; // Default file extension
  130. dlg.Filter = "Excel数据表格文件(*.xls)|*.xls"; // 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. ds.WriteXml(dlg.FileName);
  152. }
  153. }
  154. catch (Exception ex)
  155. {
  156. LOG.WriteExeption(ex);
  157. MessageBox.Show("导出系统日志发生错误", "导出失败", MessageBoxButton.OK, MessageBoxImage.Warning);
  158. }
  159. }
  160. private string GetSourceWhere()
  161. {
  162. //if (!SearchPMA) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorA);
  163. //if (!SearchPMB) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorB);
  164. //if (!SearchPMC) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorC);
  165. //if (!SearchPMD) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorD);
  166. //if (!SearchSystem) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.System);
  167. //if (!SearchLL) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.Loadlock);
  168. //if (!SearchTM) sqlEvent += string.Format(" and \"Source\"<>'{0}' and \"Source\"<>'{1}' and \"Source\"<>'{2}' ",
  169. // ChamberSet.Loadlock, ChamberSet.Buffer1, ChamberSet.Cooldown);
  170. return "";
  171. }
  172. /*
  173. *
  174. * gid integer NOT NULL DEFAULT nextval('event_data_gid_seq'::regclass),
  175. event_id integer,
  176. event_enum text,
  177. type text,
  178. source text,
  179. description text,
  180. level text,
  181. occur_time timestamp without time zone,
  182. CONSTRAINT event_data_pkey PRIMARY KEY (gid)
  183. */
  184. /// <summary>
  185. /// 查询
  186. /// </summary>
  187. void Search()
  188. {
  189. Task.Factory.StartNew(() =>
  190. {
  191. try
  192. {
  193. string sqlEvent = "";
  194. string sqlOperationLog = "";
  195. string sql = "";
  196. if (SearchAlarmEvent || SearchWarningEvent || SearchInfoEvent)
  197. {
  198. 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"));
  199. sqlEvent += GetSourceWhere();
  200. sqlEvent += " and (FALSE ";
  201. if (SearchAlarmEvent) sqlEvent += " OR \"level\"='Alarm' ";
  202. if (SearchWarningEvent) sqlEvent += " OR \"level\"='Warning' ";
  203. if (SearchInfoEvent) sqlEvent += " OR \"level\"='Information' ";
  204. sqlEvent += " ) ";
  205. if (!string.IsNullOrWhiteSpace(SelectedEvent) && SelectedEvent != "All") sqlEvent += string.Format(" and lower(\"event_enum\")='{0}' ", SelectedEvent.ToLower());
  206. if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlEvent += string.Format(" and lower(\"description\") like '%{0}%' ", SearchKeyWords.ToLower());
  207. }
  208. if (SearchOpeLog)
  209. {
  210. //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"));
  211. //if (!SearchPMA) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorA);
  212. //if (!SearchPMB) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorB);
  213. //if (!SearchPMC) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorC);
  214. //if (!SearchPMD) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorD);
  215. //if (!SearchSystem) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.System);
  216. //if (!SearchLL) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.Loadlock);
  217. //if (!SearchTM) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' and \"ChamberId\"<>'{1}' and \"ChamberId\"<>'{2}' ",
  218. // ChamberSet.Loadlock, ChamberSet.Buffer1, ChamberSet.Cooldown);
  219. //if (!string.IsNullOrWhiteSpace(SelectedUser) && SelectedUser != "不限") sqlOperationLog += string.Format(" and lower(\"UserName\")='{0}' ", SelectedUser.ToLower());
  220. //if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlOperationLog += string.Format(" and lower(\"Content\") like '%{0}%' ", SearchKeyWords.ToLower());
  221. }
  222. sql = sqlEvent;
  223. if (!string.IsNullOrEmpty(sqlOperationLog))
  224. {
  225. if (string.IsNullOrEmpty(sql))
  226. {
  227. sql = sqlOperationLog;
  228. }
  229. else
  230. {
  231. sql += " UNION ALL " + sqlOperationLog;
  232. }
  233. }
  234. if (!string.IsNullOrEmpty(sql) && QueryDBEventFunc != null)
  235. {
  236. sql += " order by \"occur_time\" asc limit 2000;";
  237. List<EventItem> lstEvent = QueryDBEventFunc(sql);
  238. if (lstEvent == null)
  239. return;
  240. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  241. {
  242. SearchedResult = new ObservableCollection<SystemLogItem>();
  243. string logTypeStr;
  244. foreach (EventItem ev in lstEvent)
  245. {
  246. switch (ev.Level)
  247. {
  248. case EventLevel.Information: logTypeStr = "Info"; break;
  249. case EventLevel.Warning: logTypeStr = "Warning"; break;
  250. case EventLevel.Alarm: logTypeStr = "Alarm"; break;
  251. default: logTypeStr = "Undefine"; break;
  252. }
  253. SearchedResult.Add(new SystemLogItem()
  254. {
  255. Time = ((DateTime)ev.OccuringTime).ToString("yyyy/MM/dd HH:mm:ss.fff"),
  256. LogType = logTypeStr,
  257. Detail = ev.Description,
  258. TargetChamber = ev.Source,
  259. Initiator = "",
  260. Icon = new BitmapImage(new Uri(string.Format("pack://application:,,,/MECF.Framework.Common;component/Resources/SystemLog/{0}.png", ev.Level.ToString()), UriKind.Absolute))
  261. });
  262. }
  263. InvokePropertyChanged("SearchedResult");
  264. if (SearchedResult.Count >= 2000)
  265. {
  266. //MessageBox.Show("Only display max 2000 items,reset the query condition", "query too many result", MessageBoxButton.OK, MessageBoxImage.Warning);
  267. }
  268. }));
  269. }
  270. else
  271. {
  272. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  273. {
  274. SearchedResult = new ObservableCollection<SystemLogItem>();
  275. InvokePropertyChanged("SearchedResult");
  276. }));
  277. }
  278. }
  279. catch (Exception ex)
  280. {
  281. LOG.WriteExeption(ex);
  282. }
  283. });
  284. }
  285. }
  286. }