public static CacheHeaderAction Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "options", Route = "api/ProductMapping")] HttpRequest req, ExecutionContext context, TraceWriter log) { var productMap = ProductTitleRepository.GetMapping(context.FunctionAppDirectory).OrderBy(_ => _.Value); CacheHeaderAction result = new CacheHeaderAction(TimeSpan.FromHours(2), new JsonResult(productMap.ToList())); return(result); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "feed")] HttpRequest req, ExecutionContext context, TraceWriter log) { // TODO: enable link for product filter var productFilter = new List <string>(); if (req.Query.ContainsKey("products")) { productFilter = req.Query["products"].First().Split(',').ToList(); } var result = await _docChangesRepository.GetChangesPerDayAsync(1, null); var feed = new Feed { Title = "Azure Documentation Updates", Link = new System.Uri("https://azuredocsupdates.azurewebsites.net/"), //Copyright = "Copyright", // TODO: Description = "" }; feed.Items = GenerateFeed(result, productFilter, context.FunctionAppDirectory).ToList(); var actionResult = new CacheHeaderAction(TimeSpan.FromHours(4), new ContentResult { Content = feed.Serialize(), ContentType = "text/xml", StatusCode = 200 }); return(actionResult); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "{*path}")] HttpRequest req, // {*path:regex(^(?!admin).*$)} ExecutionContext context, TraceWriter log) { _telemetryClient.Context.Cloud.RoleName = context.FunctionName; var requestTelemetry = new RequestTelemetry { Name = $"{req.Method} {req.Path}", Url = new Uri(req.GetDisplayUrl()) }; requestTelemetry.Properties["httpMethod"] = req.Method; requestTelemetry.Context.Operation.Id = context.InvocationId.ToString(); requestTelemetry.Context.Operation.Name = context.FunctionName; var operation = _telemetryClient.StartOperation(requestTelemetry); IActionResult result; try { var functionAppDirectory = context.FunctionAppDirectory; #if DEBUG if (Debugger.IsAttached) { functionAppDirectory = functionAppDirectory + "\\..\\..\\.."; } #endif var filePath = GetFilePath(req.Path, functionAppDirectory); var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); if (filePath.EndsWith("index.html")) { using (var reader = new StreamReader(stream)) { var html = await reader.ReadToEndAsync(); // html = html.Replace("{{productfilter-options-placeholder}}", GetProductfilterOptions(context.FunctionAppDirectory)); result = new CacheHeaderAction(TimeSpan.FromHours(2), new FileContentResult(Encoding.UTF8.GetBytes(html), new MediaTypeHeaderValue(GetMimeType(filePath))) { LastModified = File.GetLastWriteTimeUtc(filePath) }); } } else { result = new CacheHeaderAction(TimeSpan.FromHours(2), new FileStreamResult(stream, new MediaTypeHeaderValue(GetMimeType(filePath))) { LastModified = File.GetLastWriteTimeUtc(filePath) }); } operation.Telemetry.Success = true; operation.Telemetry.ResponseCode = "200"; _telemetryClient.StopOperation(operation); } catch (Exception e) { _telemetryClient.TrackException(e); operation.Telemetry.Success = false; operation.Telemetry.ResponseCode = "400"; _telemetryClient.StopOperation(operation); result = new NotFoundResult(); } return(result); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "options", Route = "api/ChangeFeeed")] HttpRequest req, ExecutionContext context, TraceWriter log) { int requestedPage = 1; if (req.Query.ContainsKey("page") && (!int.TryParse(req.Query["page"], out requestedPage) || requestedPage > _docChangesRepository.MaxPages)) { return(new BadRequestObjectResult("Please pass the page on the query string or in the request body")); } var productFilter = new List <string>(); if (req.Query.ContainsKey("products")) { productFilter = req.Query["products"].First().Split(',').ToList(); } string dateFilter = null; if (req.Query.ContainsKey("date")) { dateFilter = req.Query["date"].First(); } // TODO: lock var cacheKey = $"ChangesPerDay_Page{requestedPage}_Date{dateFilter}"; if (_cacheUpdated <= DateTime.UtcNow.AddHours(-2) || !_cache.ContainsKey(cacheKey)) { _cache[cacheKey] = await _docChangesRepository.GetChangesPerDayAsync(requestedPage, dateFilter); _cacheUpdated = DateTime.UtcNow; } var results = _cache[cacheKey]; // TODO: remove 'diff-' at diff anchors processing results.SelectMany(_ => _.DocChanges) .SelectMany(_ => _.Commits) .Where(_ => !string.IsNullOrWhiteSpace(_.DeepLink)) .ToList() .ForEach(_ => _.DeepLink = _.DeepLink.Replace("diff-", "")); var inMemoryQuery = (from c in results group c by c.Id into g select new { ChangedAtDate = g.Key, Products = g.SelectMany(_ => _.DocChanges) .Where(_ => productFilter.Count == 0 || productFilter.Contains(_.Product)) .GroupBy(_ => _.Product) .Select(_ => new { Product = ProductTitleRepository.MapFolderToProductTitle(_.Key, context.FunctionAppDirectory), Added = _.Sum(i => i.Commits.Count(commit => commit.Status == ChangeStatus.Added)), Modified = _.Sum(i => i.Commits.Count(commit => commit.Status == ChangeStatus.Modified)), Deleted = _.Sum(i => i.Commits.Count(commit => commit.Status == ChangeStatus.Removed)), Changes = _.OrderByDescending(c => c.Commits, new CommitComparer()) }) .OrderBy(_ => _.Product) }).OrderByDescending(_ => _.ChangedAtDate); var result = new CacheHeaderAction(TimeSpan.FromHours(2), new JsonResult(inMemoryQuery.ToList())); return(result); }