using AutoamtionService.Shared.Helpers; using AutomationClient.MAUI.Models; using AutomationClient.Shared.Logger; using System.Collections.ObjectModel; namespace AutomationClient.MAUI.ViewModels { public class HistoryViewModel : BaseViewModel { /// /// Logger /// private static AppLogManager _logger = AppLogManager.GetLogger(); #region Fields private DateTime? _fromDate; private DateTime? _toDate; private string _totalFiles; private string _pulledFiles; private string _pushedFiles; private string _failedFiles; private ResultsItem _selectedResultItem; private string _portal; private TypesItem _selectedTypeItem; private string _route; private bool _busyIndicatorIsVisible; private bool _busyIndicatorIsBusy; private bool _showPopupDeleteLogsIsOpen; private HistoryReport _historyReportSelectedItem; private bool _showPopupMovedFailedToDestination; private bool _isEnabledSearchCommand; #endregion public HistoryViewModel() { InitDatetimes(); InitCommands(); HistoryReportSource = new ObservableCollection(); InitBusyIndicator(); InitPopups(); InitTypesItem(); InitResultsItem(); } #region Properties public DateTime? FromDate { get { return _fromDate; } set { if (_fromDate != value) { _fromDate = value; OnPropertyChanged(); } } } public DateTime? ToDate { get { return _toDate; } set { if (_toDate != value) { _toDate = value; OnPropertyChanged(); } } } public string TotalFiles { get { return _totalFiles; } set { if (_totalFiles != value) { _totalFiles = value; OnPropertyChanged(); } } } public string PulledFiles { get { return _pulledFiles; } set { if (_pulledFiles != value) { _pulledFiles = value; OnPropertyChanged(); } } } public string PushedFiles { get { return _pushedFiles; } set { if (_pushedFiles != value) { _pushedFiles = value; OnPropertyChanged(); } } } public string FailedFiles { get { return _failedFiles; } set { if (_failedFiles != value) { _failedFiles = value; OnPropertyChanged(); } } } public ResultsItem SelectedResultItem { get { return _selectedResultItem; } set { if (_selectedResultItem != value) { _selectedResultItem = value; OnPropertyChanged(); } } } public string Portal { get { return _portal; } set { if (_portal != value) { _portal = value; OnPropertyChanged(); } } } public TypesItem SelectedTypeItem { get { return _selectedTypeItem; } set { if (_selectedTypeItem != value) { _selectedTypeItem = value; OnPropertyChanged(); } } } public string Route { get { return _route; } set { if (_route != value) { _route = value; OnPropertyChanged(); } } } public ObservableCollection HistoryReportSource { get; private set; } public bool BusyIndicatorIsVisible { get { return _busyIndicatorIsVisible; } set { if (_busyIndicatorIsVisible != value) { _busyIndicatorIsVisible = value; OnPropertyChanged(); } } } public bool BusyIndicatorIsBusy { get { return _busyIndicatorIsBusy; } set { if (_busyIndicatorIsBusy != value) { _busyIndicatorIsBusy = value; OnPropertyChanged(); } } } public bool ShowPopupDeleteLogsIsOpen { get { return _showPopupDeleteLogsIsOpen; } set { if (_showPopupDeleteLogsIsOpen != value) { _showPopupDeleteLogsIsOpen = value; OnPropertyChanged(); } } } public bool ShowPopupMovedFailedToDestination { get { return _showPopupMovedFailedToDestination; } set { if (_showPopupMovedFailedToDestination != value) { _showPopupMovedFailedToDestination = value; OnPropertyChanged(); } } } public HistoryReport HistoryReportSelectedItem { get { return _historyReportSelectedItem; } set { if (_historyReportSelectedItem != value) { _historyReportSelectedItem = value; OnPropertyChanged(); } } } public bool IsEnabledSearchCommand { get { return _isEnabledSearchCommand; } set { if (_isEnabledSearchCommand != value) { _isEnabledSearchCommand = value; OnPropertyChanged(); } } } public ObservableCollection TypesItems { get; private set; } public ObservableCollection ResultsItems { get; private set; } #endregion #region Commands public Command SearchCommand { get; private set; } public Command DeleteLogsShowPopupCommand { get; private set; } public Command CloseDeleteLogsPopupCommand { get; private set; } public Command DeleteLogsClosePopupCommand { get; private set; } public Command ReEnqueueCommand { get; private set; } public Command CloseMovedFailedToDestination { get; private set; } #endregion #region private Methods private void InitDatetimes() { _fromDate = DateTime.Now.AddDays(-1); _toDate = DateTime.Now; } private void InitCommands() { IsEnabledSearchCommand = true; SearchCommand = new Command( execute: () => { BusyIndicatorIsVisible = true; BusyIndicatorIsBusy = true; CreateHistotyReport(); ShowRecordsSatistics(); BusyIndicatorIsVisible = false; BusyIndicatorIsBusy = false; SearchCommand.ChangeCanExecute(); }, canExecute: () => { if (!(FromDate.HasValue && ToDate.HasValue)) { IsEnabledSearchCommand = false; return false; } if (FromDate.Value <= ToDate.Value) { IsEnabledSearchCommand = true; return true; } else { IsEnabledSearchCommand = false; return IsEnabledSearchCommand; } }); DeleteLogsShowPopupCommand = new Command( execute: () => { ShowPopupDeleteLogsIsOpen = true; }); CloseDeleteLogsPopupCommand = new Command( execute: () => { ShowPopupDeleteLogsIsOpen = false; }); DeleteLogsClosePopupCommand = new Command( execute: () => { ShowPopupDeleteLogsIsOpen = false; MongoDbHelper.DeleteAllLogsRecords(); }); ReEnqueueCommand = new Command( execute: () => { if (HistoryReportSelectedItem == null) { return; } BusyIndicatorIsVisible = true; BusyIndicatorIsBusy = true; var type = HistoryReportSelectedItem.Type; if (type == "Pull") { BusyIndicatorIsVisible = false; BusyIndicatorIsBusy = false; return; } var failedFolder = HistoryReportSelectedItem.FailedFolder; if (string.IsNullOrEmpty(failedFolder)) { return; } var fileOrFolder = HistoryReportSelectedItem.File; if (!Directory.Exists(failedFolder)) { BusyIndicatorIsVisible = false; BusyIndicatorIsBusy = false; return; } else { var dirWithoutDriveAndRootFolderForFileOrFolder = fileOrFolder.ToString().Split(':')[1].Substring(1).Split('\\')[1]; if (Directory.Exists($"{failedFolder}\\{dirWithoutDriveAndRootFolderForFileOrFolder}")) { // The original directory no longer exists after it has been moved. try { Microsoft.VisualBasic.FileIO.FileSystem.MoveDirectory($"{failedFolder}\\{dirWithoutDriveAndRootFolderForFileOrFolder}", fileOrFolder); } catch (Exception) { } } else if (File.Exists($"{failedFolder}\\{dirWithoutDriveAndRootFolderForFileOrFolder}")) { try { var currentDirectory = Path.GetDirectoryName(fileOrFolder); if (!Directory.Exists(currentDirectory)) { Directory.CreateDirectory(currentDirectory); } File.Copy($"{failedFolder}\\{dirWithoutDriveAndRootFolderForFileOrFolder}", fileOrFolder); File.Delete($"{failedFolder}\\{dirWithoutDriveAndRootFolderForFileOrFolder}"); } catch (Exception) { } } else { _logger.Error("File or folder doesn't exists"); } ShowPopupMovedFailedToDestination = true; BusyIndicatorIsVisible = false; BusyIndicatorIsBusy = false; } }); CloseMovedFailedToDestination = new Command( execute: () => { ShowPopupMovedFailedToDestination = false; }); } private void InitBusyIndicator() { BusyIndicatorIsVisible = false; BusyIndicatorIsBusy = false; } private void InitPopups() { ShowPopupDeleteLogsIsOpen = false; ShowPopupMovedFailedToDestination = false; } private void InitTypesItem() { TypesItems = new ObservableCollection() { new TypesItem { Value = "Push" }, new TypesItem { Value = "Pull" }, }; SelectedTypeItem = TypesItems[0]; } private void InitResultsItem() { ResultsItems = new ObservableCollection() { new ResultsItem { Value = "OK" }, new ResultsItem { Value = "Failed" }, }; SelectedResultItem = ResultsItems[0]; } public void CreateHistotyReport() { if (!(FromDate.HasValue && ToDate.HasValue)) { return; } var historyReport = new ObservableCollection(); var logsFiles = MongoDbHelper.GetAllLogsFiles(); foreach (var logFile in logsFiles) { string succeded = logFile.GetValue("Result").AsString; string type = logFile.GetValue("PullOrPush").AsString; string endTime = logFile.GetValue("TimeStamp").AsString; string file = logFile.GetValue("File").AsString; string route = logFile.GetValue("RouteName").AsString; string failedFolder = logFile.GetValue("FailedFolder").AsString; string portal = logFile.GetValue("Portal").AsString; historyReport.Add(new HistoryReport { Succeded = succeded, Type = type == "1" ? "Push" : "Pull", EndTime = endTime, File = file, Route = route, FailedFolder = failedFolder, Portal = portal }); } if (SelectedResultItem != null && !string.IsNullOrEmpty(SelectedResultItem.Value)) { historyReport = new ObservableCollection(historyReport.Where(hr => hr.Succeded == SelectedResultItem.Value)); } if (!string.IsNullOrEmpty(Portal)) { historyReport = new ObservableCollection(historyReport.Where(hr => hr.Portal == Portal)); } if (SelectedTypeItem != null && !string.IsNullOrEmpty(SelectedTypeItem.Value)) { historyReport = new ObservableCollection(historyReport.Where(hr => hr.Type == SelectedTypeItem.Value)); } if (!string.IsNullOrEmpty(Route)) { historyReport = new ObservableCollection(historyReport.Where(hr => hr.Route == Route)); } var finalHistoryReport = new ObservableCollection(historyReport.Where(hr => Convert.ToDateTime(hr.EndTime) >= FromDate.Value && Convert.ToDateTime(hr.EndTime) <= ToDate.Value)); HistoryReportSource.Clear(); foreach (var item in finalHistoryReport) { HistoryReportSource.Add(item); } } private void ShowRecordsSatistics() { var pullRecordsCounter = 0; var pushRecordsCounter = 0; var failedRecordsCounter = 0; foreach (var item in HistoryReportSource) { if (item.Succeded == ConstantsHelper.OK) { if (item.Type == "Push") { pushRecordsCounter++; } else if (item.Type == "Pull") { pullRecordsCounter++; } } else { if (item.FailedFolder != string.Empty) { failedRecordsCounter++; } } } var totalRecordsCounter = pushRecordsCounter + pullRecordsCounter + failedRecordsCounter; TotalFiles = Convert.ToString(totalRecordsCounter); PushedFiles = Convert.ToString(pushRecordsCounter); PulledFiles = Convert.ToString(pullRecordsCounter); FailedFiles = Convert.ToString(failedRecordsCounter); #endregion } } }