diff --git a/XenAdmin/Dialogs/VMProtectionRecovery/PolicyHistory.cs b/XenAdmin/Dialogs/VMProtectionRecovery/PolicyHistory.cs index 5a0aa0eb4..fefdd2cf7 100644 --- a/XenAdmin/Dialogs/VMProtectionRecovery/PolicyHistory.cs +++ b/XenAdmin/Dialogs/VMProtectionRecovery/PolicyHistory.cs @@ -72,6 +72,31 @@ namespace XenAdmin.Dialogs.VMProtectionRecovery } private IVMPolicy _policy; + public void StartRefreshTab() + { + /* hoursFromNow has 3 possible values: + 1) 0 -> top 10 messages (default) + 2) 24 -> messages from past 24 Hrs + 3) 7 * 24 -> messages from lst 7 days */ + + var hoursFromNow = 0; + switch (comboBox1.SelectedIndex) + { + case 0: /* default value*/ + break; + case 1: + hoursFromNow = 24; + break; + case 2: + hoursFromNow = 7 * 24; + break; + } + + PureAsyncAction action = _policy.getAlertsAction(_policy, hoursFromNow); + action.Completed += action_Completed; + action.RunAsync(); + } + public void RefreshTab(IVMPolicy policy) { _policy = policy; @@ -83,7 +108,7 @@ namespace XenAdmin.Dialogs.VMProtectionRecovery else { comboBox1.Enabled = true; - RefreshGrid(_policy.PolicyAlerts); + StartRefreshTab(); } } @@ -168,24 +193,7 @@ namespace XenAdmin.Dialogs.VMProtectionRecovery { if (_policy != null) { - if (comboBox1.SelectedIndex == 0) - RefreshGrid(_policy.PolicyAlerts); - else if (comboBox1.SelectedIndex == 1) - { - dataGridView1.Rows.Clear(); - panelLoading.Visible = true; - PureAsyncAction action = _policy.getAlertsAction(_policy, 24) ; - action.Completed += action_Completed; - action.RunAsync(); - } - else if (comboBox1.SelectedIndex == 2) - { - dataGridView1.Rows.Clear(); - panelLoading.Visible = true; - PureAsyncAction action = _policy.getAlertsAction(_policy, 7 * 24); - action.Completed += action_Completed; - action.RunAsync(); - } + StartRefreshTab(); } } diff --git a/XenAdmin/Dialogs/VMProtectionRecovery/VMProtectionPoliciesDialog.cs b/XenAdmin/Dialogs/VMProtectionRecovery/VMProtectionPoliciesDialog.cs index 3d9c067f6..ec1a46fd1 100644 --- a/XenAdmin/Dialogs/VMProtectionRecovery/VMProtectionPoliciesDialog.cs +++ b/XenAdmin/Dialogs/VMProtectionRecovery/VMProtectionPoliciesDialog.cs @@ -44,7 +44,7 @@ using XenAdmin.Core; using XenAdmin.Dialogs.VMProtectionRecovery; using XenCenterLib; using XenAdmin.Alerts; - +using System.Linq; namespace XenAdmin.Dialogs.VMProtection_Recovery { @@ -201,43 +201,57 @@ namespace XenAdmin.Dialogs.VMPolicies dataGridView1.SuspendLayout(); var selectedPolicy = currentSelected; dataGridView1.Rows.Clear(); + var policyList = VMGroup.VMPolicies(Pool.Connection.Cache); + + /* creating a dictionary to hold (policy_uuid, message list) */ + + Dictionary> policyMessage = new Dictionary>(); + + /* populate the dictionary with policy uuid */ + + foreach (var policy in policyList) + { + policy.PolicyAlerts.Clear(); + List messageList = new List(); + policyMessage.Add(policy.uuid, messageList); + } + + /* iterate through all messages and populate the dictionary with message list */ - /* filter out the messages for VMSS as the VMSS does not have recent alerts and that need to be populated below*/ - List vmssMessages = new List(); if (!VMGroup.isVMPolicyVMPP) { var messages = Pool.Connection.Cache.Messages; + List value = new List(); + foreach (var message in messages) { if (message.cls == cls.VMSS) { - vmssMessages.Add(message); + if (policyMessage.TryGetValue(message.obj_uuid, out value)) + { + value.Add(message); + } + } } } - foreach (var policy in VMGroup.VMPolicies(Pool.Connection.Cache)) - { - if (!VMGroup.isVMPolicyVMPP) - { - policy.PolicyAlerts.Clear(); - List processedMessages = new List(); - /*for VMSS: Populate the alerts from Messages by filtering out the alerts for this schedule - This is not required in VMPP as the VMPP record itself has the recentAlerts */ - foreach (var message in vmssMessages) - { - if (message.obj_uuid == policy.uuid) - { - policy.PolicyAlerts.Add(new PolicyAlert(message.priority, message.name, message.timestamp)); - processedMessages.Add(message); - } - } - vmssMessages.RemoveAll(message => processedMessages.Contains(message)); - } + /* add only 10 messages for each policy and referesh the rows*/ + + foreach (var policy in policyList) + { + /* message list need not be always sorted */ + + var messageListSorted = policyMessage[policy.uuid].OrderByDescending(message => message.timestamp).ToList(); + for (int messageCount = 0; messageCount < 10 && messageCount < messageListSorted.Count; messageCount++) + { + policy.PolicyAlerts.Add(new PolicyAlert(messageListSorted[messageCount].priority, messageListSorted[messageCount].name, messageListSorted[messageCount].timestamp)); + } if (dataGridView1.ColumnCount > 0) dataGridView1.Rows.Add(new PolicyRow(policy)); } - RefreshButtons(); + + RefreshButtons(); if (selectedPolicy != null) { foreach (PolicyRow row in dataGridView1.Rows) diff --git a/XenModel/Actions/VMSS/GetVMSSAlertsAction.cs b/XenModel/Actions/VMSS/GetVMSSAlertsAction.cs index fafb29e83..4daedbac5 100644 --- a/XenModel/Actions/VMSS/GetVMSSAlertsAction.cs +++ b/XenModel/Actions/VMSS/GetVMSSAlertsAction.cs @@ -35,6 +35,7 @@ using System.Text; using XenAdmin.Alerts; using XenAPI; using System.Diagnostics; +using System.Linq; namespace XenAdmin.Actions { @@ -52,18 +53,45 @@ namespace XenAdmin.Actions protected override void Run() { var now = DateTime.Now; - var messages = Pool.Connection.Cache.Messages; + var messages = Pool.Connection.Cache.Messages.OrderByDescending(message => message.timestamp).ToList(); var listAlerts = new List(); + DateTime currentTime = DateTime.Now; + DateTime offset = currentTime.Add(new TimeSpan(- _hoursFromNow, 0, 0)); + + /* _hoursFromNow has 3 possible values: + 1) 0 -> top 10 messages + 2) 24 -> messages from past 24 Hrs + 3) 7 * 24 -> messages from lst 7 days */ - /*for VMSS: Populate the alerts from Messages by filtering out the alerts for this schedule - This is not required in VMPP as the VMPP record itself has the recentAlerts */ - foreach (var message in messages) + if (_hoursFromNow == 0) { - if (message.cls == cls.VMSS && message.obj_uuid == VMSS.uuid) + int messageCounter = 0; + foreach (var message in messages) { - listAlerts.Add(new PolicyAlert(message.priority, message.name, message.timestamp)); + if (message.cls == cls.VMSS && message.obj_uuid == VMSS.uuid && messageCounter < 10) + { + listAlerts.Add(new PolicyAlert(message.priority, message.name, message.timestamp)); + messageCounter++; + } + else if (messageCounter >= 10) + break; } - } + } + else + { + foreach (var message in messages) + { + if (message.cls == cls.VMSS && message.obj_uuid == VMSS.uuid && message.timestamp > offset) + { + listAlerts.Add(new PolicyAlert(message.priority, message.name, message.timestamp)); + } + + /* since the messages are sorted on timestamp you need not scan the entire message list */ + + else if (message.timestamp < offset) + break; + } + } VMSS.Alerts = new List(listAlerts); Debug.WriteLine(string.Format("GetAlerts took: {0}", DateTime.Now - now)); diff --git a/XenModel/XenAPI-Extensions/VMSS.cs b/XenModel/XenAPI-Extensions/VMSS.cs index 1ada6b1c2..0d37cdfef 100644 --- a/XenModel/XenAPI-Extensions/VMSS.cs +++ b/XenModel/XenAPI-Extensions/VMSS.cs @@ -306,11 +306,6 @@ namespace XenAPI { get { - foreach (var recent in _alerts) - { - if (!_alerts.Contains(recent)) - _alerts.Add(recent); - } return _alerts; } set { _alerts = value; }