Skip to content

Commit ed1c5e2

Browse files
committed
introduce IPluginGraphAttributes to make configuring graph attributes more flexible
1 parent 41a19eb commit ed1c5e2

File tree

6 files changed

+59
-62
lines changed

6 files changed

+59
-62
lines changed

src/Smdn.Net.MuninNode/Smdn.Net.MuninNode/NodeBase.cs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -822,24 +822,11 @@ CancellationToken cancellationToken
822822
);
823823
}
824824

825-
var graphAttrs = plugin.GraphAttributes;
826-
827-
var responseLines = new List<string>() {
828-
$"graph_title {graphAttrs.Title}",
829-
$"graph_category {graphAttrs.Category}",
830-
$"graph_args {graphAttrs.Arguments}",
831-
$"graph_scale {(graphAttrs.Scale ? "yes" : "no")}",
832-
$"graph_vlabel {graphAttrs.VerticalLabel}",
833-
};
825+
var responseLines = new List<string>(capacity: 20);
834826

835-
if (graphAttrs.UpdateRate.HasValue)
836-
responseLines.Add($"update_rate {(int)graphAttrs.UpdateRate.Value.TotalSeconds}");
837-
if (graphAttrs.Width.HasValue)
838-
responseLines.Add($"graph_width {graphAttrs.Width.Value}");
839-
if (graphAttrs.Height.HasValue)
840-
responseLines.Add($"graph_height {graphAttrs.Height.Value}");
841-
if (!string.IsNullOrEmpty(graphAttrs.Order))
842-
responseLines.Add($"graph_order {graphAttrs.Order}");
827+
responseLines.AddRange(
828+
plugin.GraphAttributes.EnumerateAttributes()
829+
);
843830

844831
// The fields referenced by {fieldname}.negative must be defined ahread of others,
845832
// and thus lists the negative field settings first.

src/Smdn.Net.MuninNode/Smdn.Net.MuninPlugin/IPlugin.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ public interface IPlugin {
1212
/// <remarks>This value is used as the plugin name returned by the 'list' argument, or the plugin name specified by the 'fetch' argument.</remarks>
1313
string Name { get; }
1414

15-
/// <summary>Gets a <see cref="PluginGraphAttributes"/> that represents the graph attributes when the field values (<see cref="IPluginField"/>) are drawn as a graph.</summary>
15+
/// <summary>Gets a <see cref="IPluginGraphAttributes"/> that represents the graph attributes when the field values (<see cref="IPluginField"/>) are drawn as a graph.</summary>
16+
/// <seealso cref="IPluginGraphAttributes"/>
1617
/// <seealso cref="PluginGraphAttributes"/>
17-
PluginGraphAttributes GraphAttributes { get; }
18+
IPluginGraphAttributes GraphAttributes { get; }
1819

1920
/// <summary>Gets a <see cref="IPluginDataSource"/> that serves as the data source for the plugin.</summary>
2021
/// <seealso cref="IPluginDataSource"/>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// SPDX-FileCopyrightText: 2024 smdn <smdn@smdn.jp>
2+
// SPDX-License-Identifier: MIT
3+
using System.Collections.Generic;
4+
5+
namespace Smdn.Net.MuninPlugin;
6+
7+
/// <summary>
8+
/// Provides an interface that abstracts the plugin graph attributes, related to the drawing of a single graph.
9+
/// </summary>
10+
/// <seealso href="https://guide.munin-monitoring.org/en/latest/reference/plugin.html#global-attributes">Plugin reference - Global attributes</seealso>
11+
public interface IPluginGraphAttributes {
12+
/// <summary>
13+
/// Enumerates plugin graph attributes defined by types that implement this interface.
14+
/// </summary>
15+
/// <returns><see cref="IEnumerable{String}"/> that enumerates graph attributes.</returns>
16+
IEnumerable<string> EnumerateAttributes();
17+
}

src/Smdn.Net.MuninNode/Smdn.Net.MuninPlugin/Plugin.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class Plugin : IPlugin, IPluginDataSource, INodeSessionCallback {
1515
public IReadOnlyCollection<IPluginField> Fields { get; }
1616

1717
#pragma warning disable CA1033
18+
IPluginGraphAttributes IPlugin.GraphAttributes => GraphAttributes;
19+
1820
IPluginDataSource IPlugin.DataSource => this;
1921

2022
IReadOnlyCollection<IPluginField> IPluginDataSource.Fields => Fields;

src/Smdn.Net.MuninNode/Smdn.Net.MuninPlugin/PluginGraphAttributes.cs

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ namespace Smdn.Net.MuninPlugin;
1111
/// Defines graph attributes that should be returned when the plugin is called with the 'config' argument.
1212
/// This type represents the collection of 'field name attributes'.
1313
/// </summary>
14+
/// <seealso cref="IPluginGraphAttributes"/>
1415
/// <seealso href="https://guide.munin-monitoring.org/en/latest/reference/plugin.html#global-attributes">Plugin reference - Global attributes</seealso>
15-
public sealed class PluginGraphAttributes {
16+
public sealed class PluginGraphAttributes : IPluginGraphAttributes {
1617
/// <summary>Gets a value for the <c>graph_title</c>.</summary>
1718
/// <seealso href="https://guide.munin-monitoring.org/en/latest/reference/plugin.html#graph-title">Plugin reference - Global attributes - graph_title</seealso>
1819
public string Title { get; }
@@ -56,45 +57,17 @@ public PluginGraphAttributes(
5657
string category,
5758
string verticalLabel,
5859
bool scale,
59-
string arguments,
60-
TimeSpan? updateRate = null,
61-
int? width = null,
62-
int? height = null
63-
)
64-
: this(
65-
title: title,
66-
category: category,
67-
verticalLabel: verticalLabel,
68-
scale: scale,
69-
arguments: arguments,
70-
updateRate: updateRate,
71-
width: width,
72-
height: height,
73-
order: null
74-
)
75-
{
76-
}
77-
78-
[Obsolete("This member will be deprecated in future version.")]
79-
public PluginGraphAttributes(
80-
string title,
81-
string category,
82-
string verticalLabel,
83-
bool scale,
84-
string arguments,
85-
TimeSpan updateRate,
86-
int? width = null,
87-
int? height = null
60+
string arguments
8861
)
8962
: this(
9063
title: title,
9164
category: category,
9265
verticalLabel: verticalLabel,
9366
scale: scale,
9467
arguments: arguments,
95-
updateRate: updateRate,
96-
width: width,
97-
height: height,
68+
updateRate: null,
69+
width: null,
70+
height: null,
9871
order: null
9972
)
10073
{
@@ -147,4 +120,22 @@ public PluginGraphAttributes(
147120

148121
UpdateRate = updateRate;
149122
}
123+
124+
public IEnumerable<string> EnumerateAttributes()
125+
{
126+
yield return $"graph_title {Title}";
127+
yield return $"graph_category {Category}";
128+
yield return $"graph_args {Arguments}";
129+
yield return $"graph_scale {(Scale ? "yes" : "no")}";
130+
yield return $"graph_vlabel {VerticalLabel}";
131+
132+
if (UpdateRate.HasValue)
133+
yield return $"update_rate {(int)UpdateRate.Value.TotalSeconds}";
134+
if (Width.HasValue)
135+
yield return $"graph_width {Width.Value}";
136+
if (Height.HasValue)
137+
yield return $"graph_height {Height.Value}";
138+
if (!string.IsNullOrEmpty(Order))
139+
yield return $"graph_order {Order}";
140+
}
150141
}

tests/Smdn.Net.MuninNode/Smdn.Net.MuninNode/NodeBase.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ await reader.ReadLineAsync(cancellationToken),
289289

290290
private class PseudoPluginWithSessionCallback : IPlugin, INodeSessionCallback {
291291
public string Name => throw new NotImplementedException();
292-
public PluginGraphAttributes GraphAttributes => throw new NotImplementedException();
292+
public IPluginGraphAttributes GraphAttributes => throw new NotImplementedException();
293293
public IPluginDataSource DataSource => throw new NotImplementedException();
294294

295295
private readonly bool setSessionCallbackNull;
@@ -683,7 +683,7 @@ private static System.Collections.IEnumerable YieldTestCases_ProcessCommandAsync
683683
"config plugin1",
684684
new Action<IReadOnlyList<string>>(
685685
responseLines => {
686-
AssertGraphConfigurations(responseLines, plugins[0]);
686+
AssertGraphConfigurations(responseLines, (PluginGraphAttributes)plugins[0].GraphAttributes);
687687

688688
// plugin1field1
689689
Assert.That(responseLines, Has.Member("plugin1field1.label plugin1field1"), "plugin1field1.label");
@@ -701,7 +701,7 @@ private static System.Collections.IEnumerable YieldTestCases_ProcessCommandAsync
701701
"config plugin2",
702702
new Action<IReadOnlyList<string>>(
703703
responseLines => {
704-
AssertGraphConfigurations(responseLines, plugins[1]);
704+
AssertGraphConfigurations(responseLines, (PluginGraphAttributes)plugins[1].GraphAttributes);
705705

706706
// plugin2field1
707707
Assert.That(responseLines, Has.Member("plugin2field1.label plugin2field1"), "plugin2field1.label");
@@ -712,17 +712,16 @@ private static System.Collections.IEnumerable YieldTestCases_ProcessCommandAsync
712712

713713
static void AssertGraphConfigurations(
714714
IReadOnlyList<string> responseLines,
715-
IPlugin expectedPlugin
715+
PluginGraphAttributes attributes
716716
)
717717
{
718-
var graph = expectedPlugin.GraphAttributes;
719-
var scaleString = graph.Scale ? "yes" : "no";
718+
var scaleString = attributes.Scale ? "yes" : "no";
720719

721-
Assert.That(responseLines, Has.Member($"graph_title {graph.Title}"), "graph_title");
722-
Assert.That(responseLines, Has.Member($"graph_category {graph.Category}"), "graph_category");
723-
Assert.That(responseLines, Has.Member($"graph_args {graph.Arguments}"), "graph_args");
720+
Assert.That(responseLines, Has.Member($"graph_title {attributes.Title}"), "graph_title");
721+
Assert.That(responseLines, Has.Member($"graph_category {attributes.Category}"), "graph_category");
722+
Assert.That(responseLines, Has.Member($"graph_args {attributes.Arguments}"), "graph_args");
724723
Assert.That(responseLines, Has.Member($"graph_scale {scaleString}"), "graph_scale");
725-
Assert.That(responseLines, Has.Member($"graph_vlabel {graph.VerticalLabel}"), "graph_vlabel");
724+
Assert.That(responseLines, Has.Member($"graph_vlabel {attributes.VerticalLabel}"), "graph_vlabel");
726725
}
727726
}
728727

0 commit comments

Comments
 (0)