示例#1
0
        public List <T> GetMany(FilterDefinition <T> filter, SortDefinition <T> sorter, ProjectionDefinition <T> projection)
        {
            IMongoCollection <T> myCollection = GetCollection();
            AggregateOptions     opts         = new AggregateOptions()
            {
                AllowDiskUse = true,
                BatchSize    = int.MaxValue,
                MaxTime      = TimeSpan.FromMinutes(10)
            };

            IAggregateFluent <T> aggregate = GetAggregateFluent(opts);

            if (filter == null)
            {
                filter = Builders <T> .Filter.Empty;
            }
            aggregate = aggregate.Match(filter);

            if (sorter != null)
            {
                aggregate.Sort(sorter);
            }
            if (projection != null)
            {
                aggregate = aggregate.Project <T>(projection);
            }

            List <T> result = aggregate.ToListAsync().GetAwaiter().GetResult();

            return(result);
        }
        public async Task <BuildVersion[]> SelectAllVersions(int limit = -1, int skip = 0)
        {
            IAggregateFluent <BsonDocument> query = _buildCollection.Aggregate().Group(new BsonDocument("_id",
                                                                                                        new BsonDocument
            {
                new BsonElement(nameof(BuildVersion.Major), $"${nameof(Build.MajorVersion)}"),
                new BsonElement(nameof(BuildVersion.Minor), $"${nameof(Build.MinorVersion)}")
            })).Sort(new BsonDocument
            {
                new BsonElement($"_id.{nameof(BuildVersion.Major)}", -1),
                new BsonElement($"_id.{nameof(BuildVersion.Minor)}", -1)
            }).Skip(skip);

            if (limit > 0)
            {
                query = query.Limit(limit);
            }

            List <BsonDocument> grouping = await query.ToListAsync();

            return((from g in grouping
                    select new BuildVersion
            {
                Major = (uint)g["_id"].AsBsonDocument[nameof(BuildVersion.Major)].AsInt32,
                Minor = (uint)g["_id"].AsBsonDocument[nameof(BuildVersion.Minor)].AsInt32
            }).ToArray());
        }
        public async Task <long> SelectAllLabsCount()
        {
            IAggregateFluent <BsonDocument> query = _buildCollection.Aggregate().Group(new BsonDocument("_id", new BsonDocument(nameof(Build.Lab), $"${nameof(Build.Lab)}"))).Sort(new BsonDocument("_id", 1));

            List <BsonDocument> grouping = await query.ToListAsync();

            return(grouping.Count);
        }
示例#4
0
        public async Task <List <Build> > SelectBuildsByStringSearch(string term, int limit = -1)
        {
            IAggregateFluent <Build> query = _buildCollection.Aggregate().Match(b => b.FullBuildString != null).Match(b => b.FullBuildString != "").Match(b => b.FullBuildString.ToLower().Contains(term.ToLower()));

            if (limit > 0)
            {
                query = query.Limit(limit);
            }

            return(await query.ToListAsync());
        }
        public async Task <string[]> SelectLabsForVersion(int major, int minor)
        {
            IAggregateFluent <BsonDocument> query = _buildCollection.Aggregate().Match(new BsonDocument
            {
                new BsonElement(nameof(Build.MajorVersion), major),
                new BsonElement(nameof(Build.MinorVersion), minor)
            }).Group(new BsonDocument("_id", $"${nameof(Build.Lab)}")).Sort(new BsonDocument("_id", 1));

            List <BsonDocument> grouping = await query.ToListAsync();

            return((from g in grouping
                    where !g["_id"].IsBsonNull
                    select g["_id"].AsString).ToArray());
        }
        public async Task <string[]> SelectAllLabs(int limit = -1, int skip = 0)
        {
            IAggregateFluent <BsonDocument> query = _buildCollection.Aggregate().Group(new BsonDocument("_id", $"${nameof(Build.Lab)}")).Sort(new BsonDocument("_id", 1)).Skip(skip);

            if (limit > 0)
            {
                query = query.Limit(limit);
            }

            List <BsonDocument> grouping = await query.ToListAsync();

            return((from g in grouping
                    where !g["_id"].IsBsonNull
                    select g["_id"].AsString).ToArray());
        }
示例#7
0
        public async Task <List <FamilyOverview> > SelectFamilyOverviews()
        {
            IAggregateFluent <BsonDocument> families = _buildCollection.Aggregate()
                                                       .Sort(sortByOrder)
                                                       .Group(new BsonDocument
            {
                new BsonElement("_id", $"${nameof(Build.Family)}"),
                new BsonElement(nameof(FamilyOverview.Count), new BsonDocument("$sum", 1)),
                new BsonElement(nameof(FamilyOverview.Latest), new BsonDocument("$first", "$$CURRENT"))
            })
                                                       .Sort(new BsonDocument("_id", -1));

            List <BsonDocument> result = await families.ToListAsync();

            return((from o in result
                    select BsonSerializer.Deserialize <FamilyOverview>(o)).ToList());
        }
示例#8
0
        //public async Task<IEnumerable<TestCase>> AllTestsByDate(string date, string endDate, string sort, int? limit)
        //{
        //    try
        //    {
        //        var aggregate = _context.Notes.Aggregate()
        //            .Match(r => r.Date >= DateTime.Parse(date) && r.Date <= DateTime.Parse(endDate));
        //        var results = await aggregate.ToListAsync();
        //        var sorting = await aggregate.Sort(sort).ToListAsync();
        //        return results;
        //    }
        //    catch (Exception ex)
        //    {
        //        // log or manage the exception
        //        throw ex;
        //    }
        //}

        public async Task <IEnumerable <object> > GetAllTestsByDate(string date, string endDate, string sort, int limit)
        {
            IAggregateFluent <TestCase> aggregate = null;

            try
            {
                if (string.IsNullOrEmpty(date) && string.IsNullOrEmpty(endDate))
                {
                    aggregate = _context.Notes.Aggregate()
                                .Match(r => r.Date == DateTime.Now.Date);
                }
                if (!string.IsNullOrEmpty(date) && string.IsNullOrEmpty(endDate))
                {
                    aggregate = _context.Notes.Aggregate()
                                .Match(r => r.Date == DateTime.Parse(date));
                }
                if (!string.IsNullOrEmpty(date) && !string.IsNullOrEmpty(endDate))
                {
                    aggregate = _context.Notes.Aggregate()
                                .Match(r => r.Date >= DateTime.Parse(date).Date&& r.Date <= DateTime.Parse(endDate).Date);
                }
                if ((limit != 0))
                {
                    aggregate = aggregate.Limit(Convert.ToInt32(limit));
                }

                if (!string.IsNullOrEmpty(sort))
                {
                    aggregate = aggregate.Sort(Builders <TestCase> .Sort.Descending(x => x.Result));
                    // aggregate = aggregate.Sort(Builders<TestCase>.Sort.Descending(sort));
                    // aggregate = aggregate.Sort(Builders<TestCase>.Sort.Ascending(sort));
                }


                var results = await aggregate.ToListAsync();

                return(results);
            }
            catch (Exception ex)
            {
                // log or manage the exception
                throw ex;
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <typeparam name="TID"></typeparam>
        /// <param name="filter"></param>
        /// <param name="group"></param>
        /// <param name="sortExp"></param>
        /// <param name="sortType"></param>
        /// <param name="limit"></param>
        /// <param name="skip"></param>
        /// <param name="readPreference"></param>
        /// <returns></returns>
        public Task <List <TResult> > AggregateAsync <TResult, TID>(FilterDefinition <TEntity> filter, ProjectionDefinition <TEntity, TResult> group, Expression <Func <TEntity, object> > sortExp = null, SortType sortType = SortType.Ascending, int limit = 0, int skip = 0, ReadPreference readPreference = null)
        {
            if (filter == null)
            {
                filter = Builders <TEntity> .Filter.Empty;
            }
            IAggregateFluent <TEntity> aggregateFluent  = base.CreateAggregate(filter, base.CreateSortDefinition <TEntity>(sortExp, sortType), readPreference);
            IAggregateFluent <TResult> aggregateFluent2 = aggregateFluent.Group <TResult>(group);

            if (skip > 0)
            {
                aggregateFluent2 = aggregateFluent2.Skip(skip);
            }
            if (limit > 0)
            {
                aggregateFluent2 = aggregateFluent2.Limit(limit);
            }
            return(aggregateFluent2.ToListAsync(default(CancellationToken)));
        }
        public async Task <FrontBuildGroup[]> SelectAllGroups(int limit = -1, int skip = 0)
        {
            IAggregateFluent <BsonDocument> query = _buildCollection.Aggregate().Group(new BsonDocument
            {
                new BsonElement("_id",
                                new BsonDocument
                {
                    new BsonElement(nameof(BuildGroup.Major), $"${nameof(Build.MajorVersion)}"),
                    new BsonElement(nameof(BuildGroup.Minor), $"${nameof(Build.MinorVersion)}"),
                    new BsonElement(nameof(BuildGroup.Build), $"${nameof(Build.Number)}"),
                    new BsonElement(nameof(BuildGroup.Revision), $"${nameof(Build.Revision)}")
                }),
                new BsonElement("date", new BsonDocument("$max", $"${nameof(Build.BuildTime)}")),
                new BsonElement("count", new BsonDocument("$sum", 1))
            }).Sort(new BsonDocument
            {
                new BsonElement($"_id.{nameof(BuildGroup.Major)}", -1),
                new BsonElement($"_id.{nameof(BuildGroup.Minor)}", -1),
                new BsonElement($"_id.{nameof(BuildGroup.Build)}", -1),
                new BsonElement($"_id.{nameof(BuildGroup.Revision)}", -1)
            }).Skip(skip);

            if (limit > 0)
            {
                query = query.Limit(limit);
            }

            List <BsonDocument> grouping = await query.ToListAsync();

            return((from g in grouping
                    select new FrontBuildGroup
            {
                Key = new BuildGroup
                {
                    Major = (uint)g["_id"].AsBsonDocument[nameof(BuildGroup.Major)].AsInt32,
                    Minor = (uint)g["_id"].AsBsonDocument[nameof(BuildGroup.Minor)].AsInt32,
                    Build = (uint)g["_id"].AsBsonDocument[nameof(BuildGroup.Build)].AsInt32,
                    Revision = (uint?)g["_id"].AsBsonDocument[nameof(BuildGroup.Revision)].AsNullableInt32
                },
                LastBuild = g["date"].ToNullableUniversalTime(),
                BuildCount = g["count"].AsInt32
            }).ToArray());
        }
示例#11
0
        private async Task InitializeAsync(IAggregateFluent <T> source, int pageIndex, int pageSize)
        {
            var range = source.Skip(pageIndex * pageSize).Limit(pageSize + 1).ToList();
            int total = range.Count > pageSize ? range.Count : pageSize;

            TotalCount = (await source.ToListAsync()).Count;
            if (pageSize > 0)
            {
                TotalPages = total / pageSize;
            }

            if (total % pageSize > 0)
            {
                TotalPages++;
            }

            PageSize  = pageSize;
            PageIndex = pageIndex;
            AddRange(range.Take(pageSize));
        }
示例#12
0
        public PagedList(IAggregateFluent <T> source, int pageIndex, int pageSize)
        {
            var range = source.Skip(pageIndex * pageSize).Limit(pageSize + 1).ToList();
            int total = range.Count > pageSize ? range.Count : pageSize;

            TotalCount = source.ToListAsync().Result.Count;
            if (pageSize > 0)
            {
                TotalPages = total / pageSize;
            }

            if (total % pageSize > 0)
            {
                TotalPages++;
            }

            PageSize  = pageSize;
            PageIndex = pageIndex;
            AddRange(range.Take(pageSize));
        }
        public async Task <int[]> SelectAllYears(int limit = -1, int skip = 0)
        {
            IAggregateFluent <BsonDocument> query =
                _buildCollection.Aggregate()
                .Match(Builders <Build> .Filter.Ne(b => b.BuildTime, null))
                .Group(new BsonDocument("_id", new BsonDocument("$year", $"${nameof(Build.BuildTime)}")))
                .Sort(new BsonDocument("_id", -1))
                .Skip(skip);

            if (limit > 0)
            {
                query = query.Limit(limit);
            }

            List <BsonDocument> grouping = await query.ToListAsync();

            return((from g in grouping
                    where !g["_id"].IsBsonNull
                    select g["_id"].AsInt32).ToArray());
        }
示例#14
0
        public static Dictionary <string, OptionStatusLine> GetOptionStatus(MongoConnection mongoConnection)
        {
            IMongoCollection <OptionResult> datas = mongoConnection.Database.GetCollection <OptionResult>(typeof(OptionResult).Name);

            BsonDocument projectionDocument = GetOptionResultProjectionBsonDocument();
            BsonDocument groupingDocument   = GetOptionResultGroupingBsonDocument();

            IAggregateFluent <BsonDocument> aggregateFluent = datas.Aggregate().Project(projectionDocument).Group(groupingDocument);

            Task <List <BsonDocument> > aggregateTask = aggregateFluent.ToListAsync();

            List <BsonDocument> aggregatedDocuments = aggregateTask.Result;

            Dictionary <string, OptionStatusLine> resultDictionary = new Dictionary <string, OptionStatusLine>();

            foreach (BsonDocument optionResultGroup in aggregatedDocuments)
            {
                string name            = optionResultGroup["_id"]["name"].AsString;
                bool   success         = optionResultGroup["_id"]["success"].AsBoolean;
                bool   within10Minutes = optionResultGroup["_id"]["within10Minutes"].AsBoolean;
                bool   within1Hour     = optionResultGroup["_id"]["within1Hour"].AsBoolean;
                bool   within24Hours   = optionResultGroup["_id"]["within24Hours"].AsBoolean;
                int    count           = optionResultGroup["count"].AsInt32;

                OptionStatusLine line;
                if (resultDictionary.ContainsKey(name))
                {
                    line = resultDictionary[name];
                }
                else
                {
                    line = new OptionStatusLine();
                    resultDictionary.Add(name, line);
                }

                UpdateLine(success, within10Minutes, within1Hour, within24Hours, count, line);
            }

            return(resultDictionary);
        }
示例#15
0
 public async ValueTask <List <TEntity> > ToListAsync(CancellationToken cancellationToken)
 {
     return(await _source.ToListAsync(cancellationToken).ConfigureAwait(false));
 }
示例#16
0
 protected Task <List <TAggregatedEntity> > ExecuteAsync <TAggregatedEntity>(
     IAggregateFluent <TAggregatedEntity> query)
 {
     return(query.ToListAsync());
 }