public DatabaseMetricsStorage(IDictionary <string, string> appSettingDictionary)
        {
            string connectionString = MetricsAppSettings.GetSetting(appSettingDictionary, MetricsAppSettings.SqlConfigurationKey);
            int    commandTimeout   = MetricsAppSettings.TryGetIntSetting(appSettingDictionary, MetricsAppSettings.CommandTimeoutKey) ?? 0;

            _cstr           = new SqlConnectionStringBuilder(connectionString);
            _commandTimeout = commandTimeout > 0 ? commandTimeout : 5;
        }
        public CatalogMetricsStorage(IDictionary <string, string> appSettingDictionary)
        {
            // CatalogFlushTimePeriod is the time for which the service may be idle before stats is memory is flushed into catalog. Default value is 1 minute or 60000 ms
            CatalogFlushTimePeriod = MetricsAppSettings.TryGetIntSetting(appSettingDictionary, MetricsAppSettings.CatalogFlushTimePeriod) ?? 60000;
            CatalogFlushTimer      = new Timer(TimerTask, this, CatalogFlushTimePeriod, CatalogFlushTimePeriod);

            string connectionString = MetricsAppSettings.GetSetting(appSettingDictionary, MetricsAppSettings.SqlConfigurationKey);
            int    commandTimeout   = MetricsAppSettings.TryGetIntSetting(appSettingDictionary, MetricsAppSettings.CommandTimeoutKey) ?? 0;

            _cstr           = new SqlConnectionStringBuilder(connectionString);
            _commandTimeout = commandTimeout > 0 ? commandTimeout : 5;

            string websiteInstanceId = Environment.GetEnvironmentVariable(WEBSITE_INSTANCE_ID);

            if (String.IsNullOrEmpty(websiteInstanceId))
            {
                throw new ArgumentException("Environment variable WEBSITE_INSTANCE_ID is not defined");
            }

            Storage rootStorageOfAllCatalogs = null;

            string catalogLocalDirectory = MetricsAppSettings.TryGetSetting(appSettingDictionary, MetricsAppSettings.CatalogLocalDirectoryKey);

            if (!String.IsNullOrEmpty(catalogLocalDirectory))
            {
                string catalogBaseAddress = MetricsAppSettings.GetSetting(appSettingDictionary, MetricsAppSettings.CatalogBaseAddressKey);

                // NOTE that rootStorageOfAllCatalogs is at 'catalogLocalDirectory' and has a base address of 'catalogBaseAddress'
                // However, CatalogStorage, which is specific to this website instance, is at 'catalogDirectory/websiteInstanceId' and has a base address of 'catalogBaseAddress/websiteInstanceId'
                rootStorageOfAllCatalogs = new FileStorage(catalogBaseAddress, catalogLocalDirectory);
                CatalogStorage           = new FileStorage(Path.Combine(catalogBaseAddress, websiteInstanceId), Path.Combine(catalogLocalDirectory, websiteInstanceId));
            }
            else
            {
                string catalogStorageAccountKey = MetricsAppSettings.GetSetting(appSettingDictionary, MetricsAppSettings.CatalogStorageAccountKey);
                string catalogPath = MetricsAppSettings.GetSetting(appSettingDictionary, MetricsAppSettings.CatalogPathKey);

                var catalogStorageAccount = CloudStorageAccount.Parse(catalogStorageAccountKey);

                string containerName;
                string prefix;
                GetContainerNameAndPrefix(catalogPath, out containerName, out prefix);

                // NOTE that rootStorageOfAllCatalogs is at 'catalogStorageAccount/containerName/prefix' and a base address derived from that blob directory
                // However, CatalogStorage, which is specific to this website instance, is at 'catalogStorageAccount/containerName/prefix/websiteInstanceId' and a base address derived from that blob directory
                rootStorageOfAllCatalogs = new AzureStorage(catalogStorageAccount, containerName, prefix);
                CatalogStorage           = new AzureStorage(catalogStorageAccount, containerName, Path.Combine(prefix, websiteInstanceId));
            }

            CreateIndexesFile(rootStorageOfAllCatalogs, websiteInstanceId);
            CatalogPageSize = MetricsAppSettings.TryGetIntSetting(appSettingDictionary, MetricsAppSettings.CatalogPageSizeKey) ?? 500;
            CatalogItemPackageStatsCount = MetricsAppSettings.TryGetIntSetting(appSettingDictionary, MetricsAppSettings.CatalogItemPackageStatsCountKey) ?? 1000;
        }
        public PackageStatsHandler(IDictionary <string, string> appSettingDictionary)
        {
            _metricsStorageList = new List <MetricsStorage>();

            bool shouldUseDB      = MetricsAppSettings.TryGetBooleanSetting(appSettingDictionary, MetricsAppSettings.ShouldUseDB);
            bool shouldUseCatalog = MetricsAppSettings.TryGetBooleanSetting(appSettingDictionary, MetricsAppSettings.ShouldUseCatalog);

            if (shouldUseDB)
            {
                _metricsStorageList.Add(new DatabaseMetricsStorage(appSettingDictionary));
            }

            if (shouldUseCatalog)
            {
                _metricsStorageList.Add(new CatalogMetricsStorage(appSettingDictionary));
            }

            if (_metricsStorageList.Count == 0)
            {
                throw new ArgumentException("Neither ShouldUseDB nor ShouldUseCatalog is set to true. Invalid app setting");
            }
        }