// Copyright Epic Games, Inc. All Rights Reserved. using HordeServer.Api; using HordeServer.Utilities; using MongoDB.Bson.Serialization.Attributes; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace HordeServer.Models { using TemplateRefId = StringId; /// /// Information about a page to display in the dashboard for a stream /// [BsonKnownTypes(typeof(JobsTab))] public abstract class StreamTab { /// /// Title of this page /// [BsonRequired] public string Title { get; set; } /// /// Private constructor for serialization /// private StreamTab() { Title = null!; } /// /// Constructor /// /// Title of this page public StreamTab(string Title) { this.Title = Title; } /// /// Constructor /// /// Public request object public StreamTab(CreateStreamTabRequest Request) { Title = Request.Title; } /// /// Creates an instance from a request /// /// The request object /// New tab instance public static StreamTab FromRequest(CreateStreamTabRequest Request) { CreateJobsTabRequest? JobsRequest = Request as CreateJobsTabRequest; if (JobsRequest != null) { return new JobsTab(JobsRequest); } throw new Exception($"Unknown tab request type '{Request.GetType()}'"); } /// /// Creates a response object /// /// Response object public abstract GetStreamTabResponse ToResponse(); } /// /// Describes a column to display on the jobs page /// [BsonKnownTypes(typeof(JobsTabLabelColumn), typeof(JobsTabParameterColumn))] public abstract class JobsTabColumn { /// /// Heading for this column /// public string Heading { get; set; } /// /// Relative width of this column. /// public int? RelativeWidth { get; set; } /// /// Private constructor for serialization /// protected JobsTabColumn() { Heading = null!; RelativeWidth = 1; } /// /// Constructor /// /// Heading for this column /// Relative width of this column. public JobsTabColumn(string Heading, int? RelativeWidth) { this.Heading = Heading; this.RelativeWidth = RelativeWidth; } /// /// Constructor /// /// Public request object public JobsTabColumn(CreateJobsTabColumnRequest Request) { this.Heading = Request.Heading; this.RelativeWidth = Request.RelativeWidth; } /// /// Converts this object to a JSON response object /// /// Response object public abstract GetJobsTabColumnResponse ToResponse(); } /// /// Column that contains a set of labels /// public class JobsTabLabelColumn : JobsTabColumn { /// /// Category of labels to display in this column. If null, includes any label not matched by another column. /// public string? Category { get; set; } /// /// Private constructor for serialization /// protected JobsTabLabelColumn() { } /// /// Constructor /// /// Heading for this column /// Category of labels to display in this column /// Relative width of this column. public JobsTabLabelColumn(string Heading, string? Category, int? RelativeWidth) : base(Heading, RelativeWidth) { this.Category = Category; } /// /// Converts this object to a JSON response object /// /// Response object public override GetJobsTabColumnResponse ToResponse() { return new GetJobsTabLabelColumnResponse(Heading, Category, RelativeWidth); } } /// /// Include the value for a parameter on the jobs tab /// public class JobsTabParameterColumn : JobsTabColumn { /// /// Name of a parameter to show in this column. Should be in the form of a prefix, eg. "-set:Foo=" /// public string? Parameter { get; set; } /// /// Private constructor for serialization /// protected JobsTabParameterColumn() { } /// /// Constructor /// /// Heading for this column /// Category of labels to display in this column /// Relative width of this column. public JobsTabParameterColumn(string Heading, string? Parameter, int? RelativeWidth) : base(Heading, RelativeWidth) { this.Parameter = Parameter; } /// /// Converts this object to a JSON response object /// /// Response object public override GetJobsTabColumnResponse ToResponse() { return new GetJobsTabParameterColumnResponse(Heading, Parameter, RelativeWidth); } } /// /// Describes a job page /// [BsonDiscriminator("Jobs")] public class JobsTab : StreamTab { /// /// Whether to show job names /// public bool ShowNames { get; set; } /// /// Names of jobs to include on this page. If there is only one name specified, the name column does not need to be displayed. /// public List? JobNames { get; set; } /// /// List of templates to display on this tab. /// public List? Templates { get; set; } /// /// Columns to display in the table /// public List? Columns { get; set; } /// /// Constructor /// /// Title for this page /// Show names of jobs on the page /// Template names to include on this page /// Names of jobs to include on this page /// Columns ot display in the table public JobsTab(string Title, bool ShowNames, List? Templates, List? JobNames, List? Columns) : base(Title) { this.ShowNames = ShowNames; this.Templates = Templates; this.JobNames = JobNames; this.Columns = Columns; } /// /// Constructor /// /// Public request object public JobsTab(CreateJobsTabRequest Request) : base(Request) { ShowNames = Request.ShowNames; Templates = Request.Templates?.ConvertAll(x => new TemplateRefId(x)); JobNames = Request.JobNames; Columns = Request.Columns?.ConvertAll(x => x.ToModel()); } /// public override GetStreamTabResponse ToResponse() { return new GetJobsTabResponse(Title, ShowNames, Templates?.ConvertAll(x => x.ToString()), JobNames, Columns?.ConvertAll(x => x.ToResponse())); } } }