public void WhiteSpaceExplicitlySet()
        {
            var innerContent = new DashboardCustomContent("<p>hello world.</p>");
            var content      = new DashboardListItemWithNormalWhitespace()
            {
                InnerContent = innerContent
            };

            // Get the "content" div in the list item.
            var element = content.ToXmlFragment()?.FirstChild?.SelectSingleNode("div[@class='content']");

            Assert.IsNotNull(element);

            // Ensure that the whitespace is overridden.
            Assert.AreEqual("white-space: normal", element.Attributes["style"]?.Value);
        }
        /// <summary>
        /// Creates <see cref="DashboardListItem"/> collection containing information about the background operations
        /// detailed in <paramref name="backgroundOperations"/>.
        /// </summary>
        /// <param name="backgroundOperations">The background operations.</param>
        /// <returns>The list items.</returns>
        public static IEnumerable <DashboardListItem> AsDashboardListItems <TSecureConfiguration>
        (
            this IEnumerable <TaskQueueBackgroundOperation <TSecureConfiguration> > backgroundOperations
        )
            where TSecureConfiguration : class, new()
        {
            // Sanity.
            if (null == backgroundOperations || false == backgroundOperations.Any())
            {
                yield break;
            }

            // Output each background operation as a list item.
            foreach (var bgo in backgroundOperations)
            {
                // Sanity.
                if (null == bgo)
                {
                    continue;
                }

                // If we should not show it then skip.
                if (false == bgo.ShowBackgroundOperationInDashboard)
                {
                    continue;
                }

                // Show the description?
                var htmlString = "";
                if (false == string.IsNullOrWhiteSpace(bgo.Description))
                {
                    htmlString += new DashboardCustomContent($"<p><em>{System.Security.SecurityElement.Escape(bgo.Description)}</em></p>").ToXmlString();
                }

                // Show when it should run.
                switch (bgo.RepeatType)
                {
                case TaskQueueBackgroundOperationRepeatType.NotRepeating:
                    htmlString += "<p>Runs on demand (does not repeat).<br /></p>";
                    break;

                case TaskQueueBackgroundOperationRepeatType.Interval:
                    htmlString += bgo.Interval?.ToDashboardDisplayString();
                    break;

                case TaskQueueBackgroundOperationRepeatType.Schedule:
                    htmlString += bgo.Schedule?.ToDashboardDisplayString();
                    break;

                default:
                    htmlString = "<p><em>Unhandled repeat type: " + bgo.RepeatType + "</em><br /></p>";
                    break;
                }

                // Get known executions (prior, running and future).
                var executions = bgo
                                 .GetAllExecutions()
                                 .ToList();
                var isRunning   = executions.Any(e => e.State == MFilesAPI.MFTaskState.MFTaskStateInProgress);
                var isScheduled = executions.Any(e => e.State == MFilesAPI.MFTaskState.MFTaskStateWaiting);

                // Create the (basic) list item.
                var listItem = new DashboardListItemWithNormalWhitespace()
                {
                    Title         = bgo.Name,
                    StatusSummary = new DomainStatusSummary()
                    {
                        Label = isRunning
                                                ? "Running"
                                                : isScheduled ? "Scheduled" : "Stopped"
                    }
                };

                // If this background operation has a run command then render it.
                if (bgo.ShowRunCommandInDashboard)
                {
                    var cmd = new DashboardDomainCommand
                    {
                        DomainCommandID = bgo.DashboardRunCommand.ID,
                        Title           = bgo.DashboardRunCommand.DisplayName,
                        Style           = DashboardCommandStyle.Link
                    };
                    listItem.Commands.Add(cmd);
                }

                // Set the list item content.
                listItem.InnerContent = new DashboardCustomContent
                                        (
                    htmlString
                    + executions?
                    .AsDashboardContent()?
                    .ToXmlString()
                                        );

                // Add the list item.
                yield return(listItem);
            }
        }
Exemple #3
0
        /// <summary>
        /// Returns some dashboard content that shows the background operations and their current status.
        /// </summary>
        /// <returns>The dashboard content.</returns>
        public IEnumerable <DashboardListItem> GetDashboardContent(TaskQueueResolver taskQueueResolver)
        {
            if (null == taskQueueResolver)
            {
                yield break;
            }

            foreach (var queue in taskQueueResolver.GetQueues())
            {
                // Sanity.
                if (string.IsNullOrWhiteSpace(queue))
                {
                    continue;
                }

                // Get information about the queues.
                TaskQueueAttribute          queueSettings = null;
                System.Reflection.FieldInfo fieldInfo     = null;
                try
                {
                    queueSettings = taskQueueResolver.GetQueueSettings(queue);
                    fieldInfo     = taskQueueResolver.GetQueueFieldInfo(queue);
                }
                catch
                {
                    // Throws if the queue is incorrect.
                    SysUtils.ReportToEventLog
                        ($"Cannot load details for queue {queue}; is there a static field with the [TaskQueue] attribute?",
                        System.Diagnostics.EventLogEntryType.Warning
                        );
                    continue;
                }


                // Skip anything broken.
                if (null == queueSettings || 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 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.
                        SysUtils.ReportToEventLog
                        (
                            $"Cannot load processor details for task type {processor.Type} on queue {queue}.",
                            System.Diagnostics.EventLogEntryType.Warning
                        );
                        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;

                    // Show the description?
                    var htmlString = "";
                    if (false == string.IsNullOrWhiteSpace(showOnDashboardAttribute?.Description))
                    {
                        htmlString += new DashboardCustomContent($"<p><em>{System.Security.SecurityElement.Escape(showOnDashboardAttribute?.Description)}</em></p>").ToXmlString();
                    }

                    // Does it have any configuration instructions?
                    IRecurrenceConfiguration recurrenceConfiguration = null;
                    if (this
                        .VaultApplication?
                        .RecurringOperationConfigurationManager?
                        .TryGetValue(queue, processor.Type, out recurrenceConfiguration) ?? false)
                    {
                        htmlString += recurrenceConfiguration.ToDashboardDisplayString();
                    }
                    else
                    {
                        htmlString += "<p>Does not repeat.<br /></p>";
                    }

                    // Get known executions (prior, running and future).
                    var executions = this
                                     .GetAllExecutions <TaskDirective>(queue, processor.Type)
                                     .ToList();
                    var isRunning   = executions.Any(e => e.State == MFilesAPI.MFTaskState.MFTaskStateInProgress);
                    var isScheduled = executions.Any(e => e.State == MFilesAPI.MFTaskState.MFTaskStateWaiting);

                    // Create the (basic) list item.
                    var listItem = new DashboardListItemWithNormalWhitespace()
                    {
                        Title         = showOnDashboardAttribute?.Name ?? processor.Type,
                        StatusSummary = new DomainStatusSummary()
                        {
                            Label = isRunning
                                                        ? "Running"
                                                        : isScheduled ? "Scheduled" : "Stopped"
                        }
                    };

                    // Should we show the run command?
                    {
                        var key = $"{queue}-{processor.Type}";
                        lock (this._lock)
                        {
                            if (this.TaskQueueRunCommands.ContainsKey(key))
                            {
                                var cmd = new DashboardDomainCommand
                                {
                                    DomainCommandID = this.TaskQueueRunCommands[key].ID,
                                    Title           = this.TaskQueueRunCommands[key].DisplayName,
                                    Style           = DashboardCommandStyle.Link
                                };
                                listItem.Commands.Add(cmd);
                            }
                        }
                    }

                    // Set the list item content.
                    listItem.InnerContent = new DashboardCustomContent
                                            (
                        htmlString
                        + executions?
                        .AsDashboardContent()?
                        .ToXmlString()
                                            );

                    // Add the list item.
                    yield return(listItem);
                }
            }
        }