ZocMonLib.ConfigSeed.Seed C# (CSharp) Method

Seed() public method

Seed the local data for a monitor configuration.
public Seed ( string configName, MonitorReductionType monitorReductionType ) : MonitorInfo
configName string
monitorReductionType MonitorReductionType
return MonitorInfo
        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;
        }

Usage Example

Example #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);
            }
All Usage Examples Of ZocMonLib.ConfigSeed::Seed