This commit is contained in:
2023-01-23 18:48:43 -07:00
parent 56ec81e080
commit 662863a11e
15 changed files with 247 additions and 52 deletions

View File

@ -1,6 +1,8 @@
@page "/Export/{Model?}"
@page "/Export"
@using Microsoft.AspNetCore.Components.Web
@using MudBlazor
@using OI.Metrology.Shared.DataModels
@namespace OI.Metrology.ClientHub.Pages
@ -10,31 +12,31 @@
<hr />
<form asp-controller="Export" asp-action="ExportData" method="post" class="form-inline" style="min-height:480px;">
<div class="form-group">
<label for="ToolType">Tool Type</label>
<div class="form-control" id="ToolType" name="ToolType"></div>
@* @Microsoft.AspNetCore.Html.ValidationMessage("ToolType", new { @class = "text-danger" }) *@
</div>
<div class="form-group">
<label for="StartDate">Start Time</label>
<div class="form-control mb-2 mr-sm-2" id="StartDateControl"></div>
<div class="form-control mb-2 mr-sm-2" id="StartTimeControl"></div>
@* @Microsoft.AspNetCore.Html.ValidationMessage("StartDate", new { @class = "text-danger" }) *@
</div>
<div class="form-group">
<label for="EndDate">End Time</label>
<div class="form-control mb-2 mr-sm-2" id="EndDateControl"></div>
<div class="form-control mb-2 mr-sm-2" id="EndTimeControl"></div>
@* @Microsoft.AspNetCore.Html.ValidationMessage("EndDate", new { @class = "text-danger" }) *@
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Export Data</button>
</div>
<div class="form-group">
@* @Microsoft.AspNetCore.Html.ValidationMessage("Exception", new { @class = "text-danger" }) *@
</div>
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
</form>
@if (_TimeSpan is null || _DateRange is null || _ToolTypeNameId is null || _ToolTypeNameIdCollection is null)
{
<p><em>Loading...</em></p>
}
else
{
<table id="ExportData">
<tr>
<td>
<MudSelect @bind-Value="_ToolTypeNameId" ToStringFunc="@_ConvertFunc" Label="Tool Type" Placeholder="Please Select" AdornmentColor="Color.Primary">
@foreach (ToolTypeNameId toolTypeNameId in _ToolTypeNameIdCollection)
{
<MudSelectItem Value="@toolTypeNameId" />
}
</MudSelect>
</td>
<td>
<MudDateRangePicker Label="Date Range" @bind-DateRange="_DateRange" />
</td>
<td>
<MudTimePicker Label="Start and End Time" AmPm="true" @bind-Time="_TimeSpan" />
</td>
<td>
<MudButton Variant="Variant.Filled" Color="Color.Info" OnClick="DownloadAsync">Download</MudButton>
</td>
</tr>
</table>
}

View File

@ -1,24 +1,26 @@
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using MudBlazor;
using OI.Metrology.Shared.DataModels;
using OI.Metrology.Shared.Models.Stateless;
using System.Net;
namespace OI.Metrology.ClientHub.Pages;
public partial class Export
{
protected TimeSpan? _TimeSpan;
protected DateRange? _DateRange;
protected ToolTypeNameId? _ToolTypeNameId;
protected ToolTypeNameId[]? _ToolTypeNameIdCollection;
[Inject] protected HttpClient? HttpClient { get; set; }
[Inject] protected IJSRuntime? JSRuntime { get; set; }
[Inject] protected Models.AppSettings? AppSettings { get; set; }
[Parameter] public Metrology.Shared.ViewModels.Export? Model { get; set; }
[Inject] protected ILogger<AwaitingDisposition>? Logger { get; set; }
public Export()
{
Model ??= new()
{
ToolType = "",
StartTime = DateTime.Now.AddMonths(-1),
EndTime = DateTime.Now
};
}
protected Func<ToolTypeNameId, string> _ConvertFunc = toolTypeNameId => string.Concat(toolTypeNameId?.ToolTypeName);
protected override Task OnAfterRenderAsync(bool firstRender)
{
@ -32,5 +34,44 @@ public partial class Export
}
return Task.CompletedTask;
}
protected override async Task OnInitializedAsync()
{
_ToolTypeNameId ??= new();
_TimeSpan ??= new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, 00);
_DateRange ??= new DateRange(DateTime.Now.AddMonths(-1).Date, DateTime.Now.Date);
if (Logger is null)
throw new NullReferenceException(nameof(Logger));
if (HttpClient is null)
throw new NullReferenceException(nameof(HttpClient));
string controllerName = IToolTypesController<object>.GetRouteName();
try
{
Result<ToolTypeNameId[]>? result = await HttpClient.GetFromJsonAsync<Result<ToolTypeNameId[]>>($"api/{controllerName}");
if (result is not null)
_ToolTypeNameIdCollection = result.Results;
}
catch (Exception)
{
string json = await HttpClient.GetStringAsync($"api/{controllerName}");
Logger.LogInformation(message: json);
}
}
protected async Task DownloadAsync()
{
if (JSRuntime is null)
throw new NullReferenceException(nameof(JSRuntime));
if (HttpClient is null)
throw new NullReferenceException(nameof(HttpClient));
if (_TimeSpan is null || _DateRange is null || _ToolTypeNameId is null || _ToolTypeNameIdCollection is null || _ToolTypeNameId.ToolTypeName is null || _DateRange.Start is null || _DateRange.End is null)
return;
string controllerName = IToolTypesController<object>.GetRouteName();
string endTime = _DateRange.End.Value.AddTicks(_TimeSpan.Value.Ticks).ToString();
string startTime = _DateRange.Start.Value.AddTicks(_TimeSpan.Value.Ticks).ToString();
string fileName = $"Export_{_ToolTypeNameId.ToolTypeName}_{startTime:yyyyMMddHHmm}_to_{endTime:yyyyMMddHHmm}.csv";
string query = $"datebegin={startTime:MM/dd/yyyy hh:mm tt}&dateend={endTime:MM/dd/yyyy hh:mm tt}&filename={WebUtility.UrlEncode(fileName)}";
await JSRuntime.InvokeVoidAsync("triggerFileDownload", fileName, $"{HttpClient.BaseAddress}api/{controllerName}/{_ToolTypeNameId.ID}/csv?{query}");
}
}

View File

@ -1,5 +1,4 @@
@page "/RunInfo/{Model?}"
@page "/RunInfo/{ToolTypeId:int?}/{HeaderId:int?}"
@page "/RunInfo/{ToolTypeId:int?}/{HeaderId:int?}"
@using Microsoft.AspNetCore.Components.Web
@using MudBlazor

View File

@ -8,7 +8,6 @@ public partial class RunInfo
[Parameter] public int? HeaderId { get; set; }
[Parameter] public int? ToolTypeId { get; set; }
[Parameter] public Metrology.Shared.ViewModels.RunInfo? Model { get; set; }
[Inject] protected IJSRuntime? JSRuntime { get; set; }
[Inject] protected Models.AppSettings? AppSettings { get; set; }
@ -21,9 +20,10 @@ public partial class RunInfo
throw new NullReferenceException(nameof(JSRuntime));
if (AppSettings is null)
throw new NullReferenceException(nameof(AppSettings));
int initialHeaderId = Model is not null ? Model.HeaderID : HeaderId is not null ? HeaderId.Value : 0;
string initialHeaderAttachmentId = Model is not null ? Model.HeaderAttachmentID.ToString() : string.Empty;
int initialToolTypeID = Model is not null ? Model.ToolTypeID : ToolTypeId is not null ? ToolTypeId.Value : 1;
Metrology.Shared.ViewModels.RunInfo? model = null;
int initialHeaderId = model is not null ? model.HeaderID : HeaderId is not null ? HeaderId.Value : 0;
string initialHeaderAttachmentId = model is not null ? model.HeaderAttachmentID.ToString() : string.Empty;
int initialToolTypeID = model is not null ? model.ToolTypeID : ToolTypeId is not null ? ToolTypeId.Value : 1;
return JSRuntime.InvokeVoidAsync("initRunInfo", AppSettings.ApiUrl, initialToolTypeID, initialHeaderId, initialHeaderAttachmentId).AsTask();
}
return Task.CompletedTask;

View File

@ -30,6 +30,7 @@ internal class Program
_ = builder.Services.AddSingleton(_ => appSettings);
_ = builder.Services.AddSingleton<WeatherForecastService>();
_ = builder.Services.AddSingleton<WeatherForecastService>();
_ = builder.Services.AddScoped(serviceProvider => new HttpClient { BaseAddress = new Uri(appSettings.ApiUrl) });
WebApplication app = builder.Build();

View File

@ -537,4 +537,12 @@ function initRunInfo(apiUrl, initialToolTypeID, initialHeaderId, initialHeaderAt
$("#LoadHeadersButton").click();
}
}, 180000);
};
};
function triggerFileDownload(fileName, url) {
const anchorElement = document.createElement('a');
anchorElement.href = url;
anchorElement.download = fileName ?? '';
anchorElement.click();
anchorElement.remove();
}

View File

@ -1,7 +1,7 @@
body {
padding-top: 50px;
padding-bottom: 20px;
background-color:darkgrey;
background-color: darkgrey;
}
/* Set padding to keep content from hitting the edges */
@ -104,3 +104,7 @@ div.modal-content-warning {
.mud-table-cell {
font-size: 1.875rem;
}
#ExportData {
zoom: 2;
}