Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve counter performance. #1021

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions backend/src/Migrations/MigrationPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Migrations;

public sealed class MigrationPath : IMigrationPath
{
private const int CurrentVersion = 25;
private const int CurrentVersion = 26;
private readonly IServiceProvider serviceProvider;

public MigrationPath(IServiceProvider serviceProvider)
Expand Down Expand Up @@ -114,10 +114,16 @@ public MigrationPath(IServiceProvider serviceProvider)
}
}

// Version 13: Json refactoring
// Version 13: Json refactoring.
if (version < 13)
{
yield return serviceProvider.GetRequiredService<ConvertRuleEventsJson>();
}

// Version 27: New rule statistics using normal usage collection.
if (version < 26)
{
yield return serviceProvider.GetRequiredService<CopyRuleStatistics>();
}
}
}
66 changes: 66 additions & 0 deletions backend/src/Migrations/Migrations/MongoDb/CopyRuleStatistics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
using Squidex.Domain.Apps.Entities.Rules;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Migrations;
using Squidex.Infrastructure.MongoDb;

namespace Migrations.Migrations.MongoDb;

public sealed class CopyRuleStatistics : IMigration
{
private readonly IMongoDatabase database;
private readonly IRuleUsageTracker ruleUsageTracker;

[BsonIgnoreExtraElements]
public class Document
{
public DomainId AppId { get; private set; }

public DomainId RuleId { get; private set; }

public int NumFailed { get; private set; }

public int NumSucceeded { get; private set; }
}

public CopyRuleStatistics(IMongoDatabase database, IRuleUsageTracker ruleUsageTracker)
{
this.database = database;
this.ruleUsageTracker = ruleUsageTracker;
}

public async Task UpdateAsync(
CancellationToken ct)
{
var collectionName = "RuleStatistics";

// Do not create the collection if not needed.
if (!await database.CollectionExistsAsync(collectionName, ct))
{
return;
}

var collection = database.GetCollection<Document>(collectionName);

await foreach (var document in collection.Find(new BsonDocument()).ToAsyncEnumerable(ct))
{
await ruleUsageTracker.TrackAsync(
document.AppId,
document.RuleId,
default,
0,
document.NumSucceeded,
document.NumFailed,
ct);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,26 @@ async Task IRuleUsageTracker.TrackAsync(DomainId appId, DomainId ruleId, DateOnl

var tasks = new List<Task>
{
usageTracker.TrackAsync(date, appKey, ruleId.ToString(), counters, ct),
usageTracker.TrackAsync(SummaryDate, appKey, ruleId.ToString(), counters, ct)
};

if (date != default)
{
tasks.Add(usageTracker.TrackAsync(date, appKey, ruleId.ToString(), counters, ct));
}

var (_, _, teamId) = await GetPlanForAppAsync(appId, true, ct);

if (teamId != null)
{
var teamKey = TeamRulesKey(teamId.Value);

tasks.Add(usageTracker.TrackAsync(date, teamKey, appId.ToString(), counters, ct));
tasks.Add(usageTracker.TrackAsync(SummaryDate, teamKey, appId.ToString(), counters, ct));

if (date != default)
{
tasks.Add(usageTracker.TrackAsync(date, teamKey, appId.ToString(), counters, ct));
}
}

await Task.WhenAll(tasks);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ public Task TrackAsync(DateOnly date, string key, string? category, Counters cou

category = GetCategory(category);

#pragma warning disable MA0105 // Use the lambda parameters instead of using a closure
jobs.AddOrUpdate((key, category, date), counters, (k, p) => p.SumpUpCloned(counters));
#pragma warning restore MA0105 // Use the lambda parameters instead of using a closure
// Create a copy of the counters on add, so that we do not share it.
jobs.AddOrUpdate((key, category, date), (_, args) => new Counters(args), (_, v, args) => v.Merge(args), counters);

return Task.CompletedTask;
}
Expand Down Expand Up @@ -191,7 +190,7 @@ public async Task<Counters> GetAsync(string key, DateOnly fromDate, DateOnly toD

foreach (var usage in queried)
{
result.SumpUp(usage.Counters);
result.Merge(usage.Counters);
}

return result;
Expand Down
9 changes: 1 addition & 8 deletions backend/src/Squidex.Infrastructure/UsageTracking/Counters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,7 @@ public long GetInt64(string name)
return (long)value;
}

public Counters SumpUpCloned(Counters counters)
{
var result = new Counters(this);

return result.SumpUp(counters);
}

public Counters SumpUp(Counters source)
public Counters Merge(Counters source)
{
foreach (var (key, value) in source)
{
Expand Down
3 changes: 3 additions & 0 deletions backend/src/Squidex/Config/Domain/StoreServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ public static void AddSquidexStoreServices(this IServiceCollection services, ICo
services.AddTransientAs<ConvertOldSnapshotStores>()
.As<IMigration>();

services.AddTransientAs<CopyRuleStatistics>()
.As<IMigration>();

services.AddTransientAs(c => new DeleteContentCollections(GetDatabase(c, mongoContentDatabaseName)))
.As<IMigration>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ export class CodeEditorComponent extends StatefulControlComponent<{}, any> imple
printMargin: !this.singleLine,
showGutter: !this.singleLine,
});

this.aceEditor.commands.bindKey('Enter|Shift-Enter', this.singleLine ? 'null' : undefined);
}

private setValue(value: string) {
Expand Down