Skip to content

Commit bed3a32

Browse files
committed
feat(Correlator): Continue development of the Correlator app (WIP)
feat(CLI): Added commands to manage correlations, correlators and operators Signed-off-by: Charles d'Avernas <charles.davernas@neuroglia.io>
1 parent d4d5420 commit bed3a32

File tree

34 files changed

+920
-35
lines changed

34 files changed

+920
-35
lines changed

src/api/Synapse.Api.Application/Commands/Resources/Generic/CreateResourceCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class CreateResourceCommandHandler<TResource>(IResourceRepository reposit
4747
public virtual async Task<IOperationResult<TResource>> HandleAsync(CreateResourceCommand<TResource> command, CancellationToken cancellationToken)
4848
{
4949
if (command.Resource.GetName().Trim().EndsWith('-')) command.Resource.Metadata.Name = $"{command.Resource.GetName().Trim()}{Guid.NewGuid().ToString("N")[..15]}";
50-
var resource = await repository.AddAsync(command.Resource, false, cancellationToken);
50+
var resource = await repository.AddAsync(command.Resource, false, cancellationToken).ConfigureAwait(false);
5151
return new OperationResult<TResource>((int)HttpStatusCode.Created, resource);
5252
}
5353

src/api/Synapse.Api.Application/Queries/Resources/Generic/GetResourceQuery.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public class GetResourceQueryHandler<TResource>(IResourceRepository repository)
5959
/// <inheritdoc/>
6060
public virtual async Task<IOperationResult<TResource>> HandleAsync(GetResourceQuery<TResource> query, CancellationToken cancellationToken)
6161
{
62-
return this.Ok(await repository.GetAsync<TResource>(query.Name, query.Namespace, cancellationToken).ConfigureAwait(false));
62+
var resource = await repository.GetAsync<TResource>(query.Name, query.Namespace, cancellationToken).ConfigureAwait(false) ?? throw new ProblemDetailsException(ResourceProblemDetails.ResourceNotFound(new ResourceReference<TResource>(query.Name, query.Namespace)));
63+
return this.Ok(resource);
6364
}
6465

6566
}

src/api/Synapse.Api.Client.Core/Services/ISynapseApiClient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ namespace Synapse.Api.Client.Services;
1919
public interface ISynapseApiClient
2020
{
2121

22+
/// <summary>
23+
/// Gets the Synapse API used to manage <see cref="Correlation"/>s
24+
/// </summary>
25+
INamespacedResourceApiClient<Correlation> Correlations { get; }
26+
2227
/// <summary>
2328
/// Gets the Synapse API used to manage <see cref="Correlator"/>s
2429
/// </summary>

src/api/Synapse.Api.Client.Http/Services/SynapseHttpApiClient.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public SynapseHttpApiClient(IServiceProvider serviceProvider, ILoggerFactory log
6363
/// </summary>
6464
protected HttpClient HttpClient { get; }
6565

66+
/// <inheritdoc/>
67+
public INamespacedResourceApiClient<Correlation> Correlations { get; private set; } = null!;
68+
6669
/// <inheritdoc/>
6770
public INamespacedResourceApiClient<Correlator> Correlators { get; private set; } = null!;
6871

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright © 2024-Present Neuroglia SRL. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"),
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
using Neuroglia.Data.Infrastructure.ResourceOriented;
15+
using Synapse.Cli.Commands.Correlations;
16+
17+
namespace Synapse.Cli.Commands;
18+
19+
/// <summary>
20+
/// Represents the <see cref="Command"/> used to manage <see cref="Correlation"/>s
21+
/// </summary>
22+
public class CorrelationCommand
23+
: Command
24+
{
25+
26+
/// <summary>
27+
/// Gets the <see cref="CorrelationCommand"/>'s name
28+
/// </summary>
29+
public const string CommandName = "correlation";
30+
/// <summary>
31+
/// Gets the <see cref="CorrelationCommand"/>'s description
32+
/// </summary>
33+
public const string CommandDescription = "Manages correlations";
34+
35+
/// <inheritdoc/>
36+
public CorrelationCommand(IServiceProvider serviceProvider, ILoggerFactory loggerFactory, ISynapseApiClient api)
37+
: base(serviceProvider, loggerFactory, api, CommandName, CommandDescription)
38+
{
39+
this.AddAlias("correlations");
40+
this.AddAlias("corel");
41+
this.AddCommand(ActivatorUtilities.CreateInstance<CreateCorrelationCommand>(this.ServiceProvider));
42+
this.AddCommand(ActivatorUtilities.CreateInstance<ListCorrelationsCommand>(this.ServiceProvider));
43+
this.AddCommand(ActivatorUtilities.CreateInstance<DeleteCorrelationCommand>(this.ServiceProvider));
44+
}
45+
46+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright © 2024-Present Neuroglia SRL. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"),
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
namespace Synapse.Cli.Commands.Correlations;
15+
16+
/// <summary>
17+
/// Represents the <see cref="Command"/> used to create a new <see cref="Correlation"/>
18+
/// </summary>
19+
internal class CreateCorrelationCommand
20+
: Command
21+
{
22+
23+
/// <summary>
24+
/// Gets the <see cref="CreateCorrelationCommand"/>'s name
25+
/// </summary>
26+
public const string CommandName = "create";
27+
/// <summary>
28+
/// Gets the <see cref="CreateCorrelationCommand"/>'s description
29+
/// </summary>
30+
public const string CommandDescription = "Creates a new correlation.";
31+
32+
/// <inheritdoc/>
33+
public CreateCorrelationCommand(IServiceProvider serviceProvider, ILoggerFactory loggerFactory, ISynapseApiClient api)
34+
: base(serviceProvider, loggerFactory, api, CommandName, CommandDescription)
35+
{
36+
this.Add(new Argument<string>("name") { Description = "The name of the correlation to create." });
37+
this.Handler = CommandHandler.Create<string>(this.HandleAsync);
38+
}
39+
40+
/// <summary>
41+
/// Handles the <see cref="CreateCorrelationCommand"/>
42+
/// </summary>
43+
/// <param name="name">The name of the correlation to create</param>
44+
/// <returns>A new awaitable <see cref="Task"/></returns>
45+
public async Task HandleAsync(string name)
46+
{
47+
ArgumentException.ThrowIfNullOrWhiteSpace(name);
48+
await this.Api.Correlations.CreateAsync(new()
49+
{
50+
Metadata = new()
51+
{
52+
Name = name
53+
}
54+
});
55+
Console.WriteLine($"correlation/{name} created");
56+
}
57+
58+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright © 2024-Present Neuroglia SRL. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"),
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
namespace Synapse.Cli.Commands.Correlations;
15+
16+
/// <summary>
17+
/// Represents the <see cref="Command"/> used to delete a single <see cref="Correlation"/>
18+
/// </summary>
19+
internal class DeleteCorrelationCommand
20+
: Command
21+
{
22+
23+
/// <summary>
24+
/// Gets the <see cref="DeleteCorrelationCommand"/>'s name
25+
/// </summary>
26+
public const string CommandName = "delete";
27+
/// <summary>
28+
/// Gets the <see cref="DeleteCorrelationCommand"/>'s description
29+
/// </summary>
30+
public const string CommandDescription = "Deletes the specified correlation";
31+
32+
/// <inheritdoc/>
33+
public DeleteCorrelationCommand(IServiceProvider serviceProvider, ILoggerFactory loggerFactory, ISynapseApiClient api)
34+
: base(serviceProvider, loggerFactory, api, CommandName, CommandDescription)
35+
{
36+
this.AddAlias("del");
37+
this.Add(new Argument<string>("name") { Description = "The name of the correlation to delete" });
38+
this.Add(CommandOptions.Confirm);
39+
this.Handler = CommandHandler.Create<string, string, bool>(this.HandleAsync);
40+
}
41+
42+
/// <summary>
43+
/// Handles the <see cref="DeleteCorrelationCommand"/>
44+
/// </summary>
45+
/// <param name="name">The name of the correlation to delete</param>
46+
/// <param name="namespace">The namespace of the workflow to delete</param>
47+
/// <param name="y">A boolean indicating whether or not to ask for the user's confirmation</param>
48+
/// <returns>A new awaitable <see cref="Task"/></returns>
49+
public async Task HandleAsync(string name, string @namespace, bool y)
50+
{
51+
if (!y)
52+
{
53+
Console.Write($"Are you sure you wish to delete the correlation '{name}'? Press 'y' to confirm, or any other key to cancel: ");
54+
var inputKey = Console.ReadKey();
55+
Console.WriteLine();
56+
if (inputKey.Key != ConsoleKey.Y) return;
57+
}
58+
await this.Api.Correlations.DeleteAsync(name, @namespace);
59+
Console.WriteLine($"correlation/{name} deleted");
60+
}
61+
62+
static class CommandOptions
63+
{
64+
65+
public static Option<string> Namespace => new(["-n", "--namespace"], () => "default", "The namespace the correlation to delete belongs to");
66+
67+
public static Option<bool> Confirm => new(["-y", "--yes"], () => false, "Delete the correlation without prompting confirmation");
68+
69+
}
70+
71+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright © 2024-Present Neuroglia SRL. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"),
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
using Neuroglia.Data.Infrastructure.ResourceOriented;
15+
16+
namespace Synapse.Cli.Commands.Correlations;
17+
18+
/// <summary>
19+
/// Represents the <see cref="Command"/> used to list<see cref="Correlation"/>s
20+
/// </summary>
21+
internal class ListCorrelationsCommand
22+
: Command
23+
{
24+
25+
/// <summary>
26+
/// Gets the <see cref="ListCorrelationsCommand"/>'s name
27+
/// </summary>
28+
public const string CommandName = "list";
29+
/// <summary>
30+
/// Gets the <see cref="ListCorrelationsCommand"/>'s description
31+
/// </summary>
32+
public const string CommandDescription = "Lists correlations";
33+
34+
/// <inheritdoc/>
35+
public ListCorrelationsCommand(IServiceProvider serviceProvider, ILoggerFactory loggerFactory, ISynapseApiClient api)
36+
: base(serviceProvider, loggerFactory, api, CommandName, CommandDescription)
37+
{
38+
this.AddAlias("ls");
39+
this.Add(CommandOptions.Namespace);
40+
this.Handler = CommandHandler.Create(this.HandleAsync);
41+
}
42+
43+
/// <summary>
44+
/// Handles the <see cref="ListCorrelationsCommand"/>
45+
/// </summary>
46+
/// <param name="namespace">The namespace the workflow to list belong to</param>
47+
/// <returns>A new awaitable <see cref="Task"/></returns>
48+
public async Task HandleAsync(string @namespace)
49+
{
50+
var table = new Table();
51+
var isEmpty = true;
52+
table.Border(TableBorder.None);
53+
table.AddColumn("NAME");
54+
table.AddColumn("CREATED AT", column =>
55+
{
56+
column.Alignment = Justify.Center;
57+
});
58+
await foreach (var correlation in await this.Api.Correlations.ListAsync(@namespace))
59+
{
60+
isEmpty = false;
61+
table.AddRow
62+
(
63+
correlation.GetName(),
64+
correlation.Metadata.CreationTimestamp.ToString()!
65+
);
66+
}
67+
if (isEmpty)
68+
{
69+
AnsiConsole.WriteLine($"No resource found");
70+
return;
71+
}
72+
AnsiConsole.Write(table);
73+
}
74+
75+
static class CommandOptions
76+
{
77+
78+
public static Option<string> Namespace => new(["-n", "--namespace"], () => string.Empty, "The namespace the correlation to list belong to.");
79+
80+
}
81+
82+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright © 2024-Present Neuroglia SRL. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"),
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
using Neuroglia.Data.Infrastructure.ResourceOriented;
15+
using Synapse.Cli.Commands.Correlators;
16+
17+
namespace Synapse.Cli.Commands;
18+
19+
/// <summary>
20+
/// Represents the <see cref="Command"/> used to manage <see cref="Correlator"/>s
21+
/// </summary>
22+
public class CorrelatorCommand
23+
: Command
24+
{
25+
26+
/// <summary>
27+
/// Gets the <see cref="OperatorCommand"/>'s name
28+
/// </summary>
29+
public const string CommandName = "correlator";
30+
/// <summary>
31+
/// Gets the <see cref="OperatorCommand"/>'s description
32+
/// </summary>
33+
public const string CommandDescription = "Manages correlators";
34+
35+
/// <inheritdoc/>
36+
public CorrelatorCommand(IServiceProvider serviceProvider, ILoggerFactory loggerFactory, ISynapseApiClient api)
37+
: base(serviceProvider, loggerFactory, api, CommandName, CommandDescription)
38+
{
39+
this.AddAlias("correlators");
40+
this.AddAlias("cor");
41+
this.AddCommand(ActivatorUtilities.CreateInstance<GetCorrelatorCommand>(this.ServiceProvider));
42+
this.AddCommand(ActivatorUtilities.CreateInstance<ListCorrelatorsCommand>(this.ServiceProvider));
43+
}
44+
45+
}

0 commit comments

Comments
 (0)