Exemplo n.º 1
0
        public void AddResponseDocument(string requestId, string filePath)
        {
            if (!_responseDocuments.Any(d => d.FileInfo != null && string.Equals(d.FileInfo.FullName, filePath, StringComparison.OrdinalIgnoreCase)))
            {
                log.Debug("Add response document: " + filePath);

                FileInfo file = new FileInfo(filePath);
                Guid     id   = QueryComposerModelProcessor.NewGuid();

                Document document = new Document(id.ToString("D"), GetMimeType(filePath), file.Name);
                document.Size = Convert.ToInt32(file.Length);

                _responseDocuments.Add(new DocumentEx {
                    ID = id, Document = document, FileInfo = file
                });
            }

            if (status.Code == RequestStatus.StatusCode.Pending)
            {
                status.Code = RequestStatus.StatusCode.AwaitingResponseApproval;
            }
        }
Exemplo n.º 2
0
 public DocumentEx()
 {
     ID = QueryComposerModelProcessor.NewGuid();
 }
Exemplo n.º 3
0
        public void Start(string requestId, bool viewSQL = false)
        {
            if (_request == null && !IsDistributedRegressionRequest)
            {
                throw new NullReferenceException("The deserialized request is null, please make sure that RequestDocument is called first and that the request document is not null.");
            }

            var queryResults        = new List <DTO.QueryComposer.QueryComposerResponseQueryResultDTO>();
            var queryCanPostProcess = new List <Tuple <Guid, bool, string> >();

            _request.SyncHeaders();

            foreach (var query in _request.Queries)
            {
                using (QueryComposer.IModelAdapter adapter = GetModelAdapter(query))
                {
                    adapter.Initialize(Settings, requestId);

                    if (viewSQL && !adapter.CanViewSQL)
                    {
                        queryResults.Add(
                            new DTO.QueryComposer.QueryComposerResponseQueryResultDTO {
                            ID         = query.Header.ID,
                            QueryStart = DateTimeOffset.UtcNow,
                            QueryEnd   = DateTimeOffset.UtcNow,
                            Errors     = new[] {
                                new DTO.QueryComposer.QueryComposerResponseErrorDTO {
                                    QueryID     = query.Header.ID,
                                    Code        = "-1",
                                    Description = $"The adapter \"{ adapter.GetType().FullName }\" does not support providing the SQL of a query."
                                }
                            }
                        }
                            );

                        queryCanPostProcess.Add(new Tuple <Guid, bool, string>(query.Header.ID, false, string.Empty));

                        continue;
                    }

                    if (IsDistributedRegressionRequest)
                    {
                        var drAdapter   = adapter as QueryComposer.Adapters.DistributedRegression.DistributedRegressionModelAdapter;
                        var queryResult = new DTO.QueryComposer.QueryComposerResponseQueryResultDTO {
                            ID = query.Header.ID, QueryStart = DateTimeOffset.UtcNow
                        };

                        try
                        {
                            DocumentEx[] outputDocuments = drAdapter.StartRequest(_drDocuments).ToArray();
                            if (outputDocuments != null && outputDocuments.Length > 0)
                            {
                                _responseDocuments.AddRange(outputDocuments);
                            }
                        }
                        catch (Exception ex)
                        {
                            queryResult.Errors = new[] {
                                new DTO.QueryComposer.QueryComposerResponseErrorDTO {
                                    QueryID     = query.Header.ID,
                                    Code        = "-1",
                                    Description = QueryComposer.ExceptionHelpers.UnwindException(ex)
                                }
                            };
                        }

                        queryResult.QueryEnd = DateTimeOffset.UtcNow;

                        queryResults.Add(queryResult);
                        queryCanPostProcess.Add(new Tuple <Guid, bool, string>(query.Header.ID, false, string.Empty));

                        continue;
                    }

                    DateTimeOffset queryStart = DateTimeOffset.UtcNow;
                    try
                    {
                        foreach (var result in adapter.Execute(query, viewSQL))
                        {
                            queryResults.Add(result);
                            if (adapter.CanPostProcess(result, out string postProcessMessage))
                            {
                                queryCanPostProcess.Add(new Tuple <Guid, bool, string>(query.Header.ID, true, postProcessMessage));
                            }
                            else
                            {
                                queryCanPostProcess.Add(new Tuple <Guid, bool, string>(query.Header.ID, false, string.Empty));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        queryResults.Add(
                            new DTO.QueryComposer.QueryComposerResponseQueryResultDTO {
                            ID         = query.Header.ID,
                            QueryStart = queryStart,
                            QueryEnd   = DateTimeOffset.UtcNow,
                            Errors     = new[] {
                                new DTO.QueryComposer.QueryComposerResponseErrorDTO {
                                    QueryID     = query.Header.ID,
                                    Code        = "-1",
                                    Description = QueryComposer.ExceptionHelpers.UnwindException(ex)
                                }
                            }
                        }
                            );
                        queryCanPostProcess.Add(new Tuple <Guid, bool, string>(query.Header.ID, false, string.Empty));
                    }
                } //end of adapter using
            }     //end of processing each query


            var response = new DTO.QueryComposer.QueryComposerResponseDTO {
                Header = new DTO.QueryComposer.QueryComposerResponseHeaderDTO {
                    DocumentID = viewSQL ? SqlResponseDocumentID : QueryComposerModelProcessor.NewGuid(),
                    RequestID  = _request.Header.ID
                },
                Queries = queryResults
            };

            response.RefreshQueryDates();
            response.RefreshErrors();

            string serializedResponse = Newtonsoft.Json.JsonConvert.SerializeObject(response, Newtonsoft.Json.Formatting.None);

            byte[] resultContent = System.Text.Encoding.UTF8.GetBytes(serializedResponse);

            Document resultDocument = new Document(response.Header.DocumentID.Value.ToString("D"), "application/json", "response.json");

            resultDocument.Size       = resultContent.Length;
            resultDocument.IsViewable = true;
            resultDocument.Kind       = "MultiQuery.JSON";

            if (viewSQL)
            {
                _SqlResponseDocument = new DocumentEx {
                    ID = SqlResponseDocumentID, Document = resultDocument, Content = resultContent
                };
            }
            else
            {
                _SqlResponseDocument = null;
                _responseDocuments.Clear();
                _responseDocuments.Add(new DocumentEx {
                    ID = response.Header.DocumentID.Value, Document = resultDocument, Content = resultContent
                });
                _currentResponse = response;
            }

            status.PostProcess = queryCanPostProcess.Any(q => q.Item2 == true);
            status.Message     = queryCanPostProcess.Where(q => !string.IsNullOrEmpty(q.Item3)).Any() ? string.Join("\r\n", queryCanPostProcess.Where(q => !string.IsNullOrEmpty(q.Item3)).Select(q => q.Item3).ToArray()) : string.Empty;
            status.Code        = (response.Errors != null && response.Errors.Any()) ? RequestStatus.StatusCode.Error : (string.IsNullOrEmpty(status.Message) ? RequestStatus.StatusCode.Complete : RequestStatus.StatusCode.CompleteWithMessage);
        }