/// <summary>
        /// Process data
        /// </summary>
        /// <param name="data">Data to process</param>
        public void Process(TableMonitorData data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }

            // primary keys
            var primaryKeyNames = new List <string>();
            var columns         = new List <string>();

            if (string.IsNullOrWhiteSpace(data.ConnectionStringName))
            {
                data.ConnectionStringName = "default";
            }

            sqlService.OpenConnection((connection) =>
            {
                primaryKeyNames = connection.Query <string>("SELECT cname FROM sys.syscolumns WHERE in_primary_key = 'Y' AND tname = :tableName ORDER BY cname", new { tableName = data.TableName }).ToList();

                if (!string.IsNullOrWhiteSpace(data.PrimaryKeys))
                {
                    primaryKeyNames.AddRange(data.PrimaryKeys.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()));
                }

                // Assert primary columns
                if (!primaryKeyNames.Any())
                {
                    throw new Exception($"The table {data.TableName} has no primary columns.");
                }

                columns = connection.Query <string>("SELECT cname FROM sys.syscolumns WHERE tname = :tableName ORDER BY cname", new { tableName = data.TableName }).ToList();

                foreach (var nonePrimaryKey in primaryKeyNames)
                {
                    Console.WriteLine($"Primary key: {nonePrimaryKey}");
                }

                var statement = $"SELECT string({string.Join(",", WrapInQuotes(primaryKeyNames))}) as primary_key_column, {string.Join(",", WrapInQuotes(columns))} FROM {data.TableName} ORDER BY {string.Join(",", WrapInQuotes(primaryKeyNames))}";
                int counter   = 0;

                try
                {
                    var enumerator = connection.Query(statement);
                    Console.WriteLine($"TableMonitor statement: {statement}");

                    foreach (var dapperRow in enumerator.Select(x => (IDictionary <string, object>)x))
                    {
                        counter++;
                        if (counter % 100 == 0)
                        {
                            Console.WriteLine($"Processed {data.TableName} data: {counter}");
                        }

                        // Get and remove pk
                        var primaryKey = dapperRow["primary_key_column"]?.ToString();
                        dapperRow.Remove("primary_key_column");

                        // Generate hash
                        var hash = GenerateHash(dapperRow);

                        // Find row in existing data
                        var existingData = data.Row.FirstOrDefault(x => x.PrimaryKey == primaryKey);

                        // Data not found
                        if (existingData == null)
                        {
                            // Filter primary keys
                            var rowCopy = new Dictionary <string, object>(dapperRow);
                            var row     = dapperRow.Where(x => primaryKeyNames.Any(y => y.ToLower() == x.Key.ToLower())).ToDictionary(y => y.Key, v => v.Value);

                            existingData = new TableMonitorDataRow
                            {
                                Hash       = hash,
                                Updated    = true,
                                PrimaryKey = primaryKey,

                                // Set row containting only pks
                                Row = row
                            };

                            // Invoke event. All columns must be included here
                            DataAdded?.Invoke(this, new AffectedRowEventArgs {
                                TableName = data.TableName, Row = rowCopy
                            });

                            // Add data
                            data.Row.Add(existingData);
                        }
                        // Data changed
                        else if (hash != existingData.Hash)
                        {
                            // Filter primary keys
                            var rowCopy = new Dictionary <string, object>(dapperRow);

                            existingData.Hash    = hash;
                            existingData.Updated = true;

                            // Invoke event. All columns must be included here
                            DataChanged?.Invoke(this, new AffectedRowEventArgs {
                                TableName = data.TableName, Row = rowCopy
                            });
                        }
                        else
                        {
                            existingData.Updated = true;
                        }
                    }

                    Console.WriteLine($"Processed {data.TableName} data: {counter}");

                    // Create copy of rows
                    foreach (var removedData in data.Row.Where(x => !x.Updated))
                    {
                        counter++;
                        if (counter % 100 == 0)
                        {
                            Console.WriteLine($"Processed {data.TableName} data: {counter}");
                        }

                        // Invoke remove event
                        DataRemoved?.Invoke(this, new AffectedRowEventArgs
                        {
                            TableName = data.TableName,
                            Row       = removedData.Row
                        });
                    }

                    Console.WriteLine($"Processed {data.TableName} data: {counter}");

                    // Just add updated rows
                    data.Row = data.Row.Where(x => x.Updated).ToList();
                }
                catch (Exception ex)
                {
                    var message = $"Could not process data. Query-Statement: {statement}.\r\nFailed row: {counter}.\r\nPrimary keys: {string.Join(",", primaryKeyNames)}.\r\nColumns: {string.Join(",", columns)}";
                    LogManagerInstance.Instance.Error($"Error in TableMonitorService.Process\r\n{message}");

                    throw new Exception(message, ex);
                }
            }, data.ConnectionStringName);
        }
예제 #2
0
 private void OnDataRemoved(DataType data)
 {
     DataRemoved?.Invoke(data);
 }
예제 #3
0
 protected void OnDataRemoved(IProfilingData data) => DataRemoved?.Invoke(data);
예제 #4
0
        private void EndRead(IAsyncResult asyncResult)
        {
            try
            {
                var        parameter = _readByteDelegate.EndInvoke(asyncResult);
                var        size      = Sender.Connection.BinaryReader.ReadInt32();
                var        bytes     = Sender.Connection.BinaryReader.ReadBytes(size);
                Serializer serializer;
                OnlineClientInformation client;
                int clientId;

                PackageInformation packageInformation = null;
                if (PackageReceived != null)
                {
                    packageInformation = new PackageInformation
                    {
                        Size       = bytes.Length + 1,
                        Timestamp  = DateTime.Now,
                        IsReceived = true
                    }
                }
                ;

                switch ((FromClientPackage)parameter)
                {
                case FromClientPackage.ResponseToAdministration:
                case FromClientPackage.ResponseToAdministrationCompressed:
                    var isCompressed = parameter == (byte)FromClientPackage.ResponseToAdministrationCompressed;
                    var data         = isCompressed
                            ? LZF.Decompress(bytes, 1)
                            : bytes;

                    if (packageInformation != null)
                    {
                        packageInformation.Description = (FromClientPackage)parameter + " " +
                                                         CurrentController.DescribePackage(bytes[0], data,
                                                                                           isCompressed ? 0 : 1);
                    }
                    CurrentController?.PackageReceived(bytes[0], data, isCompressed ? 0 : 1);
                    break;

                case FromClientPackage.ResponseLoginOpen:
                    clientId = BitConverter.ToInt32(bytes, 0);
                    client   = _loginsPending.FirstOrDefault(x => x.Id == clientId);
                    if (client == null)
                    {
                        Logger.Error((string)Application.Current.Resources["CouldNotFindClient"]);
                        break;
                    }
                    _loginsPending.Remove(client);
                    Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        CurrentController = new ClientController(client, Sender, this);
                        ((Commander)CurrentController.Commander).ConnectionInfo.PackageSent +=
                            _packageSentEventHandler;
                        LoginOpened?.Invoke(this, EventArgs.Empty);
                    }));
                    break;

                case FromClientPackage.NewClientConnected:
                    serializer =
                        new Serializer(new[] { typeof(ClientInformation), typeof(OnlineClientInformation) });
                    client = serializer.Deserialize <OnlineClientInformation>(bytes);
                    Logger.Info(string.Format((string)Application.Current.Resources["NewClientConnected"],
                                              client.IpAddress, client.Port, client.UserName));

                    lock (_clientListUpdateLock)
                        Application.Current.Dispatcher.Invoke(() => ClientProvider.NewClientConnected(client));
                    NewClientConnected?.Invoke(this, client);
                    break;

                case FromClientPackage.ClientConnected:
                    serializer =
                        new Serializer(new[] { typeof(ClientInformation), typeof(OnlineClientInformation) });
                    client = serializer.Deserialize <OnlineClientInformation>(bytes);
                    Logger.Info(string.Format((string)Application.Current.Resources["NewClientConnected"],
                                              client.IpAddress, client.Port, client.UserName));

                    lock (_clientListUpdateLock)
                        Application.Current.Dispatcher.Invoke(() => ClientProvider.ClientConnected(client));

                    ClientConnected?.Invoke(this, client);
                    break;

                case FromClientPackage.ClientDisconnected:
                    var disconnectedClientId = BitConverter.ToInt32(bytes, 0);
                    if (CurrentController != null && CurrentController.Client.Id == disconnectedClientId)
                    {
                        CurrentController.Dispose();
                        CurrentController = null;
                    }

                    lock (_clientListUpdateLock)
                        Application.Current.Dispatcher.Invoke(
                            () => ClientProvider.ClientDisconnected(disconnectedClientId));
                    ClientDisconnected?.Invoke(this, disconnectedClientId);
                    break;

                case FromClientPackage.ComputerInformationAvailable:
                    var clientWithComputerInformationId = BitConverter.ToInt32(bytes, 0);
                    Application.Current.Dispatcher.BeginInvoke(
                        new Action(
                            () => ClientProvider.ComputerInformationAvailable(clientWithComputerInformationId)));
                    break;

                case FromClientPackage.PasswordsAvailable:
                    var clientWithPasswordsId = BitConverter.ToInt32(bytes, 0);
                    ClientProvider.PasswordsAvailable(clientWithPasswordsId);
                    break;

                case FromClientPackage.GroupChanged:
                    var newGroupNameLength = BitConverter.ToInt32(bytes, 0);
                    var newGroupName       = Encoding.UTF8.GetString(bytes, 4, newGroupNameLength);
                    var clients            = new Serializer(typeof(List <int>)).Deserialize <List <int> >(bytes,
                                                                                                          4 + newGroupNameLength);

                    ClientProvider.ClientGroupChanged(clients, newGroupName);
                    Logger.Receive((string)Application.Current.Resources["GroupChanged"]);
                    break;

                case FromClientPackage.ClientsRemoved:
                    serializer = new Serializer(typeof(List <int>));
                    var removedClientsIds = serializer.Deserialize <List <int> >(bytes);
                    lock (_clientListUpdateLock)
                        Application.Current.Dispatcher.Invoke(
                            () => ClientProvider.ClientRemoved(removedClientsIds));

                    if (removedClientsIds.Count == 1)
                    {
                        Logger.Receive((string)Application.Current.Resources["ClientRemoved"]);
                    }
                    else
                    {
                        Logger.Receive(string.Format((string)Application.Current.Resources["ClientsRemoved"],
                                                     removedClientsIds.Count));
                    }
                    break;

                case FromClientPackage.DynamicCommandsRemoved:
                    DynamicCommandsRemoved?.Invoke(this,
                                                   new Serializer(typeof(List <int>)).Deserialize <List <int> >(bytes));
                    break;

                case FromClientPackage.PluginLoaded:
                    clientId = BitConverter.ToInt32(bytes, 0);
                    var pluginInfo = new Serializer(typeof(PluginInfo)).Deserialize <PluginInfo>(bytes, 4);
                    ClientProvider.ClientPluginAvailable(clientId, pluginInfo);
                    PluginLoaded?.Invoke(this,
                                         new PluginLoadedEventArgs(clientId, pluginInfo.Guid, pluginInfo.Version, true));
                    break;

                case FromClientPackage.PluginLoadFailed:
                    clientId = BitConverter.ToInt32(bytes, 0);
                    PluginLoadingFailed?.Invoke(this,
                                                new PluginLoadedEventArgs(clientId, new Guid(bytes.Skip(4).Take(16).ToArray()),
                                                                          Encoding.ASCII.GetString(bytes.Skip(20).ToArray()), false));
                    break;

                case FromClientPackage.DataTransferProtocolResponse:
                    if (packageInformation != null)
                    {
                        packageInformation.Description = "DataTransferProtocolResponse - " +
                                                         DataTransferProtocolFactory.DescribeReceivedData(bytes, 0);
                    }
                    DataTransferProtocolFactory.Receive(bytes);
                    break;

                case FromClientPackage.ResponseActiveWindow:
                    clientId = BitConverter.ToInt32(bytes, 0);

                    var clientViewModel = ClientProvider.Clients.FirstOrDefault(x => x.Id == clientId);
                    if (clientViewModel != null)
                    {
                        clientViewModel.ActiveWindow = Encoding.UTF8.GetString(bytes, 4, bytes.Length - 4);
                    }
                    break;

                case FromClientPackage.ResponseScreenshot:
                    clientId = BitConverter.ToInt32(bytes, 0);

                    var clientViewModel2 = ClientProvider.Clients.FirstOrDefault(x => x.Id == clientId);
                    if (clientViewModel2 != null)
                    {
                        using (var stream = new MemoryStream(bytes, 4, bytes.Length - 4))
                            using (var image = (Bitmap)Image.FromStream(stream))
                                clientViewModel2.Thumbnail = BitmapConverter.ToBitmapSource(image);
                    }
                    break;

                case FromClientPackage.DataRemoved:
                    DataRemoved?.Invoke(this, new Serializer(typeof(List <int>)).Deserialize <List <int> >(bytes));
                    break;

                case FromClientPackage.PasswordsRemoved:
                    var clientIds = new Serializer(typeof(List <int>)).Deserialize <List <int> >(bytes);
                    foreach (var id in clientIds)
                    {
                        ClientProvider.PasswordsRemoved(id);
                    }

                    PasswordsRemoved?.Invoke(this, clientIds);
                    break;

                case FromClientPackage.DataDownloadPackage:
                    DownloadDataReceived?.Invoke(this, bytes);
                    break;

                case FromClientPackage.StaticCommandPluginReceived:
                    StaticCommandReceived?.Invoke(this, bytes);
                    break;

                case FromClientPackage.StaticCommandPluginTransmissionFailed:
                    StaticCommandTransmissionFailed?.Invoke(this, bytes);
                    break;

                case FromClientPackage.DynamicCommandAdded:
                    DynamicCommandAdded?.Invoke(this,
                                                new Serializer(RegisteredDynamicCommand.RequiredTypes).Deserialize <RegisteredDynamicCommand>(bytes));
                    break;

                case FromClientPackage.DynamicCommandEventsAdded:
                    DynamicCommandEventsAdded?.Invoke(this,
                                                      new Serializer(typeof(List <DynamicCommandEvent>)).Deserialize <List <DynamicCommandEvent> >(
                                                          bytes));
                    break;

                case FromClientPackage.DynamicCommandStatusUpdate:
                    DynamicCommandStatusUpdated?.Invoke(this,
                                                        new DynamicCommandStatusUpdatedEventArgs(BitConverter.ToInt32(bytes, 0),
                                                                                                 (DynamicCommandStatus)bytes[4]));
                    break;

                case FromClientPackage.ResponseLibraryInformation:
                    LibraryInformationReceived?.Invoke(this,
                                                       new LibraryInformationEventArgs(BitConverter.ToInt32(bytes, 0),
                                                                                       (PortableLibrary)BitConverter.ToInt32(bytes, 4)));
                    break;

                case FromClientPackage.ResponseLibraryLoadingResult:
                    LibraryLoadingResultReceived?.Invoke(this,
                                                         new LibraryInformationEventArgs(BitConverter.ToInt32(bytes, 0),
                                                                                         (PortableLibrary)BitConverter.ToInt32(bytes, 4)));
                    break;

                case FromClientPackage.ActiveCommandsChanged:
                    ActiveCommandsChanged?.Invoke(this,
                                                  new Serializer(typeof(ActiveCommandsUpdate)).Deserialize <ActiveCommandsUpdate>(bytes, 0));
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                if (packageInformation != null)
                {
                    if (string.IsNullOrEmpty(packageInformation.Description))
                    {
                        packageInformation.Description = ((FromClientPackage)parameter).ToString();
                    }
                    PackageReceived?.Invoke(this, packageInformation);
                }

                _readByteDelegate.BeginInvoke(EndRead, null);
            }
            catch (Exception ex)
            {
                if (!(ex is IOException) || ex.HResult != -2147024858)
                {
                    LogManager.GetCurrentClassLogger().Warn(ex, "Disconnected from server");
                    if (Application.Current != null)
                    {
                        Logger.Error(string.Format((string)Application.Current.Resources["DisconnectedFromServerException"],
                                                   ex.Message));
                    }
                }
                else if (Application.Current != null)
                {
                    Logger.Warn((string)Application.Current.Resources["DisconnectedFromServer"]);
                }

                else
                {
                    LogManager.GetCurrentClassLogger().Warn("NullReference");
                }

                Dispose();
                Disconnected?.Invoke(this, EventArgs.Empty);
            }
        }
예제 #5
0
 internal static void NotifyDataRemoved(IAbsoluteKeyFrameCollection data) => DataRemoved?.Invoke(null, EventArgsHelper.Create(data));