123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596 |
- 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
- {
- /// <summary>
- /// 时间
- /// </summary>
- public string Time { get; set; }
- /// <summary>
- /// ICON
- /// </summary>
- public object Icon { get; set; }
- /// <summary>
- /// 类型:操作日志|事件|其他
- /// </summary>
- public string LogType { get; set; }
- /// <summary>
- /// 针对腔体
- /// </summary>
- public string TargetChamber { get; set; }
- /// <summary>
- /// 发起方
- /// </summary>
- public string Initiator { get; set; }
- /// <summary>
- /// 详情
- /// </summary>
- 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<string> SourceLP { get; set; }
- public ObservableCollection<string> sourcelp { get; set; }
- //public ObservableCollection<string> sourceLP { get; set; }
- private string _selectedValueLP;
- public string SelectedValueLP
- {
- get
- {
- return _selectedValueLP;
- }
- set
- {
- _selectedValueLP = value;
- NotifyOfPropertyChange("SelectedValueLP");
- }
- }
- public ObservableCollection<string> ToolTipValueLP { get; set; }
- public ObservableCollection<string> SourceDS { get; set; }
- public ObservableCollection<string> 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<string> result = new List<string>();
- 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<string>();
- //sourceLP = new ObservableCollection<string>();
- //SourceLP = new ObservableCollection<string>(new[] { "LP1", "LP2", "LP3","lp4" });
- //SourceDS = new ObservableCollection<string>();
- //sourceDS = new ObservableCollection<string>();
- //ToolTipValueLP = new ObservableCollection<string>();
- tbLoadPort1SelectionChangedCommand = new BaseCommand<object>(tbLoadPort1Selection);
- searchedResult = new ObservableCollection<SystemLogItem>();
- sourcelp = new ObservableCollection<string>();
- //tbLoadPort2SelectionChangedCommand = new BaseCommand<object>(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<string> EventList { get; set; }
- public string SelectedEvent { get; set; }
- public ObservableCollection<string> UserList { get; set; }
- public string SelectedUser { get; set; }
- public ObservableCollection<SystemLogItem> SearchedResult { get; set; }
- public ObservableCollection<SystemLogItem> searchedResult { get; set; }
- public Func<string, List<EventItem>> QueryDBEventFunc { get; set; }
- public Func<List<string>> QueryEventList { get; set; }
- /// <summary>
- /// 预先载入数据
- /// </summary>
- public void Preload()
- {
- //初始化所有的枚举事件类型
- EventList = new ObservableCollection<string>();
- EventList.Add("All");
- if (QueryEventList != null)
- {
- List<string> 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<Caliburn.Micro.IWindowManager>();
- 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;
- }
- }
- }
- /// <summary>
- /// 筛选信息
- /// </summary>
- 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);
- }
- }
- /// <summary>
- /// 命令
- /// </summary>
- /// <param name="o"></param>
- 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<Caliburn.Micro.IWindowManager>();
- SelectFilterConditionViewModel selectFilterViewModel = new SelectFilterConditionViewModel();
- (windowManager as Caliburn.Micro.WindowManager)?.ShowDialogWithTitle(selectFilterViewModel, null, "Select Condition");
- SelectedValueLP = selectFilterViewModel.FilterConditions;
- }
- /// <summary>
- /// 导出
- /// </summary>
- 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<bool> 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)
- */
- /// <summary>
- /// 查询
- /// </summary>
- 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<EventItem> lstEvent = QueryDBEventFunc(sql);
- if (lstEvent == null)
- return;
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- SearchedResult = new ObservableCollection<SystemLogItem>();
- 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<SystemLogItem>();
- 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;
- }
- }
- }
- }
|