public async Task CreateAsync(SimilaritiesReport similarity)
 {
     foreach (var collection in similarity.Collections)
     {
         var dto  = collection.Properties.Select(x => new ContextCollectionPropertyDbEntity(x)).ToArray();
         var json = EntitySerializer.Serialize(dto);
         if (collection.Id == 0)
         {
             using (var cmd = _uow.CreateDbCommand())
             {
                 cmd.CommandText =
                     @"INSERT INTO IncidentContextCollections (IncidentId, Name, Properties) 
               VALUES(@incidentId, @name, @props)";
                 cmd.AddParameter("incidentId", collection.IncidentId);
                 cmd.AddParameter("name", collection.Name);
                 cmd.AddParameter("props", json);
                 await cmd.ExecuteNonQueryAsync();
             }
         }
         else
         {
             using (var cmd = _uow.CreateDbCommand())
             {
                 cmd.CommandText =
                     @"UPDATE IncidentContextCollections SET Properties=@props WHERE Id = @id";
                 cmd.AddParameter("id", collection.Id);
                 cmd.AddParameter("props", json);
                 await cmd.ExecuteNonQueryAsync();
             }
         }
     }
 }
        /// <summary>
        ///     Process an event asynchronously.
        /// </summary>
        /// <param name="e">event to process</param>
        /// <returns>
        ///     Task to wait on.
        /// </returns>
        public async Task HandleAsync(IMessageContext context, ReportAddedToIncident e)
        {
            if (e.IsStored != true)
            {
                return;
            }

            _logger.Debug("Updating similarities " + e.Incident.Id);
            var adapters = _adapterRepository.GetAdapters();
            var sw2      = new Stopwatch();

            sw2.Start();
            long beginStep, afterFindStep, afterReposStep;

            try
            {
                _logger.Debug("Finding for incident: " + e.Incident.Id);
                beginStep = sw2.ElapsedMilliseconds;

                var similaritiesReport = _similarityRepository.FindForIncident(e.Incident.Id);
                var isNew = false;
                if (similaritiesReport == null)
                {
                    similaritiesReport = new SimilaritiesReport(e.Incident.Id);
                    isNew = true;
                }

                var analyzer = new SimilarityAnalyzer(similaritiesReport);
                afterFindStep = sw2.ElapsedMilliseconds;
                analyzer.AddReport(e.Report, adapters);

                if (isNew)
                {
                    _logger.Debug("Creating new similarity report...");
                    await _similarityRepository.CreateAsync(similaritiesReport);
                }
                else
                {
                    _logger.Debug("Updating existing similarity report...");
                    await _similarityRepository.UpdateAsync(similaritiesReport);
                }

                afterReposStep = sw2.ElapsedMilliseconds;
                _logger.Debug("similarities done ");
            }
            catch (Exception exception)
            {
                _logger.Error("failed to add report to incident " + e.Incident.Id, exception);

                // Live changes since we get deadlocks?
                // TODO: WHY do we get deadlocks, aren't we the only ones reading from the similarity tables?
                return;
            }
            sw2.Stop();
            if (sw2.ElapsedMilliseconds > 200)
            {
                _logger.InfoFormat("Slow similarity handling, times: {0}/{1}/{2}/{3}", beginStep, afterFindStep, afterReposStep,
                                   sw2.ElapsedMilliseconds);
            }
        }
        /// <summary>
        ///     Process an event asynchronously.
        /// </summary>
        /// <param name="e">event to process</param>
        /// <returns>
        ///     Task to wait on.
        /// </returns>
        public async Task HandleAsync(IMessageContext context, ReportAddedToIncident e)
        {
            _logger.Debug("Updating similarities");
            var adapters = _adapterRepository.GetAdapters();
            var sw2      = new Stopwatch();

            sw2.Start();
            long step1, step2, step3;

            try
            {
                _logger.Debug("Finding for incident: " + e.Incident.Id);
                var similaritiesReport = _similarityRepository.FindForIncident(e.Incident.Id);

                step1 = sw2.ElapsedMilliseconds;
                var isNew = false;
                if (similaritiesReport == null)
                {
                    _logger.Debug("Not found, creating a new");
                    similaritiesReport = new SimilaritiesReport(e.Incident.Id);
                    isNew = true;
                }

                var analyzer = new SimilarityAnalyzer(similaritiesReport);
                step2 = sw2.ElapsedMilliseconds;
                analyzer.AddReport(e.Report, adapters);

                if (isNew)
                {
                    _logger.Debug("Creating...");
                    await _similarityRepository.CreateAsync(similaritiesReport);
                }
                else
                {
                    _logger.Debug("Updating...");
                    await _similarityRepository.UpdateAsync(similaritiesReport);
                }

                step3 = sw2.ElapsedMilliseconds;
                _logger.Debug("similarities done ");
            }
            catch (Exception exception)
            {
                _logger.Error("failed to add report to incident " + e.Incident.Id, exception);
                return;
            }
            sw2.Stop();
            if (sw2.ElapsedMilliseconds > 200)
            {
                _logger.InfoFormat("Slow similarity handling, times: {0}/{1}/{2}/{3}", step1, step2, step3,
                                   sw2.ElapsedMilliseconds);
            }
        }
 public async Task UpdateAsync(SimilaritiesReport similarity)
 {
     await CreateAsync(similarity);
 }
 public SimilarityAnalyzer(SimilaritiesReport similarity)
 {
     _similarity = similarity;
 }