예제 #1
0
            public void ShouldBeAbleToPullExistingConfigFromCache()
            {
                var monitorInfoDictionary = new ConcurrentDictionary<string, MonitorInfo>();

                var reduceLevel = new ReduceLevel();

                var monitorConfig = new MonitorConfig();
                monitorConfig.MonitorReductionType = MonitorReductionType.DefaultAccumulate;
                monitorConfig.ReduceLevels = new List<ReduceLevel> { reduceLevel };

                var monitorConfigsDictionary = new ConcurrentDictionary<string, MonitorConfig>();
                monitorConfigsDictionary.TryAdd("Test", monitorConfig);

                var cache = new Mock<IDataCache>();
                cache.SetupGet(x => x.MonitorInfo).Returns(monitorInfoDictionary);
                cache.SetupGet(x => x.MonitorConfigs).Returns(monitorConfigsDictionary);

                var settings = BuildSettings();

                var seeder = new ConfigSeed(cache.Object, settings.Object);
                var result = seeder.Seed("Test", MonitorReductionType.DefaultAccumulate);

                Assert.NotNull(result);
                Assert.Same(monitorConfig, result.MonitorConfig);
                Assert.Same(reduceLevel, result.FirstReduceLevel);
            }
예제 #2
0
파일: ConfigSeed.cs 프로젝트: Zocdoc/ZocMon
        /// <summary>
        /// Seed the local data for a monitor configuration.
        /// </summary>
        /// <param name="configName"></param>
        /// <param name="monitorReductionType"></param>
        /// <returns></returns>
        public MonitorInfo Seed(string configName, MonitorReductionType monitorReductionType)
        {
            configName.ThrowIfNull("configName");
            monitorReductionType.ThrowIfNull("monitorReductionType");

            MonitorInfo monitorInfo = null;
            bool tablesCreated = false;

            configName = Support.ValidateConfigName(configName);

            // Look for static cached data/state (i.e. its already been seeded)
            if (!_cache.MonitorInfo.TryGetValue(configName, out monitorInfo))
            {
                // Look for an existing monitor configuration
                MonitorConfig monitorConfig;
                if (!_cache.MonitorConfigs.TryGetValue(configName, out monitorConfig))
                {
                    // Create new monitorconfig record (will get inserted into db during Flush())
                    var aggregationClassName = monitorReductionType == MonitorReductionType.DefaultAccumulate ? "ZocMonLib.ReduceMethods.ReduceMethodAccumulate" : "ZocMonLib.ReduceMethods.ReduceMethodAverage";
                    var aggregationClass = _settings.ReduceMethodProvider.Retrieve(aggregationClassName);
                    monitorConfig = new MonitorConfig
                                        {
                                            Name = configName,
                                            MonitorReductionType = monitorReductionType,
                                            ReduceLevels = new List<ReduceLevel>
                                                               {
                                                                   new ReduceLevel
                                                                       {
                                                                           MonitorConfigName = configName,
                                                                           Resolution = 60 * 1000,
                                                                           HistoryLength = 7 * 24 * 60 * 60 * 1000,
                                                                           AggregationClassName = aggregationClassName,
                                                                           AggregationClass = aggregationClass
                                                                       },
                                                                   new ReduceLevel
                                                                       {
                                                                           MonitorConfigName = configName,
                                                                           Resolution = 5 * 60 * 1000,
                                                                           HistoryLength = 7 * 24 * 60 * 60 * 1000,
                                                                           AggregationClassName = aggregationClassName,
                                                                           AggregationClass = aggregationClass
                                                                       },
                                                                   new ReduceLevel
                                                                       {
                                                                           MonitorConfigName = configName,
                                                                           Resolution = 60 * 60 * 1000,
                                                                           HistoryLength = 7 * 24 * 60 * 60 * 1000,
                                                                           AggregationClassName = aggregationClassName,
                                                                           AggregationClass = aggregationClass
                                                                       },
                                                                   new ReduceLevel
                                                                       {
                                                                           MonitorConfigName = configName,
                                                                           Resolution = 24 * 60 * 60 * 1000,
                                                                           HistoryLength = 7 * 24 * 60 * 60 * 1000,
                                                                           AggregationClassName = aggregationClassName,
                                                                           AggregationClass = aggregationClass
                                                                       }
                                                               }
                                        };
                }
                else
                {
                    // If this monitor already exists in monitorconfigs, we know the tables have already been created
                    tablesCreated = true;
                }

                // Don't validate custom configs (as they could be anything....)
                if (!MonitorReductionType.Custom.Equals(monitorReductionType))
                {
                    if (!monitorReductionType.Equals(monitorConfig.MonitorReductionType))
                        throw new DataException("Wrong reduction type for monitor \"" + configName + "\"");
                }

                // WTF why only the first

                var reduceLevel = monitorConfig.ReduceLevels.First();

                monitorInfo = new MonitorInfo
                                  {
                                      MonitorConfig = monitorConfig,
                                      FirstReduceLevel = reduceLevel,
                                      MonitorRecords = new List<MonitorRecord<double>>(),
                                      TablesCreated = tablesCreated
                                  };

                _cache.MonitorInfo.AddOrUpdate(configName, monitorInfo, (key, oldValue) => monitorInfo);
            }

            return monitorInfo;
        }
예제 #3
0
        private static TestData PopulateTestData(bool isAll)
        {
            var testData = new TestData();

            var reduceStatus = new Mock<IRecordReduceStatus>();
            reduceStatus.Setup(x => x.IsReducing()).Returns(false).Verifiable();
            reduceStatus.Setup(x => x.DoneReducing()).Verifiable();

            var reductionLevel1 = new ReduceLevel { Resolution = 1000, AggregationClass = new ReduceMethodAccumulate() };
            var reductionLevel2 = new ReduceLevel { Resolution = 60000, AggregationClass = new ReduceMethodAccumulate() };

            var monitorConfigSingle = new MonitorConfig { Name = "Single", ReduceLevels = new List<ReduceLevel> { reductionLevel1, reductionLevel2 } };
            var monitorInfoSingle = new MonitorInfo { MonitorConfig = monitorConfigSingle };

            var monitorInfoDictionary = new ConcurrentDictionary<string, MonitorInfo>();

            var monitorConfigDictionary = new ConcurrentDictionary<string, MonitorConfig>();
            monitorConfigDictionary.TryAdd("Single", monitorConfigSingle);

            var seeder = new Mock<IConfigSeed>();
            seeder.Setup(x => x.Seed("Single", MonitorReductionType.Custom)).Returns(monitorInfoSingle).Verifiable();

            var cache = new Mock<IDataCache>();
            cache.SetupGet(x => x.MonitorInfo).Returns(monitorInfoDictionary).Verifiable();
            if (isAll)
                cache.SetupGet(x => x.MonitorConfigs).Returns(monitorConfigDictionary).Verifiable();

            var connection = new Mock<IDbConnection>();
            connection.Setup(x => x.Open()).Verifiable();
            if (!isAll)
                connection.Setup(x => x.Close()).Verifiable();
            var connectionInstance = connection.Object;

            var dbProviderFactory = new Mock<IStorageFactory>();
            dbProviderFactory.Setup(x => x.CreateConnection()).Returns(connectionInstance).Verifiable();

            var requiringReduction = new List<MonitorRecord<double>> { new MonitorRecord<double>(new DateTime(2012, 1, 15, 6, 30, 5), 1), new MonitorRecord<double>(new DateTime(2012, 1, 15, 6, 30, 10), 2), new MonitorRecord<double>(new DateTime(2012, 1, 15, 6, 30, 15), 4) };
            var reducedRecord = new MonitorRecord<double>(new DateTime(2012, 1, 15, 6, 30, 30), 7);
            var reduced = new StorageLastReduced { Record = reducedRecord, Time = new DateTime(2012, 1, 15, 6, 30, 0) };

            var storage = new Mock<IStorageCommands>();
            storage.Setup(x => x.RetrieveLastReducedData("SingleMinutelyData", 60000, connectionInstance)).Returns(reduced).Verifiable();
            storage.Setup(x => x.SelectListRequiringReduction("SingleSecondlyData", true, new DateTime(2012, 1, 15, 6, 30, 0), connectionInstance)).Returns(requiringReduction).Verifiable();
            storage.Setup(x => x.UpdateIfExists("SingleMinutelyData", It.IsAny<MonitorRecord<double>>(), true, connectionInstance)).Callback(() => testData.UpdateIfExistsCount++).Returns(true).Verifiable();
            storage.Setup(x => x.Flush("SingleMinutelyData", It.IsAny<IEnumerable<MonitorRecord<double>>>(), connectionInstance)).Verifiable();
            storage.Setup(x => x.ClearReducedData("Single", It.IsAny<DateTime>(), It.IsAny<ReduceLevel>(), connectionInstance)).Callback(() => testData.DeletedCount++).Verifiable();

            var reduceAggregater = new Mock<IRecordReduceAggregate>();
            reduceAggregater.Setup(x => x.Aggregate(new DateTime(2012, 1, 15, 6, 30, 0), reductionLevel2, requiringReduction, It.IsAny<IDictionary<DateTime, IList<MonitorRecord<double>>>>()))
                .Callback(() => testData.AggregateCount++)
                .Returns((DateTime lastReductionTime, ReduceLevel targetReduceLevel, IList<MonitorRecord<double>> sourceAggregationList, IDictionary<DateTime, IList<MonitorRecord<double>>> destinationAggregatedList) => { destinationAggregatedList.Add(new DateTime(2012, 1, 15, 6, 30, 0), requiringReduction); return new DateTime(2012, 1, 15, 6, 30, 0); });

            var comparisonsData = new Mock<IRecordCompare>();
            comparisonsData.Setup(x => x.CalculateComparisons("Single", monitorInfoSingle, It.IsAny<ReduceLevel>(), connectionInstance)).Callback(() => testData.CalculateCount++).Verifiable();

            var settings = BuildSettings();
            settings.Setup(x => x.ConfigSeed).Returns(seeder.Object);

            testData.Settings = settings;
            testData.Connection = connection;
            testData.ReduceStatus = reduceStatus;
            testData.ReduceAggregater = reduceAggregater;
            testData.Cache = cache;
            testData.Seeder = seeder;
            testData.ComparisonsData = comparisonsData;
            testData.Storage = storage;
            testData.DbProviderFactory = dbProviderFactory;

            return testData;
        }