Lean  $LEAN_TAG$
SubscriptionDataSourceReader.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15 */
16 
17 using System;
18 using System.IO;
19 using QuantConnect.Data;
20 using QuantConnect.Logging;
23 
25 {
26  /// <summary>
27  /// Provides a factory method for creating <see cref="ISubscriptionDataSourceReader"/> instances
28  /// </summary>
29  public static class SubscriptionDataSourceReader
30  {
31  private static readonly bool ShowMissingDataLogs = Config.GetBool("show-missing-data-logs", false);
32 
33  /// <summary>
34  /// Creates a new <see cref="ISubscriptionDataSourceReader"/> capable of handling the specified <paramref name="source"/>
35  /// </summary>
36  /// <param name="source">The subscription data source to create a factory for</param>
37  /// <param name="dataCacheProvider">Used to cache data</param>
38  /// <param name="config">The configuration of the subscription</param>
39  /// <param name="date">The date to be processed</param>
40  /// <param name="isLiveMode">True for live mode, false otherwise</param>
41  /// <param name="factory">The base data instance factory</param>
42  /// <param name="dataProvider">The data provider to use</param>
43  /// <returns>A new <see cref="ISubscriptionDataSourceReader"/> that can read the specified <paramref name="source"/></returns>
44  public static ISubscriptionDataSourceReader ForSource(SubscriptionDataSource source, IDataCacheProvider dataCacheProvider, SubscriptionDataConfig config, DateTime date, bool isLiveMode, BaseData factory, IDataProvider dataProvider, IObjectStore objectStore)
45  {
47  switch (source.Format)
48  {
49  case FileFormat.Csv:
50  reader = new TextSubscriptionDataSourceReader(dataCacheProvider, config, date, isLiveMode, objectStore);
51  break;
52 
53  case FileFormat.UnfoldingCollection:
54  reader = new CollectionSubscriptionDataSourceReader(dataCacheProvider, config, date, isLiveMode, objectStore);
55  break;
56 
57  case FileFormat.ZipEntryName:
58  reader = new ZipEntryNameSubscriptionDataSourceReader(dataCacheProvider, config, date, isLiveMode);
59  break;
60 
61  case FileFormat.Index:
62  return new IndexSubscriptionDataSourceReader(dataCacheProvider, config, date, isLiveMode, dataProvider, objectStore);
63 
64  case FileFormat.FoldingCollection:
65  reader = new BaseDataCollectionAggregatorReader(dataCacheProvider, config, date, isLiveMode, objectStore);
66  break;
67 
68  default:
69  throw new NotImplementedException("SubscriptionFactory.ForSource(" + source + ") has not been implemented yet.");
70  }
71 
72  // wire up event handlers for logging missing files
73  if (ShowMissingDataLogs && source.TransportMedium == SubscriptionTransportMedium.LocalFile)
74  {
75  if (!factory.IsSparseData())
76  {
77  reader.InvalidSource += (sender, args) => Log.Error($"SubscriptionDataSourceReader.InvalidSource(): File not found: {args.Source.Source}");
78  }
79  }
80 
81  return reader;
82  }
83 
84  /// <summary>
85  /// Creates cache directory if not existing and deletes old files from the cache
86  /// </summary>
87  public static void CheckRemoteFileCache()
88  {
89  // create cache directory if not existing
90  if (!Directory.Exists(Globals.Cache)) Directory.CreateDirectory(Globals.Cache);
91 
92  var frontierToDelete = DateTime.Now.AddHours(-24);
93  // clean old files out of the cache
94  foreach (var file in Directory.EnumerateFiles(Globals.Cache))
95  {
96  if (File.GetCreationTime(file) < frontierToDelete) File.Delete(file);
97  }
98  }
99  }
100 }