Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CA-386317: Add localization support for data-source descriptions #3262

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions XenAdmin/Controls/CustomDataGraph/DataPlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,10 @@ protected override void OnDrawToBuffer(PaintEventArgs paintEventArgs)
SizeF labelsize = new SizeF(0,0);
if (SelectedPoint != null && DataKey.SelectedDataSet != null)
{
string label = string.Format("{0} - {1} = {2}",
var friendlyName = Helpers.GetFriendlyDataSourceName(DataKey.SelectedDataSet.DataSourceName, DataKey.SelectedDataSet.XenObject);
var label = string.Format("{0} - {1} = {2}",
DataPlotNav.XRange.GetString(SelectedPoint.X + ArchiveMaintainer.ClientServerOffset.Ticks),
DataKey.SelectedDataSet.FriendlyName,
friendlyName,
SelectedPoint.Y >= 0 ? SelectedYRange.GetString(SelectedPoint.Y) : Messages.GRAPHS_NO_DATA);
labelsize = paintEventArgs.Graphics.MeasureString(label,Palette.LabelFont);
paintEventArgs.Graphics.DrawString(label, Palette.LabelFont, Palette.LabelBrush, SlightlySmaller.Right - labelsize.Width, SlightlySmaller.Top - (labelsize.Height + 1));
Expand Down
26 changes: 9 additions & 17 deletions XenAdmin/Controls/CustomDataGraph/DataSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,23 @@ public class DataSet : IComparable<DataSet>
public readonly IXenObject XenObject;
public readonly string Id = "";
public readonly string DataSourceName;
public string FriendlyName { get; }
private readonly int _multiplyingFactor = 1;
public readonly DataRange CustomYRange = new DataRange(1, 0, 1, Unit.None, RangeScaleMode.Auto);
public bool Hide { get; }

public DataSet(IXenObject xo, bool hide, string datasourceName, List<Data_source> datasources)
public DataSet(IXenObject xo, bool hide, string dataSourceName, List<Data_source> dataSources)
{
XenObject = xo;
Hide = datasourceName == "memory" || datasourceName == "memory_total_kib" || hide;
Hide = dataSourceName == "memory" || dataSourceName == "memory_total_kib" || hide;

DataSourceName = datasourceName;
DataSourceName = dataSourceName;

if (xo is Host host)
Id = $"host:{host.uuid}:{datasourceName}";
Id = $"host:{host.uuid}:{dataSourceName}";
else if (xo is VM vm)
Id = $"vm:{vm.uuid}:{datasourceName}";
Id = $"vm:{vm.uuid}:{dataSourceName}";

if (datasourceName == "memory_free_kib")
FriendlyName = Helpers.GetFriendlyDataSourceName("memory_used_kib", xo);
else if (datasourceName == "memory_internal_free")
FriendlyName = Helpers.GetFriendlyDataSourceName("memory_internal_used", xo);
else
FriendlyName = Helpers.GetFriendlyDataSourceName(datasourceName, xo);

var units = datasources.FirstOrDefault(d => datasourceName == d.name_label)?.units;
var units = dataSources.FirstOrDefault(d => dataSourceName == d.name_label)?.units;

switch (units)
{
Expand Down Expand Up @@ -152,7 +144,7 @@ public DataSet(IXenObject xo, bool hide, string datasourceName, List<Data_source
break;
}

if (datasourceName == "memory_free_kib" || datasourceName == "memory_internal_free")
if (dataSourceName == "memory_free_kib" || dataSourceName == "memory_internal_free")
{
var max = GetMemoryMax(xo);
var resolution = GetMemoryResolution(max);
Expand Down Expand Up @@ -301,7 +293,7 @@ private int GetStart(List<DataPoint> points, long p, int start, int end)

public override string ToString()
{
return FriendlyName;
return Helpers.GetFriendlyDataSourceName(DataSourceName, XenObject);
}

public DataPoint OnMouseMove(MouseActionArgs args)
Expand Down Expand Up @@ -558,7 +550,7 @@ public int CompareTo(DataSet other)

int comp = DisplayArea.CompareTo(other.DisplayArea);
if (comp == 0)
return StringUtility.NaturalCompare(FriendlyName, other.FriendlyName);
return StringUtility.NaturalCompare(this.ToString(), other.ToString());
return comp;
}

Expand Down
29 changes: 13 additions & 16 deletions XenAdmin/Controls/CustomDataGraph/GraphHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public DesignedGraph()
public DesignedGraph(DesignedGraph sourceGraph) : this()
{
DisplayName = sourceGraph.DisplayName;
foreach (DataSourceItem dsi in sourceGraph.DataSourceItems)
foreach (var dsi in sourceGraph.DataSourceItems)
{
DataSourceItems.Add(new DataSourceItem(dsi.DataSource, dsi.ToString(), dsi.Color, dsi.Id));
DataSourceItems.Add(new DataSourceItem(dsi.DataSource, dsi.FriendlyName, dsi.FriendlyDescription, dsi.Color, dsi.Id));
}
}

Expand Down Expand Up @@ -126,17 +126,19 @@ public class DataSourceItem : IComparable<DataSourceItem>, IEquatable<DataSource

public bool Enabled { get; set; }
public bool Hidden { get; }
private readonly string friendlyName;
public string FriendlyName { get; }
public string FriendlyDescription { get; }
public Color Color;
public bool ColorChanged;
public string Id { get; }
public Helpers.DataSourceCategory Category { get; }

public DataSourceItem(Data_source ds, string friendlyname, Color color, string id)
public DataSourceItem(Data_source ds, string friendlyName, string friendlyDescription, Color color, string id)
{
DataSource = ds;
Enabled = DataSource.enabled;
friendlyName = friendlyname;
FriendlyName = friendlyName;
FriendlyDescription = friendlyDescription;
Color = color;
Id = id;
Hidden = string.IsNullOrEmpty(ds.units) || ds.units == "unknown";
Expand All @@ -153,12 +155,12 @@ public string GetDataSource()

public override string ToString()
{
return friendlyName;
return FriendlyName;
}

public int CompareTo(DataSourceItem other)
{
return ToString().CompareTo(other.ToString());
return string.Compare(FriendlyName, other.FriendlyName, StringComparison.Ordinal);
}

public bool Equals(DataSourceItem other)
Expand All @@ -181,16 +183,11 @@ public static List<DataSourceItem> BuildList(IXenObject xenObject, List<Data_sou
if (dataSource.name_label == "memory_total_kib" || dataSource.name_label == "memory")
continue;

string friendlyName;
if (dataSource.name_label == "memory_free_kib" && xenObject is Host)
friendlyName = Helpers.GetFriendlyDataSourceName("memory_used_kib", xenObject);
else if (dataSource.name_label == "memory_internal_free" && xenObject is VM)
friendlyName = Helpers.GetFriendlyDataSourceName("memory_internal_used", xenObject);
else
friendlyName = Helpers.GetFriendlyDataSourceName(dataSource.name_label, xenObject);
var friendlyName = Helpers.GetFriendlyDataSourceName(dataSource.name_label, xenObject) ?? dataSource.name_label;
var friendlyDescription = Helpers.GetFriendlyDataSourceDescription(dataSource.name_label, xenObject) ?? dataSource.name_description;

string itemUuid = Palette.GetUuid(dataSource.name_label, xenObject);
dataSourceItems.Add(new DataSourceItem(dataSource, friendlyName, Palette.GetColour(itemUuid), itemUuid));
var itemUuid = Palette.GetUuid(dataSource.name_label, xenObject);
dataSourceItems.Add(new DataSourceItem(dataSource, friendlyName, friendlyDescription, Palette.GetColour(itemUuid), itemUuid));
}

// Filter old datasources only if we have their replacement ones
Expand Down
2 changes: 1 addition & 1 deletion XenAdmin/Controls/CustomDataGraph/GraphList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ private void LoadDefaultGraphs()
void AddDataSource(string uuid, List<string> dsuuids, DesignedGraph dg)
{
dsuuids.Add(uuid);
dg.DataSourceItems.Add(new DataSourceItem(new Data_source(), "", Palette.GetColour(uuid), uuid));
dg.DataSourceItems.Add(new DataSourceItem(new Data_source(), string.Empty, string.Empty, Palette.GetColour(uuid), uuid));
}

private string elevatedUsername;
Expand Down
8 changes: 4 additions & 4 deletions XenAdmin/Dialogs/GraphDetailsDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private void PopulateDataGridView()
if (!toolStripMenuItemDisabled.Checked && !dataSourceItem.Enabled)
continue;

if (!searchTextBox.Matches(dataSourceItem.ToString()))
if (!searchTextBox.Matches(dataSourceItem.FriendlyName))
continue;

var displayOnGraph = _designedGraph.DataSourceItems.Contains(dataSourceItem);
Expand Down Expand Up @@ -183,7 +183,7 @@ private void EnableDataSource()
return;

buttonEnable.Enabled = false;
var action = new EnableDataSourceAction(_graphList.XenObject, dataSource, row.Dsi.ToString());
var action = new EnableDataSourceAction(_graphList.XenObject, dataSource, row.Dsi.FriendlyName);

using (var dialog = new ActionProgressDialog(action, ProgressBarStyle.Marquee)
{
Expand Down Expand Up @@ -408,8 +408,8 @@ public DataSourceGridViewRow(DataSourceItem dataSourceItem, bool displayOnGraph)
Dsi = dataSourceItem;

_checkBoxCell.Value = displayOnGraph;
_dataSourceCell.Value = Dsi.ToString();
_nameDescription.Value = Dsi.DataSource.name_description;
_dataSourceCell.Value = Dsi.FriendlyName;
_nameDescription.Value = Dsi.FriendlyDescription;
_typeCell.Value = Dsi.Category.ToStringI18N();
_enabledCell.Value = Dsi.Enabled.ToYesNoStringI18n();
_colorCell.Value = Dsi.Color;
Expand Down
5 changes: 3 additions & 2 deletions XenModel/FriendlyNameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ public class FriendlyNameManager
/// <summary>
/// Returns null if no match is found.
/// </summary>
public static string GetFriendlyName(string s)
public static string GetFriendlyName(string s, bool ignoreAssert = false)
{
var result = FriendlyNames.GetString(s);
#if DEBUG
Debug.Assert(result != null, $"{s} doesn't exist in FriendlyNames");
if(!ignoreAssert)
Debug.Assert(result != null, $"{s} doesn't exist in FriendlyNames");
#endif
switch (s)
{
Expand Down
18 changes: 18 additions & 0 deletions XenModel/FriendlyNames.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions XenModel/FriendlyNames.resx
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,12 @@
<data name="Description-host.system_status-yum" xml:space="preserve">
<value>YUM repository information and RPM package database listing</value>
</data>
<data name="Description-performance.memory_internal_used" xml:space="preserve">
<value>Memory used as reported by the guest agent</value>
</data>
<data name="Description-performance.memory_used_kib" xml:space="preserve">
<value>Total amount of used memory</value>
</data>
<data name="Description-VM.ha_restart_priority.AlwaysRestart" xml:space="preserve">
<value>Always try to restart VM</value>
</data>
Expand Down
28 changes: 25 additions & 3 deletions XenModel/Utils/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -719,12 +719,34 @@ public static DataSourceCategory GetDataSourceCategory(string name)
return DataSourceCategory.Custom;
}

private static string GetDataSourceId(string originalId)
{
switch (originalId)
{
case "memory_free_kib":
return "memory_used_kib";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are the wrong way around

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original was performing the same overrides, unless I'm missing something :/

memory_free_kib 👉 memory_used_kib
memory_internal_free 👉 memory_internal_used

if (dataSource.name_label == "memory_free_kib" && xenObject is Host)
                    friendlyName = Helpers.GetFriendlyDataSourceName("memory_used_kib", xenObject);
                else if (dataSource.name_label == "memory_internal_free" && xenObject is VM)
                    friendlyName = Helpers.GetFriendlyDataSourceName("memory_internal_used", xenObject);
                else
                    friendlyName = Helpers.GetFriendlyDataSourceName(dataSource.name_label, xenObject);

and

if (datasourceName == "memory_free_kib")
                FriendlyName = Helpers.GetFriendlyDataSourceName("memory_used_kib", xo);
            else if (datasourceName == "memory_internal_free")
                FriendlyName = Helpers.GetFriendlyDataSourceName("memory_internal_used", xo);
            else
                FriendlyName = Helpers.GetFriendlyDataSourceName(datasourceName, xo);

case "memory_internal_free":
return "memory_internal_used";
default:
return originalId;
}
}

public static string GetFriendlyDataSourceDescription(string name, IXenObject iXenObject)
{
var id = GetDataSourceId(name);
// suppressing the assert check since we don't support localization anymore, so there's no need to override all descriptions.
return iXenObject == null ? id : FriendlyNameManager.GetFriendlyName($"Description-performance.{id}", true);
}

public static string GetFriendlyDataSourceName(string name, IXenObject iXenObject)
{
var id = GetDataSourceId(name);
if (iXenObject == null)
return name;
string s = GetFriendlyDataSourceName_(name, iXenObject);
return string.IsNullOrEmpty(s) ? name : s;
return id;

var friendlyDataSourceName = GetFriendlyDataSourceName_(id, iXenObject);
return string.IsNullOrEmpty(friendlyDataSourceName) ? id : friendlyDataSourceName;
}

private static string GetFriendlyDataSourceName_(string name, IXenObject iXenObject)
Expand Down
Loading