mirror of https://github.com/Squidex/squidex.git
35 changed files with 216 additions and 94 deletions
@ -0,0 +1,34 @@ |
|||
// ==========================================================================
|
|||
// ApiWeightAttribute.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System; |
|||
using Microsoft.AspNetCore.Mvc.Filters; |
|||
|
|||
namespace Squidex.Pipeline |
|||
{ |
|||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] |
|||
public sealed class ApiCostsAttribute : ActionFilterAttribute |
|||
{ |
|||
private readonly double weight; |
|||
|
|||
private sealed class WeightFeature : IAppTrackingWeightFeature |
|||
{ |
|||
public double Weight { get; set; } |
|||
} |
|||
|
|||
public ApiCostsAttribute(double weight) |
|||
{ |
|||
this.weight = weight; |
|||
} |
|||
|
|||
public override void OnActionExecuting(ActionExecutingContext context) |
|||
{ |
|||
context.HttpContext.Features.Set<IAppTrackingWeightFeature>(new WeightFeature { Weight = weight }); |
|||
} |
|||
} |
|||
} |
|||
@ -1,50 +0,0 @@ |
|||
// ==========================================================================
|
|||
// AppTrackingFilter.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Diagnostics; |
|||
using Microsoft.AspNetCore.Mvc.Filters; |
|||
using Squidex.Infrastructure.UsageTracking; |
|||
|
|||
// ReSharper disable InvertIf
|
|||
|
|||
namespace Squidex.Pipeline |
|||
{ |
|||
public sealed class AppTrackingFilter : ActionFilterAttribute |
|||
{ |
|||
private readonly IUsageTracker usageTracker; |
|||
|
|||
public AppTrackingFilter(IUsageTracker usageTracker) |
|||
{ |
|||
this.usageTracker = usageTracker; |
|||
} |
|||
|
|||
public override void OnActionExecuting(ActionExecutingContext context) |
|||
{ |
|||
var appFeature = context.HttpContext.Features.Get<IAppFeature>(); |
|||
|
|||
if (appFeature?.App != null) |
|||
{ |
|||
context.HttpContext.Items["AppWatch"] = Stopwatch.StartNew(); |
|||
} |
|||
} |
|||
|
|||
public override void OnActionExecuted(ActionExecutedContext context) |
|||
{ |
|||
var appFeature = context.HttpContext.Features.Get<IAppFeature>(); |
|||
|
|||
if (appFeature?.App != null) |
|||
{ |
|||
var stopWatch = (Stopwatch)context.HttpContext.Items["AppWatch"]; |
|||
|
|||
stopWatch.Stop(); |
|||
|
|||
usageTracker.TrackAsync(appFeature.App.Id.ToString(), stopWatch.ElapsedMilliseconds); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,48 @@ |
|||
// ==========================================================================
|
|||
// AppTrackingMiddleware.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
using System.Diagnostics; |
|||
using System.Threading.Tasks; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Squidex.Infrastructure.UsageTracking; |
|||
|
|||
// ReSharper disable InvertIf
|
|||
|
|||
namespace Squidex.Pipeline |
|||
{ |
|||
public sealed class AppTrackingMiddleware |
|||
{ |
|||
private readonly RequestDelegate next; |
|||
private readonly IUsageTracker usageTracker; |
|||
|
|||
public AppTrackingMiddleware(RequestDelegate next, IUsageTracker usageTracker) |
|||
{ |
|||
this.next = next; |
|||
|
|||
this.usageTracker = usageTracker; |
|||
} |
|||
|
|||
public async Task Invoke(HttpContext context) |
|||
{ |
|||
var stopWatch = Stopwatch.StartNew(); |
|||
|
|||
await next(context); |
|||
|
|||
var appFeature = context.Features.Get<IAppFeature>(); |
|||
|
|||
if (appFeature?.App != null) |
|||
{ |
|||
stopWatch.Stop(); |
|||
|
|||
var weight = context.Features.Get<IAppTrackingWeightFeature>()?.Weight ?? 1; |
|||
|
|||
await usageTracker.TrackAsync(appFeature.App.Id.ToString(), weight, stopWatch.ElapsedMilliseconds); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,15 @@ |
|||
// ==========================================================================
|
|||
// IAppTrackingWeightFeature.cs
|
|||
// Squidex Headless CMS
|
|||
// ==========================================================================
|
|||
// Copyright (c) Squidex Group
|
|||
// All rights reserved.
|
|||
// ==========================================================================
|
|||
|
|||
namespace Squidex.Pipeline |
|||
{ |
|||
public interface IAppTrackingWeightFeature |
|||
{ |
|||
double Weight { get; } |
|||
} |
|||
} |
|||
Loading…
Reference in new issue