Skip to content

Commit 16da172

Browse files
authored
Merge pull request #298 from KodrAus/feat/retention-policy-stream
Support signal expressions when creating retention policies
2 parents 5f74da2 + 27ad2a0 commit 16da172

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/SeqCli/Cli/Commands/RetentionPolicy/CreateCommand.cs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414

1515
using System;
1616
using System.Threading.Tasks;
17+
using Seq.Api.Model.Signals;
1718
using SeqCli.Cli.Features;
1819
using SeqCli.Config;
1920
using SeqCli.Connection;
21+
using SeqCli.Signals;
2022
using SeqCli.Syntax;
2123
using SeqCli.Util;
2224
using Serilog;
@@ -33,7 +35,8 @@ class CreateCommand : Command
3335
readonly OutputFormatFeature _output;
3436

3537
string? _afterDuration;
36-
bool _deleteAllEvents; // Currently the only supported option
38+
bool _deleteAllEvents;
39+
string? _deleteMatchingSignal;
3740

3841
public CreateCommand(SeqConnectionFactory connectionFactory, SeqCliConfig config)
3942
{
@@ -48,6 +51,15 @@ public CreateCommand(SeqConnectionFactory connectionFactory, SeqCliConfig config
4851
"delete-all-events",
4952
"The policy should delete all events (currently the only supported option)",
5053
_ => _deleteAllEvents = true);
54+
55+
Options.Add(
56+
"delete=",
57+
"Stream incoming events to this app instance as they're ingested; optionally accepts a signal expression limiting which events should be streamed",
58+
s =>
59+
{
60+
_deleteMatchingSignal = s;
61+
}
62+
);
5163

5264
_connection = Enable<ConnectionFeature>();
5365
_output = Enable(new OutputFormatFeature(config.Output));
@@ -57,12 +69,29 @@ protected override async Task<int> Run()
5769
{
5870
var connection = _connectionFactory.Connect(_connection);
5971

60-
if (!_deleteAllEvents)
72+
SignalExpressionPart? removedSignalExpression;
73+
74+
// Exactly one of `delete-all-events` or `delete` must be specified
75+
if (_deleteAllEvents)
76+
{
77+
if (!string.IsNullOrEmpty(_deleteMatchingSignal))
78+
{
79+
Log.Error("Only one of the `delete-all-events` or `delete` options may be specified");
80+
return 1;
81+
}
82+
83+
removedSignalExpression = null;
84+
}
85+
else if (string.IsNullOrEmpty(_deleteMatchingSignal))
6186
{
62-
Log.Error("The `delete-all-events` option must be specified");
87+
Log.Error("Either the `delete-all-events` or `delete` options must be specified");
6388
return 1;
6489
}
65-
90+
else
91+
{
92+
removedSignalExpression = SignalExpressionParser.ParseExpression(_deleteMatchingSignal!);
93+
}
94+
6695
if (_afterDuration == null)
6796
{
6897
Log.Error("A duration must be specified using `after`");
@@ -73,6 +102,7 @@ protected override async Task<int> Run()
73102

74103
var policy = await connection.RetentionPolicies.TemplateAsync();
75104
policy.RetentionTime = duration;
105+
policy.RemovedSignalExpression = removedSignalExpression;
76106

77107
policy = await connection.RetentionPolicies.AddAsync(policy);
78108

test/SeqCli.EndToEnd/RetentionPolicy/RetentionPolicyBasicsTestCase.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,15 @@ public async Task ExecuteAsync(SeqConnection connection, ILogger logger, CliComm
3535

3636
exit = runner.Exec("retention list", $"-i {id}");
3737
Assert.Equal(1, exit);
38+
39+
var deleteSignal = "signal-m33303,(signal-m33301~signal-m33302)";
40+
exit = runner.Exec("retention create", $"--after 10h --delete \"{deleteSignal}\"");
41+
Assert.Equal(0, exit);
42+
43+
exit = runner.Exec("retention create", "--after 10h");
44+
Assert.Equal(1, exit);
45+
46+
exit = runner.Exec("retention create", $"--after 10h --delete-all-events --delete \"{deleteSignal}\"");
47+
Assert.Equal(1, exit);
3848
}
3949
}

0 commit comments

Comments
 (0)