public async Task <ExecutionChartResponseModel> Execute(ExecutionChartReportModel model)
        {
            var result = new ExecutionChartResponseModel();

            using (var mysqlDbConnection = new MySqlConnection(model.DatabaseConnection.ConnectionString))
            {
                mysqlDbConnection.Open();
                using (var command = new MySqlCommand(model.FormattedString, mysqlDbConnection))
                {
                    _chartReportQueryBuilder
                    .Init(model.FormattedString, model.MappingProjection, options =>
                    {
                        options.FieldFormat    = "`{0}`";
                        options.AllowBoolIsInt = true;
                        options.DateCompare    = "date({0}){1}date({2})";
                        options.MonthCompare   = "month({0}){1}month({2})";
                        options.YearCompare    = "year({0}){1}year({2})";
                    })
                    .AddFilters(model.FilterValues)
                    .AddParameters(model.Parameters)
                    .AddMapper((a, b) =>
                    {
                        return(_cSharpMapper.GetCSharpObjectByType(a, b));
                    });
                    if (model.IsRealTime && model.LastComparedDate.HasValue && !string.IsNullOrEmpty(model.ComparedRealTimeField))
                    {
                        _chartReportQueryBuilder.AddRealTime(model.ComparedRealTimeField, model.LastComparedDate.Value, DateTime.UtcNow);
                    }

                    var chartQuery = _chartReportQueryBuilder.Build();
                    command.CommandText = chartQuery.CombinedQuery;

                    if (chartQuery.DbParameters.Count > 0)
                    {
                        command.Parameters.AddRange(ConvertToParameters(chartQuery.DbParameters).ToArray());
                    }
                    using (var reader = await command.ExecuteReaderAsync())
                    {
                        using (var dt = new DataTable())
                        {
                            dt.Load(reader);
                            result.IsSuccess = true;
                            if (dt.Rows.Count > 0)
                            {
                                result.Result = await _chartReportProjection.ProjectionFromDataTable(dt, model.MappingProjection);

                                result.IsSuccess = true;
                            }
                            else
                            {
                                result.IsSuccess = false;
                            }
                        }
                    }
                }
            }

            return(result);
        }
        public async Task <ExecutionChartResponseModel> Execute(ExecutionChartReportModel model)
        {
            var mongoDatabase = new MongoClient(model.DatabaseConnection.ConnectionString).GetDatabase(model.DatabaseConnection.DataSource);

            var filterStages = new List <PipelineStageDefinition <BsonDocument, BsonDocument> >();

            if (model.IsRealTime && !string.IsNullOrEmpty(model.ComparedRealTimeField))
            {
                if (model.LastComparedDate.HasValue)
                {
                    var andDoc = new BsonDocument
                    {
                        { "$and", new BsonArray(new List <BsonDocument>
                            {
                                new BsonDocument
                                {
                                    { model.ComparedRealTimeField, new BsonDocument
                                      {
                                          { "$gt", new BsonDateTime(model.LastComparedDate.Value) }
                                      } }
                                },
                                new BsonDocument
                                {
                                    { model.ComparedRealTimeField, new BsonDocument
                                      {
                                          { "$lte", new BsonDateTime(DateTime.UtcNow) }
                                      } }
                                }
                            }) }
                    };
                    filterStages.Add(new BsonDocument
                    {
                        { "$match", andDoc }
                    });
                }
                else
                {
                    var matchDoc = new BsonDocument
                    {
                        {
                            "$match",
                            new BsonDocument
                            {
                                { model.ComparedRealTimeField,
                                  new BsonDocument
                                  {
                                      { "$lte", new BsonDateTime(DateTime.UtcNow) }
                                  } }
                            }
                        }
                    };
                    filterStages.Add(matchDoc);
                }
            }

            if (model.FilterValues != null && model.FilterValues.Any())
            {
                foreach (var filterValue in model.FilterValues)
                {
                    filterStages.Add(GetFilter(filterValue));
                }
            }

            var result = await _queryExecution.ExecuteAsync(
                mongoDatabase,
                model.FormattedString,
                model.MappingProjection,
                model.Parameters?.Select(a => new Models.Databases.ExecuteParamModel {
                Name = a.Name, RemoveQuotes = a.ReplaceDQuotes, ReplaceValue = a.Value
            }),
                filterStages);

            bool isSuccess = result != null;

            if (isSuccess)
            {
                var tempResult = result as IEnumerable <dynamic>;
                if (tempResult.Any())
                {
                    var first    = tempResult.First();
                    var hasGroup = false;
                    if (first is ExpandoObject)
                    {
                        hasGroup = ((IDictionary <string, object>)first).ContainsKey("group");
                    }
                    else
                    {
                        hasGroup = (first as JObject).ContainsKey("group");
                    }
                    if (hasGroup)
                    {
                        result = tempResult.GroupBy(a => a.group).Where(a => a != null).Select(a => new { series = a.Select(b => new { b.name, b.value }), name = a.Key });
                    }
                }
            }

            return(new ExecutionChartResponseModel {
                IsSuccess = isSuccess, Result = result
            });
        }