mirror of https://github.com/Squidex/squidex.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
2.4 KiB
70 lines
2.4 KiB
// ==========================================================================
|
|
// Squidex Headless CMS
|
|
// ==========================================================================
|
|
// Copyright (c) Squidex UG (haftungsbeschraenkt)
|
|
// All rights reserved. Licensed under the MIT license.
|
|
// ==========================================================================
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Squidex.Infrastructure;
|
|
using Squidex.Infrastructure.UsageTracking;
|
|
|
|
#pragma warning disable CS0649
|
|
|
|
namespace Squidex.Domain.Apps.Entities.Assets
|
|
{
|
|
public partial class AssetUsageTracker : IAssetUsageTracker
|
|
{
|
|
private const string CounterTotalCount = "TotalAssets";
|
|
private const string CounterTotalSize = "TotalSize";
|
|
private static readonly DateTime SummaryDate;
|
|
private readonly IUsageTracker usageTracker;
|
|
|
|
public AssetUsageTracker(IUsageTracker usageTracker)
|
|
{
|
|
Guard.NotNull(usageTracker, nameof(usageTracker));
|
|
|
|
this.usageTracker = usageTracker;
|
|
}
|
|
|
|
public async Task<long> GetTotalSizeAsync(DomainId appId)
|
|
{
|
|
var key = GetKey(appId);
|
|
|
|
var counters = await usageTracker.GetAsync(key, SummaryDate, SummaryDate, null);
|
|
|
|
return counters.GetInt64(CounterTotalSize);
|
|
}
|
|
|
|
public async Task<IReadOnlyList<AssetStats>> QueryAsync(DomainId appId, DateTime fromDate, DateTime toDate)
|
|
{
|
|
var enriched = new List<AssetStats>();
|
|
|
|
var usages = await usageTracker.QueryAsync(GetKey(appId), fromDate, toDate);
|
|
|
|
if (usages.TryGetValue("*", out var byCategory1))
|
|
{
|
|
AddCounters(enriched, byCategory1);
|
|
}
|
|
else if (usages.TryGetValue("Default", out var byCategory2))
|
|
{
|
|
AddCounters(enriched, byCategory2);
|
|
}
|
|
|
|
return enriched;
|
|
}
|
|
|
|
private static void AddCounters(List<AssetStats> enriched, List<(DateTime, Counters)> details)
|
|
{
|
|
foreach (var (date, counters) in details)
|
|
{
|
|
var totalCount = counters.GetInt64(CounterTotalCount);
|
|
var totalSize = counters.GetInt64(CounterTotalSize);
|
|
|
|
enriched.Add(new AssetStats(date, totalCount, totalSize));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|