using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Windows.Media.Imaging; using Aitex.Core.RT.Event; using Aitex.Core.RT.Log; using Aitex.Core.RT.SCCore; using MECF.Framework.Common.DataCenter; using MECF.Framework.Common.Utilities; using MECF.Framework.UI.Client.ClientBase; using OpenSEMI.ClientBase; using OpenSEMI.ClientBase.Command; namespace MECF.Framework.UI.Client.CenterViews.DataLogs.Event { public class SystemLogItem { /// /// 时间 /// public string Time { get; set; } /// /// ICON /// public object Icon { get; set; } /// /// 类型:操作日志|事件|其他 /// public string LogType { get; set; } /// /// 针对腔体 /// public string TargetChamber { get; set; } /// /// 发起方 /// public string Initiator { get; set; } /// /// 详情 /// public string Detail { get; set; } } public class EventViewModel : BaseModel { public int AlarmCount { get; set; } public int WarningCount { get; set; } public int InformationCount { get; set; } public bool IsPermission { get => this.Permission == 3; } private EventView view; public ObservableCollection SourceLP { get; set; } public ObservableCollection sourcelp { get; set; } //public ObservableCollection sourceLP { get; set; } private string _selectedValueLP; public string SelectedValueLP { get { return _selectedValueLP; } set { _selectedValueLP = value; NotifyOfPropertyChange("SelectedValueLP"); } } public ObservableCollection ToolTipValueLP { get; set; } public ObservableCollection SourceDS { get; set; } public ObservableCollection sourceDS { get; set; } public string SelectedValueDS { get; set; } public ICommand tbLoadPort1SelectionChangedCommand { get; set; } //public ICommand tbLoadPort2SelectionChangedCommand { get; set; } public EventViewModel() { this.DisplayName = "Event"; this.QueryDBEventFunc = (sql) => QueryDataClient.Instance.Service.QueryDBEvent(sql); this.QueryEventList = () => { List result = new List(); foreach (var eventName in Enum.GetNames(typeof(EventEnum))) result.Add(eventName); return result; }; var now = DateTime.Today; SearchBeginTime = new DateTime(now.Year, now.Month, now.Day, 00, 00, 00); ;// -new TimeSpan(1, 0, 0, 0); SearchEndTime = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59); SelectedUser = "All"; SearchKeyWords = string.Empty; SearchAlarmEvent = true; SearchWarningEvent = true; SearchInfoEvent = true; SearchOpeLog = false; SearchPMA = false; SearchPMB = false; SearchPMC = false; SearchPMD = false; //SearchCoolDown = false; SearchTM = false; SearchLL = false; //SearchBuf1 = false; SearchSystem = false; SourceLP = new ObservableCollection(); //sourceLP = new ObservableCollection(); //SourceLP = new ObservableCollection(new[] { "LP1", "LP2", "LP3","lp4" }); //SourceDS = new ObservableCollection(); //sourceDS = new ObservableCollection(); //ToolTipValueLP = new ObservableCollection(); tbLoadPort1SelectionChangedCommand = new BaseCommand(tbLoadPort1Selection); searchedResult = new ObservableCollection(); sourcelp = new ObservableCollection(); //tbLoadPort2SelectionChangedCommand = new BaseCommand(tbLoadPort2Selection); } protected override void OnInitialize() { base.OnInitialize(); } protected override void OnViewLoaded(object _view) { base.OnViewLoaded(_view); this.view = (EventView)_view; this.view.wfTimeFrom.Content =SearchBeginTime.ToString("yyyy-MM-dd HH:mm:ss"); this.view.wfTimeTo.Content = SearchEndTime.ToString("yyyy-MM-dd HH:mm:ss"); this.Preload(); } private bool _SearchAlarmEvent; public bool SearchAlarmEvent { get => _SearchAlarmEvent; set { _SearchAlarmEvent = value; NotifyOfPropertyChange("SearchAlarmEvent"); } } private bool _SearchWarningEvent; public bool SearchWarningEvent { get => _SearchWarningEvent; set { _SearchWarningEvent = value; NotifyOfPropertyChange("SearchWarningEvent"); } } private bool _SearchInfoEvent; public bool SearchInfoEvent { get => _SearchInfoEvent; set { _SearchInfoEvent = value; NotifyOfPropertyChange("SearchInfoEvent"); } } public bool SearchOpeLog { get; set; } public bool SearchPMA { get; set; } public bool SearchPMB { get; set; } public bool SearchPMC { get; set; } public bool SearchPMD { get; set; } //public bool SearchCoolDown { get; set; } public bool SearchTM { get; set; } public bool SearchLL { get; set; } //public bool SearchBuf1 { get; set; } public bool SearchSystem { get; set; } public string SearchKeyWords { get; set; } public string SearchDSKeyWords { get; set; } public DateTime SearchBeginTime { get; set; } public DateTime SearchEndTime { get; set; } public string Keywords { get; set; } public ObservableCollection EventList { get; set; } public string SelectedEvent { get; set; } public ObservableCollection UserList { get; set; } public string SelectedUser { get; set; } public ObservableCollection SearchedResult { get; set; } public ObservableCollection searchedResult { get; set; } public Func> QueryDBEventFunc { get; set; } public Func> QueryEventList { get; set; } /// /// 预先载入数据 /// public void Preload() { //初始化所有的枚举事件类型 EventList = new ObservableCollection(); EventList.Add("All"); if (QueryEventList != null) { List evList = QueryEventList(); foreach (string ev in evList) EventList.Add(ev); } SelectedEvent = "All"; //初始化所有的用户 //PreloadUsers(); //所有属性更新 //NotifyOfPropertyChange(); //第一次默认查询 if (SearchedResult == null) Search(); } public void GetFormSearchedResult() { try { if (SearchedResult != null) { SourceLP.Clear(); searchedResult.Clear(); SearchedResult.ToList().ForEach(i => searchedResult.Add(i)); SourceLP.Add("ALL"); sourcelp.Add("ALL"); foreach (var result in SearchedResult) { if (!SourceLP.Contains(result.TargetChamber)) { SourceLP.Add(result.TargetChamber); sourcelp.Add(result.TargetChamber); } } } } catch (Exception ex) { LOG.Write(ex); } } public void SelectDate(string SelectType) { var windowManager = Caliburn.Micro.Core.IoC.Get(); if (SelectType=="Start") { this.SearchBeginTime = Convert.ToDateTime(this.view.wfTimeFrom.Content); SelectDateViewModel selectdateViewModel = new SelectDateViewModel(SearchBeginTime); var result = (windowManager as Caliburn.Micro.WindowManager)?.ShowDialogWithTitle(selectdateViewModel, null, "Start Time"); if (result==true) { this.view.wfTimeFrom.Content = selectdateViewModel.SearchDate; } } else { this.SearchEndTime = Convert.ToDateTime(this.view.wfTimeTo.Content); SelectDateViewModel selectdateViewModel = new SelectDateViewModel(SearchEndTime); var result = (windowManager as Caliburn.Micro.WindowManager)?.ShowDialogWithTitle(selectdateViewModel, null, "End Time"); if (result==true) { this.view.wfTimeTo.Content = selectdateViewModel.SearchDate; } } } /// /// 筛选信息 /// public void Filter() { try { if (SelectedValueLP != ""&& SearchedResult != null&& SearchedResult.Count>0) { SearchedResult.Clear(); searchedResult.ToList().ForEach(i => SearchedResult.Add(i)); string[] lsvp = SelectedValueLP.Split(','); SourceLP.ToList().ForEach(i => { if (!lsvp.Contains(i) && i != "ALL") SearchedResult.ToList().ForEach(m => { if (m.TargetChamber == i) SearchedResult.Remove(m); }); });// sourceLP.Add(i); }); } else SearchedResult.Clear(); if (SearchedResult != null && !string.IsNullOrWhiteSpace(SearchDSKeyWords)) { SearchedResult.ToList().ForEach(m => { if (!m.Detail.Contains(SearchDSKeyWords)) SearchedResult.Remove(m); }); } NotifyOfPropertyChange("SearchedResult"); } catch (Exception ex) { LOG.Write(ex); MessageBox.Show("筛选信息失败", "筛选失败", MessageBoxButton.OK, MessageBoxImage.Warning); } } /// /// 命令 /// /// public void tbLoadPort1Selection(object o) { if (o != null) { var Item = o as ItemSelectionData; if (Item.SelectItem == "ALL") { if (Item.IsSelect) { SourceLP.ToList().ForEach(sp => { if (!sourcelp.Contains(sp)) sourcelp.Add(sp); }); } else { sourcelp.Clear(); } } } } public void SelectValueLP() { var windowManager =Caliburn.Micro.Core.IoC.Get(); SelectFilterConditionViewModel selectFilterViewModel = new SelectFilterConditionViewModel(); (windowManager as Caliburn.Micro.WindowManager)?.ShowDialogWithTitle(selectFilterViewModel, null, "Select Condition"); SelectedValueLP = selectFilterViewModel.FilterConditions; } /// /// 导出 /// public void Export() { try { Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); dlg.DefaultExt = ".xlsx"; // Default file extension dlg.FileName = $"Operation Log_{DateTime.Now:yyyyMMdd_HHmmss}"; dlg.Filter = "Excel数据表格文件(*.xlsx)|*.xlsx"; // Filter files by extension Nullable result = dlg.ShowDialog();// Show open file dialog box if (result == true) // Process open file dialog box results { System.Data.DataSet ds = new System.Data.DataSet(); ds.Tables.Add(new System.Data.DataTable("系统运行日志")); ds.Tables[0].Columns.Add("Type"); ds.Tables[0].Columns.Add("Time"); ds.Tables[0].Columns.Add("System"); ds.Tables[0].Columns.Add("Content"); foreach (var item in SearchedResult) { var row = ds.Tables[0].NewRow(); row[0] = item.LogType; row[1] = item.Time; row[2] = item.TargetChamber; row[3] = item.Detail; ds.Tables[0].Rows.Add(row); } if (!ExcelHelper.ExportToExcel(dlg.FileName, ds, out string reason)) { MessageBox.Show($"Export failed, {reason}", "Export", MessageBoxButton.OK, MessageBoxImage.Warning); return; } MessageBox.Show($"Export succeed, file save as {dlg.FileName}", "Export", MessageBoxButton.OK, MessageBoxImage.Information); } } catch (Exception ex) { LOG.Write(ex); MessageBox.Show("导出系统日志发生错误", "导出失败", MessageBoxButton.OK, MessageBoxImage.Warning); } } private string GetSourceWhere() { //if (!SearchPMA) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorA); //if (!SearchPMB) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorB); //if (!SearchPMC) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorC); //if (!SearchPMD) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.ReactorD); //if (!SearchSystem) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.System); //if (!SearchLL) sqlEvent += string.Format(" and \"Source\"<>'{0}' ", ChamberSet.Loadlock); //if (!SearchTM) sqlEvent += string.Format(" and \"Source\"<>'{0}' and \"Source\"<>'{1}' and \"Source\"<>'{2}' ", // ChamberSet.Loadlock, ChamberSet.Buffer1, ChamberSet.Cooldown); return ""; } /* *gid integer NOT NULL DEFAULT nextval('event_data_gid_seq'::regclass), event_id integer, event_enum text, type text, source text, description text, level text, occur_time timestamp without time zone, CONSTRAINT event_data_pkey PRIMARY KEY (gid) */ /// /// 查询 /// public void Search() { this.SearchBeginTime = Convert.ToDateTime(this.view.wfTimeFrom.Content); this.SearchEndTime = Convert.ToDateTime(this.view.wfTimeTo.Content); Task.Factory.StartNew(() => { try { if (SearchBeginTime > SearchEndTime) { MessageBox.Show("Time range invalid, start time should be early than end time"); return; } string sqlEvent = ""; string sqlOperationLog = ""; string sql = ""; int logCount = 2000; if (SearchAlarmEvent || SearchWarningEvent || SearchInfoEvent) { sqlEvent = string.Format("SELECT \"event_id\", \"role_id\", \"user_name\", \"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")); sqlEvent += GetSourceWhere(); sqlEvent += " and (FALSE "; if (SearchAlarmEvent) sqlEvent += " OR \"level\"='Alarm' "; if (SearchWarningEvent) sqlEvent += " OR \"level\"='Warning' "; if (SearchInfoEvent) sqlEvent += " OR \"level\"='Information' "; sqlEvent += " ) "; if (!string.IsNullOrWhiteSpace(SelectedEvent) && SelectedEvent != "All") sqlEvent += string.Format(" and lower(\"event_enum\")='{0}' ", SelectedEvent.ToLower()); if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlEvent += string.Format(" and lower(\"description\") like '%{0}%' ", SearchKeyWords.ToLower()); } if (SearchOpeLog) { //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")); //if (!SearchPMA) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorA); //if (!SearchPMB) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorB); //if (!SearchPMC) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorC); //if (!SearchPMD) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.ReactorD); //if (!SearchSystem) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.System); //if (!SearchLL) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' ", ChamberSet.Loadlock); //if (!SearchTM) sqlOperationLog += string.Format(" and \"ChamberId\"<>'{0}' and \"ChamberId\"<>'{1}' and \"ChamberId\"<>'{2}' ", // ChamberSet.Loadlock, ChamberSet.Buffer1, ChamberSet.Cooldown); //if (!string.IsNullOrWhiteSpace(SelectedUser) && SelectedUser != "不限") sqlOperationLog += string.Format(" and lower(\"UserName\")='{0}' ", SelectedUser.ToLower()); //if (!string.IsNullOrWhiteSpace(SearchKeyWords)) sqlOperationLog += string.Format(" and lower(\"Content\") like '%{0}%' ", SearchKeyWords.ToLower()); } sql = sqlEvent; if (!string.IsNullOrEmpty(sqlOperationLog)) { if (string.IsNullOrEmpty(sql)) { sql = sqlOperationLog; } else { sql += " UNION ALL " + sqlOperationLog; } } var config = QueryDataClient.Instance.Service.GetConfig("System.LogCount"); if (config != null) { logCount = (int)config; } if (!string.IsNullOrEmpty(sql) && QueryDBEventFunc != null) { sql += $" order by \"occur_time\" desc limit {logCount};"; List lstEvent = QueryDBEventFunc(sql); if (lstEvent == null) return; Application.Current.Dispatcher.BeginInvoke(new Action(() => { SearchedResult = new ObservableCollection(); string logTypeStr; foreach (EventItem ev in lstEvent) { switch (ev.Level) { case EventLevel.Information: logTypeStr = "Info"; break; case EventLevel.Warning: logTypeStr = "Warning"; break; case EventLevel.Alarm: logTypeStr = "Alarm"; break; default: logTypeStr = "Undefine"; break; } SearchedResult.Add(new SystemLogItem() { Time = ((DateTime)ev.OccuringTime).ToString("yyyy/MM/dd HH:mm:ss.fff"), LogType = logTypeStr, Detail = ev.Description, TargetChamber = ev.Source, Initiator = "", Icon = new BitmapImage(new Uri(string.Format("pack://application:,,,/MECF.Framework.Common;component/Resources/SystemLog/{0}.png", ev.Level.ToString()), UriKind.Absolute)) }); } NotifyOfPropertyChange("SearchedResult"); if (SearchedResult.Count >= 2000) { //MessageBox.Show("Only display max 2000 items,reset the query condition", "query too many result", MessageBoxButton.OK, MessageBoxImage.Warning); } GetFormSearchedResult(); })); } else { Application.Current.Dispatcher.BeginInvoke(new Action(() => { SearchedResult = new ObservableCollection(); NotifyOfPropertyChange("SearchedResult"); })); } } catch (Exception ex) { LOG.Write(ex); } }); } public void QueryOptionCmd(string value) { switch (value) { case "Alarm": AlarmCount++; if (AlarmCount % 2 == 0) SearchAlarmEvent = true; else SearchAlarmEvent = false; break; case "Warning": WarningCount++; if (WarningCount % 2 == 0) SearchWarningEvent = true; else SearchWarningEvent = false; break; case "Information": InformationCount++; if (InformationCount % 2 == 0) SearchInfoEvent = true; else SearchInfoEvent = false; break; default: break; } } } }