//Call this method to update public void Update() { while (Empty == false) //Note - opportunity for efficiency... >:) { //step 1: Remove old data from the collections. lock (coinLock) { if (CurrentData != null && CurrentData.Count > 0) { //Step 1: Remove old data from the collections. CurrentData.TakeAndBreak(x => x.LastUpdate > DateTime.UtcNow.AddMinutes(-MinuteInterval)); //Step 2: Get new data from the database and add it to the collections. DataCalls.GetRecentCurrentData(Instrument.InstrumentID, MinuteInterval, CurrentDataIndex).ForEach(x => CurrentData.Add(x)); CurrentDataIndex = this.CurrentData.Max(x => x.CurrentID); } } lock (coinAggLock) { if (CurrentAggData != null && CurrentAggData.Count > 0) { CurrentAggData.TakeAndBreak(x => x.LastUpdate > DateTime.UtcNow.AddMinutes(-MinuteInterval)); DataCalls.GetRecentCurrentAggData(Instrument.InstrumentID, MinuteInterval, CurrentAggDataIndex).ForEach(x => CurrentAggData.Add(x)); CurrentAggDataIndex = this.CurrentAggData.Max(x => x.CurrentAggID); } } Thread.Sleep(50000); } }
public static CurrentAggData AddCurrentAggData(CurrentAggData currentAggData) { using (MarketsEntities context = new MarketsEntities()) { if (currentAggData.Instruments != null) { context.Instruments.Attach(currentAggData.Instruments); } if (currentAggData.Markets != null) { context.Markets.Attach(currentAggData.Markets); } if (currentAggData.Exchanges != null) { context.Exchanges.Attach(currentAggData.Exchanges); } currentAggData = context.CurrentAggData.Add(currentAggData); context.SaveChanges(); } return(currentAggData); }
private void HandleData(string queuedMessage) { string id = Regex.Match(queuedMessage, @"^([0-9]{1,2})").Value; string value = queuedMessage.Substring(id.Length); if (!string.IsNullOrEmpty(value)) { dynamic thing = Newtonsoft.Json.JsonConvert.DeserializeObject <dynamic>(value); if (id == "0" && isPinging == false) { PingModel pingModel = JsonConvert.DeserializeObject <PingModel>(value); ThreadPool.QueueUserWorkItem(_ => Ping(pingModel)); isPinging = true; } if (id == "42") { CryptoCompareSet set = new CryptoCompareSet(thing[1].ToString()); if (set.Length < 6) { return; } if (set.Type == "5") { if (set.Ignore == true) { return; } int exchangeID = 0; int instrumentID = 0; int interval = 0; // = Manager.FirstOrDefault(x => x.instrument.InstrumentID == instrument.InstrumentID); using (MarketsEntities context = new MarketsEntities()) { Instruments instrument = context.Instruments.FirstOrDefault(x => x.Name == set.LASTMARKET && x.BaseCurrency == set.FromCurrency && x.QuoteCurrency == set.ToCurrency && x.Active == true); if (instrument == null) { instrument = DataCalls.AddInstrument(new Instruments() { ExchangeID = 2, MarketID = 1, Name = set.LASTMARKET, BaseCurrency = set.FromCurrency, QuoteCurrency = set.ToCurrency, Active = true, Interval = 60 }); } exchangeID = instrument.ExchangeID; instrumentID = instrument.InstrumentID; interval = instrument.Interval; }; InstrumentManager current = Manager.FirstOrDefault(x => x.InstrumentID == instrumentID && x.Type == 5); if (current != null) { if (DateTime.UtcNow <= current.Last.AddSeconds(interval)) { return; } else { current.Last = DateTime.UtcNow; } } else if (current == null) { Manager.Add(new InstrumentManager() { InstrumentID = instrumentID, Last = DateTime.UtcNow, Type = 5 }); } CurrentAggData currentAggData = new CurrentAggData() { ExchangeID = exchangeID, InstrumentID = instrumentID, MarketID = 1, Flag = set.Flag, Price = set.Price, LastUpdate = FromUnixTime(set.LastUpdate), LastVolume = set.LastVolume, LastVolumeTo = set.LastVolumeTo, LastTradeId = set.LastTradeId, Volume24Hour = set.VOLUME24HOUR, Volume24HourTo = set.VOLUME24HOURTO, Low24Hour = set.LOW24HOUR, High24Hour = set.HIGH24HOUR, Open24Hour = set.OPEN24HOUR, Volume24h = set.Volume24h, Volume24hTo = set.Volume24hTo, MaskInt = set.MaskInt }; DataCalls.AddCurrentAggData(currentAggData); } else { if ((set.Flag == 1 || set.Flag == 2 || set.Flag == 4)) //|| set.Flag == 5)) { int exchangeID = 0; int instrumentID = 0; int interval = 0; // = Manager.FirstOrDefault(x => x.instrument.InstrumentID == instrument.InstrumentID); using (MarketsEntities context = new MarketsEntities()) { Instruments instrument = context.Instruments.FirstOrDefault(x => x.Name == set.ExchangeName && x.BaseCurrency == set.FromCurrency && x.QuoteCurrency == set.ToCurrency && x.Active == true); if (instrument == null) { instrument = DataCalls.AddInstrument(new Instruments() { ExchangeID = 2, MarketID = 1, Name = set.ExchangeName, BaseCurrency = set.FromCurrency, QuoteCurrency = set.ToCurrency, Active = true, Interval = 60 }); } exchangeID = instrument.ExchangeID; instrumentID = instrument.InstrumentID; interval = instrument.Interval; }; InstrumentManager current = Manager.FirstOrDefault(x => x.InstrumentID == instrumentID && x.Type == 4); if (current != null) { if (DateTime.UtcNow <= current.Last.AddSeconds(interval)) { return; } else { current.Last = DateTime.UtcNow; } } else if (current == null) { Manager.Add(new InstrumentManager() { InstrumentID = instrumentID, Last = DateTime.UtcNow, Type = 4 }); } CurrentData currentData = new CurrentData() { //Instruments = instrument, ExchangeID = exchangeID, InstrumentID = instrumentID, MarketID = 1, Flag = set.Flag, Price = set.Price, LastUpdate = FromUnixTime(set.LastUpdate), LastVolume = set.LastVolume, LastVolumeTo = set.LastVolumeTo, LastTradeId = set.LastTradeId, Volume24h = set.Volume24h, Volume24hTo = set.Volume24hTo, MaskInt = set.MaskInt }; DataCalls.AddCurrentData(currentData); } } } return; } else { return; } }