Skip to content

Commit 0c82270

Browse files
authored
Implement metrics (#7130)
1 parent b316bb5 commit 0c82270

File tree

2 files changed

+127
-24
lines changed

2 files changed

+127
-24
lines changed

src/Elastic.Clients.Elasticsearch/Core/Infer/Metric/Metrics.cs

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,87 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
68
using Elastic.Transport;
79

810
namespace Elastic.Clients.Elasticsearch;
911

12+
/// <summary>
13+
/// Represents a collection of unique metric names to be included in URL paths to limit the request.
14+
/// </summary>
1015
public sealed class Metrics : IEquatable<Metrics>, IUrlParameter
1116
{
12-
// TODO: Complete this
17+
private static readonly HashSet<string> EmptyMetrics = new();
1318

14-
//internal Metrics(IndicesStatsMetric metric) => Value = metric;
19+
/// <summary>
20+
/// An instance of <see cref="Metrics"/> representing all statistics.
21+
/// </summary>
22+
public static Metrics All { get; } = new("_all");
1523

16-
//internal Metrics(NodesStatsMetric metric) => Value = metric;
24+
/// <summary>
25+
/// Initializes a new instance of the <see cref="Metrics"/> class containing a single metric name.
26+
/// </summary>
27+
public Metrics(string metric)
28+
{
29+
if (string.IsNullOrEmpty(metric))
30+
Values = EmptyMetrics;
1731

18-
//internal Metrics(NodesInfoMetric metric) => Value = metric;
32+
Values = new HashSet<string>()
33+
{
34+
metric
35+
};
36+
}
1937

20-
//internal Metrics(ClusterStateMetric metric) => Value = metric;
38+
/// <summary>
39+
/// Initializes a new instance of the <see cref="Metrics"/> class containing a collection of metric names.
40+
/// </summary>
41+
public Metrics(IEnumerable<string> metrics)
42+
{
43+
if (metrics is null)
44+
Values = EmptyMetrics;
2145

22-
//internal Metrics(WatcherStatsMetric metric) => Value = metric;
46+
Values = new HashSet<string>(metrics);
47+
}
2348

24-
//internal Metrics(NodesUsageMetric metric) => Value = metric;
49+
private HashSet<string> Values { get; }
2550

26-
internal Enum Value { get; }
51+
/// <inheritdoc />
52+
public bool Equals(Metrics other)
53+
{
54+
if (other is null) return false;
2755

28-
public bool Equals(Metrics other) => Value.Equals(other.Value);
56+
// Equality is true when the metrics names in both instances are equal, regardless of their order in the set.
57+
return Values.OrderBy(t => t).SequenceEqual(other.Values.OrderBy(t => t));
58+
}
2959

30-
string IUrlParameter.GetString(ITransportConfiguration settings) => string.Empty; // TODO Value.GetStringValue();
60+
string IUrlParameter.GetString(ITransportConfiguration settings) => GetString();
3161

32-
//public static implicit operator Metrics(IndicesStatsMetric metric) => new Metrics(metric);
62+
/// <inheritdoc />
63+
public override string ToString() => GetString();
3364

34-
//public static implicit operator Metrics(NodesStatsMetric metric) => new Metrics(metric);
65+
private string GetString()
66+
{
67+
if (Values == EmptyMetrics)
68+
return string.Empty;
3569

36-
//public static implicit operator Metrics(NodesInfoMetric metric) => new Metrics(metric);
70+
return string.Join(",", Values);
71+
}
3772

38-
//public static implicit operator Metrics(ClusterStateMetric metric) => new Metrics(metric);
73+
/// <inheritdoc />
74+
public override int GetHashCode() => Values != null ? Values.GetHashCode() : 0;
3975

40-
//public static implicit operator Metrics(WatcherStatsMetric metric) => new Metrics(metric);
41-
42-
//public static implicit operator Metrics(NodesUsageMetric metric) => new Metrics(metric);
43-
44-
public bool Equals(Enum other) => Value.Equals(other);
45-
46-
public override bool Equals(object obj) => obj is Enum e ? Equals(e) : obj is Metrics m && Equals(m.Value);
76+
public static bool operator ==(Metrics left, Metrics right) => Equals(left, right);
77+
public static bool operator !=(Metrics left, Metrics right) => !Equals(left, right);
4778

48-
public override int GetHashCode() => Value != null ? Value.GetHashCode() : 0;
79+
public static implicit operator Metrics(string metric) => new(metric);
80+
public static implicit operator Metrics(string[] metrics) => new(metrics);
4981

50-
public static bool operator ==(Metrics left, Metrics right) => Equals(left, right);
82+
/// <inheritdoc />
83+
public override bool Equals(object obj)
84+
{
85+
if (obj is not Metrics metrics) return false;
5186

52-
public static bool operator !=(Metrics left, Metrics right) => !Equals(left, right);
87+
return Equals(metrics);
88+
}
5389
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Tests.Core.Extensions;
6+
7+
using M = Elastic.Clients.Elasticsearch.Metrics;
8+
9+
namespace Tests.Common.UrlParameters.Metrics;
10+
11+
public class MetricsTests
12+
{
13+
[U]
14+
public void Equal()
15+
{
16+
var metrics = M.All;
17+
18+
M[] equal = { M.All };
19+
20+
foreach (var value in equal)
21+
{
22+
(value == metrics).ShouldBeTrue(value);
23+
value.Should().Be(metrics);
24+
}
25+
26+
metrics.Should().Be(M.All);
27+
}
28+
29+
[U]
30+
public void SequenceEqual()
31+
{
32+
M metricsOne = new[] { "completion", "merge" };
33+
M metricsTwo = new[] { "merge", "completion" };
34+
35+
(metricsOne == metricsTwo).Should().BeTrue();
36+
metricsOne.Should().Be(metricsTwo);
37+
}
38+
39+
[U]
40+
public void ToStringOverride()
41+
{
42+
M metrics = new[] { "completion", "merge" };
43+
metrics.ToString().Should().Be("completion,merge");
44+
}
45+
46+
[U]
47+
public void NotEqual()
48+
{
49+
var metrics = M.All;
50+
51+
M[] notEqual = { "completion", "merge" };
52+
53+
foreach (var value in notEqual)
54+
{
55+
(value != metrics).ShouldBeTrue(value);
56+
value.Should().NotBe(metrics);
57+
}
58+
}
59+
60+
[U]
61+
public void Null()
62+
{
63+
var value = M.All;
64+
(value == null).Should().BeFalse();
65+
(null == value).Should().BeFalse();
66+
}
67+
}

0 commit comments

Comments
 (0)