public void ThenRefreshForOptionIsAndNextRefreshIsAvailableAt(RefreshOption option, RefresStatus expectedStatus) { var refreshingMechnism = objectContainer.Resolve <IRefreshingMechnism>(); var actualStatus = refreshingMechnism.GetRefreshStatus(option, _currentTime); Assert.AreEqual(expectedStatus, actualStatus.Status); }
public void GivenThatThereWasNoRefreshActionsBeforeForOption(RefreshOption option, string accountID) { var refreshRepo = objectContainer.Resolve <IRefreshRepository>(); var refreshes = refreshRepo.GetAll(accountID).Where(x => x.Option == option).ToList(); Assert.AreEqual(0, refreshes.Count); }
public RefreshResults GetRefreshStatus(RefreshOption option, DateTime currentTime) { var refreshFact = GetLastRefresh(option); if (refreshFact == null) { return new RefreshResults { Status = RefresStatus.Ready, SecondsLeft = 0 } } ; int secondesToAdd = GetDelayValue(option); var left = (int)(refreshFact.LastAction.AddSeconds(secondesToAdd) - currentTime).TotalSeconds; return(refreshFact.LastAction.AddSeconds(secondesToAdd) > currentTime ? new RefreshResults { Status = RefresStatus.NotReady, SecondsLeft = left } : new RefreshResults { Status = RefresStatus.Ready, SecondsLeft = 0 }); }
public RefreshFact GetLastRefresh(RefreshOption option) { var refreshFacts = _refreshRepository.GetAll(_accountManagement.GetLoggedAccount().ID).Where(x => x.Option == option); return(refreshFacts.OrderByDescending(x => x.LastAction).FirstOrDefault()); }
public int GetDelayValue(RefreshOption option) { var parameterName = $"Delay_for_option_{option}_in_sec"; return(Int32.Parse(_configRepository.GetParameterValue(parameterName))); }
public void AddRefreshFactForLoggedAccount(RefreshOption option, DateTime actionTime) { _refreshRepository.Add(new RefreshFact { ID = Guid.NewGuid().ToString(), Option = option, LastAction = actionTime }, _accountManagement.GetLoggedAccount().ID); }
public static RDResult GetReferenceData(string security_, IEnumerable<string> fields_, Dictionary<string, string> overrides_, RefreshOption option_) { RDResult ret = null; string path = string.Format(@"{0}\{1}.refCache",CACHE_PATH, security_); DateTime? lastWrite = null; { if (File.Exists(path)) { lastWrite = new FileInfo(path).LastWriteTime; FileStream file = null; try { file = new FileStream(path, FileMode.Open, FileAccess.Read); BinaryFormatter bf = new BinaryFormatter(); ret = bf.Deserialize(file) as RDResult; } finally { if (file != null) { file.Close(); file.Dispose(); file = null; } } } } bool needToMakeCall = false; if (ret != null) { foreach (string field in fields_) if (ret.FieldNames.Contains<string>(field) == false) { needToMakeCall = true; break; } } if (!needToMakeCall) { switch (option_) { case RefreshOption.AlwaysRefresh: needToMakeCall = true; break; case RefreshOption.RefreshIfOlderThanToday: if (!(lastWrite.HasValue && lastWrite.Value.Date == DateTime.Today)) needToMakeCall = true; break; case RefreshOption.RefreshIfOllderThan24Hours: if (!(lastWrite.HasValue && Math.Abs((lastWrite.Value.Date - DateTime.Now).TotalHours)<24d)) needToMakeCall = true; break; } } if (ret != null && !needToMakeCall) return ret; { lock (typeof(HistoryRequester)) { Request req = Core.Instance.SyncRefService.CreateRequest("ReferenceDataRequest"); Element securities = req.GetElement("securities"); Element fields = req.GetElement("fields"); Element requestOverrides = req.GetElement("overrides"); securities.AppendValue(security_); foreach (string field in fields_) fields.AppendValue(field); if (overrides_ != null && overrides_.Count > 0) { foreach (string key in overrides_.Keys) { Element ovr = requestOverrides.AppendElement(); ovr.SetElement("fieldId", key); ovr.SetElement("value", overrides_[key]); } } CorrelationID id = new CorrelationID(Core.NextID()); Core.Instance.SyncSession.SendRequest(req, id); while (true) { Event eventObj = Core.Instance.SyncSession.NextEvent(); if (eventObj.Type == Event.EventType.RESPONSE) { foreach (Message msg in eventObj.GetMessages()) { if (msg.HasElement("responseError")) { } else { Element secDataArray = msg.GetElement("securityData"); // we have only submitted one security so there should only be one element Element secData = secDataArray.GetValueAsElement(0); Element securityElement = secData.GetElement("security"); ret = new RDResult(securityElement.GetValueAsString()); Element fieldData = secData.GetElement("fieldData"); // return toplevel result is always a SEQUENCE of results, so assume and iterate over them... for (int j = 0; j < fieldData.NumElements; ++j) { Element e = fieldData.GetElement(j); RDFieldData rdData = null; string name = e.Name.ToString(); RDDataType dtype = (RDDataType)Enum.Parse(typeof(RDDataType), e.Datatype.ToString()); if (e.Datatype == Schema.Datatype.SEQUENCE) { if (e.NumValues > 0) { object[,] vals = new object[e.NumValues, e.GetValueAsElement(0).NumElements]; for (int y = 0; y < e.NumValues; ++y) { var rowElement = e.GetValueAsElement(y); for (int yy = 0; yy < rowElement.NumElements; ++yy) { var cellElement = rowElement.GetElement(yy); vals[y, yy] = getValueFromElement(cellElement); } } rdData = new RDFieldData(name, dtype, vals); } else rdData = null; } else { rdData = new RDFieldData(name, dtype, getValueFromElement(e)); } //else if (e.Datatype == Schema.Datatype.DATE || e.Datatype == Schema.Datatype.DATETIME) //{ // DateTime date = e.GetValueAsDatetime().ToSystemDateTime(); // rdData = new RDFieldData(name, dtype, date); //} //else if (e.Datatype == Schema.Datatype.STRING) //{ // string s = e.GetValueAsString(); // rdData = new RDFieldData(name, dtype, s); //} //else if (e.Datatype == Schema.Datatype.INT32) //{ // int v = e.GetValueAsInt32(); // rdData = new RDFieldData(name, dtype, v); //} //else if (e.Datatype == Schema.Datatype.FLOAT32) //{ // float f = e.GetValueAsFloat32(); // rdData = new RDFieldData(name, dtype, f); //} //else if (e.Datatype == Schema.Datatype.FLOAT64) //{ // double d = e.GetValueAsFloat64(); // rdData = new RDFieldData(name, dtype, d); //} //else //{ //} if (rdData != null) ret.Add(rdData); } } } break; // break out of listening loop to messages } } } } if (ret != null) { try { Stream a = File.OpenWrite(path); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(a, ret); a.Flush(); a.Close(); a.Dispose(); } catch { } } return ret; }
public static RDResult GetReferenceData(string security_, string field_, Dictionary<string, string> overrides_, RefreshOption option_) { return GetReferenceData(security_, new[] {field_}, overrides_, option_); }
public static DatedDataCollectionGen<double> GetHistory(DateTime startDate_, string security_, string field_, RefreshOption option_, Dictionary<string, string> overrides_ = null) { DatedDataCollectionGen<double> ret = null; lock (CACHE_PATH) { string key = string.Format("{0}_{1}_{2}", security_, field_, startDate_.ToString("ddMMMyyyy")); string path = string.Format(@"{0}\{1}", CACHE_PATH, key); if (File.Exists(path) && overrides_ == null) { var lastWriteTime = File.GetLastWriteTime(path).Date; bool doRead = true; switch (option_) { case RefreshOption.AlwaysRefresh: case RefreshOption.NeverRefreshIfAlreadyGot: doRead = false; break; case RefreshOption.RefreshIfOlderThanToday: doRead = lastWriteTime < DateTime.Today; break; case RefreshOption.RefreshIfOllderThan24Hours: doRead = lastWriteTime < DateTime.Now.AddDays(-1d); break; } if (!doRead) { File.Delete(path); } else { FileStream file = null; try { file = new FileStream(path, FileMode.Open, FileAccess.Read); BinaryFormatter bf = new BinaryFormatter(); ret = bf.Deserialize(file) as DatedDataCollectionGen<double>; } finally { if (file != null) { file.Close(); file.Dispose(); file = null; } } } } if (ret == null || ret.Length == 0) { lock (typeof(HistoryRequester)) { Request req = Core.Instance.SyncRefService.CreateRequest("HistoricalDataRequest"); req.GetElement("securities").AppendValue(security_); req.GetElement("fields").AppendValue(field_); req.Set("startDate", startDate_.ToString("yyMMdd")); // set overrides if (overrides_ != null) { Element overrides = req.GetElement("overrides"); foreach (string k in overrides_.Keys) { Element or = overrides.AppendElement(); or.SetElement("fieldId", k); or.SetElement("value", overrides_[k]); //req.Set(k, overrides_[k]); } } CorrelationID id = new CorrelationID(Core.NextID()); Core.Instance.SyncSession.SendRequest(req, id); while (true) { Event eventObj = Core.Instance.SyncSession.NextEvent(); if (eventObj.Type == Event.EventType.RESPONSE) { foreach (Message msg in eventObj.GetMessages()) { if (msg.HasElement("responseError")) { } else { Element secDataArray = msg.GetElement("securityData"); if (secDataArray.HasElement("securityError")) { } //else if (secDataArray.HasElement("fieldExceptions")) //{ // Element error = secDataArray.GetElement("fieldExceptions"); // for (int errorIndex = 0; errorIndex < error.NumValues; ++errorIndex) // { // Element errorException = error.GetValueAsElement(errorIndex); // string field = errorException.GetElementAsString("fieldId"); // Element errorInfo = errorException.GetElement("errorInfo"); // string message = errorInfo.GetElementAsString("message"); // Console.WriteLine(string.Format("{0} - {1}", field, message)); // } //} else { // process securities data for (int i = 0; i < secDataArray.NumValues; ++i) { foreach (Element secData in secDataArray.Elements) { if (secData.Name.Equals("fieldData")) { double[] vals = new double[secData.NumValues]; System.DateTime[] dates = new System.DateTime[secData.NumValues]; for (int j = 0; j < secData.NumValues; ++j) { Element o = secData.GetValueAsElement(j); dates[j] = o.GetElementAsDate("date").ToSystemDateTime(); vals[j] = Convert.ToDouble(o.GetElementAsFloat64(field_)); } ret = new DatedDataCollectionGen<double>(dates, vals); } } } } } } break; } } } if (ret != null) { try { Stream a = File.OpenWrite(path); BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(a, ret); a.Flush(); a.Close(); a.Dispose(); } catch { } } } } return ret; }
public void WhenPlayerWillUseRefreshForOptionAt(RefreshOption option, DateTime actionTime) { var refreshingMechnism = objectContainer.Resolve <IRefreshingMechnism>(); refreshingMechnism.AddRefreshFactForLoggedAccount(option, actionTime); }
public void WhenMechanizmWillSetRefreshToForOption(RefresStatus expectedStatus, RefreshOption option) { var refreshingMechnism = objectContainer.Resolve <IRefreshingMechnism>(); var actualStatus = refreshingMechnism.GetRefreshStatus(option, _currentTime); Assert.AreEqual(expectedStatus, actualStatus.Status); }
public void GivenRefreshTimeForOptionIsSetToSeconds(RefreshOption option, string delayInSeconds) { var configRepo = objectContainer.Resolve <IConfigRepository>(); configRepo.SetConfigParameter($"Delay_for_option_{option}_in_sec", delayInSeconds); }