/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
using System;
using System.IO;
using QuantConnect.Data;
using QuantConnect.Logging;
using QuantConnect.Interfaces;
using QuantConnect.Configuration;
namespace QuantConnect.Lean.Engine.DataFeeds
{
///
/// Provides a factory method for creating instances
///
public static class SubscriptionDataSourceReader
{
private static readonly bool ShowMissingDataLogs = Config.GetBool("show-missing-data-logs", false);
///
/// Creates a new capable of handling the specified
///
/// The subscription data source to create a factory for
/// Used to cache data
/// The configuration of the subscription
/// The date to be processed
/// True for live mode, false otherwise
/// The base data instance factory
/// The data provider to use
/// A new that can read the specified
public static ISubscriptionDataSourceReader ForSource(SubscriptionDataSource source, IDataCacheProvider dataCacheProvider, SubscriptionDataConfig config, DateTime date, bool isLiveMode, BaseData factory, IDataProvider dataProvider, IObjectStore objectStore)
{
ISubscriptionDataSourceReader reader;
switch (source.Format)
{
case FileFormat.Csv:
reader = new TextSubscriptionDataSourceReader(dataCacheProvider, config, date, isLiveMode, objectStore);
break;
case FileFormat.UnfoldingCollection:
reader = new CollectionSubscriptionDataSourceReader(dataCacheProvider, config, date, isLiveMode, objectStore);
break;
case FileFormat.ZipEntryName:
reader = new ZipEntryNameSubscriptionDataSourceReader(dataCacheProvider, config, date, isLiveMode);
break;
case FileFormat.Index:
return new IndexSubscriptionDataSourceReader(dataCacheProvider, config, date, isLiveMode, dataProvider, objectStore);
case FileFormat.FoldingCollection:
reader = new BaseDataCollectionAggregatorReader(dataCacheProvider, config, date, isLiveMode, objectStore);
break;
default:
throw new NotImplementedException("SubscriptionFactory.ForSource(" + source + ") has not been implemented yet.");
}
// wire up event handlers for logging missing files
if (ShowMissingDataLogs && source.TransportMedium == SubscriptionTransportMedium.LocalFile)
{
if (!factory.IsSparseData())
{
reader.InvalidSource += (sender, args) => Log.Error($"SubscriptionDataSourceReader.InvalidSource(): File not found: {args.Source.Source}");
}
}
return reader;
}
///
/// Creates cache directory if not existing and deletes old files from the cache
///
public static void CheckRemoteFileCache()
{
// create cache directory if not existing
if (!Directory.Exists(Globals.Cache)) Directory.CreateDirectory(Globals.Cache);
var frontierToDelete = DateTime.Now.AddHours(-24);
// clean old files out of the cache
foreach (var file in Directory.EnumerateFiles(Globals.Cache))
{
if (File.GetCreationTime(file) < frontierToDelete) File.Delete(file);
}
}
}
}