private static async Task <DigestResult> GetDigestResult(string query, ILogger log) { DigestResult result = new DigestResult(); // generate request ID to allow issue tracking string requestId = Guid.NewGuid().ToString(); try { using (var httpClient = new HttpClient()) { httpClient.DefaultRequestHeaders.Add("x-api-key", AiAppKey); httpClient.DefaultRequestHeaders.Add("x-ms-app", "FunctionTemplate"); httpClient.DefaultRequestHeaders.Add("x-ms-client-request-id", requestId); string apiPath = $"{AppInsightsApi}/{AiAppId}/query?clientId={requestId}×pan=P1W&query={query}"; using (var httpResponse = await httpClient.GetAsync(apiPath)) { // throw exception when unable to determine the metric value httpResponse.EnsureSuccessStatusCode(); var resultJson = await httpResponse.Content.ReadAsAsync <JToken>(); result = new DigestResult { TotalRequests = resultJson.SelectToken("tables[0].rows[0][0]")?.ToObject <long>().ToString("N0"), FailedRequests = resultJson.SelectToken("tables[0].rows[0][1]")?.ToObject <long>().ToString("N0"), RequestsDuration = resultJson.SelectToken("tables[0].rows[0][2]")?.ToString(), TotalDependencies = resultJson.SelectToken("tables[0].rows[0][3]")?.ToObject <long>().ToString("N0"), FailedDependencies = resultJson.SelectToken("tables[0].rows[0][4]")?.ToObject <long>().ToString("N0"), DependenciesDuration = resultJson.SelectToken("tables[0].rows[0][5]")?.ToString(), TotalViews = resultJson.SelectToken("tables[0].rows[0][6]")?.ToObject <long>().ToString("N0"), TotalExceptions = resultJson.SelectToken("tables[0].rows[0][7]")?.ToObject <long>().ToString("N0"), OverallAvailability = resultJson.SelectToken("tables[0].rows[0][8]")?.ToString(), AvailabilityDuration = resultJson.SelectToken("tables[0].rows[0][9]")?.ToString() }; return(result); } } } catch (Exception ex) { //log.Error($"[Error]: Client Request ID {requestId}: {ex.Message}"); // optional - throw to fail the function return(result); } }
public bool Execute(IDigestProvider <string> provider, List <SkypeChat> chats, List <SkypeContact> contacts) { DigestResult <string> digest = provider.GenerateDigest(chats, contacts); if (!string.IsNullOrWhiteSpace(digest.Result)) { try { GMailSender.SendEmail(_emailToSend, digest.Result, _gmailAccount, _gmailPassword); return(true); } catch { } } return(false); }
public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, ILogger log) { // Get parameterfrom the query string string name = req.Query["name"]; //...or use a default if (name == "") { name = "<SOME_AI_NAME_RESOURCE>"; } DigestResult result = await GetDigestResult(GetQueryString(name), log : log); var message = await SendEMail(result, name, log : log); // Return the message if not using SendGrid binding return((ActionResult) new OkObjectResult(result)); }
private static async Task <SendGrid.Helpers.Mail.SendGridMessage> SendEMail(DigestResult result, string name, ILogger log) { // https://www.jankowskimichal.pl/en/2018/11/sending-emails-from-azure-functions-v2-sendgrid/ // https://sendgrid.com/docs/for-developers/sending-email/v2-csharp-code-example/#using-sendgrids-c-library // Microsoft.Azure.WebJobs.Extensions.SendGrid var today = DateTime.Today.ToShortDateString(); var message = new SendGridMessage { Subject = $"Msg ({today})!", From = MailHelper.StringToEmailAddress("<SOME_SENDER_EMAIL>") }; message.AddTo("<YOUR_EMAIL>"); var body = GetHtmlContentValue(name, today, result); message.AddContent("text/html", body); var transportWeb = new SendGrid.SendGridClient(SendGridAPI); await transportWeb.SendEmailAsync(message); return(message); }
private static string GetHtmlContentValue(string appName, string today, DigestResult result) { // update the HTML template accordingly for your need return($@" <html><body> <p style='text-align: center;'><strong>{appName} daily telemetry report {today}</strong></p> <p style='text-align: center;'>The following data shows insights based on telemetry from last 24 hours.</p> <table align='center' style='width: 95%; max-width: 480px;'><tbody> <tr> <td style='min-width: 150px; text-align: left;'><strong>Total requests</strong></td> <td style='min-width: 100px; text-align: right;'><strong>{result.TotalRequests}</strong></td> </tr> <tr> <td style='min-width: 120px; padding-left: 5%; text-align: left;'>Failed requests</td> <td style='min-width: 100px; text-align: right;'>{result.FailedRequests}</td> </tr> <tr> <td style='min-width: 120px; padding-left: 5%; text-align: left;'>Average response time</td> <td style='min-width: 100px; text-align: right;'>{result.RequestsDuration} ms</td> </tr> <tr> <td colspan='2'><hr /></td> </tr> <tr> <td style='min-width: 150px; text-align: left;'><strong>Total dependencies</strong></td> <td style='min-width: 100px; text-align: right;'><strong>{result.TotalDependencies}</strong></td> </tr> <tr> <td style='min-width: 120px; padding-left: 5%; text-align: left;'>Failed dependencies</td> <td style='min-width: 100px; text-align: right;'>{result.FailedDependencies}</td> </tr> <tr> <td style='min-width: 120px; padding-left: 5%; text-align: left;'>Average response time</td> <td style='min-width: 100px; text-align: right;'>{result.DependenciesDuration} ms</td> </tr> <tr> <td colspan='2'><hr /></td> </tr> <tr> <td style='min-width: 150px; text-align: left;'><strong>Total views</strong></td> <td style='min-width: 100px; text-align: right;'><strong>{result.TotalViews}</strong></td> </tr> <tr> <td style='min-width: 150px; text-align: left;'><strong>Total exceptions</strong></td> <td style='min-width: 100px; text-align: right;'><strong>{result.TotalExceptions}</strong></td> </tr> <tr> <td colspan='2'><hr /></td> </tr> <tr> <td style='min-width: 150px; text-align: left;'><strong>Overall Availability</strong></td> <td style='min-width: 100px; text-align: right;'><strong>{result.OverallAvailability} %</strong></td> </tr> <tr> <td style='min-width: 120px; padding-left: 5%; text-align: left;'>Average response time</td> <td style='min-width: 100px; text-align: right;'>{result.AvailabilityDuration} ms</td> </tr> </tbody></table> </body></html> "); }