Пример #1
0
        /// <summary>
        /// Saves a calculation that has been dispatched by the batchworker.
        /// </summary>
        /// <param name="calculation">The calculation.</param>
        /// <param name="sealingSlabs">The sealing slabs.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="dbInsertFactor">The database insert factor.</param>
        /// <exception cref="System.Exception">
        /// </exception>
        private void SaveDispatched(Calculation calculation, IEnumerable <SealingSlab> sealingSlabs, CancellationToken cancellationToken, out double dbInsertFactor)
        {
            cancellationToken.ThrowIfCancellationRequested();

            dbInsertFactor = 0;

            var stateRunning = stateDepot.GetByName(State.NameRunning);

            if (calculation.StateId != stateRunning.Id)
            {
                throw new Exception($"Not in state {stateRunning.Name} for saving dispatched calulation {calculation.Id}.");
            }

            if (sealingSlabs == null)
            {
                throw new Exception($"No sealing slabs for calulation {calculation.Id}.");
            }

            foreach (var sealingSlab in sealingSlabs)
            {
                sealingSlab.CalculationId = calculation.Id;
            }

            DateTime timeStart = DateTime.Now;

            int maxIteration = sealingSlabDepot.GetMaxIteration(calculation.Id);

            // Continue only with sealing slabs that haven't been saved yet.
            // Only take the defined amount of iterations.
            var iterationSealingSlabs = sealingSlabs
                                        .Where(x => x.Iteration > maxIteration && x.Iteration <= App.Config.SaveIterationsCount)
                                        .GroupBy(x => x.Iteration);

            foreach (var sealingSlabGroup in iterationSealingSlabs)
            {
                // Use transaction to make sealing slabs consistent per iteration.
                using (var transaction = database.GetTransaction())
                {
                    sealingSlabDepot.InsertBulk(sealingSlabGroup, App.Config.SealingSlabChunkSize);

                    transaction.Complete();

                    cancellationToken.ThrowIfCancellationRequested();
                }
            }

            var    diff             = (DateTime.Now - timeStart);
            double measuredSeconds  = diff.TotalSeconds;
            int    sealingSlabCount = iterationSealingSlabs.Sum(x => x.Count());

            dbInsertFactor = EstimationService.CalculateDBInsertFactor(calculation.SealingSlabRadiusPixels, sealingSlabCount, measuredSeconds, App.Config.SealingSlabChunkSize);

            App.Logger.Info($"Calculated DBInsertFactor: {dbInsertFactor}.");

            var stateDone = stateDepot.GetByName(State.NameDone);

            calculation.StateId = stateDone.Id;

            calculationDepot.Save(calculation); // database
        }
 /// <summary>
 /// Gets the highest iteration.
 /// </summary>
 /// <param name="calculationId">The calculation identifier.</param>
 /// <returns></returns>
 public int GetMaxIteration(int calculationId)
 {
     return(sealingSlabDepot.GetMaxIteration(calculationId));
 }