示例#1
0
        public async Task <List <CatalogBO> > GetAsync()
        {
            var catalogList = await _queries.GetAsync();

            if (catalogList != null)
            {
                return(catalogList
                       .Select(rec => MapEntityToModel(rec))
                       .ToList());
            }

            return(null);
        }
示例#2
0
        public async Task <RuleTestResult> ExecuteRuleByEnvironmentIdAsync(Guid ruleId, DatabaseEnvironmentBO databaseEnvironment)
        {
            int?ruleDetailsDestinationId = null;
            var rule = await _ruleService.GetAsync(ruleId);

            var executionLogs = await _ruleExecutionLogQueries.GetByRuleIdAsync(ruleId);

            var connectionString = databaseEnvironment.GetConnectionString();

            var stopWatch = System.Diagnostics.Stopwatch.StartNew();

            RuleTestResult testResult = await ExecuteRuleAsync(rule, connectionString, databaseEnvironment.UserParams, databaseEnvironment.TimeoutInMinutes);

            var containerParent = await _collectionQueries.GetAsync(rule.ContainerId);

            if (containerParent.ParentContainerId != null)
            {
                var collectionParent = await _collectionQueries.GetAsync(containerParent.ParentContainerId.Value);

                ruleDetailsDestinationId = collectionParent.RuleDetailsDestinationId;
            }

            RuleExecutionLog ruleExecutionLog = new RuleExecutionLog()
            {
                Id                       = 0,
                Evaluation               = testResult.Evaluation,
                RuleId                   = ruleId,
                StatusId                 = (int)testResult.Status,
                Result                   = testResult.Result,
                Response                 = testResult.Evaluation ? "Ok" : testResult.ErrorMessage,
                DatabaseEnvironmentId    = databaseEnvironment.Id,
                ExecutedSql              = testResult.ExecutedSql,
                DiagnosticSql            = rule.DiagnosticSql,
                RuleDetailsDestinationId = ruleDetailsDestinationId
            };

            if (ruleExecutionLog.RuleDetailsDestinationId == null || ruleExecutionLog.RuleDetailsDestinationId.Value == 0)
            {
                ruleExecutionLog.RuleDetailsDestinationId = null;
            }

            testResult.LastExecuted          = executionLogs.Any() ? executionLogs.FirstOrDefault().ExecutionDate : (DateTime?)null;
            ruleExecutionLog.ExecutionTimeMs = stopWatch.ElapsedMilliseconds;
            ruleExecutionLog.Result          = testResult.Result;

            var newRuleExecutionLog = await _ruleExecutionLogCommands.AddAsync(ruleExecutionLog);

            testResult.TestResults = await _ruleService.GetTopResults(ruleId, databaseEnvironment.Id);

            if (!testResult.Evaluation)
            {
                testResult.DiagnosticSql = rule.DiagnosticSql;
            }

            try
            {
                //Validate if the rule is going to any queue table
                if (ruleDetailsDestinationId != null && ruleDetailsDestinationId.Value > 0)
                {
                    var existCatalog = await _catalogQueries.GetAsync(ruleDetailsDestinationId.Value);

                    if (existCatalog != null)
                    {
                        int maxNumberResults = databaseEnvironment.MaxNumberResults.Value;
                        if (rule.MaxNumberResults != null)
                        {
                            maxNumberResults = rule.MaxNumberResults.Value;
                        }

                        await InsertDiagnosticSqlIntoDetails(rule, newRuleExecutionLog, connectionString, databaseEnvironment.UserParams, existCatalog.Name, maxNumberResults);
                    }
                }
            }
            catch (Exception ex)
            {
                newRuleExecutionLog.Result   = -1;
                newRuleExecutionLog.Response = ex.Message;
                await _ruleExecutionLogCommands.UpdateAsync(newRuleExecutionLog);
            }

            return(testResult);
        }
        public async Task <ContainerBO> GetToCommunityAsync(Guid containerId)
        {
            Container container = await this._collectionQueries.GetAsync(containerId);

            var collectionCategory = new ContainerBO();

            collectionCategory.Id              = container.Id;
            collectionCategory.Name            = container.Name;
            collectionCategory.Description     = container.Description;
            collectionCategory.EnvironmentType = container.EnvironmentType;
            collectionCategory.ChildContainers = new List <ContainerBO>();
            collectionCategory.ContainerTypeId = container.ContainerTypeId;
            //collectionCategory.CreatedByUserId = container.CreatedByUserId;
            collectionCategory.Tags = new List <TagBO>();
            collectionCategory.Tags = (await _tagQueries.GetByContainerIdAsync(collectionCategory.Id)).Select(rec => new TagBO(rec)).ToList();
            collectionCategory.RuleDetailsDestinationId = container.RuleDetailsDestinationId;

            var environmentCatalog = await _catalogQueries.GetAsync(container.EnvironmentType);

            if (environmentCatalog != null)
            {
                collectionCategory.CatalogEnvironmentType = new CatalogBO
                {
                    CatalogType = environmentCatalog.CatalogType,
                    Description = environmentCatalog.Description,
                    Name        = environmentCatalog.Name
                }
            }
            ;

            if (collectionCategory.Tags.Any())
            {
                collectionCategory.Tags = collectionCategory.Tags.Where(rec => rec.IsPublic).ToList();
            }

            if (collectionCategory.RuleDetailsDestinationId != null)
            {
                var existCatalog = await _catalogQueries.GetAsync(collectionCategory.RuleDetailsDestinationId.Value);

                if (existCatalog != null)
                {
                    var listColumns = await _edFiRuleExecutionLogDetailQueries.GetColumnsByTableAsync(existCatalog.Name, "destination");

                    if (listColumns != null)
                    {
                        collectionCategory.ContainerDestination = new ContainerDestinationBO
                        {
                            CatalogId            = existCatalog.Id,
                            ContainerId          = collectionCategory.Id,
                            DestinationName      = existCatalog.Name,
                            DestinationStructure = JsonConvert.SerializeObject(listColumns)
                        };
                    }
                }
            }

            foreach (var itemContainer in container.ChildContainers)
            {
                var category = new ContainerBO
                {
                    Id                = itemContainer.Id,
                    Name              = itemContainer.Name,
                    Description       = itemContainer.Description,
                    ParentContainerId = itemContainer.ParentContainerId,
                    Rules             = new List <RuleBO>(),
                    Tags              = new List <TagBO>()
                };

                category.Tags  = (await _tagQueries.GetByContainerIdAsync(itemContainer.Id)).Select(rec => new TagBO(rec)).ToList();
                category.Rules = await _ruleService.GetByContainerIdAsync(itemContainer.Id);

                if (category.Tags.Any())
                {
                    category.Tags = category.Tags.Where(rec => rec.IsPublic).ToList();
                }

                if (category.Rules.Any())
                {
                    category.Rules.ForEach(rec =>
                    {
                        if (rec.Tags != null && rec.Tags.Any())
                        {
                            rec.Tags = rec.Tags.Where(recTags => recTags.IsPublic).ToList();
                        }
                    });
                }

                collectionCategory.ChildContainers.Add(category);
            }
            return(collectionCategory);
        }
示例#4
0
        public async Task <RuleExecutionLogDetailBO> GetByRuleExecutionLogIdAsync(int id)
        {
            RuleExecutionLogDetailBO result = new RuleExecutionLogDetailBO
            {
                Columns = new List <string>(),
                Rows    = new List <Dictionary <string, string> >()
            };

            var ruleExecutionLog = await _queriesRuleExecutionLog.GetAsync(id);

            if (ruleExecutionLog != null && ruleExecutionLog.RuleDetailsDestinationId != null)
            {
                var existRule = await _queriesRule.GetAsync(ruleExecutionLog.RuleId);

                var existDatabaseEnvironment = await _queriesDatabaseEnvironments.GetAsync(ruleExecutionLog.DatabaseEnvironmentId);

                result.RuleName          = existRule.Name;
                result.EnvironmentName   = existDatabaseEnvironment.Name;
                result.ExecutionDateTime = ruleExecutionLog.ExecutionDate.ToLocalTime().ToString("MM/dd/yyyy HH:mm");

                var catalog = await _queriesCatalog.GetAsync(ruleExecutionLog.RuleDetailsDestinationId.Value);

                if (catalog != null)
                {
                    result.DestinationTable  = catalog.Name;
                    result.RuleDiagnosticSql = ruleExecutionLog.DiagnosticSql;

                    Dictionary <string, string> columnsFromLog = new Dictionary <string, string>();
                    if (!string.IsNullOrEmpty(ruleExecutionLog.DetailsSchema))
                    {
                        columnsFromLog = JsonConvert.DeserializeObject <Dictionary <string, string> >(ruleExecutionLog.DetailsSchema);
                    }

                    var reader = await _queries.GetByRuleExecutionLogIdAsync(id, catalog.Name);

                    if (reader != null)
                    {
                        List <string> columnsToIgnore = new List <string>()
                        {
                            "id", "otherdetails", "ruleexecutionlogid"
                        };
                        List <string> columnsToExport = new List <string>();

                        foreach (DataColumn column in reader.Columns)
                        {
                            string columnName = column.ColumnName.ToLower();
                            if (!columnsToIgnore.Contains(columnName) && (columnsFromLog.Count == 0 || columnsFromLog.ContainsKey(columnName)))
                            {
                                columnsToExport.Add(columnName);
                            }
                        }

                        List <Dictionary <string, string> > rowsToExport = new List <Dictionary <string, string> >();

                        foreach (DataRow row in reader.Rows)
                        {
                            Dictionary <string, string> newRow = new Dictionary <string, string>();
                            foreach (var column in columnsToExport)
                            {
                                newRow.Add(column, row[column].ToString());
                            }

                            string otherDetails = row.Field <string>("otherdetails").ToString();
                            if (!string.IsNullOrEmpty(otherDetails))
                            {
                                Dictionary <string, string> jsonValues = JsonConvert.DeserializeObject <Dictionary <string, string> >(otherDetails);
                                foreach (var element in jsonValues)
                                {
                                    if (columnsToExport.Contains(element.Key) || columnsFromLog.ContainsKey(element.Key))
                                    {
                                        newRow.Add(element.Key, element.Value);
                                    }
                                }
                            }
                            rowsToExport.Add(newRow);
                        }

                        if (columnsFromLog.Count > 0)
                        {
                            columnsToExport = columnsFromLog.Select(rec => rec.Key).ToList();
                        }

                        result.RuleExecutionLogId = id;
                        result.Columns            = columnsToExport;
                        result.Rows = rowsToExport;
                    }
                }
            }

            return(result);
        }