public static async Task <Guid> GetLeagueSummaryCreateQueryRequestActivity( [ActivityTrigger] DurableActivityContext context, ILogger log) { #region Logging if (null != log) { log.LogInformation($"GetLeagueSummaryCreateQueryRequestActivity started - instance {context.InstanceId} "); } #endregion QueryRequest <Get_League_Summary_Definition> queryRequest = context.GetInput <QueryRequest <Get_League_Summary_Definition> >(); if (queryRequest.QueryUniqueIdentifier.Equals(Guid.Empty)) { queryRequest.QueryUniqueIdentifier = Guid.NewGuid(); } // Create a new Query record to hold the event stream for this query... QueryLogRecord <Get_League_Summary_Definition> qryRecord = QueryLogRecord <Get_League_Summary_Definition> .Create( queryRequest.QueryName, queryRequest.GetParameters(), queryRequest.ReturnTarget, queryRequest.ReturnPath, queryRequest.QueryUniqueIdentifier); if (null != qryRecord) { EventStream queryEvents = new EventStream(Constants.Domain_Query, queryRequest.QueryName, qryRecord.QueryUniqueIdentifier.ToString()); if (null != queryEvents) { await queryEvents.CreateIfNotExists(); // Set the context for the events to be written using queryEvents.SetContext(new WriteContext("GetLeagueSummaryCreateQueryRequestActivity", context.InstanceId)); // Log the query creation await queryEvents.AppendEvent(new TheLongRun.Common.Events.Query.QueryCreated(queryRequest.QueryName, qryRecord.QueryUniqueIdentifier)); } // Return the ID of the query record we created return(qryRecord.QueryUniqueIdentifier); } // If we got here there was a problem so return an empty GUID return(Guid.Empty); }
public static async Task <ActivityResponse> LogQueryProjectionInFlightActivity( [ActivityTrigger] DurableActivityContext context, ILogger log) { // TODO: Pass this as a parameter CQRSAzure.EventSourcing.IWriteContext writeContext = null; ActivityResponse resp = new ActivityResponse() { FunctionName = "LogQueryProjectionInFlightActivity" }; ProjectionRequest projectionRequest = context.GetInput <ProjectionRequest>(); if (null != projectionRequest) { await QueryLogRecord.LogProjectionStarted( projectionRequest.CorrelationIdentifier, projectionRequest.ParentRequestName, projectionRequest.ProjectionName, projectionRequest.DomainName, projectionRequest.AggregateTypeName, projectionRequest.AggregateInstanceUniqueIdentifier, projectionRequest.AsOfDate, projectionRequest.CorrelationIdentifier.ToString(), writeContext ); resp.Message = $"Started running projection query {projectionRequest.CorrelationIdentifier} : {projectionRequest}) "; } else { resp.Message = "Unable to get projection request from context"; resp.FatalError = true; } return(resp); }
public List <QueryLogRecord> GetQueryLog(DateTime _startDate, DateTime _endDate, string _userName) { List <QueryLogRecord> _ret = new List <QueryLogRecord>(); string _sql = "select ID,SJ,QUERY_STR,YHID,"; _sql += "(select yhm from qx2_yhxx yh where yh.yhid=t.YHID) YHM, "; _sql += "ZHTJ_ZZJG2.Get_YHXM(YHID) YHXM from QUERY_LOG t "; _sql += " where (SJ between :KSRQ and :JSRQ ) "; if (_userName.Trim().Length > 0) { _sql += string.Format(" and yhid in (select yhid from qx2_yhxx t where yhm = '{0}' or xm = '{1}') ", _userName, _userName); } _sql += " order by SJ DESC "; OracleParameter[] _param = { new OracleParameter(":KSRQ", OracleDbType.Date), new OracleParameter(":JSRQ", OracleDbType.Date) }; _param[0].Value = _startDate; _param[1].Value = _endDate; OracleDataReader dr = OracleHelper.ExecuteReader(OracleHelper.ConnectionStringProfile, CommandType.Text, _sql, _param); while (dr.Read()) { QueryLogRecord _mitem = new QueryLogRecord( dr.IsDBNull(0) ? "" : dr.GetDecimal(0).ToString(), dr.IsDBNull(1) ? DateTime.Now : dr.GetDateTime(1), dr.IsDBNull(4) ? "" : dr.GetString(4), dr.IsDBNull(2) ? "" : dr.GetString(2) ); _ret.Add(_mitem); } dr.Close(); return(_ret); }
public static async Task <ActivityResponse> LogQueryProjectionResultActivity( [ActivityTrigger] DurableActivityContext context, ILogger log ) { CQRSAzure.EventSourcing.IWriteContext writeContext = null; // TODO: Pass this as a parameter ActivityResponse resp = new ActivityResponse() { FunctionName = "LogQueryProjectionResultActivity" }; // get the ProjectionResultsRecord ProjectionResultsRecord <object> data = context.GetInput <ProjectionResultsRecord <object> >(); if (null != data) { await QueryLogRecord.LogProjectionResult(data.CorrelationIdentifier, data.ParentRequestName, data.ProjectionName, data.DomainName, data.AggregateTypeName, data.EntityUniqueIdentifier, data.CurrentAsOfDate, data.Result, data.CurrentSequenceNumber, writeContext); resp.Message = $"Saved projection result to query {data.ParentRequestName} - {data.CorrelationIdentifier} "; } else { resp.Message = "Unable to get projection result from context"; resp.FatalError = true; } return(resp); }
/// <summary> /// Send out the results for a completed "Get-League-Summary" query /// </summary> /// <param name="queryId"> /// Unique identifier of the query for which we want to send out the results /// </param> /// <param name="log"> /// Trace target for logging the outcomes of this operation /// </param> private static async Task <ActivityResponse> OutputResultsGetLeagueSummaryQuery( string queryName, string queryId, ILogger log) { ActivityResponse ret = new ActivityResponse() { FunctionName = "OutputResultsGetLeagueSummaryQuery" }; Guid queryGuid; if (Guid.TryParse(queryId, out queryGuid)) { // Get the current state of the query... Projection getQueryState = new Projection(@"Query", queryName, queryGuid.ToString(), nameof(Query_Summary_Projection)); if (null != getQueryState) { #region Logging if (null != log) { log.LogDebug($"Projection processor created in OutputResultsGetLeagueSummaryQuery"); } #endregion // Run the query summary projection Query_Summary_Projection qryProjection = new Query_Summary_Projection(log); await getQueryState.Process(qryProjection); if ((qryProjection.CurrentSequenceNumber > 0) || (qryProjection.ProjectionValuesChanged())) { // Process the query state as is now... #region Logging if (null != log) { log.LogDebug($"Query { qryProjection.QueryName } projection run for {queryGuid } in OutputResultsGetLeagueSummaryQuery"); } #endregion // Ignore queries in an invalid state or not yet validated... if (qryProjection.CurrentState == Query_Summary_Projection.QueryState.Invalid) { // No need to run projections on an invalid query #region Logging if (null != log) { log.LogWarning($"Query {queryGuid} state is {qryProjection.CurrentState} so no output processed in OutputResultsGetLeagueSummaryQuery"); } #endregion ret.Message = $"Query {queryGuid} state is {qryProjection.CurrentState} so no output processed in OutputResultsGetLeagueSummaryQuery"; ret.FatalError = true; return(ret); } // Check all the projections have been run.. Query_Projections_Projection qryProjectionState = new Query_Projections_Projection(log); await getQueryState.Process(qryProjectionState); if ((qryProjectionState.CurrentSequenceNumber > 0) || (qryProjectionState.ProjectionValuesChanged())) { if (qryProjectionState.UnprocessedRequests.Count == 0) { if (qryProjectionState.ProcessedRequests.Count > 0) { // Turn the projections into a query return (This could include a collate step) Get_League_Summary_Definition_Return projectionReturn = new Get_League_Summary_Definition_Return(queryGuid, qryProjectionState.ProcessedRequests[0].AggregateInstanceKey); if (qryProjectionState.ProcessedRequests[0].ProjectionTypeName == typeof(Leagues.League.projection.League_Summary_Information).Name) { Leagues.League.projection.League_Summary_Information projectionResult = ((Newtonsoft.Json.Linq.JObject)qryProjectionState.ProcessedRequests[0].ReturnedValue).ToObject <Leagues.League.projection.League_Summary_Information>(); if (null != projectionResult) { projectionReturn.Location = projectionResult.Location; projectionReturn.Date_Incorporated = projectionResult.Date_Incorporated; projectionReturn.Twitter_Handle = projectionResult.Twitter_Handle; } else { #region Logging if (null != log) { log.LogError($"Unable to convert {qryProjectionState.ProcessedRequests[0].ReturnedValue} to {nameof(Leagues.League.projection.League_Summary_Information)} in OutputResultsGetLeagueSummaryQuery"); } #endregion } } // Get all the output targets Query_Outputs_Projection qryOutputs = new Query_Outputs_Projection(log); await getQueryState.Process(qryOutputs); if ((qryOutputs.CurrentSequenceNumber > 0) || (qryOutputs.ProjectionValuesChanged())) { #region Logging if (null != log) { log.LogDebug($"Sending results to output targets in OutputResultsGetLeagueSummaryQuery"); } #endregion foreach (string location in qryOutputs.Targets.Keys) { #region Logging if (null != log) { log.LogDebug($"Target : { location} - type {qryOutputs.Targets[location]} in OutputResultsGetLeagueSummaryQuery"); } #endregion // Send the output to the location... QueryLogRecord.SendOutput(location, qryOutputs.Targets[location], ret); } ret.Message = $"Sent results to output targets ({qryOutputs.Targets.Keys.Count}) in OutputResultsGetLeagueSummaryQuery"; return(ret); } else { // No outputs set #region Logging if (null != log) { log.LogWarning($"No output targets found in OutputResultsGetLeagueSummaryQuery"); } #endregion ret.Message = $"No output targets found in OutputResultsGetLeagueSummaryQuery"; return(ret); } } else { // No processed projections found #region Logging if (null != log) { log.LogWarning($"Query {queryGuid} state is has no processed projections so no output processed in OutputResultsGetLeagueSummaryQuery"); } #endregion ret.Message = $"Query {queryGuid} state is has no processed projections so no output processed in OutputResultsGetLeagueSummaryQuery"; return(ret); } } else { #region Logging if (null != log) { log.LogWarning($"Query {queryGuid} still has unprocessed projections so no output processed in OutputResultsGetLeagueSummaryQuery"); } #endregion ret.Message = $"Query {queryGuid} still has unprocessed projections so no output processed in OutputResultsGetLeagueSummaryQuery"; return(ret); } } else { ret.Message = $"Projection run but has no results"; ret.FatalError = true; return(ret); } } } } ret.Message = $"Query identifier blank or not set when running OutputResultsGetLeagueSummaryQuery"; ret.FatalError = true; return(ret); }
/// <summary> /// Perform the validation on the parameters of this "Get League Summary" query and, if all OK, mark it /// as allowed to continue /// </summary> /// <param name="queryId"> /// Unique identifier of the query event stream for this "Get League Summary" query /// </param> /// <param name="log"> /// If set, output progress to this trace writer /// </param> private static async Task <bool> ValidateGetLeagueSummaryQuery(string queryId, ILogger log) { const string QUERY_NAME = @"get-league-summary"; Guid queryGuid; if (Guid.TryParse(queryId, out queryGuid)) { // Get the current state of the query... Projection getQueryState = new Projection(@"Query", QUERY_NAME, queryGuid.ToString(), nameof(Query_Summary_Projection)); if (null != getQueryState) { #region Logging if (null != log) { log.LogDebug($"Projection processor created in ValidateGetLeagueSummaryQuery"); } #endregion // Run the query summary projection Query_Summary_Projection qryProjection = new Query_Summary_Projection(log); await getQueryState.Process(qryProjection); if ((qryProjection.CurrentSequenceNumber > 0) || (qryProjection.ProjectionValuesChanged())) { // Process the query state as is now... #region Logging if (null != log) { log.LogDebug($"Query { qryProjection.QueryName } projection run for {queryGuid} in ValidateGetLeagueSummaryQuery"); } #endregion if (qryProjection.CurrentState == Query_Summary_Projection.QueryState.Completed) { // No need to validate a completed query #region Logging if (null != log) { log.LogWarning($"Query {queryGuid} is complete so no need to validate in ValidateGetLeagueSummaryQuery"); } #endregion return(true); } if (qryProjection.CurrentState == Query_Summary_Projection.QueryState.Invalid) { // No need to validate an already invalid query #region Logging if (null != log) { log.LogWarning($"Query {queryGuid} is already marked as invalid so no need to validate in ValidateGetLeagueSummaryQuery"); } #endregion return(false); } if (qryProjection.CurrentState == Query_Summary_Projection.QueryState.Validated) { // No need to validate an already validated query #region Logging if (null != log) { log.LogWarning($"Query {queryGuid} is already validated so no need to validate in ValidateGetLeagueSummaryQuery"); } #endregion return(true); } // Validations - 1: Check league name is not empty if (qryProjection.ParameterIsSet(nameof(Get_League_Summary_Definition.League_Name))) { string leagueNameParam = qryProjection.GetParameter <string>(nameof(Get_League_Summary_Definition.League_Name)); if (string.IsNullOrWhiteSpace(leagueNameParam)) { await QueryLogRecord.LogQueryValidationError(queryGuid, QUERY_NAME, true, "League name may not be blank"); #region Logging if (null != log) { log.LogWarning($"Query {QUERY_NAME} :: {queryGuid} has a blank league name in ValidateGetLeagueSummaryQuery"); } #endregion return(false); } else { // any additional validation could go here (?).. return(true); } } else { // Parameter is mandatory but may not be set yet so leave the query as is #region Logging if (null != log) { log.LogWarning($"Query { qryProjection.QueryName } has no value specified for the parameter {nameof(Get_League_Summary_Definition.League_Name)} in ValidateGetLeagueSummaryQuery"); } #endregion return(false); } } } } return(false); }
/// <summary> /// Add projection requests for the projections that need to be run in order to get the data /// for this Get League Summary query /// </summary> /// <param name="queryId"> /// Unique identifier of the query /// </param> /// <param name="log"> /// Optional tracing output /// </param> private static async Task RequestProjectionsGetLeagueSummaryQuery( string queryName, string queryId, ILogger log) { Guid queryGuid; if (Guid.TryParse(queryId, out queryGuid)) { // Get the current state of the query... Projection getQueryState = new Projection(@"Query", queryName, queryGuid.ToString(), nameof(Query_Summary_Projection)); if (null != getQueryState) { #region Logging if (null != log) { log.LogInformation($"Projection processor created to get query state from RequestProjectionsGetLeagueSummaryQuery"); } #endregion // Run the query summary projection Query_Summary_Projection qryProjection = new Query_Summary_Projection(log); await getQueryState.Process(qryProjection); if ((qryProjection.CurrentSequenceNumber > 0) || (qryProjection.ProjectionValuesChanged())) { // Process the query state as is now... #region Logging if (null != log) { log.LogDebug($"Query { qryProjection.QueryName } projection run for {queryGuid } in RequestProjectionsGetLeagueSummaryQuery"); } #endregion // Ignore queries in an invalid state... if ((qryProjection.CurrentState == Query_Summary_Projection.QueryState.Completed) || (qryProjection.CurrentState == Query_Summary_Projection.QueryState.Invalid)) { // No need to validate a completed query #region Logging if (null != log) { log.LogWarning($"Query {queryGuid} state is {qryProjection.CurrentState} so no projections requested in RequestProjectionsGetLeagueSummaryQuery"); } #endregion return; } // Get the league parameter if (qryProjection.ParameterIsSet(nameof(Get_League_Summary_Definition.League_Name))) { string leagueNameParam = qryProjection.GetParameter <string>(nameof(Get_League_Summary_Definition.League_Name)); if (string.IsNullOrWhiteSpace(leagueNameParam)) { #region Logging if (null != log) { log.LogError($"Query {queryName } :: {queryGuid} has a blank league name in RequestProjectionsGetLeagueSummaryQuery"); } #endregion } else { // Find out what projections have been already requested Query_Projections_Projection qryProjectionsRequested = new Query_Projections_Projection(); await getQueryState.Process(qryProjectionsRequested); if ((qryProjectionsRequested.CurrentSequenceNumber > 0) || (qryProjectionsRequested.ProjectionValuesChanged())) { // Process the query state as is now... if ((qryProjectionsRequested.UnprocessedRequests.Count == 0) && (qryProjectionsRequested.ProcessedRequests.Count == 0)) { // No projections have been added to this so add the "get league summary" request await QueryLogRecord.RequestProjection(queryGuid, qryProjection.QueryName, nameof(Leagues.League.projection.League_Summary_Information), "Leagues", "League", leagueNameParam, null); } else { if (qryProjectionsRequested.UnprocessedRequests.Count > 0) { #region Logging if (null != log) { log.LogWarning($"Query {queryName} projection in progress for {queryGuid } in RequestProjectionsGetLeagueSummaryQuery"); } #endregion } if (qryProjectionsRequested.ProcessedRequests.Count > 0) { #region Logging if (null != log) { log.LogWarning($"Query {queryName} projection already processed for {queryGuid } in RequestProjectionsGetLeagueSummaryQuery"); } #endregion } } } } } } } } }
/// <summary> /// Take an un-processed projection request from the Get League Summary query and process it /// </summary> /// <param name="queryId"> /// The unique identifier of the query for which to process a projection /// </param> /// <param name="log"> /// The trace output to log to (if set) /// </param> private static async Task <ActivityResponse> ProcessProjectionsGetLeagueSummaryQuery( string queryName, string queryId, ILogger log) { ActivityResponse ret = new ActivityResponse() { FunctionName = "ProcessProjectionsGetLeagueSummaryQuery" }; Guid queryGuid; try { if (Guid.TryParse(queryId, out queryGuid)) { // Get the current state of the query... Projection getQueryState = new Projection(@"Query", queryName, queryId, nameof(Query_Summary_Projection)); if (null != getQueryState) { #region Logging if (null != log) { log.LogDebug($"Projection processor created in RequestProjectionsGetLeagueSummaryQuery"); } #endregion // Run the query summary projection Query_Summary_Projection qryProjection = new Query_Summary_Projection(log); await getQueryState.Process(qryProjection); if ((qryProjection.CurrentSequenceNumber > 0) || (qryProjection.ProjectionValuesChanged())) { // Process the query state as is now... #region Logging if (null != log) { log.LogDebug($"Query { qryProjection.QueryName } projection run for {queryGuid } in RequestProjectionsGetLeagueSummaryQuery"); } #endregion // Ignore queries in an invalid state... if ((qryProjection.CurrentState == Query_Summary_Projection.QueryState.Completed) || (qryProjection.CurrentState == Query_Summary_Projection.QueryState.Invalid)) { // No need to validate a completed query #region Logging if (null != log) { log.LogWarning($"Query {queryGuid} state is {qryProjection.CurrentState} so no projections requested in RequestProjectionsGetLeagueSummaryQuery"); } #endregion return(ret); } // Find out what projections have been already requested Query_Projections_Projection qryProjectionsRequested = new Query_Projections_Projection(); await getQueryState.Process(qryProjectionsRequested); if ((qryProjectionsRequested.CurrentSequenceNumber > 0) || (qryProjectionsRequested.ProjectionValuesChanged())) { // Process the query state as is now... if ((qryProjectionsRequested.UnprocessedRequests.Count == 1) && (qryProjectionsRequested.ProcessedRequests.Count == 0)) { // Run the requested projection var nextProjectionRequest = qryProjectionsRequested.UnprocessedRequests[0]; if (null != nextProjectionRequest) { #region Logging if (null != log) { log.LogDebug($"Query {queryName} running projection {nextProjectionRequest.ProjectionTypeName } for {queryGuid } in ProcessProjectionsGetLeagueSummaryQuery"); } #endregion ret.Message = $"Query {queryName} running projection {nextProjectionRequest.ProjectionTypeName } for {queryGuid } in ProcessProjectionsGetLeagueSummaryQuery"; if (nextProjectionRequest.ProjectionTypeName == typeof(Leagues.League.projection.League_Summary_Information).Name) { // run the League_Summary_Information projection.. Projection leagueEvents = new Projection(nextProjectionRequest.DomainName, nextProjectionRequest.AggregateType, nextProjectionRequest.AggregateInstanceKey, typeof(Leagues.League.projection.League_Summary_Information).Name); if (null != leagueEvents) { Leagues.League.projection.League_Summary_Information prjLeagueInfo = new Leagues.League.projection.League_Summary_Information(); await leagueEvents.Process(prjLeagueInfo); if (null != prjLeagueInfo) { if ((prjLeagueInfo.CurrentSequenceNumber > 0) || (prjLeagueInfo.ProjectionValuesChanged())) { // append the projection result to the query await QueryLogRecord.LogProjectionResult(queryGuid, qryProjection.QueryName, nameof(Leagues.League.projection.League_Summary_Information), leagueEvents.DomainName, leagueEvents.AggregateTypeName, leagueEvents.AggregateInstanceKey, prjLeagueInfo.CurrentAsOfDate, prjLeagueInfo, prjLeagueInfo.CurrentSequenceNumber); #region Logging if (null != log) { log.LogDebug($"Query {queryName } projection {nextProjectionRequest.ProjectionTypeName } key {nextProjectionRequest.AggregateInstanceKey } run to sequence number {prjLeagueInfo.CurrentSequenceNumber } in ProcessProjectionsGetLeagueSummaryQuery"); } #endregion ret.Message = $"Query {queryName } projection {nextProjectionRequest.ProjectionTypeName } key {nextProjectionRequest.AggregateInstanceKey } run to sequence number {prjLeagueInfo.CurrentSequenceNumber } "; return(ret); } else { #region Logging if (null != log) { log.LogWarning($"Query {queryName } running projection {nextProjectionRequest.ProjectionTypeName } for {queryGuid } returned no data in ProcessProjectionsGetLeagueSummaryQuery"); } #endregion ret.Message = $"Query { queryName} unable to create event stream for projection { nextProjectionRequest.ProjectionTypeName} returned no data in ProcessProjectionsGetLeagueSummaryQuery"; return(ret); } } } else { #region Logging if (null != log) { log.LogError($"Query {queryName} unable to create event stream for projection {nextProjectionRequest.ProjectionTypeName } key {nextProjectionRequest.AggregateInstanceKey } in ProcessProjectionsGetLeagueSummaryQuery"); } #endregion ret.Message = $"Query { queryName} unable to create event stream for projection { nextProjectionRequest.ProjectionTypeName} key { nextProjectionRequest.AggregateInstanceKey}"; return(ret); } } else { return(ret); } } } else { if (qryProjectionsRequested.UnprocessedRequests.Count == 0) { #region Logging if (null != log) { log.LogWarning($"Query {queryName } projection not yet requested for {queryGuid } in ProcessProjectionsGetLeagueSummaryQuery"); } #endregion ret.Message = $"Query {queryName } projection not yet requested for {queryGuid }"; return(ret); } if (qryProjectionsRequested.ProcessedRequests.Count == 1) { #region Logging if (null != log) { log.LogWarning($"Query {queryName } projection already processed for {queryGuid } in ProcessProjectionsGetLeagueSummaryQuery"); } #endregion ret.Message = $"Query {queryName } projection already processed for {queryGuid }"; return(ret); } } } } } } else { #region Logging if (null != log) { log.LogError($"Projection processor not passed a correct query identifier : {queryId} for query {queryName} "); } #endregion } } catch (Exception ex) { ret.Message = ex.ToString(); ret.FatalError = true; } return(ret); }