123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625 |
- using Aitex.Core.RT.Event;
- using Aitex.Core.RT.Log;
- using Aitex.Core.Utilities;
- using MECF.Framework.Common.DataCenter;
- using MECF.Framework.Common.Utilities;
- using MECF.Framework.UI.Client.ClientBase;
- using OpenSEMI.ClientBase.Command;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Input;
- using System.Windows.Media.Imaging;
- 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 PerPageItem
- {
- public string Value { get; set; }
- }
- public class EventViewModel : BaseModel
- {
- private Dictionary<EventLevel, BitmapImage> _bitmapImageByEventLevel;
- public bool SearchAlarmEvent { get; set; }
- public bool SearchWarningEvent { get; set; }
- public bool SearchInfoEvent { get; set; }
- 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; }
- 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; }
- public string SelectedValueLP { get; set; }
- 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 ICommand NavigateCommand { get; set; }
- public ObservableCollection<PerPageItem> PerPageItems { get; set; }
- public bool IsLoading { get; set; }
- private int _currentPage;
- private int _countPerPage;
- private int _totalPage;
- private int _total;
- private PerPageItem _selectedPerPage;
- public PerPageItem SelectedPerPage
- {
- get
- {
- return _selectedPerPage;
- }
- set
- {
- _selectedPerPage = value;
- Task.Run(() => DisplayResult(false));
- }
- }
- public string PageInfo { get; set; }
- private List<EventItem> _resultEvent;
- public bool EnableFirst { get; set; }
- public bool EnablePrevious { get; set; }
- public bool EnableNext { get; set; }
- public bool EnableLast { 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 = now;// -new TimeSpan(1, 0, 0, 0);
- SearchEndTime = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, 999);
- SelectedUser = "All";
- SearchKeyWords = string.Empty;
- SearchAlarmEvent = false;
- SearchWarningEvent = false;
- SearchInfoEvent = false;
- 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);
- PerPageItems = new ObservableCollection<PerPageItem>();
- //PerPageItems.Add(new PerPageItem() { Value = "All" });
- PerPageItems.Add(new PerPageItem() { Value = "30" });
- //PerPageItems.Add(new PerPageItem() { Value = "300" });
- //PerPageItems.Add(new PerPageItem() { Value = "3000" });
- //PerPageItems.Add(new PerPageItem() { Value = "30000" });
- _selectedPerPage = PerPageItems[0];
- PageInfo = "0/0";
- NavigateCommand = new BaseCommand<string>((o) => Navigate(o), (o) => true);
- _bitmapImageByEventLevel = new Dictionary<EventLevel, BitmapImage>();
- EnumLoop<EventLevel>.ForEach(p=> {
- _bitmapImageByEventLevel[p] = new BitmapImage(new Uri(string.Format("pack://application:,,,/MECF.Framework.UI.Core;component/Resources/SystemLog/{0}.png", p.ToString()), UriKind.Absolute));
- });
- SearchedResult = new ObservableCollection<SystemLogItem>();
- _resultEvent = new List<EventItem>();
- }
- protected override void OnInitialize()
- {
- base.OnInitialize();
- }
- public void Navigate(string args)
- {
- LOG.Info($"DataLog->EventLog->{typeof(EventViewModel)}->Navigate ");
- if (args == "first")
- _currentPage = 1;
- if (args == "previous")
- _currentPage--;
- if (args == "next")
- _currentPage++;
- if (args == "last")
- _currentPage = _totalPage;
- DisplayResult(false);
- }
- protected override void OnViewLoaded(object _view)
- {
- base.OnViewLoaded(_view);
- this.view = (EventView)_view;
- this.view.wfTimeFrom.Value = this.SearchBeginTime;
- this.view.wfTimeTo.Value = this.SearchEndTime;
- //this.Preload();
- }
- protected override void OnDeactivate(bool close)
- {
- base.OnDeactivate(close);
- ClearData();
- }
- 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";
- 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);
- }
- }
- /// <summary>
- /// 筛选信息
- /// </summary>
- public void Filter()
- {
- try
- {
- LOG.Info($"DataLog->EventLog->{typeof(EventViewModel)}->Filter ");
- if (SelectedValueLP != "")
- {
- 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();
- }
- }
- }
- }
- /// <summary>
- /// 导出
- /// </summary>
- public void Export()
- {
- try
- {
- LOG.Info($"DataLog->EventLog->{typeof(EventViewModel)}->Export ");
- 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()
- {
- return "";
- }
- public void Search()
- {
- LOG.Info($"DataLog->EventLog->{typeof(EventViewModel)}->Search ");
- if (IsLoading)
- return;
- this.SearchBeginTime = this.view.wfTimeFrom.Value;
- this.SearchEndTime = this.view.wfTimeTo.Value;
- if (SearchBeginTime > SearchEndTime || (SearchEndTime - SearchBeginTime).Days > 7)
- {
- MessageBox.Show("Time range invalid, start time should be early than end time and max interval 7 day");
- SearchedResult.Clear();
- _total = 0;
- UpdateUI(true);
- return;
- }
- UpdateUI(true);
- SetLoading(true);
-
- Task.Factory.StartNew(() =>
- {
- try
- {
- GetCount();
- UpdateUI(true);
- GetData();
- UpdateData();
- SetLoading(false);
- }
- catch (Exception ex)
- {
- SetLoading(false);
- LOG.Write(ex);
- }
- });
- }
- private void DisplayResult(bool firstTimeDisplay)
- {
- if (IsLoading || _total <= 0)
- return;
- UpdateUI(firstTimeDisplay);
- SetLoading(true);
- Task.Factory.StartNew(() =>
- {
- try
- {
- GetData();
- UpdateData();
- SetLoading(false);
- }
- catch (Exception ex)
- {
- SetLoading(false);
- LOG.Write(ex);
- }
- });
- }
- private void ClearData()
- {
- SearchAlarmEvent = false;
- SearchWarningEvent = false;
- SearchInfoEvent = false;
- SearchOpeLog = false;
- SearchKeyWords = string.Empty;
- SearchDSKeyWords = string.Empty;
- searchedResult?.Clear();
- SourceLP?.Clear();
- SearchedResult?.Clear();
- _total = 0;
- var now = DateTime.Today;
- SearchBeginTime = now;
- SearchEndTime = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59, 999);
- this.view.wfTimeFrom.Value = this.SearchBeginTime;
- this.view.wfTimeTo.Value = this.SearchEndTime;
- NotifyOfPropertyChange(nameof(SearchedResult));
- NotifyOfPropertyChange(nameof(SourceLP));
- NotifyOfPropertyChange(nameof(SearchKeyWords));
- NotifyOfPropertyChange(nameof(SearchDSKeyWords));
- NotifyOfPropertyChange(nameof(SearchAlarmEvent));
- NotifyOfPropertyChange(nameof(SearchWarningEvent));
- NotifyOfPropertyChange(nameof(SearchInfoEvent));
- UpdateUI(true);
- }
- private void UpdateUI(bool firstTimeDisplay)
- {
- if (firstTimeDisplay)
- {
- _currentPage = 1;
- }
- _countPerPage = int.Parse(SelectedPerPage.Value);
- _totalPage = _total / _countPerPage + (_total % _countPerPage > 0 ? 1 : 0);
- if (_currentPage < 1)
- _currentPage = 1;
- if (_currentPage > _totalPage)
- _currentPage = _totalPage;
- EnableFirst = _currentPage > 1;
- EnablePrevious = _currentPage > 1;
- EnableNext = _currentPage < _totalPage;
- EnableLast = _currentPage < _totalPage;
- PageInfo = $"{_currentPage}/{_totalPage}";
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- NotifyOfPropertyChange(nameof(PageInfo));
- NotifyOfPropertyChange(nameof(EnableFirst));
- NotifyOfPropertyChange(nameof(EnableLast));
- NotifyOfPropertyChange(nameof(EnableNext));
- NotifyOfPropertyChange(nameof(EnablePrevious));
- }));
- }
- private void SetLoading(bool isLoading)
- {
- IsLoading = isLoading;
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- NotifyOfPropertyChange(nameof(IsLoading));
- }));
- }
- private void UpdateData()
- {
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- SearchedResult.Clear();
- if (_resultEvent != null && _resultEvent.Count > 0)
- {
- string logTypeStr;
- for (int j = 0; j < _resultEvent.Count; j++)
- {
- EventItem ev = _resultEvent[j];
- 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 = _bitmapImageByEventLevel[ev.Level]
- });
- }
- NotifyOfPropertyChange("SearchedResult");
- }
- GetFormSearchedResult();
- }));
- }
- private void GetData()
- {
- if (SearchAlarmEvent || SearchWarningEvent || SearchInfoEvent)
- {
- _resultEvent.Clear();
- var 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"));
- 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 (_currentPage < 1)
- _currentPage = 1;
- if (_currentPage > _totalPage)
- _currentPage = _totalPage;
- sqlEvent += $" order by \"occur_time\" ASC limit {_countPerPage} offset {(_currentPage - 1) * _countPerPage};";
- _resultEvent.AddRange(QueryDBEventFunc(sqlEvent));
- }
- }
- private void GetCount()
- {
- if (SearchAlarmEvent || SearchWarningEvent || SearchInfoEvent)
- {
- var sqlEvent = string.Format("SELECT COUNT(*) 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());
- _total = QueryDataClient.Instance.Service.GetCount(sqlEvent);
- }
- }
- }
- }
|