public IAnalysis CreateAnalysis(AnalysisTemplate template, AnalysisViewTemplate viewTemplate)
        {
            ActiveAnalysisConfiguration analysisConfiguration;

            var id       = AnalysisId.CreateNew();
            var analysis = new ActiveAnalysis(id,
                                              template,
                                              _taskScheduler,
                                              _dataSourceAnalyserEngine,
                                              TimeSpan.FromMilliseconds(100));

            try
            {
                analysisConfiguration = new ActiveAnalysisConfiguration(id, template, viewTemplate);
                lock (_syncRoot)
                {
                    _templates.Add(analysisConfiguration);
                    _analyses.Add(analysis.Id, analysis);
                    _lastSavedAnalyses.Add(analysis.Id, analysisConfiguration);
                }
            }
            catch (Exception)
            {
                analysis.Dispose();                 //< ActiveAnalysis actually spawns new analyses on the engine so we should cancel those in case an exception si thrown here...
                throw;
            }

            SaveAsync(analysisConfiguration).Wait();

            return(analysis);
        }
        public void TestDispose()
        {
            var group = new Tailviewer.BusinessLogic.Analysis.ActiveAnalysis(_template, _taskScheduler, _analysisEngine.Object, TimeSpan.Zero);

            group.Dispose();

            _taskScheduler.PeriodicTaskCount.Should()
            .Be(0, "because all tasks should've been stopped when the group is disposed of");
        }
        public void TestAddRemove1()
        {
            var group = new Tailviewer.BusinessLogic.Analysis.ActiveAnalysis(_template, _taskScheduler, _analysisEngine.Object, TimeSpan.Zero);

            _template.Analysers.Should().BeEmpty();

            var analyser = group.Add(new LogAnalyserFactoryId("foobar"), null);

            _template.Analysers.Should().HaveCount(1);

            group.Remove(analyser);
            _template.Analysers.Should().BeEmpty();
        }
        private async Task RestoreSavedAnalysesAsync()
        {
            try
            {
                var files = await EnumerateAnalysesAsync();

                foreach (var filePath in files)
                {
                    using (var stream = await _filesystem.OpenRead(filePath))
                    {
                        var configuration = ReadAnalysis(stream);
                        if (configuration != null)
                        {
                            var analysis = new ActiveAnalysis(configuration.Id,
                                                              configuration.Template,
                                                              _taskScheduler,
                                                              _dataSourceAnalyserEngine,
                                                              TimeSpan.FromMilliseconds(100));

                            try
                            {
                                lock (_syncRoot)
                                {
                                    _templates.Add(configuration);
                                    _lastSavedAnalyses.Add(analysis.Id, configuration);

                                    _analyses.Add(analysis.Id, analysis);
                                }
                            }
                            catch (Exception)
                            {
                                analysis
                                .Dispose();                                         //< ActiveAnalysis actually spawns new analyses on the engine so we should cancel those in case an exception si thrown here...
                                throw;
                            }
                        }
                    }
                }
            }
            catch (DirectoryNotFoundException e)
            {
                Log.DebugFormat("Unable to restore existing analyses: {0}", e);
                await CreateAnalysisFolderAsync();
            }
            catch (Exception e)
            {
                Log.ErrorFormat("Caught unexpected exception while trying to restore existing analyses: {0}", e);
            }
        }