|
3 | 3 | // See the LICENSE file in the project root for more information.
|
4 | 4 |
|
5 | 5 | using System;
|
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
6 | 8 | using Elastic.Transport;
|
7 | 9 |
|
8 | 10 | namespace Elastic.Clients.Elasticsearch;
|
9 | 11 |
|
| 12 | +/// <summary> |
| 13 | +/// Represents a collection of unique metric names to be included in URL paths to limit the request. |
| 14 | +/// </summary> |
10 | 15 | public sealed class Metrics : IEquatable<Metrics>, IUrlParameter
|
11 | 16 | {
|
12 |
| - // TODO: Complete this |
| 17 | + private static readonly HashSet<string> EmptyMetrics = new(); |
13 | 18 |
|
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"); |
15 | 23 |
|
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; |
17 | 31 |
|
18 |
| - //internal Metrics(NodesInfoMetric metric) => Value = metric; |
| 32 | + Values = new HashSet<string>() |
| 33 | + { |
| 34 | + metric |
| 35 | + }; |
| 36 | + } |
19 | 37 |
|
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; |
21 | 45 |
|
22 |
| - //internal Metrics(WatcherStatsMetric metric) => Value = metric; |
| 46 | + Values = new HashSet<string>(metrics); |
| 47 | + } |
23 | 48 |
|
24 |
| - //internal Metrics(NodesUsageMetric metric) => Value = metric; |
| 49 | + private HashSet<string> Values { get; } |
25 | 50 |
|
26 |
| - internal Enum Value { get; } |
| 51 | + /// <inheritdoc /> |
| 52 | + public bool Equals(Metrics other) |
| 53 | + { |
| 54 | + if (other is null) return false; |
27 | 55 |
|
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 | + } |
29 | 59 |
|
30 |
| - string IUrlParameter.GetString(ITransportConfiguration settings) => string.Empty; // TODO Value.GetStringValue(); |
| 60 | + string IUrlParameter.GetString(ITransportConfiguration settings) => GetString(); |
31 | 61 |
|
32 |
| - //public static implicit operator Metrics(IndicesStatsMetric metric) => new Metrics(metric); |
| 62 | + /// <inheritdoc /> |
| 63 | + public override string ToString() => GetString(); |
33 | 64 |
|
34 |
| - //public static implicit operator Metrics(NodesStatsMetric metric) => new Metrics(metric); |
| 65 | + private string GetString() |
| 66 | + { |
| 67 | + if (Values == EmptyMetrics) |
| 68 | + return string.Empty; |
35 | 69 |
|
36 |
| - //public static implicit operator Metrics(NodesInfoMetric metric) => new Metrics(metric); |
| 70 | + return string.Join(",", Values); |
| 71 | + } |
37 | 72 |
|
38 |
| - //public static implicit operator Metrics(ClusterStateMetric metric) => new Metrics(metric); |
| 73 | + /// <inheritdoc /> |
| 74 | + public override int GetHashCode() => Values != null ? Values.GetHashCode() : 0; |
39 | 75 |
|
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); |
47 | 78 |
|
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); |
49 | 81 |
|
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; |
51 | 86 |
|
52 |
| - public static bool operator !=(Metrics left, Metrics right) => !Equals(left, right); |
| 87 | + return Equals(metrics); |
| 88 | + } |
53 | 89 | }
|
0 commit comments