Example #1
0
        private static async Task <Get_League_Summary_Definition_Return> ProcessLeagueSummaryInformationProjection(
            string projectionName,
            string leagueName,
            ILogger log)
        {
            try
            {
                Projection leagueEvents = new Projection("Leagues",
                                                         "League",
                                                         leagueName,
                                                         projectionName);

                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()))
                        {
                            return(new Get_League_Summary_Definition_Return(Guid.Empty, leagueName)
                            {
                                Date_Incorporated = prjLeagueInfo.Date_Incorporated,
                                Location = prjLeagueInfo.Location,
                                Twitter_Handle = prjLeagueInfo.Twitter_Handle
                            });
                        }
                    }
                }
                else
                {
                    #region Logging
                    if (null != log)
                    {
                        log.LogError("Unable to create league events projection");
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                #region Logging
                if (null != log)
                {
                    log.LogError($"Unable to perform projection {ex.Message}");
                }
                #endregion
            }

            return(null);
        }
        /// <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);
        }
        private static async Task <ProjectionResultsRecord <Get_League_Summary_Definition_Return> > ProcessLeagueSummaryInformationProjection(
            string projectionName,
            string leagueName,
            ILogger log)
        {
            try
            {
                Projection leagueEvents = new Projection("Leagues",
                                                         "League",
                                                         leagueName,
                                                         projectionName);

                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()))
                        {
                            Get_League_Summary_Definition_Return value = new Get_League_Summary_Definition_Return(Guid.Empty, leagueName)
                            {
                                Date_Incorporated = prjLeagueInfo.Date_Incorporated,
                                Location          = prjLeagueInfo.Location,
                                Twitter_Handle    = prjLeagueInfo.Twitter_Handle
                            };

                            return(new ProjectionResultsRecord <Get_League_Summary_Definition_Return>()
                            {
                                CurrentAsOfDate = prjLeagueInfo.CurrentAsOfDate,
                                CurrentSequenceNumber = (int)prjLeagueInfo.CurrentSequenceNumber,
                                AggregateTypeName = leagueEvents.AggregateTypeName,
                                DomainName = leagueEvents.DomainName,
                                EntityUniqueIdentifier = leagueEvents.AggregateInstanceKey,
                                Result = value
                            });
                        }
                    }
                }
                else
                {
                    #region Logging
                    if (null != log)
                    {
                        log.LogError("Unable to create league events projection");
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                #region Logging
                if (null != log)
                {
                    log.LogError($"Unable to perform projection {ex.Message}");
                }
                #endregion
            }

            return(null);
        }
Example #4
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);
        }
        /// <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(@"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)
                                    {
                                        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)
                                        {
                                            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);
        }