public static void ProcessQueue([QueueTrigger(queueName: Utility.QUEUE)] string request, [CosmosDB(Utility.URL_TRACKING, Utility.URL_STATS, CreateIfNotExists = true, ConnectionStringSetting = "CosmosDb")] out dynamic doc, ILogger log) { try { AnalyticsEntry parsed = Utility.ParseQueuePayload(request); var page = parsed?.LongUrl?.AsPage(); var analytics = parsed.LongUrl.ExtractCampaignMediumAndAlias(); 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(); 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.LogInformation($"CosmosDB: {doc.id}|{doc.page}|{parsed.ShortUrl}|{campaign}|{medium}"); } catch (Exception ex) { log.LogError("An unexpected error occurred", ex); throw; } }
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); }