EventViewModel.cs 14 KB

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