Create a custom Azure Dashboard Tile Using an Azure Function and Markdown Format

The tiles on an Azure Dashboard will only display data from Azure itself. So if you have data from the outside, like a database, you need to make a few workarounds.

Tiles of an Azure Dashboard

Now, you cannot make your custom tile as pretty as these tiles. This is not Grafana after all. But you are able to add a dashboard that displays markdown (the kindergarden version of HTML) and this markdown can come from an URL. So with a little compromise you can in fact make a custom tile.

STEP 1: CREATE AN AZURE FUNCTION

First you must create an Azure Function that can provide the markdown to display.

Create a HTTPTrigger endpoint:

HTTP Trigger Endpoint

Depending on what you are monitoring, the code will differ. The important part is that you return markdown text. This is an example of a method that will draw a SQL table using the markdown format:

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;
using System.Text;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
{
    var connectionString = Environment.GetEnvironmentVariable("myConnectionString");
    StringBuilder sb = new StringBuilder();
    sb.AppendLine("| Customer | DateTime1 | DateTime2 | DateTime3 | DateTime4 | ");
    sb.AppendLine("|----------|-----------|-----------|-----------|-----------| ");

    string sql = "SELECT * FROM [dbo].[MyTable]";
 
    using (var conn = new SqlConnection(connectionString))
    using (var command = new SqlCommand(sql, conn)) 
    {
        conn.Open();
        using (SqlDataReader dr = command.ExecuteReader())
        {
            while (dr.Read())
            {
                var dateTime1 = dr["DateTime1"];
                var dateTime2 = dr["DateTime2"];
                var dateTime3 = dr["DateTime3"];
                var dateTime4 = dr["DateTime4"];
                sb.AppendLine($"| {dr["CustomerName"].ToString()} | {ToLocalTime(dateTime1)} | {ToLocalTime(dateTime2)} | {ToLocalTime(dateTime3)} | {ToLocalTime(dateTime4)} | ");
            }  
        }
    }
    return new OkObjectResult(sb.ToString());
}

private static string ToLocalTime(object o)
{
    if (DBNull.Value.Equals(o))
        return string.Empty;
    DateTime dt = Convert.ToDateTime(o);
    var timeZone = TimeZoneInfo.FindSystemTimeZoneById("Romance Standard Time");
    return TimeZoneInfo.ConvertTimeFromUtc(dt, timeZone).ToString("yyyy-MM-dd HH:mm");
}

STEP 2: GET THE URL OF THE AZURE FUNCTION

Grab the function URL and copy it to your clipboard.

STEP 3: ADD A MARKDOWN TILE FROM THE GALERY

In the gallery, choose the Markdown tile. Instead of adding static markdown, allow the tile to get the markdown from an URL. The URL is the Azure Function URL.

Add a markdown tile to your dashboard

STEP 4: ENJOY YOUR NEW DASHBOARD

Granted, the output is not as nice as the built in tiles, but if you squint your eyes you won’t notice:

Custom Azure Dashboard Tile

MORE TO READ:

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in c#, Microsoft Azure, .NET Core and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.