Skip to content

Instantly share code, notes, and snippets.

View JeremyLikness's full-sized avatar

Jeremy Likness JeremyLikness

View GitHub Profile
@JeremyLikness
JeremyLikness / function.cs
Created September 2, 2017 14:26
Function code for generating the short URL from an integer key
public static readonly string Alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
public static readonly int Base = Alphabet.Length;
public static string Encode(int i)
{
if (i == 0)
{
return Alphabet[0].ToString();
}
var s = string.Empty;
@JeremyLikness
JeremyLikness / function.cs
Created September 2, 2017 14:29
Code to parse request
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, NextId keyTable, CloudTable tableOut, TraceWriter log)
{
if (req == null)
{
return req.CreateResponse(HttpStatusCode.NotFound);
}
Request input = await req.Content.ReadAsAsync<Request>();
if (input == null)
@JeremyLikness
JeremyLikness / function.cs
Created September 2, 2017 14:39
Check for existence of key and create it first pass
if (keyTable == null)
{
keyTable = new NextId
{
PartitionKey = "1",
RowKey = "KEY",
Id = 1024
};
var keyAdd = TableOperation.Insert(keyTable);
await tableOut.ExecuteAsync(keyAdd);
@JeremyLikness
JeremyLikness / function.cs
Created September 2, 2017 14:41
Generating the new URL entry
var shortUrl = Encode(keyTable.Id++);
var newUrl = new ShortUrl
{
PartitionKey = $"{shortUrl.First()}",
RowKey = $"{shortUrl}",
Url = url
};
var singleAdd = TableOperation.Insert(newUrl);
await tableOut.ExecuteAsync(singleAdd);
@JeremyLikness
JeremyLikness / function.cs
Created September 2, 2017 14:43
Final results
var operation = TableOperation.Replace(keyTable);
await tableOut.ExecuteAsync(operation);
return req.CreateResponse(HttpStatusCode.OK, result);
@JeremyLikness
JeremyLikness / req.resp.json
Created September 2, 2017 14:51
Request and responses
{
"req": {
"tagSource": false,
"tagMediums": true,
"input": "https://blog.jeremylikness.com/"
},
"response": [{
"ShortUrl":"https://jlik.me/az52",
"LongUrl":"https://blog.jeremylikness.com/?utm_medium=twitter"
}, {
@JeremyLikness
JeremyLikness / function.cs
Created September 2, 2017 15:00
Redirect function
public static HttpResponseMessage Run(HttpRequestMessage req, CloudTable inputTable,
string shortUrl, TraceWriter log)
{
var redirectUrl = FALLBACK_URL;
if (!String.IsNullOrWhiteSpace(shortUrl))
{
shortUrl = shortUrl.Trim().ToLower();
var partitionKey = $"{shortUrl.First()}";
TableOperation operation = TableOperation.Retrieve<ShortUrl>(partitionKey, shortUrl);
TableResult result = inputTable.Execute(operation);
@JeremyLikness
JeremyLikness / function.cs
Created September 2, 2017 15:10
Custom Application Insights telemetry
public static TelemetryClient telemetry = new TelemetryClient()
{
InstrumentationKey =
System.Environment.GetEnvironmentVariable("APPINSIGHTS_INSTRUMENTATIONKEY")
};
@JeremyLikness
JeremyLikness / function.cs
Created September 2, 2017 15:11
Measuring the time to complete a table operation
var startTime = DateTime.UtcNow;
var timer = System.Diagnostics.Stopwatch.StartNew();
TableOperation operation = TableOperation.Retrieve<ShortUrl>(
partitionKey, shortUrl);
TableResult result = inputTable.Execute(operation);
telemetry.TrackDependency("AzureTableStorage", "Retrieve",
startTime, timer.Elapsed, result.Result != null);
@JeremyLikness
JeremyLikness / function.cs
Created September 2, 2017 15:16
Tracking medium and page view
telemetry.TrackEvent(fullUrl.Medium);
telemetry.TrackPageView(redirectUrl);