Exemple #1
0
        public AffinityGroupItemCountsRepository(ICassandraSession session, IMapper mapper)
        {
            _session = session;
            _mapper  = mapper;

            // Preparing statements
            TableAttribute tableAttribute = typeof(AffinityGroupItemCountsEntity).GetCustomAttributes(typeof(TableAttribute), true).Select(a => (TableAttribute)a).First();
            string         entityName     = tableAttribute.Name;

            string rowKeyPropertyName = NameOfHelper.PropertyName <AffinityGroupItemCountsEntity>(x => x.AffinityGroupSignalType);
            string itemIdPropertyName = NameOfHelper.PropertyName <AffinityGroupItemCountsEntity>(x => x.ItemId);
            string countPropertyName  = NameOfHelper.PropertyName <AffinityGroupItemCountsEntity>(x => x.Count);

            _getByIdStatement =
                new Lazy <PreparedStatement>(() => _session.Get().Prepare(string.Format("SELECT * FROM \"{0}\" WHERE \"{1}\" = ? AND \"{2}\" = ?", entityName, rowKeyPropertyName, itemIdPropertyName)));
            _getStatement =
                new Lazy <PreparedStatement>(
                    () => _session.Get().Prepare(string.Format("SELECT * FROM \"{0}\" WHERE \"{1}\" = ?", entityName, rowKeyPropertyName)));
            _incStatement =
                new Lazy <PreparedStatement>(
                    () =>
                    _session.Get()
                    .Prepare(string.Format("UPDATE  \"{0}\" SET \"{1}\" = \"{1}\" + 1 WHERE \"{2}\" = ? AND \"{3}\" = ?", entityName, countPropertyName, rowKeyPropertyName, itemIdPropertyName)));
            _decStatement =
                new Lazy <PreparedStatement>(
                    () =>
                    _session.Get()
                    .Prepare(string.Format("UPDATE  \"{0}\" SET \"{1}\" = \"{1}\" - 1 WHERE \"{2}\" = ? AND \"{3}\" = ?", entityName, countPropertyName, rowKeyPropertyName, itemIdPropertyName)));
        }
Exemple #2
0
        public AffinityGroupMostSignaledRepository(ICassandraSession session, IMapper mapper)
        {
            _session = session;
            _mapper  = mapper;

            // Preparing statements
            TableAttribute tableAttribute = typeof(AffinityGroupMostSignaledEntity).GetCustomAttributes(typeof(TableAttribute), true).Select(a => (TableAttribute)a).First();
            string         entityName     = tableAttribute.Name;

            string rowKeyPropertyName = NameOfHelper.PropertyName <AffinityGroupMostSignaledEntity>(x => x.AffinityGroupSignalType);
            string countPropertyName  = NameOfHelper.PropertyName <AffinityGroupMostSignaledEntity>(x => x.Count);
            string itemIdPropertyName = NameOfHelper.PropertyName <AffinityGroupMostSignaledEntity>(x => x.ItemId);

            _insertStatement =
                new Lazy <PreparedStatement>(
                    () => _session.Get().Prepare(string.Format("INSERT INTO \"{0}\" (\"{1}\",\"{2}\",\"{3}\") VALUES(?,?,?)", entityName, rowKeyPropertyName, countPropertyName, itemIdPropertyName)));
            _getStatement =
                new Lazy <PreparedStatement>(
                    () => _session.Get().Prepare(string.Format("SELECT * FROM \"{0}\" WHERE \"{1}\" = ? ORDER BY \"{2}\" DESC", entityName, rowKeyPropertyName, countPropertyName)));
            _deleteStatement =
                new Lazy <PreparedStatement>(
                    () =>
                    _session.Get()
                    .Prepare(string.Format("DELETE FROM \"{0}\" WHERE \"{1}\" = ?", entityName, rowKeyPropertyName)));
        }
        public UserCountsRepository(ICassandraSession session, IMapper mapper)
        {
            _session = session;
            _mapper  = mapper;

            // Preparing statements
            TableAttribute tableAttribute = typeof(UserCountsEntity).GetCustomAttributes(typeof(TableAttribute), true).Select(a => (TableAttribute)a).First();
            string         entityName     = tableAttribute.Name;

            string userIdPropertyName   = NameOfHelper.PropertyName <UserCountsEntity>(x => x.UserId);
            string likesPropertyName    = NameOfHelper.PropertyName <UserCountsEntity>(x => x.Likes);
            string dislikesPropertyName = NameOfHelper.PropertyName <UserCountsEntity>(x => x.Dislikes);
            string viewsPropertyName    = NameOfHelper.PropertyName <UserCountsEntity>(x => x.Views);
            string abusesPropertyName   = NameOfHelper.PropertyName <UserCountsEntity>(x => x.Abuses);

            _getStatement      = new Lazy <PreparedStatement>(() => _session.Get().Prepare(string.Format("SELECT * FROM \"{0}\" WHERE \"{1}\" = ?", entityName, userIdPropertyName)));
            _incViewsStatement =
                new Lazy <PreparedStatement>(
                    () => _session.Get().Prepare(string.Format("UPDATE  \"{0}\" SET \"{2}\" = \"{2}\" + 1 WHERE \"{1}\" = ?", entityName, userIdPropertyName, viewsPropertyName)));
            _incLikesStatement =
                new Lazy <PreparedStatement>(
                    () => _session.Get().Prepare(string.Format("UPDATE  \"{0}\" SET \"{2}\" = \"{2}\" + 1 WHERE \"{1}\" = ?", entityName, userIdPropertyName, likesPropertyName)));
            _incDislikesStatement =
                new Lazy <PreparedStatement>(
                    () => _session.Get().Prepare(string.Format("UPDATE  \"{0}\" SET \"{2}\" = \"{2}\" + 1 WHERE \"{1}\" = ?", entityName, userIdPropertyName, dislikesPropertyName)));
            _decLikesStatement =
                new Lazy <PreparedStatement>(
                    () => _session.Get().Prepare(string.Format("UPDATE  \"{0}\" SET \"{2}\" = \"{2}\" - 1 WHERE \"{1}\" = ?", entityName, userIdPropertyName, likesPropertyName)));
            _decDislikesStatement =
                new Lazy <PreparedStatement>(
                    () => _session.Get().Prepare(string.Format("UPDATE  \"{0}\" SET \"{2}\" = \"{2}\" - 1 WHERE \"{1}\" = ?", entityName, userIdPropertyName, dislikesPropertyName)));
            _incAbuseStatement =
                new Lazy <PreparedStatement>(
                    () => _session.Get().Prepare(string.Format("UPDATE  \"{0}\" SET \"{2}\" = \"{2}\" + 1 WHERE \"{1}\" = ?", entityName, userIdPropertyName, abusesPropertyName)));
        }
Exemple #4
0
        public ItemSignalsRepository(ICassandraSession session, IMapper mapper)
        {
            _session = session;
            _mapper = mapper;

            // Preparing statements
            TableAttribute tableAttribute = typeof (ItemSignalsEntity).GetCustomAttributes(typeof (TableAttribute), true).Select(a => (TableAttribute)a).First();
            string entityName = tableAttribute.Name;

            string rowKeyPropertyName = NameOfHelper.PropertyName<ItemSignalsEntity>(x => x.ItemIdSignalType);
            string isAnticolumnPropertyName = NameOfHelper.PropertyName<ItemSignalsEntity>(x => x.IsAnticolumn);
            string userIdPropertyName = NameOfHelper.PropertyName<ItemSignalsEntity>(x => x.UserId);
            string dateTimePropertyName = NameOfHelper.PropertyName<ItemSignalsEntity>(x => x.DateTime);

            _insertStatement =
                new Lazy<PreparedStatement>(
                    () =>
                        _session.Get()
                            .Prepare(string.Format("INSERT INTO \"{0}\" (\"{1}\",\"{2}\",\"{3}\",\"{4}\") VALUES(?,false,?,?)", entityName, rowKeyPropertyName, isAnticolumnPropertyName,
                                userIdPropertyName,
                                dateTimePropertyName)));
            _getStatement =
                new Lazy<PreparedStatement>(
                    () => _session.Get().Prepare(string.Format("SELECT * FROM \"{0}\" WHERE \"{1}\" = ? AND \"{2}\" = ?", entityName, rowKeyPropertyName, isAnticolumnPropertyName)));
            _deleteStatement =
                new Lazy<PreparedStatement>(
                    () =>
                        _session.Get()
                            .Prepare(string.Format("INSERT INTO \"{0}\" (\"{1}\",\"{2}\",\"{3}\",\"{4}\") VALUES(?,true,?,?)", entityName, rowKeyPropertyName, isAnticolumnPropertyName,
                                userIdPropertyName,
                                dateTimePropertyName)));
        }
Exemple #5
0
        public async Task <IEnumerable <DomainTrackingStatPerDate> > GetStatsPerDateAsync(string subscriptionId, string url, DataQueryOptions filter)
        {
            if (string.IsNullOrEmpty(subscriptionId))
            {
                throw new ArgumentNullException("subscriptionId");
            }

            // Filter
            DateTime?dateFromFilter       = null;
            DateTime?dateToFilter         = null;
            List <DataFilterRule> filters = filter.Filters.Where(f => f.Value != null).ToList();

            foreach (DataFilterRule dataFilterRule in filters)
            {
                DataFilterRule f = dataFilterRule;

                if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainTrackingStatPerDate>(x => x.Date),
                                   StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // by created date
                    DateTime date = ((DateTime)f.Value).Date;
                    switch (f.Type)
                    {
                    case DataFilterTypes.Equal:
                        dateFromFilter = date;
                        dateToFilter   = date.AddDays(1);
                        break;

                    case DataFilterTypes.LessThan:
                        dateToFilter = date;
                        break;

                    case DataFilterTypes.LessThanOrEqual:
                        dateToFilter = date.AddDays(1);
                        break;

                    case DataFilterTypes.GreaterThan:
                        dateFromFilter = date.AddDays(1);
                        break;

                    case DataFilterTypes.GreaterThanOrEqual:
                        dateFromFilter = date;
                        break;
                    }
                }
                else
                {
                    throw new NotSupportedException(string.Format("Filter {0} by property {1} is not supported", f.Type, f.Name));
                }
            }

            // Aggregate per date
            IEnumerable <TrackingStatPerDateEntity> aggregation = await _trackingStatRepository.AggregatePerDateAsync(subscriptionId, url, dateFromFilter, dateToFilter);

            // Project
            IEnumerable <DomainTrackingStatPerDate> projectedResult = aggregation.Select(r => _mapper.Map <TrackingStatPerDateEntity, DomainTrackingStatPerDate>(r));

            return(projectedResult);
        }
Exemple #6
0
    static void Main()
    {
        string strObj = null;

        Console.WriteLine(strObj.nameof(x => x.Length));               //gets the name of an object's property.
        Console.WriteLine(strObj.nameof(x => x.GetType()));            //gets the name of an object's method.
        Console.WriteLine(NameOfHelper.nameof(() => string.Empty));    //gets the name of a class' property.
        Console.WriteLine(NameOfHelper.nameof(() => string.Copy(""))); //gets the name of a class' method.
    }
Exemple #7
0
        public async Task <DataResult <DomainTrackingStatPerUrl> > GetStatsPerUrlAsync(string subscriptionId, DataQueryOptions filter)
        {
            if (string.IsNullOrEmpty(subscriptionId))
            {
                throw new ArgumentNullException("subscriptionId");
            }

            // Filter
            string   redirectUrlFilter    = null;
            DateTime?dateFromFilter       = null;
            DateTime?dateToFilter         = null;
            List <DataFilterRule> filters = filter.Filters.Where(f => f.Value != null).ToList();

            foreach (DataFilterRule dataFilterRule in filters)
            {
                DataFilterRule f = dataFilterRule;

                if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainTrackingStatPerUrl>(x => x.RedirectUrl),
                                   StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by redirect url
                    redirectUrlFilter = f.Value.ToString();
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainTrackingStatPerUrl>(x => x.DateFrom),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by date from
                    var date = (DateTime)f.Value;
                    dateFromFilter = date;
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainTrackingStatPerUrl>(x => x.DateTo),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by date to
                    var date = (DateTime)f.Value;
                    dateToFilter = date.AddDays(1);
                }
                else
                {
                    throw new NotSupportedException(string.Format("Filter {0} by property {1} is not supported", f.Type, f.Name));
                }
            }

            // Aggregate per redirect url
            DataResultEntity <TrackingStatPerUrlEntity> aggregation =
                await
                _trackingStatRepository.AggregatePerUrlAsync(subscriptionId, redirectUrlFilter, dateFromFilter, dateToFilter, filter.OrderBy,
                                                             filter.OrderByDirection == OrderByDirections.Desc, filter.Skip, filter.Take, filter.Count);

            IEnumerable <DomainTrackingStatPerUrl> projectedResult = aggregation.Results.Select(r => _mapper.Map <TrackingStatPerUrlEntity, DomainTrackingStatPerUrl>(r));

            return(new DataResult <DomainTrackingStatPerUrl>(projectedResult, aggregation.Count));
        }
        public Task UpdateUserIdFromAsync(string userId, string toUserId)
        {
            return(Task.Run(() =>
            {
                IMongoQuery query = Query <CompanyEntity> .EQ(c => c.Users, userId);

                string usersCollectionName = NameOfHelper.PropertyName <CompanyEntity>(x => x.Users);
                UpdateBuilder update = Update.Set(string.Format("{0}.$", usersCollectionName), BsonDocumentWrapper.Create(toUserId));

                Collection.Update(query, update, UpdateFlags.Multi);
            }));
        }
        public Task SetSubscriptionHasTrialClicksAsync(string subscriptionId, bool value)
        {
            return(Task.Run(() =>
            {
                string subscriptionsCollectionName = NameOfHelper.PropertyName <CompanyEntity>(x => x.Subscriptions);
                string hasTrialClicksPropertyName = NameOfHelper.PropertyName <SubscriptionEntity>(x => x.HasTrialClicks);

                IMongoQuery query = Query.EQ(string.Format("{0}._id", subscriptionsCollectionName), subscriptionId);
                UpdateBuilder update = Update.Set(string.Format("{0}.$.{1}", subscriptionsCollectionName, hasTrialClicksPropertyName), value);

                Collection.Update(query, update);
            }));
        }
        public Task SetSubscriptionLastCycleDateAsync(string subscriptionId, DateTime date)
        {
            return(Task.Run(() =>
            {
                string subscriptionsCollectionName = NameOfHelper.PropertyName <CompanyEntity>(x => x.Subscriptions);
                string lastCycleDatePropertyName = NameOfHelper.PropertyName <SubscriptionEntity>(x => x.LastCycleDate);

                IMongoQuery query = Query.EQ(string.Format("{0}._id", subscriptionsCollectionName), subscriptionId);
                UpdateBuilder update = Update.Set(string.Format("{0}.$.{1}", subscriptionsCollectionName, lastCycleDatePropertyName), date);

                Collection.Update(query, update);
            }));
        }
Exemple #11
0
        public void Intercept(IInvocation invocation)
        {
            // Calls the decorated instance.
            invocation.Proceed();


            if (invocation.GetConcreteMethod().Name ==
                NameOfHelper.MethodName <IUrlTrackingService>(x => x.TrackAsync(null)))
            {
                // TrackAsync called
                var trackTask = (Task <UrlTrackingResult>)invocation.ReturnValue;

                // Filtering bots
                if (HttpContext.Current != null)
                {
                    string userAgent = HttpContext.Current.Request.UserAgent;
                    if (!string.IsNullOrEmpty(userAgent))
                    {
                        if (_bots.Any(bot => userAgent.IndexOf(bot, StringComparison.InvariantCultureIgnoreCase) >= 0))
                        {
                            return;
                        }
                    }
                }

                // Checking result
                trackTask.ContinueWith(async t =>
                {
                    UrlTrackingResult trackingResult = t.Result;
                    if (!trackingResult.IsAccountable)
                    {
                        // skip non tariffing redirects
                        return;
                    }

                    try
                    {
                        DomainTrackingStat trackingStat = _mappingEngine.Map <UrlTrackingResult, DomainTrackingStat>(trackingResult);

                        // counting url
                        await _service.CountAsync(trackingStat);
                    }
                    catch (Exception e)
                    {
                        Trace.TraceError(string.Format("Could not count external url '{0}': {1}",
                                                       trackingResult.Redirect, e));
                    }
                });
            }
        }
Exemple #12
0
        public Task UpdateMembershipAsync(string userId, UserMembershipEntity membership)
        {
            return(Task.Run(() =>
            {
                IMongoQuery query = Query.And(
                    Query <UserEntity> .EQ(e => e.Id, userId),
                    Query <UserEntity> .ElemMatch(e => e.Memberships, builder => builder.EQ(m => m.IdentityProvider, membership.IdentityProvider)),
                    Query <UserEntity> .ElemMatch(e => e.Memberships, builder => builder.EQ(m => m.UserIdentifier, membership.UserIdentifier)));

                string membershipsCollectionName = NameOfHelper.PropertyName <UserEntity>(x => x.Memberships);
                UpdateBuilder update = Update.Set(string.Format("{0}.$", membershipsCollectionName), BsonDocumentWrapper.Create(membership));

                Collection.Update(query, update);
            }));
        }
Exemple #13
0
        public AffinityGroupMostSignaledVersionRepository(ICassandraSession session, IMapper mapper)
        {
            _session = session;
            _mapper  = mapper;

            // Preparing statements
            TableAttribute tableAttribute = typeof(AffinityGroupMostSignaledVersionEntity).GetCustomAttributes(typeof(TableAttribute), true).Select(a => (TableAttribute)a).First();
            string         entityName     = tableAttribute.Name;

            string rowKeyPropertyName  = NameOfHelper.PropertyName <AffinityGroupMostSignaledVersionEntity>(x => x.AffinityGroupSignalType);
            string versionPropertyName = NameOfHelper.PropertyName <AffinityGroupMostSignaledVersionEntity>(x => x.Version);

            _updateStatement =
                new Lazy <PreparedStatement>(
                    () => _session.Get().Prepare(string.Format("UPDATE \"{0}\" SET \"{1}\" = ? WHERE \"{2}\" = ?", entityName, versionPropertyName, rowKeyPropertyName)));
            _getStatement =
                new Lazy <PreparedStatement>(
                    () => _session.Get().Prepare(string.Format("SELECT * FROM \"{0}\" WHERE \"{1}\" = ?", entityName, rowKeyPropertyName)));
        }
        public TimeSeriesRawRepository(ICassandraSession session, IMapper mapper)
        {
            _session = session;
            _mapper  = mapper;

            // Preparing statements
            TableAttribute tableAttribute = typeof(TimeSeriesRawEntity).GetCustomAttributes(typeof(TableAttribute), true).Select(a => (TableAttribute)a).First();
            string         entityName     = tableAttribute.Name;

            string rowKeyPropertyName   = NameOfHelper.PropertyName <TimeSeriesRawEntity>(x => x.EventIdYymmddhh);
            string dateTimePropertyName = NameOfHelper.PropertyName <TimeSeriesRawEntity>(x => x.DateTime);
            string payloadPropertyName  = NameOfHelper.PropertyName <TimeSeriesRawEntity>(x => x.Payload);

            _insertStatement =
                new Lazy <PreparedStatement>(
                    () =>
                    _session.Get().Prepare(string.Format("INSERT INTO \"{0}\" (\"{1}\",\"{2}\",\"{3}\") VALUES(?,?,?)", entityName, rowKeyPropertyName, dateTimePropertyName, payloadPropertyName)));
            _getStatement =
                new Lazy <PreparedStatement>(
                    () => _session.Get().Prepare(string.Format("SELECT * FROM \"{0}\" WHERE \"{1}\" = ? AND \"{2}\" >= ? AND \"{2}\" <= ?", entityName, rowKeyPropertyName, dateTimePropertyName)));
        }
Exemple #15
0
        public TimeSeriesRollupsMinuteRepository(ICassandraSession session, IMapper mapper)
        {
            _session = session;
            _mapper  = mapper;

            // Preparing statements
            TableAttribute tableAttribute = typeof(TimeSeriesRollupsMinuteEntity).GetCustomAttributes(typeof(TableAttribute), true).Select(a => (TableAttribute)a).First();
            string         entityName     = tableAttribute.Name;

            string rowKeyPropertyName = NameOfHelper.PropertyName <TimeSeriesRollupsMinuteEntity>(x => x.EventIdHh);
            string minutePropertyName = NameOfHelper.PropertyName <TimeSeriesRollupsMinuteEntity>(x => x.Minute);
            string countPropertyName  = NameOfHelper.PropertyName <TimeSeriesRollupsMinuteEntity>(x => x.Count);

            _getStatement =
                new Lazy <PreparedStatement>(
                    () => _session.Get().Prepare(string.Format("SELECT * FROM \"{0}\" WHERE \"{1}\" = ? AND \"{2}\" >= ? AND \"{2}\" <= ?", entityName, rowKeyPropertyName, minutePropertyName)));
            _incStatement =
                new Lazy <PreparedStatement>(
                    () =>
                    _session.Get().Prepare(string.Format("UPDATE  \"{0}\" SET \"{1}\" = \"{1}\" + 1 WHERE \"{2}\" = ? AND \"{3}\" = ?", entityName, countPropertyName, rowKeyPropertyName,
                                                         minutePropertyName)));
        }
Exemple #16
0
        public Task <DataResultEntity <TrackingStatPerUrlEntity> > AggregatePerUrlAsync(string subscriptionId, string redirectUrl,
                                                                                        DateTime?dateFrom, DateTime?dateTo, string orderBy, bool?orderDesc, long?skip, long?take, bool?count)
        {
            return(Task.Run(() =>
            {
                if (!string.IsNullOrEmpty(orderBy))
                {
                    PropertyInfo[] properties = typeof(TrackingStatPerUrlEntity).GetProperties(BindingFlags.Public | BindingFlags.Instance);
                    if (properties.All(p => p.Name != orderBy))
                    {
                        throw new ArgumentException("Invalid property name for sort order", "orderBy");
                    }
                }

                if (skip.HasValue && skip.Value < 0)
                {
                    throw new ArgumentException("Skip value must be non-negative", "skip");
                }

                if (take.HasValue && take.Value <= 0)
                {
                    throw new ArgumentException("Take value must be positive", "take");
                }


                var filterPipeline = new List <BsonDocument>();

                string statSubscriptionIdPropertyName = NameOfHelper.PropertyName <TrackingStatEntity>(x => x.SubscriptionId);
                string statRedirectUrlPropertyName = NameOfHelper.PropertyName <TrackingStatEntity>(x => x.RedirectUrl);
                string statProjectIdPropertyName = NameOfHelper.PropertyName <TrackingStatEntity>(x => x.ProjectId);
                string statDatePropertyName = NameOfHelper.PropertyName <TrackingStatEntity>(x => x.Date);

                string statAggRedirectUrlPropertyName = NameOfHelper.PropertyName <TrackingStatPerUrlEntity>(x => x.RedirectUrl);
                string statAggProjectIdPropertyName = NameOfHelper.PropertyName <TrackingStatPerUrlEntity>(x => x.ProjectId);
                string statAggDateFromPropertyName = NameOfHelper.PropertyName <TrackingStatPerUrlEntity>(x => x.DateFrom);
                string statAggDateToPropertyName = NameOfHelper.PropertyName <TrackingStatPerUrlEntity>(x => x.DateTo);
                string statAggCountPropertyName = NameOfHelper.PropertyName <TrackingStatPerUrlEntity>(x => x.Count);


                // Filter
                filterPipeline.Add(new BsonDocument
                {
                    {
                        "$match",
                        new BsonDocument
                        {
                            { statSubscriptionIdPropertyName, subscriptionId }
                        }
                    }
                });

                if (!string.IsNullOrEmpty(redirectUrl))
                {
                    filterPipeline.Add(new BsonDocument
                    {
                        {
                            "$match",
                            new BsonDocument
                            {
                                {
                                    statRedirectUrlPropertyName, new BsonDocument
                                    {
                                        { "$regex", string.Format("^{0}", Regex.Escape(redirectUrl)) }
                                    }
                                }
                            }
                        }
                    });
                }

                if (dateFrom.HasValue)
                {
                    DateTime dateFromUtc = DateTime.SpecifyKind(dateFrom.Value, DateTimeKind.Utc);
                    filterPipeline.Add(new BsonDocument
                    {
                        {
                            "$match",
                            new BsonDocument
                            {
                                {
                                    statDatePropertyName, new BsonDocument
                                    {
                                        { "$gte", dateFromUtc }
                                    }
                                }
                            }
                        }
                    });
                }

                if (dateTo.HasValue)
                {
                    DateTime dateToUtc = DateTime.SpecifyKind(dateTo.Value, DateTimeKind.Utc);
                    filterPipeline.Add(new BsonDocument
                    {
                        {
                            "$match",
                            new BsonDocument
                            {
                                {
                                    statDatePropertyName, new BsonDocument
                                    {
                                        { "$lte", dateToUtc }
                                    }
                                }
                            }
                        }
                    });
                }


                // Group
                var countGroupOperator = new BsonDocument
                {
                    {
                        "$group",
                        new BsonDocument
                        {
                            {
                                "_id", new BsonDocument
                                {
                                    { statAggRedirectUrlPropertyName, string.Format("${0}", statRedirectUrlPropertyName) },
                                    { statAggProjectIdPropertyName, string.Format("${0}", statProjectIdPropertyName) },
                                }
                            }
                        }
                    }
                };

                var resultGroupOperator = new BsonDocument
                {
                    {
                        "$group",
                        new BsonDocument
                        {
                            {
                                "_id", new BsonDocument
                                {
                                    { statAggRedirectUrlPropertyName, string.Format("${0}", statRedirectUrlPropertyName) },
                                    { statAggProjectIdPropertyName, string.Format("${0}", statProjectIdPropertyName) }
                                }
                            },
                            {
                                statAggCountPropertyName, new BsonDocument
                                {
                                    { "$sum", 1 }
                                }
                            },
                            {
                                statAggDateFromPropertyName, new BsonDocument
                                {
                                    { "$min", string.Format("${0}", statDatePropertyName) }
                                }
                            },
                            {
                                statAggDateToPropertyName, new BsonDocument
                                {
                                    { "$max", string.Format("${0}", statDatePropertyName) }
                                }
                            }
                        }
                    }
                };


                // Project for compatible object
                var projectOperator = new BsonDocument
                {
                    {
                        "$project",
                        new BsonDocument
                        {
                            { "_id", 0 },
                            { statAggRedirectUrlPropertyName, string.Format("$_id.{0}", statAggRedirectUrlPropertyName) },
                            { statAggProjectIdPropertyName, string.Format("$_id.{0}", statAggProjectIdPropertyName) },
                            { statAggCountPropertyName, 1 },
                            { statAggDateFromPropertyName, 1 },
                            { statAggDateToPropertyName, 1 }
                        }
                    }
                };

                // Sort
                BsonDocument sortOperator = null;
                if (!string.IsNullOrEmpty(orderBy))
                {
                    sortOperator = new BsonDocument
                    {
                        { "$sort", new BsonDocument {
                              { orderBy, orderDesc.HasValue && orderDesc.Value ? -1 : 1 }
                          } }
                    };
                }

                // Skip
                BsonDocument skipOperator = null;
                if (skip.HasValue)
                {
                    skipOperator = new BsonDocument
                    {
                        { "$skip", skip.Value }
                    };
                }

                // Limit
                BsonDocument limitOperator = null;
                if (take.HasValue)
                {
                    limitOperator = new BsonDocument
                    {
                        { "$limit", take.Value }
                    };
                }

                // Build pipelines
                var countPipeline = new List <BsonDocument>(filterPipeline)
                {
                    countGroupOperator
                };

                var resultPipeline = new List <BsonDocument>(filterPipeline)
                {
                    resultGroupOperator, projectOperator
                };
                if (sortOperator != null)
                {
                    resultPipeline.Add(sortOperator);
                }
                if (skipOperator != null)
                {
                    resultPipeline.Add(skipOperator);
                }
                if (limitOperator != null)
                {
                    resultPipeline.Add(limitOperator);
                }

                // Execute aggregations

                // Count
                var aggArgs = new AggregateArgs {
                    Pipeline = countPipeline
                };
                long?resultCount = null;
                if (count.HasValue && count.Value)
                {
                    resultCount = Collection.Aggregate(aggArgs).LongCount();
                }

                // Result
                aggArgs.Pipeline = resultPipeline;
                IEnumerable <BsonDocument> aggResult = Collection.Aggregate(aggArgs);

                // Project
                IEnumerable <TrackingStatPerUrlEntity> result = aggResult.Select(BsonSerializer.Deserialize <TrackingStatPerUrlEntity>);

                return new DataResultEntity <TrackingStatPerUrlEntity>(result, resultCount);
            }));
        }
        public DataResult <Task <DomainUserForAdmin> > GetAsyncSequence(DataQueryOptions filter)
        {
            var query = new List <IMongoQuery>(filter.Filters.Count);

            // filtering
            List <DataFilterRule> filters = filter.Filters.Where(f => f.Value != null).ToList();

            foreach (DataFilterRule dataFilterRule in filters)
            {
                DataFilterRule f = dataFilterRule;

                if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainUserForAdmin>(x => x.UserName),
                                   StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by user name
                    query.Add(Query.Text(f.Value.ToString().ToLowerInvariant()));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainUserForAdmin>(x => x.Email),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by email
                    string email      = f.Value.ToString().ToLowerInvariant();
                    var    expression = new BsonRegularExpression(string.Format("^{0}.*", Regex.Escape(email)));
                    query.Add(Query <UserEntity> .Matches(p => p.Email, expression));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainUserForAdmin>(x => x.ProductType),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by product type
                    var productId = Int32.Parse(f.Value.ToString());
                    query.Add(Query <UserEntity> .EQ(p => p.ProductId, productId));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainUserForAdmin>(x => x.Created),
                                        StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // by created date
                    var date = (DateTime)f.Value;
                    switch (f.Type)
                    {
                    case DataFilterTypes.Equal:
                        query.Add(Query <UserEntity> .EQ(p => p.Created, date));
                        break;

                    case DataFilterTypes.LessThan:
                        query.Add(Query <UserEntity> .LT(p => p.Created, date));
                        break;

                    case DataFilterTypes.LessThanOrEqual:
                        query.Add(Query <UserEntity> .LTE(p => p.Created, date));
                        break;

                    case DataFilterTypes.GreaterThan:
                        query.Add(Query <UserEntity> .GT(p => p.Created, date));
                        break;

                    case DataFilterTypes.GreaterThanOrEqual:
                        query.Add(Query <UserEntity> .GTE(p => p.Created, date));
                        break;
                    }
                }
                else
                {
                    throw new NotSupportedException(string.Format("Filter {0} by property {1} is not supported", f.Type, f.Name));
                }
            }

            // Filter only users
            query.Add(Query <UserEntity> .EQ(p => p.Roles, DomainRoles.User));

            MongoCursor <UserEntity> cursor    = _userRepository.Collection.Find(query.Count > 0 ? Query.And(query) : Query.Null);
            IMongoSortBy             sortOrder = null;

            // sorting
            if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <DomainUserForAdmin>(x => x.UserName),
                               StringComparison.OrdinalIgnoreCase) == 0)
            {
                // order by name
                sortOrder = filter.OrderByDirection == OrderByDirections.Asc
                    ? SortBy <UserEntity> .Ascending(p => p.Name)
                    : SortBy <UserEntity> .Descending(p => p.Name);
            }
            else if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <DomainUserForAdmin>(x => x.Created),
                                    StringComparison.OrdinalIgnoreCase) == 0)
            {
                // order by created
                sortOrder = filter.OrderByDirection == OrderByDirections.Asc
                    ? SortBy <UserEntity> .Ascending(p => p.Created)
                    : SortBy <UserEntity> .Descending(p => p.Created);
            }
            else if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <DomainUserForAdmin>(x => x.ProductType),
                                    StringComparison.OrdinalIgnoreCase) == 0)
            {
                // order by product type
                sortOrder = filter.OrderByDirection == OrderByDirections.Asc
                    ? SortBy <UserEntity> .Ascending(p => p.ProductId)
                    : SortBy <UserEntity> .Descending(p => p.ProductId);
            }

            if (sortOrder != null)
            {
                cursor.SetSortOrder(sortOrder);
            }

            // paging

            if (filter.Skip.HasValue)
            {
                cursor.SetSkip(filter.Skip.Value);
            }

            if (filter.Take.HasValue)
            {
                cursor.SetLimit(filter.Take.Value);
            }

            // Count of results
            long?count = null;

            if (filter.Count)
            {
                count = cursor.Count();
            }

            // post-processing

            return(new DataResult <Task <DomainUserForAdmin> >(cursor.Select(GetUserDataAsync), count));
        }
        public async Task <HttpResponseMessage> Get(string id, ODataQueryOptions <AdminProject> options)
        {
            // Checks whether user exists
            await _userService.GetAsync(id);

            var validationSettings = new ODataValidationSettings
            {
                MaxTop = 100,
                AllowedArithmeticOperators = AllowedArithmeticOperators.None,
                AllowedFunctions           = AllowedFunctions.None,
                AllowedLogicalOperators    =
                    AllowedLogicalOperators.Equal | AllowedLogicalOperators.LessThan | AllowedLogicalOperators.LessThanOrEqual | AllowedLogicalOperators.GreaterThan |
                    AllowedLogicalOperators.GreaterThanOrEqual | AllowedLogicalOperators.And,
                AllowedQueryOptions = AllowedQueryOptions.Filter | AllowedQueryOptions.OrderBy | AllowedQueryOptions.Skip | AllowedQueryOptions.Top | AllowedQueryOptions.InlineCount
            };

            validationSettings.AllowedOrderByProperties.Add("Name");
            validationSettings.AllowedOrderByProperties.Add("Created");
            validationSettings.AllowedOrderByProperties.Add("ProductType");

            try
            {
                options.Validate(validationSettings);
            }
            catch (Exception)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ResponseMessages.InvalidQueryOptions));
            }


            // retrieving projects
            DataResult <Task <DomainProjectForAdmin> > result;
            IEnumerable <Task <AdminProject> >         projectTasks;
            DataQueryOptions filter;

            // parsing filter parameters
            try
            {
                filter = _mapper.Map <ODataQueryOptions, DataQueryOptions>(options);
            }
            catch (AutoMapperMappingException)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ResponseMessages.InvalidQueryOptions));
            }


            filter.Filters.Add(new DataFilterRule
            {
                Name  = NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.UserId),
                Type  = DataFilterTypes.Equal,
                Value = id
            });


            try
            {
                // filtering projects
                result = await _adminProjectService.GetAsyncSequenceAsync(filter);

                projectTasks = result.Results.Select(t => t.ContinueWith(u => _mapper.Map <DomainProjectForAdmin, AdminProject>(u.Result)));
            }
            catch (NotSupportedException)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ResponseMessages.BadRequest));
            }

            // loading and shaping all projects
            List <AdminProject> projects = (await Task.WhenAll(projectTasks)).ToList();

            if (filter.Count)
            {
                var pageResult = new PageResult <AdminProject>(projects, null, result.Count);
                return(Request.CreateResponse(HttpStatusCode.OK, pageResult));
            }

            return(Request.CreateResponse(HttpStatusCode.OK, projects));
        }
        public async Task <DataResult <Task <DomainProjectForAdmin> > > GetAsyncSequenceAsync(DataQueryOptions filter)
        {
            var query = new List <IMongoQuery>(filter.Filters.Count);

            // filtering
            List <DataFilterRule> filters = filter.Filters.Where(f => f.Value != null).ToList();

            foreach (DataFilterRule dataFilterRule in filters)
            {
                DataFilterRule f = dataFilterRule;

                if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.Name),
                                   StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by name
                    query.Add(Query.Text(f.Value.ToString().ToLowerInvariant()));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.ProductType),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by product type
                    var productId = Int32.Parse(f.Value.ToString());
                    query.Add(Query <ProjectEntity> .EQ(p => p.ProductId, productId));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.UserId),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by user id
                    query.Add(Query <ProjectEntity> .EQ(p => p.UserId, f.Value.ToString()));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.UserName),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by user name
                    List <UserEntity> profiles = await _userRepository.FindByNameAsync(f.Value.ToString());

                    if (profiles.Count == 0)
                    {
                        // no users found
                        return(new DataResult <Task <DomainProjectForAdmin> >(new Task <DomainProjectForAdmin>[] { }));
                    }

                    List <string> allIds = profiles.Select(prof => prof.Id).ToList();
                    query.Add(Query <ProjectEntity> .Where(p => allIds.Contains(p.UserId)));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.Created),
                                        StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // by created date
                    var date = (DateTime)f.Value;
                    switch (f.Type)
                    {
                    case DataFilterTypes.Equal:
                        query.Add(Query <ProjectEntity> .EQ(p => p.Created, date));
                        break;

                    case DataFilterTypes.LessThan:
                        query.Add(Query <ProjectEntity> .LT(p => p.Created, date));
                        break;

                    case DataFilterTypes.LessThanOrEqual:
                        query.Add(Query <ProjectEntity> .LTE(p => p.Created, date));
                        break;

                    case DataFilterTypes.GreaterThan:
                        query.Add(Query <ProjectEntity> .GT(p => p.Created, date));
                        break;

                    case DataFilterTypes.GreaterThanOrEqual:
                        query.Add(Query <ProjectEntity> .GTE(p => p.Created, date));
                        break;
                    }
                }
                else
                {
                    throw new NotSupportedException(string.Format("Filter {0} by property {1} is not supported", f.Type, f.Name));
                }
            }

            if (!filters.Any() && !string.IsNullOrEmpty(filter.OrderBy))
            {
                // MongoDb 2.6 HACK!!!
                // adding fake query to hint proper index

                if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.Name),
                                   StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // order by name
                    query.Add(Query <ProjectEntity> .Exists(p => p.Name));
                }
                else if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.Created),
                                        StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // order by created
                    query.Add(Query <ProjectEntity> .Exists(p => p.Created));
                }
                else if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.ProductType),
                                        StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // order by product type
                    query.Add(Query <ProjectEntity> .Exists(p => p.ProductId));
                }
            }

            MongoCursor <ProjectEntity> cursor = _projectRepository.Collection.Find(query.Count > 0 ? Query.And(query) : Query.Null);
            IMongoSortBy sortOrder             = null;

            // sorting
            if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.Name),
                               StringComparison.OrdinalIgnoreCase) == 0)
            {
                // order by name
                sortOrder = filter.OrderByDirection == OrderByDirections.Asc
                    ? SortBy <ProjectEntity> .Ascending(p => p.Name)
                    : SortBy <ProjectEntity> .Descending(p => p.Name);
            }
            else if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.Created),
                                    StringComparison.OrdinalIgnoreCase) == 0)
            {
                // order by created
                sortOrder = filter.OrderByDirection == OrderByDirections.Asc
                    ? SortBy <ProjectEntity> .Ascending(p => p.Created)
                    : SortBy <ProjectEntity> .Descending(p => p.Created);
            }
            else if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <DomainProjectForAdmin>(x => x.ProductType),
                                    StringComparison.OrdinalIgnoreCase) == 0)
            {
                // order by product type
                sortOrder = filter.OrderByDirection == OrderByDirections.Asc
                    ? SortBy <ProjectEntity> .Ascending(p => p.ProductId)
                    : SortBy <ProjectEntity> .Descending(p => p.ProductId);
            }

            if (sortOrder != null)
            {
                cursor.SetSortOrder(sortOrder);
            }

            // paging

            if (filter.Skip.HasValue)
            {
                cursor.SetSkip(filter.Skip.Value);
            }

            if (filter.Take.HasValue)
            {
                cursor.SetLimit(filter.Take.Value);
            }

            // Count of results
            long?count = null;

            if (filter.Count)
            {
                count = cursor.Count();
            }

            // post-processing

            return(new DataResult <Task <DomainProjectForAdmin> >(cursor.Select(GetProjectDataAsync), count));
        }
Exemple #20
0
        public async Task <IEnumerable <ExampleProject> > GetSequenceAsync(DataQueryOptions filter)
        {
            // Get example managers
            List <DomainUser> users = await _adminUserService.GetUsersInRoleAsync(DomainRoles.ExamplesManager);

            if (users.Count == 0)
            {
                return(new List <ExampleProject>());
            }

            // Get their projects
            IEnumerable <DomainProject> projects = await _projectService.GetProjectListByUsersAsync(users);

            // Only public videos
            projects = projects.Where(p => p.Access == ProjectAccess.Public);

            // Paging
            if (filter.Skip.HasValue)
            {
                projects = projects.Skip(filter.Skip.Value);
            }
            if (filter.Take.HasValue)
            {
                projects = projects.Take(filter.Take.Value);
            }

            // Sorting
            if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <ExampleProject>(x => x.Created),
                               StringComparison.OrdinalIgnoreCase) == 0)
            {
                projects = filter.OrderByDirection == OrderByDirections.Asc
                    ? projects.OrderBy(p => p.Created)
                    : projects.OrderByDescending(p => p.Created);
            }

            List <DomainProject> result = projects.ToList();

            // Aggregating project data
            List <Task <ExampleProject> > tasks = result.Select(GetProjectAsync).ToList();
            await Task.WhenAll(tasks);

            List <ExampleProject> examples = tasks.Select(t => t.Result).ToList();


            // Post-processing

            // Get comments and total comment counts
            string[] projectIds = result.Select(p => p.Id).ToArray();
            Dictionary <string, int> commentsCounts = (await _commentRepository.GetCommentsCountByProjectsAsync(projectIds)).ToDictionary(c => c.ProjectId, c => c.CommentsCount);
            Dictionary <string, List <CommentEntity> > recentComments = await _commentRepository.GetRecentCommentsByProjectsAsync(projectIds, RecentCommentsLimit);

            // Get related users list
            var userIdsSet = new HashSet <string>();

            foreach (var projectComment in recentComments)
            {
                foreach (CommentEntity c in projectComment.Value)
                {
                    if (userIdsSet.Contains(c.UserId))
                    {
                        continue;
                    }

                    userIdsSet.Add(c.UserId);
                }
            }
            Dictionary <string, UserEntity> commentUsers = (await _userRepository.GetUsersByIdsAsync(userIdsSet.ToArray())).ToDictionary(u => u.Id);

            // Append comments data to exmaples
            foreach (ExampleProject example in examples)
            {
                // comments total count
                example.Comments = commentsCounts.ContainsKey(example.Id) ? commentsCounts[example.Id] : 0;

                // comments list
                if (recentComments.ContainsKey(example.Id))
                {
                    var exampleComments = new List <Comment>();
                    List <CommentEntity> projectComments = recentComments[example.Id];
                    foreach (CommentEntity projectComment in projectComments)
                    {
                        UserEntity    commentAuthor = commentUsers.ContainsKey(projectComment.UserId) ? commentUsers[projectComment.UserId] : null;
                        DomainComment domainComment = _mapper.Map <Tuple <CommentEntity, UserEntity>, DomainComment>(new Tuple <CommentEntity, UserEntity>(projectComment, commentAuthor));

                        Comment comment = _mapper.Map <DomainComment, Comment>(domainComment);
                        if (commentAuthor != null)
                        {
                            comment.AvatarUrl = _userAvatarProvider.GetAvatar(commentAuthor.Email);
                        }

                        exampleComments.Add(comment);
                    }

                    exampleComments.Reverse();
                    example.CommentList = exampleComments;
                }
                else
                {
                    example.CommentList = new List <Comment>();
                }
            }

            return(examples);
        }
Exemple #21
0
        public Task <IEnumerable <TrackingStatPerDateEntity> > AggregatePerDateAsync(string subscriptionId, string redirectUrl, DateTime?dateFrom, DateTime?dateTo)
        {
            return(Task.Run(() =>
            {
                var filterPipeline = new List <BsonDocument>();

                string statSubscriptionIdPropertyName = NameOfHelper.PropertyName <TrackingStatEntity>(x => x.SubscriptionId);
                string statRedirectUrlPropertyName = NameOfHelper.PropertyName <TrackingStatEntity>(x => x.RedirectUrl);
                string statDatePropertyName = NameOfHelper.PropertyName <TrackingStatEntity>(x => x.Date);

                string statAggDatePropertyName = NameOfHelper.PropertyName <TrackingStatPerDateEntity>(x => x.Date);
                string statAggCountPropertyName = NameOfHelper.PropertyName <TrackingStatPerDateEntity>(x => x.Count);


                // Filter
                filterPipeline.Add(new BsonDocument
                {
                    {
                        "$match",
                        new BsonDocument
                        {
                            { statSubscriptionIdPropertyName, subscriptionId }
                        }
                    }
                });

                if (!string.IsNullOrEmpty(redirectUrl))
                {
                    filterPipeline.Add(new BsonDocument
                    {
                        {
                            "$match",
                            new BsonDocument
                            {
                                {
                                    statRedirectUrlPropertyName, new BsonDocument
                                    {
                                        { "$regex", string.Format("^{0}", Regex.Escape(redirectUrl)) }
                                    }
                                }
                            }
                        }
                    });
                }

                if (dateFrom.HasValue)
                {
                    DateTime dateFromUtc = DateTime.SpecifyKind(dateFrom.Value, DateTimeKind.Utc);
                    filterPipeline.Add(new BsonDocument
                    {
                        {
                            "$match",
                            new BsonDocument
                            {
                                {
                                    statDatePropertyName, new BsonDocument
                                    {
                                        { "$gte", dateFromUtc }
                                    }
                                }
                            }
                        }
                    });
                }

                if (dateTo.HasValue)
                {
                    DateTime dateToUtc = DateTime.SpecifyKind(dateTo.Value, DateTimeKind.Utc);
                    filterPipeline.Add(new BsonDocument
                    {
                        {
                            "$match",
                            new BsonDocument
                            {
                                {
                                    statDatePropertyName, new BsonDocument
                                    {
                                        { "$lt", dateToUtc }
                                    }
                                }
                            }
                        }
                    });
                }


                // Group by date
                // Since we have no way to combine datetime parts back to ISODate object we should substract all time parts from ISODate object before grouping
                // http://www.kamsky.org/1/post/2013/03/stupid-date-tricks-with-aggregation-framework.html

                string refDateProperty = string.Format("${0}", statDatePropertyName);
                var preGroupOperator = new BsonDocument
                {
                    {
                        "$project", new BsonDocument
                        {
                            {
                                statDatePropertyName, new BsonDocument
                                {
                                    {
                                        "$subtract", new BsonArray
                                        {
                                            string.Format("${0}", statDatePropertyName),
                                            new BsonDocument
                                            {
                                                {
                                                    "$add", new BsonArray
                                                    {
                                                        new BsonDocument
                                                        {
                                                            { "$millisecond", refDateProperty }
                                                        },
                                                        new BsonDocument
                                                        {
                                                            { "$multiply", new BsonArray {
                                                                  new BsonDocument {
                                                                      { "$second", refDateProperty }
                                                                  }, 1000
                                                              } }
                                                        },
                                                        new BsonDocument
                                                        {
                                                            { "$multiply", new BsonArray {
                                                                  new BsonDocument {
                                                                      { "$minute", refDateProperty }
                                                                  }, 60, 1000
                                                              } }
                                                        },
                                                        new BsonDocument
                                                        {
                                                            { "$multiply", new BsonArray {
                                                                  new BsonDocument {
                                                                      { "$hour", refDateProperty }
                                                                  }, 60, 60, 1000
                                                              } }
                                                        },
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                };

                var groupOperator = new BsonDocument
                {
                    {
                        "$group",
                        new BsonDocument
                        {
                            {
                                "_id", new BsonDocument
                                {
                                    {
                                        statAggDatePropertyName, refDateProperty
                                    }
                                }
                            },
                            {
                                statAggCountPropertyName, new BsonDocument
                                {
                                    { "$sum", 1 }
                                }
                            }
                        }
                    }
                };


                // Project for compatible object
                var projectOperator = new BsonDocument
                {
                    {
                        "$project",
                        new BsonDocument
                        {
                            { "_id", 0 },
                            { statAggDatePropertyName, string.Format("$_id.{0}", statAggDatePropertyName) },
                            { statAggCountPropertyName, 1 }
                        }
                    }
                };

                // Sort
                var sortOperator = new BsonDocument
                {
                    { "$sort", new BsonDocument {
                          { statAggDatePropertyName, 1 }
                      } }
                };


                // Build pipelines
                var resultPipeline = new List <BsonDocument>(filterPipeline)
                {
                    preGroupOperator, groupOperator, projectOperator, sortOperator
                };

                // Execute aggregations
                var aggArgs = new AggregateArgs {
                    Pipeline = resultPipeline
                };
                IEnumerable <BsonDocument> aggResult = Collection.Aggregate(aggArgs);

                // Project
                IEnumerable <TrackingStatPerDateEntity> result = aggResult.Select(BsonSerializer.Deserialize <TrackingStatPerDateEntity>);

                return result;
            }));
        }
Exemple #22
0
        public async Task <IEnumerable <TrendingWatch> > GetWeeklyTrendsSequenceAsync(DataQueryOptions filter)
        {
            List <DataFilterRule> filters = filter.Filters.Where(f => f.Value != null).ToList();
            long?version = null;

            foreach (DataFilterRule dataFilterRule in filters)
            {
                DataFilterRule f = dataFilterRule;
                if (string.Compare(f.Name, NameOfHelper.PropertyName <TrendingWatch>(x => x.Version),
                                   StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by product id
                    version = Int64.Parse(f.Value.ToString());
                    break;
                }
            }

            IEnumerable <DomainMostSignaledItem> data = await _cassandraStatisticsService.GetMostViewedForLastWeekAsync(StatisticsSpaces.Projects, MemoryPageSize, version);

            var pageData  = new List <DomainMostSignaledItem>();
            int count     = 0;
            int skip      = filter.Skip.HasValue ? filter.Skip.Value : 0;
            var uniqueIds = new HashSet <string>();
            var result    = new List <TrendingWatch>();

            foreach (DomainMostSignaledItem mostSignaledItem in data)
            {
                if (result.Count >= MemoryPageSize || count > MemoryPageSize * MaxScanPages)
                {
                    // memory limit reached
                    break;
                }

                if (filter.Take.HasValue && result.Count >= filter.Take.Value + skip)
                {
                    // all items loaded
                    break;
                }

                // checking uniqueness
                if (!uniqueIds.Contains(mostSignaledItem.ItemId))
                {
                    // there can be duplicated ItemIds with different Count values due to race conditions
                    uniqueIds.Add(mostSignaledItem.ItemId);
                }
                else
                {
                    // skipping duplicate
                    count++;
                    continue;
                }

                // adding to memory page
                pageData.Add(mostSignaledItem);

                // checking limits
                if ((count > 0 && count % MemoryPageSize == 0) ||
                    (filter.Take.HasValue && pageData.Count >= filter.Take.Value))
                {
                    // loading and filtering items
                    List <TrendingWatch> page = await LoadAndFilterDataAsync(pageData, filter);

                    result.AddRange(page);
                    pageData.Clear();
                }

                count++;
            }

            if (pageData.Count > 0)
            {
                // loading last page
                List <TrendingWatch> page = await LoadAndFilterDataAsync(pageData, filter);

                result.AddRange(page);
                pageData.Clear();
            }

            // Skip
            result = result.Skip(skip).ToList();

            // Take
            if (filter.Take.HasValue)
            {
                result = result.Take(filter.Take.Value).ToList();
            }

            return(result);
        }
Exemple #23
0
        private async Task <List <TrendingWatch> > LoadAndFilterDataAsync(IEnumerable <DomainMostSignaledItem> pageData, DataQueryOptions filter)
        {
            // Preparing
            int?                  productId  = null;
            WatchState?           videoState = null;
            List <DataFilterRule> filters    = filter.Filters.Where(f => f.Value != null).ToList();

            foreach (DataFilterRule dataFilterRule in filters)
            {
                DataFilterRule f = dataFilterRule;

                if (string.Compare(f.Name, NameOfHelper.PropertyName <TrendingWatch>(x => x.Generator),
                                   StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by product id
                    productId = Int32.Parse(f.Value.ToString());
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <TrendingWatch>(x => x.State),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by video state
                    videoState = (WatchState)Enum.Parse(typeof(WatchState), f.Value.ToString());
                }
            }


            // Loading
            List <DomainMostSignaledItem> items = pageData.ToList();
            List <Watch> watches = await _watchProjectService.GetByIdsAsync(items.Select(i => i.ItemId).ToArray(), string.Empty);


            // Processing projects
            var filtered = new List <TrendingWatch>();

            foreach (DomainMostSignaledItem i in items)
            {
                DomainMostSignaledItem item = i;
                Watch watch = watches.FirstOrDefault(w => w.Id == item.ItemId);

                // Display only existing & public videos
                if (watch == null || watch.Access != ProjectAccess.Public)
                {
                    continue;
                }

                // filtering by product id
                if (productId.HasValue)
                {
                    if (watch.Generator != productId.Value)
                    {
                        continue;
                    }
                }

                // filterring by state
                if (videoState.HasValue)
                {
                    if (watch.State != videoState.Value)
                    {
                        continue;
                    }
                }

                TrendingWatch trendingWatch = _mapper.Map <Watch, TrendingWatch>(watch);
                trendingWatch.Version = item.Version;

                filtered.Add(trendingWatch);
            }

            return(filtered);
        }
Exemple #24
0
        public async Task <HttpResponseMessage> Get(ODataQueryOptions <Watch> options)
        {
            var validationSettings = new ODataValidationSettings
            {
                MaxTop = 100,
                AllowedArithmeticOperators = AllowedArithmeticOperators.None,
                AllowedFunctions           = AllowedFunctions.None,
                AllowedLogicalOperators    =
                    AllowedLogicalOperators.Equal | AllowedLogicalOperators.And | AllowedLogicalOperators.GreaterThan | AllowedLogicalOperators.GreaterThanOrEqual | AllowedLogicalOperators.LessThan |
                    AllowedLogicalOperators.LessThanOrEqual,
                AllowedQueryOptions = AllowedQueryOptions.Filter | AllowedQueryOptions.OrderBy |
                                      AllowedQueryOptions.Skip | AllowedQueryOptions.Top | AllowedQueryOptions.InlineCount
            };

            validationSettings.AllowedOrderByProperties.Add("Name");
            validationSettings.AllowedOrderByProperties.Add("Created");
            validationSettings.AllowedOrderByProperties.Add("HitsCount");


            // Validating OData
            try
            {
                options.Validate(validationSettings);
            }
            catch (Exception)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ResponseMessages.InvalidQueryOptions));
            }


            // Parsing filter parameters
            DataQueryOptions filter;

            try
            {
                filter = _mapper.Map <ODataQueryOptions, DataQueryOptions>(options);
            }
            catch (AutoMapperMappingException)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ResponseMessages.InvalidQueryOptions));
            }


            // Special instructions for UserId filtering
            DataFilterRule userIdFilter = filter.Filters.FirstOrDefault(f => string.Compare(f.Name, NameOfHelper.PropertyName <Watch>(x => x.UserId),
                                                                                            StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal);

            if (userIdFilter == null || userIdFilter.Value == null)
            {
                // For backward compatibility we treats not specified or empty UserId as current user
                if (string.IsNullOrEmpty(UserId))
                {
                    throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, ResponseMessages.UnathorizedRequest));
                }

                if (userIdFilter == null)
                {
                    userIdFilter = new DataFilterRule {
                        Name = NameOfHelper.PropertyName <Watch>(x => x.UserId), Type = DataFilterTypes.Equal, Value = UserId
                    };
                    filter.Filters.Add(userIdFilter);
                }
                else
                {
                    userIdFilter.Value = UserId;
                }
            }
            else if (string.Compare(userIdFilter.Value.ToString(), UserIdAllConstant, StringComparison.OrdinalIgnoreCase) == 0)
            {
                // special constant $all means not filtering by UserId
                userIdFilter.Value = null;
            }


            // Retrieving projects
            DataResult <Watch> watchProjects;

            try
            {
                watchProjects = await _watchProjectRepository.GetSequenceAsync(filter, UserId);
            }
            catch (NotSupportedException)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ResponseMessages.BadRequest));
            }

            if (filter.Count)
            {
                var pageResult = new PageResult <Watch>(watchProjects.Results, null, watchProjects.Count);
                return(Request.CreateResponse(HttpStatusCode.OK, pageResult));
            }

            return(Request.CreateResponse(HttpStatusCode.OK, watchProjects.Results));
        }
Exemple #25
0
        public async Task <DataResult <DomainTrackingStat> > GetUrlStatsAsync(string subscriptionId, string url, DataQueryOptions filter)
        {
            if (string.IsNullOrEmpty(subscriptionId))
            {
                throw new ArgumentNullException("subscriptionId");
            }

            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("url");
            }

            IQueryable <TrackingStatEntity> data = _trackingStatRepository.Context.Where(s => s.SubscriptionId == subscriptionId && s.RedirectUrl == url);

            // Filter
            List <DataFilterRule> filters = filter.Filters.Where(f => f.Value != null).ToList();

            foreach (DataFilterRule dataFilterRule in filters)
            {
                DataFilterRule f = dataFilterRule;

                if (string.Compare(f.Name, NameOfHelper.PropertyName <DomainTrackingStat>(x => x.Date),
                                   StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // by created date
                    var date = ((DateTime)f.Value).Date;
                    switch (f.Type)
                    {
                    case DataFilterTypes.Equal:
                        data = data.Where(p => p.Date > date && p.Date < date.AddDays(1));
                        break;

                    case DataFilterTypes.LessThan:
                        data = data.Where(p => p.Date < date);
                        break;

                    case DataFilterTypes.LessThanOrEqual:
                        data = data.Where(p => p.Date < date.AddDays(1));
                        break;

                    case DataFilterTypes.GreaterThan:
                        data = data.Where(p => p.Date > date);
                        break;

                    case DataFilterTypes.GreaterThanOrEqual:
                        data = data.Where(p => p.Date >= date);
                        break;
                    }
                }
                else
                {
                    throw new NotSupportedException(string.Format("Filter {0} by property {1} is not supported", f.Type, f.Name));
                }
            }

            // Sort
            if (!string.IsNullOrEmpty(filter.OrderBy))
            {
                if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <DomainTrackingStat>(x => x.Date),
                                   StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // order by name
                    data = filter.OrderByDirection == OrderByDirections.Asc
                        ? data.OrderBy(p => p.Date)
                        : data.OrderByDescending(p => p.Date);
                }
                else
                {
                    throw new NotSupportedException(string.Format("Ordering by {0} is not supported", filter.OrderBy));
                }
            }


            // Count of results
            long?count = null;

            if (filter.Count)
            {
                count = data.LongCount();
            }


            // Paging
            if (filter.Skip.HasValue)
            {
                data = data.Skip(filter.Skip.Value);
            }

            if (filter.Take.HasValue)
            {
                data = data.Take(filter.Take.Value);
            }


            // post-processing
            IQueryable <DomainTrackingStat> results = data.Select(s => _mapper.Map <TrackingStatEntity, DomainTrackingStat>(s));

            return(new DataResult <DomainTrackingStat>(results, count));
        }
        public async Task <DataResult <Watch> > GetSequenceAsync(DataQueryOptions filter, string userId)
        {
            var query = new List <IMongoQuery>(filter.Filters.Count);

            // Filtering
            List <DataFilterRule> filters = filter.Filters.Where(f => f.Value != null).ToList();
            string requestedUserId        = null;
            var    searchList             = new List <string>();

            foreach (DataFilterRule dataFilterRule in filters)
            {
                DataFilterRule f = dataFilterRule;

                if (string.Compare(f.Name, NameOfHelper.PropertyName <Watch>(x => x.Name),
                                   StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by name
                    searchList.Add(f.Value.ToString().ToLowerInvariant());
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <Watch>(x => x.UserId),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by user id
                    requestedUserId = f.Value.ToString();
                    query.Add(Query <ProjectEntity> .EQ(p => p.UserId, requestedUserId));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <Watch>(x => x.Generator),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by product id
                    int productId = Int32.Parse(f.Value.ToString());
                    query.Add(Query <ProjectEntity> .EQ(p => p.ProductId, productId));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <Watch>(x => x.ProjectType),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by project type
                    object projectType = Enum.Parse(typeof(ProjectType), f.Value.ToString());
                    query.Add(Query <ProjectEntity> .EQ(p => p.ProjectType, (int)projectType));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <Watch>(x => x.ProjectSubtype),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by project subtype
                    object projectSubtype = Enum.Parse(typeof(ProjectSubtype), f.Value.ToString());
                    query.Add(Query <ProjectEntity> .EQ(p => p.ProjectSubtype, (int)projectSubtype));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <Watch>(x => x.External.VideoUri),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by external video uri
                    query.Add(Query <ProjectEntity> .EQ(p => p.VideoSource, f.Value));
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <Watch>(x => x.Created),
                                        StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // by created date
                    var date = (DateTime)f.Value;
                    switch (f.Type)
                    {
                    case DataFilterTypes.Equal:
                        query.Add(Query <ProjectEntity> .EQ(p => p.Created, date));
                        break;

                    case DataFilterTypes.LessThan:
                        query.Add(Query <ProjectEntity> .LT(p => p.Created, date));
                        break;

                    case DataFilterTypes.LessThanOrEqual:
                        query.Add(Query <ProjectEntity> .LTE(p => p.Created, date));
                        break;

                    case DataFilterTypes.GreaterThan:
                        query.Add(Query <ProjectEntity> .GT(p => p.Created, date));
                        break;

                    case DataFilterTypes.GreaterThanOrEqual:
                        query.Add(Query <ProjectEntity> .GTE(p => p.Created, date));
                        break;
                    }
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <Watch>(x => x.HitsCount),
                                        StringComparison.OrdinalIgnoreCase) == 0)
                {
                    // by hits count
                    int threshold = Int32.Parse(f.Value.ToString());

                    if (f.Type == DataFilterTypes.GreaterThan)
                    {
                        query.Add(Query <ProjectEntity> .GT(p => p.HitsCount, threshold));
                    }
                    else if (f.Type == DataFilterTypes.GreaterThanOrEqual)
                    {
                        query.Add(Query <ProjectEntity> .GTE(p => p.HitsCount, threshold));
                    }
                    else
                    {
                        query.Add(Query <ProjectEntity> .EQ(p => p.HitsCount, threshold));
                    }
                }
                else if (string.Compare(f.Name, NameOfHelper.PropertyName <Watch>(x => x.State),
                                        StringComparison.OrdinalIgnoreCase) == 0 && f.Type == DataFilterTypes.Equal)
                {
                    // by video state
                    var videoState = (WatchState)Enum.Parse(typeof(WatchState), f.Value.ToString());
                    if (videoState == WatchState.Uploading)
                    {
                        var avsx = Query <ProjectEntity> .EQ(p => p.AvsxFileId, null);

                        var originalFileId = Query <ProjectEntity> .EQ(p => p.OriginalVideoFileId, null);

                        var externalSource = Query <ProjectEntity> .EQ(p => p.VideoSource, null);

                        query.Add(Query.Or(avsx, Query.And(originalFileId, externalSource)));
                    }
                    else if (videoState == WatchState.Encoding)
                    {
                        var avsx = Query <ProjectEntity> .NE(p => p.AvsxFileId, null);

                        var originalFileId = Query <ProjectEntity> .NE(p => p.OriginalVideoFileId, null);

                        var encodedVideosName = NameOfHelper.PropertyName <ProjectEntity>(x => x.EncodedVideos);
                        var encodedVideos     = Query.NotExists(string.Format("{0}.0", encodedVideosName));

                        query.Add(Query.And(avsx, originalFileId, encodedVideos));
                    }
                    else if (videoState == WatchState.Ready)
                    {
                        var avsx = Query <ProjectEntity> .NE(p => p.AvsxFileId, null);

                        var externalSource = Query <ProjectEntity> .NE(p => p.VideoSource, null);

                        var originalFileId = Query <ProjectEntity> .NE(p => p.OriginalVideoFileId, null);

                        var encodedVideosName = NameOfHelper.PropertyName <ProjectEntity>(x => x.EncodedVideos);
                        var encodedVideos     = Query.Exists(string.Format("{0}.0", encodedVideosName));

                        query.Add(Query.And(avsx, Query.Or(externalSource, Query.And(originalFileId, encodedVideos))));
                    }
                }
                else
                {
                    throw new NotSupportedException(string.Format("Filter {0} by property {1} is not supported", f.Type, f.Name));
                }
            }

            string searchText = String.Join(" ", searchList);

            if (!String.IsNullOrEmpty(searchText))
            {
                query.Add(Query.Text(searchText));
            }

            // we should see only public videos for other users
            if (requestedUserId != userId)
            {
                query.Add(Query <ProjectEntity> .EQ(p => p.Access, (int)ProjectAccess.Public));
            }

            MongoCursor <ProjectEntity> cursor = _projectRepository.Collection.Find(query.Count > 0 ? Query.And(query) : Query.Null);

            // Sorting
            IMongoSortBy sortOrder = null;

            if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <Watch>(x => x.Name),
                               StringComparison.OrdinalIgnoreCase) == 0)
            {
                // order by name
                sortOrder = filter.OrderByDirection == OrderByDirections.Asc
                    ? SortBy <ProjectEntity> .Ascending(p => p.Name)
                    : SortBy <ProjectEntity> .Descending(p => p.Name);
            }
            else if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <Watch>(x => x.Created),
                                    StringComparison.OrdinalIgnoreCase) == 0)
            {
                // order by created
                sortOrder = filter.OrderByDirection == OrderByDirections.Asc
                    ? SortBy <ProjectEntity> .Ascending(p => p.Created)
                    : SortBy <ProjectEntity> .Descending(p => p.Created);
            }
            else if (string.Compare(filter.OrderBy, NameOfHelper.PropertyName <Watch>(x => x.HitsCount),
                                    StringComparison.OrdinalIgnoreCase) == 0)
            {
                // order by hits count
                sortOrder = filter.OrderByDirection == OrderByDirections.Asc
                    ? SortBy <ProjectEntity> .Ascending(p => p.HitsCount)
                    : SortBy <ProjectEntity> .Descending(p => p.HitsCount);
            }

            if (sortOrder != null)
            {
                cursor.SetSortOrder(sortOrder);
            }

            // Paging
            if (filter.Skip.HasValue)
            {
                cursor.SetSkip(filter.Skip.Value);
            }
            if (filter.Take.HasValue)
            {
                cursor.SetLimit(filter.Take.Value);
            }

            // Count of results
            long?count = null;

            if (filter.Count)
            {
                count = cursor.Count();
            }

            List <ProjectEntity> projects = cursor.ToList();

            // Get comments and total comment counts
            string[] projectIds = projects.Select(p => p.Id).ToArray();
            Dictionary <string, int> commentsCounts = (await _commentRepository.GetCommentsCountByProjectsAsync(projectIds)).ToDictionary(c => c.ProjectId, c => c.CommentsCount);
            Dictionary <string, List <CommentEntity> > recentComments = await _commentRepository.GetRecentCommentsByProjectsAsync(projectIds, RecentCommentsLimit);

            // Get project user ids united with comment user ids to load entities in batch
            string[] userIds = GetUserIds(projects, recentComments);
            Dictionary <string, UserEntity> users = (await _userRepository.GetUsersByIdsAsync(userIds)).ToDictionary(u => u.Id);

            // Post-processing
            return(new DataResult <Watch>(projects.Select(p => AggregateProject(p, userId, users, commentsCounts, recentComments)), count));
        }