Example #1
0
        public async Task <string> GetRawContentAsync(string key)
        {
            var redirectable = await _redirectableStore.LoadAsync(key);

            return(redirectable switch
            {
                MicroText text => text.Text,
                MicroUrl url => url.Url,
                _ => null
            });
Example #2
0
        public async Task <MicroUrl> LoadAsync(string key)
        {
            var result = await _redirectableStore.LoadAsync(key);

            if (result is MicroUrl url)
            {
                return(url);
            }

            return(null);
        }
        public async Task <MicroText> LoadAsync(string key)
        {
            var result = await _redirectableStore.LoadAsync(key);

            if (result is MicroText text)
            {
                return(text);
            }

            return(null);
        }
Example #4
0
        public async Task <string> GetRedirectUrlAndTrackAsync(string key, HttpContext context)
        {
            var redirectable = await _redirectableStore.LoadAsync(key);

            if (redirectable == null)
            {
                return(null);
            }

            await _visitorTracker.SaveVisitAsync(key, context);

            return(!redirectable.Enabled ? null : ComputeTargetUrl(redirectable));
        }
Example #5
0
        public async Task <MicroUrlStats> GetStatsAsync(string key)
        {
            var redirectable = await _redirectableStore.LoadAsync(key);

            if (redirectable == null)
            {
                return(null);
            }

            var from = redirectable.Created;
            var to   = DateTime.UtcNow;

            var allTimeStats = new StatCount
            {
                SeenVisitors   = new HashSet <string>(),
                UniqueVisitors = 0,
                VisitApplies   = x => true,
                Visitors       = 0
            };
            var dayStats = Enumerable.Range(0, LastXDays).Select(x => DateTime.UtcNow.Date.AddDays(-x))
                           .Select(x => new StatCount
            {
                SeenVisitors   = new HashSet <string>(),
                Visitors       = 0,
                UniqueVisitors = 0,
                VisitApplies   = item => item.Created.Date == x,
                Date           = x
            })
                           .ToList();
            var stats = new List <StatCount> {
                allTimeStats
            };

            dayStats.ForEach(stats.Add);

            await foreach (var item in _visitStore.GetVisitsOfRediretableBetween(key, from, to))
            {
                foreach (var stat in stats.Where(stat => stat.VisitApplies(item)))
                {
                    stat.Visitors++;
                    if (!stat.SeenVisitors.Contains(item.Ip))
                    {
                        stat.UniqueVisitors++;
                        stat.SeenVisitors.Add(item.Ip);
                    }
                }
            }

            return(new MicroUrlStats
            {
                Key = key,
                TargetUrl = _redirectService.ComputeTargetUrl(redirectable),
                AllTime = new HitStats
                {
                    From = from,
                    To = to,
                    Visitors = allTimeStats.Visitors,
                    UniqueVisitors = allTimeStats.UniqueVisitors
                },
                Recents = dayStats
                          .Select(statsForDay =>
                {
                    var dayEnd = statsForDay.Date.Value.AddDays(1);
                    return new HitStats
                    {
                        From = statsForDay.Date.Value,
                        To = dayEnd,
                        Visitors = statsForDay.Visitors,
                        UniqueVisitors = statsForDay.UniqueVisitors
                    };
                }).ToList()
            });
        }