-
Notifications
You must be signed in to change notification settings - Fork 18
/
TaskManagerExAsynchronousDashboardContentProvider.cs
216 lines (189 loc) · 7.06 KB
/
TaskManagerExAsynchronousDashboardContentProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
using MFiles.VAF.AppTasks;
using MFiles.VAF.Configuration.AdminConfigurations;
using MFiles.VAF.Configuration.Domain.Dashboards;
using MFiles.VAF.Configuration.Logging;
using MFilesAPI;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using static MFiles.VAF.Common.ApplicationTaskQueue.TaskQueueManager;
namespace MFiles.VAF.Extensions.Dashboards.AsynchronousDashboardContent
{
/// <summary>
/// Retrieves data from the <see cref="TaskManagerEx{TConfiguration}"/> about asynchronous operations that should
/// be rendered onto a dashboard.
/// </summary>
/// <typeparam name="TConfiguration">The type of configuration used.</typeparam>
public class TaskManagerExAsynchronousDashboardContentProvider<TConfiguration>
: IAsynchronousDashboardContentProvider
where TConfiguration : class, new()
{
/// <summary>
/// The logger to use for this class.
/// </summary>
private ILogger Logger { get; } = LogManager.GetLogger<TaskManagerExAsynchronousDashboardContentProvider<TConfiguration>>();
/// <summary>
/// The vault reference to use to retrieve data.
/// </summary>
protected Vault Vault { get; set; }
/// <summary>
/// The task manager to reference task data from.
/// </summary>
public TaskManagerEx<TConfiguration> TaskManager { get; protected set; }
/// <summary>
/// The manager that contains information about recurring tasks.
/// </summary>
public RecurringOperationConfigurationManager<TConfiguration> RecurringOperationConfigurationManager { get; protected set; }
/// <summary>
/// The task queue resolver to retrieve queue and task processor data from.
/// </summary>
public TaskQueueResolver TaskQueueResolver { get; protected set; }
public TaskManagerExAsynchronousDashboardContentProvider
(
Vault vault,
TaskManagerEx<TConfiguration> taskManager,
TaskQueueResolver taskQueueResolver,
RecurringOperationConfigurationManager<TConfiguration> recurringOperationConfigurationManager
)
{
Vault = vault ?? throw new ArgumentNullException(nameof(vault));
TaskManager = taskManager ?? throw new ArgumentNullException(nameof(taskManager));
TaskQueueResolver = taskQueueResolver ?? throw new ArgumentNullException(nameof(taskQueueResolver));
RecurringOperationConfigurationManager = recurringOperationConfigurationManager ?? throw new ArgumentNullException(nameof(recurringOperationConfigurationManager));
}
/// <inheritdoc />
public IEnumerable<KeyValuePair<DashboardQueueAndTaskDetails, IEnumerable<TaskInfo<TaskDirective>>>> GetAsynchronousDashboardContent()
{
var items = new List<KeyValuePair<DashboardQueueAndTaskDetails, IEnumerable<TaskInfo<TaskDirective>>>>();
if (null == TaskQueueResolver)
return items;
foreach (var queue in TaskQueueResolver.GetQueues())
{
// Sanity.
if (string.IsNullOrWhiteSpace(queue))
continue;
// Get information about the queues.
System.Reflection.FieldInfo fieldInfo = null;
try
{
fieldInfo = TaskQueueResolver.GetQueueFieldInfo(queue);
}
catch (Exception e)
{
// Throws if the queue is incorrect.
this.Logger?.Warn
(
e,
$"Cannot load details for queue {queue}; is there a static field with the [TaskQueue] attribute?"
);
continue;
}
// Skip anything broken.
if (null == fieldInfo)
continue;
// If it's marked as hidden then skip.
{
var attributes = fieldInfo.GetCustomAttributes(typeof(HideOnDashboardAttribute), true)
?? new HideOnDashboardAttribute[0];
if (attributes.Length != 0)
continue;
}
// Get the number of items in the queue.
var waitingTasks = TaskHelper.GetTaskIDs(Vault, queue, MFTaskState.MFTaskStateWaiting).Count;
var showDegraded = waitingTasks > AsynchronousDashboardContentSettings.DegradedDashboardThreshold;
// Get each task processor.
foreach (var processor in TaskQueueResolver.GetTaskProcessors(queue))
{
// Sanity.
if (null == processor)
continue;
// Get information about the processor..
TaskProcessorAttribute taskProcessorSettings = null;
System.Reflection.MethodInfo methodInfo = null;
try
{
taskProcessorSettings = TaskQueueResolver.GetTaskProcessorSettings(queue, processor.Type);
methodInfo = TaskQueueResolver.GetTaskProcessorMethodInfo(queue, processor.Type);
}
catch
{
// Throws if the task processor is not found.
this.Logger?.Warn
(
$"Cannot load processor details for task type {processor.Type} on queue {queue}."
);
continue;
}
// Skip anything broken.
if (null == taskProcessorSettings || null == methodInfo)
continue;
// If it's marked as hidden then skip.
{
var attributes = methodInfo.GetCustomAttributes(typeof(HideOnDashboardAttribute), true)
?? new HideOnDashboardAttribute[0];
if (attributes.Length != 0)
continue;
}
// This should be shown. Do we have any extended details?
var showOnDashboardAttribute = methodInfo.GetCustomAttributes(typeof(ShowOnDashboardAttribute), true)?
.FirstOrDefault() as ShowOnDashboardAttribute;
// Add the run command, if we should/can.
var commands = new List<DashboardCommand>();
if (showOnDashboardAttribute?.ShowRunCommand ?? false)
{
var key = $"{queue}-{processor.Type}";
if (TaskManager.TaskQueueRunCommands.TryGetValue(key, out CustomDomainCommand cmd))
{
commands.Add
(
new DashboardDomainCommand
{
DomainCommandID = cmd.ID,
Title = cmd.DisplayName,
Style = DashboardCommandStyle.Link
}
);
}
else if (!string.IsNullOrWhiteSpace(showOnDashboardAttribute.RunCommandId))
{
commands.Add
(
new DashboardDomainCommand
{
DomainCommandID = showOnDashboardAttribute.RunCommandId,
Title = showOnDashboardAttribute.RunCommandDisplayText,
Style = DashboardCommandStyle.Link
}
);
}
}
// Does it recur?
IRecurrenceConfiguration recurrenceConfiguration = null;
this.RecurringOperationConfigurationManager?
.TryGetValue(queue, processor.Type, out recurrenceConfiguration);
// Return the data.
items.Add(new KeyValuePair<DashboardQueueAndTaskDetails, IEnumerable<TaskInfo<TaskDirective>>>
(
new DashboardQueueAndTaskDetails()
{
QueueId = queue,
TaskType = processor.Type,
Name = showOnDashboardAttribute?.Name,
Description = showOnDashboardAttribute?.Description,
Commands = commands,
TasksInQueue = waitingTasks,
Order = showOnDashboardAttribute?.Order ?? ShowOnDashboardAttribute.DefaultOrder,
RecurrenceConfiguration = recurrenceConfiguration
},
// Get known executions (prior, running and future).
showDegraded
? TaskManager.GetExecutions<TaskDirective>(queue, processor.Type, MFTaskState.MFTaskStateInProgress)
: TaskManager.GetAllExecutions<TaskDirective>(queue, processor.Type)
));
}
}
return items.OrderBy(i => i.Key.Order);
}
}
}