Esempio n. 1
0
        public static void ProcessQueue([QueueTrigger(queueName: Utility.QUEUE)] string request,
                                        [CosmosDB(Utility.URL_TRACKING, Utility.URL_STATS, CreateIfNotExists = true, ConnectionStringSetting = "CosmosDb")] out dynamic doc,
                                        TraceWriter log)
        {
            try
            {
                AnalyticsEntry parsed = Utility.ParseQueuePayload(request);
                var            page   = parsed.LongUrl.AsPage(HttpUtility.ParseQueryString);

                var analytics = parsed.LongUrl.ExtractCampaignMediumAndAlias(HttpUtility.ParseQueryString);
                var campaign  = analytics.Item1;
                var medium    = analytics.Item2;
                var alias     = analytics.Item3;
                if (string.IsNullOrWhiteSpace(alias))
                {
                    alias = Utility.DEFAULT_ALIAS;
                }

                // cosmos DB
                var normalize = new[] { '/' };
                doc       = new ExpandoObject();
                doc.id    = Guid.NewGuid().ToString();
                doc.page  = page.TrimEnd(normalize);
                doc.alias = alias;
                if (!string.IsNullOrWhiteSpace(parsed.ShortUrl))
                {
                    doc.shortUrl = parsed.ShortUrl;
                }
                if (!string.IsNullOrWhiteSpace(campaign))
                {
                    doc.campaign = campaign;
                }
                if (parsed.Referrer != null)
                {
                    doc.referrerUrl  = parsed.Referrer.AsPage(HttpUtility.ParseQueryString);
                    doc.referrerHost = parsed.Referrer.DnsSafeHost;
                }
                if (!string.IsNullOrWhiteSpace(parsed.Agent))
                {
                    doc.agent = parsed.Agent;
                }
                doc.count     = 1;
                doc.timestamp = parsed.TimeStamp;
                doc.host      = parsed.LongUrl.DnsSafeHost;
                if (!string.IsNullOrWhiteSpace(medium))
                {
                    ((IDictionary <string, object>)doc).Add(medium, 1);
                }
                log.Info($"CosmosDB: {doc.id}|{doc.page}|{parsed.ShortUrl}|{campaign}|{medium}");
            }
            catch (Exception ex)
            {
                log.Error("An unexpected error occurred", ex);
                throw;
            }
        }
Esempio n. 2
0
        public static AnalyticsEntry ParseQueuePayload(string payload)
        {
            if (string.IsNullOrWhiteSpace(payload))
            {
                throw new ArgumentNullException("payload");
            }
            var parts = payload.Split('|');

            if (parts.Length != 5)
            {
                throw new ArgumentException($"Bad payload: {payload}");
            }
            var entry = new AnalyticsEntry
            {
                ShortUrl  = parts[0].Trim(),
                LongUrl   = new Uri(parts[1]),
                TimeStamp = DateTime.Parse(parts[2]),
                Referrer  = string.IsNullOrWhiteSpace(parts[3]) ? null : new Uri(parts[3]),
                Agent     = parts[4]
            };

            return(entry);
        }