Esempio n. 1
0
        /// <inheritdoc/>
        public void Start()
        {
            Task.Run(() =>
            {
                while (true)
                {
                    try
                    {
                        var socket = new Socket(SocketType.Stream, ProtocolType.Tcp);
                        socket.Connect(IPAddress.Parse(_configuration.Ip), _configuration.Port);

                        using (var stream = new NetworkStream(socket, FileAccess.Read, true))
                        {
                            _parser.BeginParse(stream, channel =>
                            {
                                var dataPoint = new TagDataPoint <ChannelValue>
                                {
                                    ControlSystem = "Terasaki",
                                    Tag           = channel.Id.ToString(),
                                    Value         = channel.Value,
                                    Timestamp     = Timestamp.UtcNow
                                };
                                _subscribers.ForEach(subscriber => subscriber(channel));
                            });
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.Error(ex, "Error while connecting to TCP stream");
                    }

                    Thread.Sleep(10000);
                }
            });
        }
Esempio n. 2
0
        public void TestParallelNotEquals()
        {
            var bag = new ConcurrentBag <object>();

            Parallel.ForEach(_confs, new ParallelOptions
            {
                MaxDegreeOfParallelism = 100,
            }, conf =>
            {
                bag.Add(_factory.GetDataConnection(conf));
                Console.WriteLine("Thread.CurrentThread.ManagedThreadId == {0}, conf == {1}", Thread.CurrentThread.ManagedThreadId, conf);
                Thread.Sleep(10); // Без задержки слишком много задач попадает в один поток
            });

            Assert.AreEqual(bag.Count, 100);

            object first;

            Assert.IsTrue(bag.TryTake(out first));

            var correct = 0;

            bag.ForEach(item =>
            {
                if (!object.ReferenceEquals(first, item))
                {
                    correct++;
                }
            });

            Console.WriteLine("correct == {0}", correct);
            Assert.IsTrue(correct >= 33);
        }
Esempio n. 3
0
 void MessageReceived(object sender, MqttApplicationMessageReceivedEventArgs eventArgs)
 {
     _logger.Information($"Received MQTT Message on topic '{eventArgs.ApplicationMessage.Topic}'");
     _parser.Parse(eventArgs.ApplicationMessage.Payload, (tagDataPoint) => {
         _subscribers.ForEach(_ => _(tagDataPoint));
     });
 }
Esempio n. 4
0
        public void TestParallelEquals()
        {
            var bag = new ConcurrentBag <object>();

            Parallel.For(0, 100, new ParallelOptions
            {
                MaxDegreeOfParallelism = 100,
            }, i =>
            {
                bag.Add(_factory.GetDataConnection(C1));
                Console.WriteLine("Thread.CurrentThread.ManagedThreadId == {0}", Thread.CurrentThread.ManagedThreadId);
                Thread.Sleep(10); // Без задержки слишком много задач попадает в один поток
            });

            Assert.AreEqual(bag.Count, 100);

            object first;

            Assert.IsTrue(bag.TryTake(out first));

            bag.ForEach(item =>
            {
                Assert.AreSame(first, item);
            });
        }
Esempio n. 5
0
        private void PrepareExecution(State state)
        {
            _state = state;
            RequestTasks();
            _tasks.ForEach(PrepareSuccessorsAndPredecessors);

            _state.GetOrCreate <List <ITask> >().AddRange(_tasks.Where(t => !_state.Get <List <ITask> >().Contains(t)));

            lock (_lockAddRemove)
                _tasks.ForEach(t =>
                {
                    _remaining.Add(t);
                    t.Completed += TaskOnCompleted;
                });

            OrderTasks(null, Ordinal);
        }
Esempio n. 6
0
        /// <summary>
        /// Creates an object of the specified base type, registering the type if necessary
        /// </summary>
        /// <param name="BaseType">The base type</param>
        /// <returns>Returns an object of the specified base type</returns>
        public virtual object Create(Type BaseType)
        {
            if (!Classes.ContainsKey(BaseType))
            {
                Setup(BaseType);
            }
            object ReturnObject = Classes[BaseType].Assembly.CreateInstance(Classes[BaseType].FullName);

            if (Classes[BaseType] != BaseType)
            {
                Aspects.ForEach(x => x.Setup(ReturnObject));
            }
            return(ReturnObject);
        }
Esempio n. 7
0
        private static string ProcessUrlWithServiceMappers(string url, EntityToken entityToken)
        {
            _serviceMappers.ForEach(service =>
            {
                url = service.ProcessUrl(url, entityToken);
            });

            return(url);
        }
 public void Dispose()
 {
     disposables.ForEach((item) =>
     {
         if (item != null)
         {
             item.Dispose();
             item = null;
         }
     });
 }
Esempio n. 9
0
        public void Dispose()
        {
            if (stopwatch.IsRunning)
            {
                var runtime = TimeSpan.FromMilliseconds(totalStopwatch.ElapsedMilliseconds);
                write($"~ {runtime.ToReadableString(2, true)}", null);
                ClearStopwatch();
            }

            subRegions?.ForEach(k => k.Dispose());
        }
Esempio n. 10
0
        public void ForParallelTest2()
        {
            var Builder = new StringBuilder();

            int[] Temp   = { 0, 0, 1, 2, 3 };
            var   Result = new ConcurrentBag <int>();

            Temp.ForParallel(0, Temp.Length - 1, (x, _) => Result.Add(x));
            Assert.Equal(5, Result.Count);
            Result.ForEach(Builder.Append);
            var OrderedString = new string(Builder.ToString().OrderBy(x => x).ToArray());

            Assert.Equal("00123", OrderedString);
        }
Esempio n. 11
0
        /// <summary>
        /// Изменение текущей культуры
        /// </summary>
        /// <param name="culture">Новая культура</param>
        public static void ChangeCulture(CultureInfo culture)
        {
            ResourceTypes.ForEach(t =>
            {
                t.GetProperty("Culture", BindingFlags.Public | BindingFlags.Static)?.SetValue(null, culture);
            });

            Thread.CurrentThread.CurrentCulture = culture;
            Application.Current?.Dispatcher?.Invoke(() => Thread.CurrentThread.CurrentCulture = culture);

            ResourceNames.ForEach(r =>
            {
                (Application.Current.FindResource(r) as ObjectDataProvider)?.Refresh();
            });
        }
        public void AddDuringEnumerateBag()
        {
            ConcurrentBag <int> bag = new ConcurrentBag <int>();

            bag.Add(1);

            ThreadPool.QueueUserWorkItem(state =>
            {
                Thread.Sleep(500);

                bag.Add(2);
            });

            bag.ForEach(false, i => Thread.Sleep(1000));

            Assert.AreEqual(2, bag.Count);
        }
Esempio n. 13
0
        public void HuntFiles(string directoryPath)
        {
            var directories = Directory.GetDirectories(directoryPath).ToList();

            directories.Add(directoryPath);
            directories.ForEach(dir =>
            {
                string[] files = Directory.GetFiles(dir, "*", SearchOption.AllDirectories);
                var batches    = files.Batch(50).ToList();
                batches.ForEach(filesBatch =>
                {
                    _files = new ConcurrentBag <FileData>(_db.GetFiles().ToList());
                    List <Task <FileInfo> > fileTasks = filesBatch.Select(f => new Task <FileInfo>(() => GetFileInfo(f))).ToList();
                    try
                    {
                        fileTasks.AsParallel().ForAll(t => t.Start());
                        Task.WaitAll(fileTasks.ToArray());
                        ConcurrentBag <FileData> parsedFile = new ConcurrentBag <FileData>();
                        List <Task> ParseTasks = fileTasks.Where(ft => !ft.IsFaulted).Select(ft => new Task(() => parsedFile.Add(new FileData(ft.Result)))).ToList();
                        ParseTasks.AsParallel().ForAll(t => t.Start());
                        Task.WaitAll(ParseTasks.ToArray());
                        parsedFile.ForEach(x =>
                        {
                            if (_files.Any(f => f.CurrentLocation == x.CurrentLocation))
                            {
                                _db.UpdateFile(x);
                            }
                            else
                            {
                                _db.InsertFile(x);
                            }
                        });
                    }
                    catch (AggregateException ae)
                    {
                        StringBuilder exceptionSB = new StringBuilder();
                        foreach (var ex in ae.Flatten().InnerExceptions)
                        {
                            exceptionSB.AppendLine(ex.ToString());
                        }
                    }
                });
            });
        }
Esempio n. 14
0
        /// <summary>
        ///   Publishes a message on the current thread asynchronous.
        /// </summary>
        /// <param name = "message">The message instance.</param>
        /// <param name="token">The <see cref="CancellationToken"/>.</param>
        public async Task PublishAsync(object message, CancellationToken token)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            Handler[] toNotify;
            using (await GetLockAsync(token))
            {
                toNotify = handlers.ToArray();
            }
            if (token.IsCancellationRequested)
            {
                return;
            }
            var messageType = message.GetType();
            var dead        = new ConcurrentBag <Handler>();
            await toNotify.ForEachAsync(async handler =>
            {
                if (!await handler.HandleAsync(messageType, message))
                {
                    if (token.IsCancellationRequested)
                    {
                        return;
                    }
                    dead.Add(handler);
                }
            },
                                        token);

            if (!dead.Any())
            {
                return;
            }
            using (await GetLockAsync(token))
            {
                dead.ForEach(x => handlers.Remove(x));
            }
        }
Esempio n. 15
0
        public void Test_Solution_Trees_vs_Examples()
        {
            ProgressIndicator pi = new ProgressIndicator(System.Reflection.MethodBase.GetCurrentMethod().Name);

            FileInfo[] files1 = new DirectoryInfo(Directories.Examples).GetFiles(FileExtensions.XmlZipMask);
            FileInfo[] files2 = new DirectoryInfo(Directories.SolutionTrees).GetFiles(FileExtensions.XmlZipMask);

            var not_all = (from file1 in files1
                           where files2.Count(fi => Directories.ConvertName(fi.FullName) == file1.Name) < 4
                           select file1).ToArray();

            var too_much = (from file2 in files2
                            where files1.All(fi => fi.Name != Directories.ConvertName(file2.Name))
                            select file2).ToArray();

            if (not_all.Count() > 0)
            {
                Assert.Fail("Is in 'Examples', but not in 'SolutionTrees' (run project 'SudokuGenerator'): " +
                            not_all.First().FullName);
            }

            if (too_much.Count() > 0)
            {
                Assert.Fail("Is in 'SolutionTrees', but not in 'Examples': " + too_much.First().FullName);
            }

            string dir3 = Directories.SolutionTrees + Path.DirectorySeparatorChar + "Diffrencies";

            new DirectoryInfo(dir3).CreateOrEmpty();

            string dir4 = Directories.SolutionTrees + Path.DirectorySeparatorChar + "Diffrencies" +
                          Path.DirectorySeparatorChar + "Added";

            new DirectoryInfo(dir4).CreateOrEmpty();

            string dir5 = Directories.SolutionTrees + Path.DirectorySeparatorChar + "Diffrencies" +
                          Path.DirectorySeparatorChar + "Removed";

            new DirectoryInfo(dir5).CreateOrEmpty();

            bool b = false;

            ConcurrentBag <string> list = new ConcurrentBag <string>();

            ConcurrentCounter counter = new ConcurrentCounter();

            Parallel.ForEach(files2, (file2, state) =>
            {
                counter.Increment();
                pi.AddLine((counter.Value * 100 / files2.Length).ToString());

                SudokuSolutionNode node2 = SudokuSolutionNode.LoadFromFile(file2.FullName);

                SudokuBoard board = SudokuBoard.LoadFromFile(files1.First(f => f.Name ==
                                                                          Directories.ConvertName(file2.FullName)).FullName);

                board = board.Rotate(ExtractRotateNumber(file2.Name));

                SudokuSolutionNode node1 = SudokuSolutionNode.CreateRoot(board);
                node1.SolveWithStepAll();

                Assert.AreEqual(node1.State, node2.State, file2.FullName);
                Assert.AreEqual(node1.Board, node2.Board, file2.FullName);
                Assert.AreEqual(node1.Solution, node2.Solution, file2.FullName);

                b |= CompareNodes(node1, node2, file2, list, 1, 1);
            });

            if (list.Count != 0)
            {
                string filename = dir3 + Path.DirectorySeparatorChar + "!Diffrencies.txt";

                using (FileStream fs = new FileStream(filename, FileMode.Create))
                {
                    StreamWriter sw = new StreamWriter(fs);
                    list.ForEach(s => sw.WriteLine(s));
                    sw.Flush();
                }

                TestContext.AddResultFile(filename);
            }

            Assert.IsFalse(b);
        }
Esempio n. 16
0
 public InMemoryEventStore <T> Setup(params SubscriptionEntity[] subscriptions)
 {
     subscriptions.ForEach(s => this.subscriptions.Add(s));
     return(this);
 }
        public async Task DnBImportAsync(ILogger log, long currentUserId)
        {
            if (RunningJobs.Contains(nameof(CompaniesHouseCheck)) ||
                RunningJobs.Contains(nameof(DnBImportAsync)))
            {
                return;
            }

            RunningJobs.Add(nameof(DnBImportAsync));
            string userEmail    = null;
            string error        = null;
            var    startTime    = VirtualDateTime.Now;
            var    totalChanges = 0;
            var    totalInserts = 0;

            try
            {
                #region Load and Prechecks

                //Load the D&B records
                var dnbOrgsPaths =
                    await _SharedBusinessLogic.FileRepository.GetFilesAsync(_SharedBusinessLogic.SharedOptions.DataPath,
                                                                            Filenames.DnBOrganisations());

                var dnbOrgsPath = dnbOrgsPaths.OrderByDescending(f => f).FirstOrDefault();
                if (string.IsNullOrEmpty(dnbOrgsPath))
                {
                    return;
                }

                if (!await _SharedBusinessLogic.FileRepository.GetFileExistsAsync(dnbOrgsPath))
                {
                    throw new Exception("Could not find " + dnbOrgsPath);
                }

                var AllDnBOrgs = await _SharedBusinessLogic.FileRepository.ReadCSVAsync <DnBOrgsModel>(dnbOrgsPath);

                if (!AllDnBOrgs.Any())
                {
                    log.LogWarning($"No records found in '{dnbOrgsPath}'");
                    return;
                }

                AllDnBOrgs = AllDnBOrgs.OrderBy(o => o.OrganisationName).ToList();

                //Check for duplicate DUNS
                var count = AllDnBOrgs.Count() - AllDnBOrgs.Select(o => o.DUNSNumber).Distinct().Count();
                if (count > 0)
                {
                    throw new Exception($"There are {count} duplicate DUNS numbers detected");
                }

                //Check for no addresses
                count = AllDnBOrgs.Count(o => !o.IsValidAddress());
                if (count > 0)
                {
                    throw new Exception(
                              $"There are {count} organisations with no address detected (i.e., no AddressLine1, AddressLine2, PostalCode, and PoBox).");
                }

                //Check for no organisation name
                count = AllDnBOrgs.Count(o => string.IsNullOrWhiteSpace(o.OrganisationName));
                if (count > 0)
                {
                    throw new Exception($"There are {count} organisations with no OrganisationName detected.");
                }

                //Check for duplicate employer references
                var allEmployerReferenceCount = AllDnBOrgs.Count(o => !string.IsNullOrWhiteSpace(o.EmployerReference));
                var employerReferences        = new SortedSet <string>(
                    AllDnBOrgs.Where(o => !string.IsNullOrWhiteSpace(o.EmployerReference))
                    .Select(o => o.EmployerReference).Distinct());
                count = allEmployerReferenceCount - employerReferences.Count;
                if (count > 0)
                {
                    throw new Exception($"There are {count} duplicate EmployerReferences detected");
                }

                //Check companies have been updated
                count = AllDnBOrgs.Count(
                    o => !string.IsNullOrWhiteSpace(o.CompanyNumber) &&
                    o.DateOfCessation == null &&
                    (o.StatusCheckedDate == null ||
                     o.StatusCheckedDate.Value.AddMonths(1) < VirtualDateTime.Now));
                if (count > 0)
                {
                    throw new Exception(
                              $"There are {count} active companies who have not been checked with companies house within the last month");
                }

                //Fix Company Number
                Parallel.ForEach(
                    AllDnBOrgs.Where(o => !string.IsNullOrWhiteSpace(o.CompanyNumber)),
                    dnbOrg =>
                {
                    if (dnbOrg.CompanyNumber.IsNumber())
                    {
                        dnbOrg.CompanyNumber = dnbOrg.CompanyNumber.PadLeft(8, '0');
                    }
                });

                //Check for duplicate company numbers
                var companyNumbers =
                    AllDnBOrgs.Where(o => !string.IsNullOrWhiteSpace(o.CompanyNumber)).Select(o => o.CompanyNumber);
                count = companyNumbers.Count() - companyNumbers.Distinct().Count();
                if (count > 0)
                {
                    throw new Exception($"There are {count} duplicate CompanyNumbers detected");
                }

                //Get the current users email address
                var user = await _SharedBusinessLogic.DataRepository.GetAll <User>()
                           .FirstOrDefaultAsync(u => u.UserId == currentUserId);

                userEmail = user?.EmailAddress;

                //Count records requiring import
                count = AllDnBOrgs.Count(
                    o => !o.GetIsDissolved() &&
                    (o.ImportedDate == null || string.IsNullOrWhiteSpace(o.CompanyNumber) ||
                     o.ImportedDate < o.StatusCheckedDate));
                if (count == 0)
                {
                    return;
                }

                var dbOrgs = _SharedBusinessLogic.DataRepository.GetAll <Organisation>().ToList();

                #endregion

                //Set all existing org employer references
                await ReferenceEmployersAsync();

                #region Set all existing org DUNS

                var dnbOrgs = AllDnBOrgs
                              .Where(o => o.OrganisationId > 0 && string.IsNullOrWhiteSpace(o.EmployerReference))
                              .ToList();
                if (dnbOrgs.Count > 0)
                {
                    foreach (var dnbOrg in dnbOrgs)
                    {
                        var org = dbOrgs.FirstOrDefault(o => o.OrganisationId == dnbOrg.OrganisationId);
                        if (org == null)
                        {
                            if (!_SharedBusinessLogic.SharedOptions.IsProduction())
                            {
                                continue;
                            }

                            throw new Exception($"OrganisationId:{dnbOrg.OrganisationId} does not exist in database");
                        }

                        if (!string.IsNullOrWhiteSpace(org.DUNSNumber))
                        {
                            continue;
                        }

                        org.DUNSNumber        = dnbOrg.DUNSNumber;
                        dnbOrg.OrganisationId = null;
                    }

                    await _SharedBusinessLogic.DataRepository.SaveChangesAsync();

                    dbOrgs = await _SharedBusinessLogic.DataRepository.GetAll <Organisation>().ToListAsync();

                    await _SharedBusinessLogic.FileRepository.SaveCSVAsync(AllDnBOrgs, dnbOrgsPath);

                    AllDnBOrgs = await _SharedBusinessLogic.FileRepository.ReadCSVAsync <DnBOrgsModel>(dnbOrgsPath);

                    AllDnBOrgs = AllDnBOrgs.OrderBy(o => o.OrganisationName).ToList();
                }

                #endregion

                var allSicCodes = await _SharedBusinessLogic.DataRepository.GetAll <SicCode>().ToListAsync();

                dnbOrgs = AllDnBOrgs.Where(o => o.ImportedDate == null || o.ImportedDate < o.StatusCheckedDate)
                          .ToList();
                while (dnbOrgs.Count > 0)
                {
                    var allBadSicCodes = new ConcurrentBag <OrganisationSicCode>();

                    var c          = 0;
                    var dbChanges  = 0;
                    var dnbChanges = 0;

                    foreach (var dnbOrg in dnbOrgs)
                    {
                        //Only do 100 records at a time
                        if (c > 100)
                        {
                            break;
                        }

                        var dbChanged = false;
                        var dbOrg     = dbOrgs.FirstOrDefault(o => o.DUNSNumber == dnbOrg.DUNSNumber);

                        var dataSource = string.IsNullOrWhiteSpace(dnbOrg.NameSource) ? "D&B" : dnbOrg.NameSource;
                        var orgName    = new OrganisationName
                        {
                            Name = dnbOrg.OrganisationName.Left(100), Source = dataSource
                        };

                        if (dbOrg == null)
                        {
                            dbOrg = string.IsNullOrWhiteSpace(dnbOrg.CompanyNumber)
                                ? null
                                : dbOrgs.FirstOrDefault(o => o.CompanyNumber == dnbOrg.CompanyNumber);
                            if (dbOrg != null)
                            {
                                dbOrg.DUNSNumber = dnbOrg.DUNSNumber;
                            }
                            else
                            {
                                dbOrg = new Organisation
                                {
                                    DUNSNumber        = dnbOrg.DUNSNumber,
                                    EmployerReference = dnbOrg.EmployerReference,
                                    OrganisationName  = orgName.Name,
                                    CompanyNumber     = string.IsNullOrWhiteSpace(dnbOrg.CompanyNumber)
                                        ? null
                                        : dnbOrg.CompanyNumber,
                                    SectorType      = dnbOrg.SectorType,
                                    DateOfCessation = dnbOrg.DateOfCessation
                                };
                                dbOrg.OrganisationNames.Add(orgName);

                                //Create a presumed in-scope for current year
                                var newScope = new OrganisationScope
                                {
                                    Organisation    = dbOrg,
                                    ScopeStatus     = ScopeStatuses.PresumedInScope,
                                    ScopeStatusDate = VirtualDateTime.Now,
                                    Status          = ScopeRowStatuses.Active,
                                    SnapshotDate    = _snapshotDateHelper.GetSnapshotDate(dbOrg.SectorType)
                                };
                                _SharedBusinessLogic.DataRepository.Insert(newScope);
                                dbOrg.OrganisationScopes.Add(newScope);

                                //Create a presumed out-of-scope for previous year
                                var oldScope = new OrganisationScope
                                {
                                    Organisation    = dbOrg,
                                    ScopeStatus     = ScopeStatuses.PresumedOutOfScope,
                                    ScopeStatusDate = VirtualDateTime.Now,
                                    Status          = ScopeRowStatuses.Active,
                                    SnapshotDate    = newScope.SnapshotDate.AddYears(-1)
                                };
                                _SharedBusinessLogic.DataRepository.Insert(oldScope);
                                dbOrg.OrganisationScopes.Add(oldScope);

                                dbOrg.SetStatus(OrganisationStatuses.Active, currentUserId, "Imported from D&B");
                            }
                        }
                        //Skip dissolved companies
                        else if (_OrganisationBusinessLogic.GetOrganisationWasDissolvedBeforeCurrentAccountingYear(dbOrg))
                        {
                            dnbOrg.ImportedDate = VirtualDateTime.Now;
                            dnbChanges++;
                            continue;
                        }
                        else if (dbOrg.OrganisationName != orgName.Name)
                        {
                            var oldOrgName = dbOrg.GetLatestName();
                            if (oldOrgName == null ||
                                _SharedBusinessLogic.SourceComparer.CanReplace(orgName.Source, oldOrgName.Source))
                            {
                                dbOrg.OrganisationName = orgName.Name;
                                dbOrg.OrganisationNames.Add(orgName);
                                dbChanged = true;
                            }
                        }

                        //Ensure D&B gas an organisationID
                        if (dnbOrg.OrganisationId == null || dnbOrg.OrganisationId.Value == 0)
                        {
                            dnbOrg.OrganisationId = dbOrg.OrganisationId;
                            dnbChanges++;
                        }

                        //Add the cessation date
                        if (dbOrg.DateOfCessation == null && dbOrg.DateOfCessation != dnbOrg.DateOfCessation)
                        {
                            dbOrg.DateOfCessation = dnbOrg.DateOfCessation;
                            dbChanged             = true;
                        }

                        //Set the employer reference
                        if (string.IsNullOrWhiteSpace(dbOrg.EmployerReference))
                        {
                            string employerReference;
                            do
                            {
                                employerReference = _OrganisationBusinessLogic.GenerateEmployerReference();
                            } while (employerReferences.Contains(employerReference));

                            dbOrg.EmployerReference = employerReference;
                            employerReferences.Add(employerReference);
                            dbChanged = true;
                        }

                        if (dnbOrg.EmployerReference != dbOrg.EmployerReference)
                        {
                            dnbOrg.EmployerReference = dbOrg.EmployerReference;
                            dnbChanges++;
                        }

                        //Add the new address
                        var fullAddress = dnbOrg.GetAddress();
                        var newAddress  = dbOrg.LatestAddress;

                        //add the address if there isnt one
                        dataSource = string.IsNullOrWhiteSpace(dnbOrg.AddressSource) ? "D&B" : dnbOrg.AddressSource;
                        if (newAddress == null ||
                            !newAddress.GetAddressString().EqualsI(fullAddress) &&
                            _SharedBusinessLogic.SourceComparer.CanReplace(dataSource, newAddress.Source))
                        {
                            var statusDate = VirtualDateTime.Now;

                            newAddress = new OrganisationAddress();
                            newAddress.Organisation    = dbOrg;
                            newAddress.CreatedByUserId = currentUserId;
                            newAddress.Address1        = dnbOrg.AddressLine1;
                            newAddress.Address2        = dnbOrg.AddressLine2;
                            newAddress.Address3        = dnbOrg.AddressLine3;
                            newAddress.County          = dnbOrg.County;
                            newAddress.Country         = dnbOrg.Country;
                            newAddress.TownCity        = dnbOrg.City;
                            newAddress.PostCode        = dnbOrg.PostalCode;
                            newAddress.PoBox           = dnbOrg.PoBox;
                            newAddress.Source          = dataSource;
                            newAddress.SetStatus(AddressStatuses.Active, currentUserId, "Imported from D&B");
                            if (dbOrg.LatestAddress != null)
                            {
                                dbOrg.LatestAddress.SetStatus(AddressStatuses.Retired, currentUserId,
                                                              $"Replaced by {newAddress.Source}");
                            }
                        }

                        //Update the sic codes
                        var newCodeIds   = dnbOrg.GetSicCodesIds();
                        var newCodesList = dnbOrg.GetSicCodesIds().ToList();
                        for (var i = 0; i < newCodesList.Count; i++)
                        {
                            var code = newCodesList[i];
                            if (code <= 0)
                            {
                                continue;
                            }

                            var sicCode = allSicCodes.FirstOrDefault(sic => sic.SicCodeId == code);
                            if (sicCode != null)
                            {
                                continue;
                            }

                            sicCode = allSicCodes.FirstOrDefault(
                                sic => sic.SicCodeId == code * 10 && sic.Description.EqualsI(dnbOrg.SicDescription));
                            if (sicCode != null)
                            {
                                newCodesList[i] = sicCode.SicCodeId;
                            }
                        }

                        newCodeIds = new SortedSet <int>(newCodesList);

                        var newCodes     = new List <OrganisationSicCode>();
                        var oldCodes     = dbOrg.GetLatestSicCodes().ToList();
                        var oldSicSource = dbOrg.GetLatestSicSource();
                        var oldCodeIds   = oldCodes.Select(s => s.SicCodeId);
                        if (dbOrg.SectorType == SectorTypes.Public)
                        {
                            newCodeIds.Add(1);
                        }

                        if (!_SharedBusinessLogic.SharedOptions.IsProduction())
                        {
                            Debug.WriteLine(
                                $"OLD:{oldCodes.Select(s => s.SicCodeId).ToDelimitedString()} NEW:{newCodeIds.ToDelimitedString()}");
                        }

                        dataSource = string.IsNullOrWhiteSpace(dnbOrg.SicSource) ? "D&B" : dnbOrg.SicSource;
                        if (!newCodeIds.SetEquals(oldCodeIds) &&
                            _SharedBusinessLogic.SourceComparer.CanReplace(dataSource, oldSicSource))
                        {
                            foreach (var code in newCodeIds)
                            {
                                if (code <= 0)
                                {
                                    continue;
                                }

                                var sicCode = allSicCodes.FirstOrDefault(sic => sic.SicCodeId == code);
                                var newSic  = new OrganisationSicCode
                                {
                                    Organisation = dbOrg, SicCodeId = code, Source = dataSource
                                };
                                if (sicCode == null)
                                {
                                    allBadSicCodes.Add(newSic);
                                    continue;
                                }

                                newCodes.Add(newSic);
                            }

                            if (newCodes.Any())
                            {
                                //Add new codes only
                                foreach (var newSic in newCodes)
                                {
                                    dbOrg.OrganisationSicCodes.Add(newSic);
                                    dbChanged = true;
                                }

                                //Retire the old codes
                                foreach (var oldSic in oldCodes)
                                {
                                    oldSic.Retired = VirtualDateTime.Now;
                                    dbChanged      = true;
                                }
                            }
                        }

                        await _SharedBusinessLogic.DataRepository.BeginTransactionAsync(
                            async() =>
                        {
                            try
                            {
                                //Save the name, Sic, EmployerReference, DateOfCessasion changes
                                if (dbChanged)
                                {
                                    await _SharedBusinessLogic.DataRepository.SaveChangesAsync();
                                }

                                //Save the changes
                                dnbOrg.ImportedDate = VirtualDateTime.Now;
                                dnbChanges++;
                                var insert = false;
                                if (dbOrg.OrganisationId == 0)
                                {
                                    _SharedBusinessLogic.DataRepository.Insert(dbOrg);
                                    await _SharedBusinessLogic.DataRepository.SaveChangesAsync();
                                    dbChanged = true;
                                    insert    = true;
                                }

                                if (newAddress != null && newAddress.AddressId == 0)
                                {
                                    dbOrg.OrganisationAddresses.Add(newAddress);
                                    dbOrg.LatestAddress = newAddress;
                                    await _SharedBusinessLogic.DataRepository.SaveChangesAsync();
                                    dbChanged = true;
                                }

                                if (dbChanged)
                                {
                                    dbChanges++;
                                    _SharedBusinessLogic.DataRepository.CommitTransaction();
                                    totalChanges++;
                                    if (insert)
                                    {
                                        totalInserts++;
                                    }

                                    //Add or remove this organisation to/from the search index
                                    await SearchBusinessLogic.UpdateSearchIndexAsync(dbOrg);
                                }
                            }
                            catch
                            {
                                _SharedBusinessLogic.DataRepository.RollbackTransaction();
                            }
                        });

                        c++;
                    }

                    //Reload all the changes
                    if (dbChanges > 0)
                    {
                        dbOrgs = await _SharedBusinessLogic.DataRepository.GetAll <Organisation>().ToListAsync();
                    }

                    //Save the D&B records
                    if (dnbChanges > 0)
                    {
                        await _SharedBusinessLogic.FileRepository.SaveCSVAsync(AllDnBOrgs, dnbOrgsPath);

                        AllDnBOrgs = await _SharedBusinessLogic.FileRepository.ReadCSVAsync <DnBOrgsModel>(dnbOrgsPath);

                        AllDnBOrgs = AllDnBOrgs.OrderBy(o => o.OrganisationName).ToList();
                        dnbOrgs    = AllDnBOrgs.Where(o => o.ImportedDate == null || o.ImportedDate < o.StatusCheckedDate)
                                     .ToList();
                    }

                    //Save the bad sic codes
                    if (allBadSicCodes.Count > 0)
                    {
                        //Create the logging tasks
                        var badSicLoggingtasks = new List <Task>();
                        allBadSicCodes.ForEach(
                            bsc => badSicLoggingtasks.Add(
                                _BadSicLog.WriteAsync(
                                    new BadSicLogModel
                        {
                            OrganisationId   = bsc.Organisation.OrganisationId,
                            OrganisationName = bsc.Organisation.OrganisationName,
                            SicCode          = bsc.SicCodeId,
                            Source           = bsc.Source
                        })));

                        //Wait for all the logging tasks to complete
                        await Task.WhenAll(badSicLoggingtasks);
                    }
                }
            }
            catch (Exception ex)
            {
                error = ex.Message;
                throw;
            }
            finally
            {
                if (!string.IsNullOrWhiteSpace(userEmail))
                {
                    var endTime  = VirtualDateTime.Now;
                    var duration = endTime - startTime;
                    try
                    {
                        if (!string.IsNullOrWhiteSpace(error))
                        {
                            await _Messenger.SendMessageAsync(
                                "D&B Import Failed",
                                userEmail,
                                $"The D&B import failed at {endTime} after {duration.ToFriendly()}.\nChanged {totalChanges} organisations including {totalInserts} new.\n\nERROR:{error}");
                        }
                        else if (totalChanges == 0)
                        {
                            await _Messenger.SendMessageAsync(
                                "D&B Import Complete",
                                userEmail,
                                "The D&B import process completed successfully with no records requiring import.");
                        }
                        else
                        {
                            await _Messenger.SendMessageAsync(
                                "D&B Import Complete",
                                userEmail,
                                $"The D&B import process completed successfully at {endTime} after {duration.ToFriendly()}.\nChanged {totalChanges} organisations including {totalInserts} new.");
                        }
                    }
                    catch (Exception ex)
                    {
                        log.LogError(ex, ex.Message);
                    }
                }

                RunningJobs.Remove(nameof(DnBImportAsync));
            }
        }
Esempio n. 18
0
        public void ProcessNotes(IEnumerable <NoteObject> notes, CancellationTokenSource cancellationTokenSource)
        {
            try
            {
                if (!licenseService.IsRegistered || (cancellationTokenSource != null && cancellationTokenSource.IsCancellationRequested))
                {
                    return;
                }

                var currentPlayerIds = new HashSet <int>(storageModel.PlayerSelectedItem.PlayerIds);

                var noteService = ServiceLocator.Current.GetInstance <IPlayerNotesService>() as IPlayerXRayNoteService;

                var sinceDate = noteService.CurrentNotesAppSettings.IsNoteCreationSinceDate ?
                                noteService.CurrentNotesAppSettings.NoteCreationSinceDate : (DateTime?)null;

                var isAdvancedLogEnabled = noteService.CurrentNotesAppSettings.IsAdvancedLogEnabled;

                var takesNotesOnHero = noteService.CurrentNotesAppSettings.TakesNotesOnHero;

                using (var session = ModelEntities.OpenStatelessSession())
                {
                    BuildPlayersDictonary(session);
                    BuildPlayersNotesDictonary(session);

                    var entitiesCount = sinceDate != null?
                                        session.Query <Handhistory>().Count(x => x.Handtimestamp >= sinceDate.Value) :
                                            session.Query <Handhistory>().Count();

                    var progressCounter = 0;
                    var progress        = 0;

                    var numOfQueries = (int)Math.Ceiling((double)entitiesCount / handHistoryRowsPerQuery);

                    LogProvider.Log.Info(CustomModulesNames.PlayerXRay, $"Processing notes [{notes.Count()}] for {entitiesCount} hands.");

                    for (var i = 0; i < numOfQueries; i++)
                    {
                        if (cancellationTokenSource != null && cancellationTokenSource.IsCancellationRequested)
                        {
                            LogProvider.Log.Info(CustomModulesNames.PlayerXRay, $"The note processing has been canceled [{i}/{numOfQueries}].");
                            break;
                        }

                        using (var pf = new PerformanceMonitor($"Proccesing notes [{i}/{numOfQueries} iteration].", isAdvancedLogEnabled, CustomModulesNames.PlayerXRay))
                        {
                            var numOfRowToStartQuery = i * handHistoryRowsPerQuery;

                            var handHistories = sinceDate != null?
                                                session.Query <Handhistory>()
                                                .Where(x => x.Handtimestamp >= sinceDate.Value)
                                                .Skip(numOfRowToStartQuery)
                                                .Take(handHistoryRowsPerQuery)
                                                .ToArray() :
                                                    session.Query <Handhistory>()
                                                    .Skip(numOfRowToStartQuery)
                                                    .Take(handHistoryRowsPerQuery)
                                                    .ToArray();

                            var notesToInsert = new ConcurrentBag <Playernotes>();

                            Parallel.ForEach(handHistories, handHistory =>
                            {
                                try
                                {
                                    var parsingResult = ParseHandHistory(handHistory);

                                    if (parsingResult != null)
                                    {
                                        var matchInfo = new GameMatchInfo
                                        {
                                            GameType  = parsingResult.Source.GameDescription.GameType,
                                            CashBuyIn = !parsingResult.Source.GameDescription.IsTournament ?
                                                        Utils.ConvertToCents(parsingResult.Source.GameDescription.Limit.BigBlind) : 0,
                                            TournamentBuyIn = parsingResult.Source.GameDescription.IsTournament ?
                                                              parsingResult.Source.GameDescription.Tournament.BuyIn.PrizePoolValue : 0
                                        };

                                        if (!Limit.IsMatch(matchInfo))
                                        {
                                            return;
                                        }

                                        var playerStatisticCreationInfo = new PlayerStatisticCreationInfo
                                        {
                                            ParsingResult = parsingResult
                                        };

                                        foreach (var player in parsingResult.Players)
                                        {
                                            // skip notes for current player (need to check setting)
                                            if (!takesNotesOnHero && currentPlayerIds.Contains(player.PlayerId))
                                            {
                                                continue;
                                            }

                                            var calculatedEquity = new Dictionary <string, Dictionary <Street, decimal> >();

                                            if (player.PlayerId != 0)
                                            {
                                                var playerNoteKey = new PlayerPokerSiteKey(player.PlayerId, player.PokersiteId);

                                                playerStatisticCreationInfo.Player = player;

                                                var playerStatistic = BuildPlayerStatistic(playerStatisticCreationInfo);

                                                var playerNotes = ProcessHand(notes, playerStatistic, parsingResult.Source);

                                                if (playerNotes.Count() == 0)
                                                {
                                                    continue;
                                                }

                                                // if player has no notes, then just save all new notes
                                                if (!playersNotesDictionary.ContainsKey(playerNoteKey) ||
                                                    (playersNotesDictionary.ContainsKey(playerNoteKey) &&
                                                     !playersNotesDictionary[playerNoteKey].ContainsKey(handHistory.Gamenumber)))
                                                {
                                                    playerNotes.ForEach(note => notesToInsert.Add(note));
                                                    continue;
                                                }

                                                var existingNotes = playersNotesDictionary[playerNoteKey][handHistory.Gamenumber];

                                                var notesToAdd = (from playerNote in playerNotes
                                                                  join existingNote in existingNotes on playerNote.Note equals existingNote.Note into gj
                                                                  from note in gj.DefaultIfEmpty()
                                                                  where note == null
                                                                  select playerNote).ToArray();

                                                notesToAdd.ForEach(note => notesToInsert.Add(note));
                                            }
                                        }
                                        ;
                                    }
                                }
                                catch (Exception hhEx)
                                {
                                    LogProvider.Log.Error(CustomModulesNames.PlayerXRay, $"Hand history {handHistory.Gamenumber} has not been processed", hhEx);
                                }

                                // reporting progress
                                progressCounter++;

                                var currentProgress = progressCounter * 100 / entitiesCount;

                                if (progress < currentProgress)
                                {
                                    progress = currentProgress;
                                    ProgressChanged?.Invoke(this, new NoteProcessingServiceProgressChangedEventArgs(progress));
                                }
                            });

                            using (var transaction = session.BeginTransaction())
                            {
                                notesToInsert.ForEach(x => session.Insert(x));
                                transaction.Commit();
                            }
                        }
                    }

                    LogProvider.Log.Info(CustomModulesNames.PlayerXRay, $"Notes have been processed.");
                }
            }
            catch (Exception e)
            {
                LogProvider.Log.Error(CustomModulesNames.PlayerXRay, "Notes have not been processed.", e);
            }
        }
 /// <summary>
 /// Provides the observer with new data.
 /// </summary>
 /// <param name="value">The current notification information.</param>
 public void OnNext(T value)
 {
     currentValue = value;
     observers.ForEach(o => o.OnNext(currentValue));
 }