private static async Task <Query_Summary_Projection_Return> ProcessQueryStatusInformationProjection(
            string queryName,
            string queryId,
            ILogger log)
        {
            Query_Summary_Projection_Return ret = null;
            Guid queryGuid;

            // use custom assembly resolve handler
            using (new AzureFunctionsResolveAssembly())
            {
                if (Guid.TryParse(queryId, out queryGuid))
                {
                    #region Logging
                    if (null != log)
                    {
                        log.LogDebug($"Getting details of query {queryName} - {queryId} in ProcessQueryStatusInformationProjection");
                    }
                    #endregion

                    // Get the current state of the command...
                    Projection getQueryState = new Projection(Constants.Domain_Query,
                                                              queryName,
                                                              queryGuid.ToString(),
                                                              nameof(Query_Summary_Projection));

                    if (null != getQueryState)
                    {
                        #region Logging
                        if (null != log)
                        {
                            log.LogDebug($"Projection processor created in ProcessCommandStatusInformationProjection");
                        }
                        #endregion

                        Query_Summary_Projection qryProjection =
                            new Query_Summary_Projection(log);

                        await getQueryState.Process(qryProjection);

                        if ((qryProjection.CurrentSequenceNumber > 0) || (qryProjection.ProjectionValuesChanged()))
                        {
                            ret = new Query_Summary_Projection_Return()
                            {
                                AsOfDate              = qryProjection.CurrentAsOfDate,
                                AsOfStepNumber        = (int)qryProjection.CurrentSequenceNumber,
                                Status                = qryProjection.CurrentState.ToString(),
                                QueryName             = queryName,
                                CorrelationIdentifier = queryId // for now the correlation id is the query id.. this may change
                            };
                        }
                    }
                }
            }

            return(ret);
        }
        /// <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
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 5
0
        /// <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(Constants.Domain_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)
                                    {
                                        dynamic projectionResult = (qryProjectionState.ProcessedRequests[0].ReturnedValue);
                                        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
                                        }
                                    }


                                    // Call the outputs processing sub-orchestration


                                    // 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.WebhookTargets)
                                        {
                                            #region Logging
                                            if (null != log)
                                            {
                                                log.LogDebug($"Target : { location} - being sent by webhook in OutputResultsGetLeagueSummaryQuery");
                                            }
                                            #endregion

                                            if (null != projectionReturn)
                                            {
                                                var payloadAsJSON = new StringContent(JsonConvert.SerializeObject(projectionReturn));
                                                using (var client = new HttpClient())
                                                {
                                                    var response = await client.PostAsync(location, payloadAsJSON);

                                                    if (!response.IsSuccessStatusCode)
                                                    {
                                                        ret.Message = $"Failed to send output to {location} webhook - {response.StatusCode} : {response.ReasonPhrase} ";
                                                        #region Logging
                                                        if (null != log)
                                                        {
                                                            log.LogError($"{ret.FunctionName } : {ret.Message}");
                                                        }
                                                        #endregion
                                                    }
                                                }
                                            }
                                        }

                                        foreach (string location in qryOutputs.EventGridTargets)
                                        {
                                            #region Logging
                                            if (null != log)
                                            {
                                                log.LogDebug($"Target : { location} - being sent by event grid message in OutputResultsGetLeagueSummaryQuery");
                                            }
                                            #endregion
                                        }

                                        foreach (string location in qryOutputs.BlobTargets)
                                        {
                                            #region Logging
                                            if (null != log)
                                            {
                                                log.LogDebug($"Target : { location} - being persisted as a blob in OutputResultsGetLeagueSummaryQuery");
                                            }
                                            #endregion
                                        }

                                        foreach (string location in qryOutputs.DurableFunctionOrchestrationTargets)
                                        {
                                            #region Logging
                                            if (null != log)
                                            {
                                                log.LogDebug($"Target : { location} - being used to trigger a durable function to wake up in OutputResultsGetLeagueSummaryQuery");
                                            }
                                            #endregion
                                        }

                                        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);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Get the league summary results as they currently are in this query
        /// </summary>
        /// <returns></returns>
        private static async Task <Get_League_Summary_Definition_Return> GetLeagueSummaryGetResults(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(Constants.Domain_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
                            return(null);
                        }

                        // 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 ret = new Get_League_Summary_Definition_Return(queryGuid,
                                                                                                                        qryProjectionState.ProcessedRequests[0].AggregateInstanceKey);

                                    if (qryProjectionState.ProcessedRequests[0].ProjectionTypeName == typeof(Leagues.League.projection.League_Summary_Information).Name)
                                    {
                                        dynamic projectionResult = (qryProjectionState.ProcessedRequests[0].ReturnedValue);
                                        if (null != projectionResult)
                                        {
                                            ret.Location          = projectionResult.Location;
                                            ret.Date_Incorporated = projectionResult.Date_Incorporated;
                                            ret.Twitter_Handle    = projectionResult.Twitter_Handle;
                                            return(ret);
                                        }
                                        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
                                        }
                                    }
                                }
                                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
                                }
                            }
                            else
                            {
                                #region Logging
                                if (null != log)
                                {
                                    log.LogWarning($"Query {queryGuid} still has unprocessed projections so no output processed in OutputResultsGetLeagueSummaryQuery");
                                }
                                #endregion
                            }
                        }
                    }
                }
            }

            return(null);
        }
Exemplo n.º 7
0
        /// <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);
        }