19 using System.Collections;
20 using System.Collections.Generic;
30 private int _totalInsightCount;
31 private int _openInsightCount;
32 private readonly List<Insight> _insightsComplete =
new();
33 private readonly Dictionary<Symbol, List<Insight>> _insights =
new();
44 return _openInsightCount;
58 return _totalInsightCount;
73 _insightsComplete.Add(item);
75 if (!_insights.TryGetValue(item.
Symbol, out var existingInsights))
77 _insights[item.
Symbol] = existingInsights =
new();
79 existingInsights.Add(item);
87 public void AddRange(IEnumerable<Insight> insights)
89 foreach (var insight
in insights)
102 return _insights.TryGetValue(item.
Symbol, out var symbolInsights)
103 && symbolInsights.Contains(item);
116 return _insights.TryGetValue(symbol, out var symbolInsights)
117 && symbolInsights.Count > 0;
129 if (_insights.TryGetValue(item.
Symbol, out var symbolInsights))
131 if (symbolInsights.Remove(item))
136 if (symbolInsights.Count == 0)
138 _insights.Remove(item.
Symbol);
153 public List<Insight>
this[
Symbol symbol]
159 return _insights[symbol]?.ToList();
166 if (_insights.TryGetValue(symbol, out var existingInsights))
168 _openInsightCount -= existingInsights?.Count ?? 0;
173 _openInsightCount += value.Count;
174 _totalInsightCount += value.Count;
176 _insights[symbol] = value;
191 var result = _insights.TryGetValue(symbol, out insights);
195 insights = insights.ToList();
208 return _insights.SelectMany(kvp => kvp.Value).ToList().GetEnumerator();
215 IEnumerator IEnumerable.GetEnumerator()
228 foreach (var symbol
in symbols)
230 if (_insights.Remove(symbol, out var existingInsights))
232 _openInsightCount -= existingInsights.Count;
245 if (_openInsightCount == 0)
251 return _insights.Min(x => x.Value.Min(i => i.CloseTimeUtc));
261 var activeInsights =
new List<Insight>();
264 foreach (var kvp
in _insights)
266 foreach (var insight
in kvp.Value)
268 if (insight.IsActive(utcTime))
270 activeInsights.Add(insight);
274 return activeInsights;
288 if(_insights.TryGetValue(symbol, out var existingInsights))
290 return existingInsights.Any(i => i.IsActive(utcTime));
303 var removedInsights =
new List<Insight>();
306 foreach (var kvp
in _insights)
308 foreach (var insight
in kvp.Value)
310 if (insight.IsExpired(utcTime))
312 removedInsights.Add(insight);
316 foreach (var insight
in removedInsights)
321 return removedInsights;
332 _insightsComplete.RemoveAll(insight => filter(insight));
335 List<Insight> insightsToRemove =
null;
336 foreach (var insights
in _insights.Values)
338 foreach (var insight
in insights)
342 insightsToRemove ??=
new ();
343 insightsToRemove.Add(insight);
347 if(insightsToRemove !=
null)
349 foreach (var insight
in insightsToRemove)
362 public List<Insight>
GetInsights(Func<Insight, bool> filter =
null)
368 return _insightsComplete.ToList();
370 return _insightsComplete.Where(filter).ToList();
381 Func<Insight, bool> convertedFilter;
382 if (filter.TryConvertToDelegate(out convertedFilter))
390 throw new ArgumentException($
"InsightCollection.GetInsights: {filter.Repr()} is not a valid argument.");