Skip to content

Commit

Permalink
Job view crashes for jobs with many report items #2311
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbasti committed Mar 21, 2024
1 parent 022b2ac commit ed04c41
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 54 deletions.
21 changes: 18 additions & 3 deletions Src/WitsmlExplorer.Api/HttpHandlers/JobHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Linq;
using System.Threading.Tasks;

using Amazon.Runtime;

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
Expand All @@ -12,6 +14,7 @@
using WitsmlExplorer.Api.Jobs;
using WitsmlExplorer.Api.Middleware;
using WitsmlExplorer.Api.Models;
using WitsmlExplorer.Api.Models.Reports;
using WitsmlExplorer.Api.Services;

namespace WitsmlExplorer.Api.HttpHandlers
Expand Down Expand Up @@ -101,10 +104,22 @@ public static IResult CancelJob(string jobId, IJobCache jobCache)
return TypedResults.NotFound();
}

[Produces(typeof(IEnumerable<object>))]
public static IResult GetReportItems(string jobId, IJobCache jobCache)
[Produces(typeof(BaseReport))]
public static IResult GetReport(string jobId, IJobCache jobCache, HttpRequest httpRequest, IConfiguration configuration, ICredentialsService credentialsService)
{
return TypedResults.Ok(jobCache.GetReportItems(jobId));
EssentialHeaders eh = new(httpRequest);
bool useOAuth2 = StringHelpers.ToBoolean(configuration[ConfigConstants.OAuth2Enabled]);
string userName = useOAuth2 ? credentialsService.GetClaimFromToken(eh.GetBearerToken(), "upn") : eh.TargetUsername;
if (!useOAuth2)
{
credentialsService.VerifyUserIsLoggedIn(eh, ServerType.Target);
}
JobInfo job = jobCache.GetJobInfoById(jobId);
if (job.Username != userName && (!useOAuth2 || !IsAdminOrDeveloper(eh.GetBearerToken())))
{
return TypedResults.Forbid();
}
return TypedResults.Ok(jobCache.GetReportByJobId(jobId));
}
}
}
12 changes: 12 additions & 0 deletions Src/WitsmlExplorer.Api/Jobs/JobInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public JobInfo()

public double Progress { get; set; }

[JsonIgnore]
public BaseReport Report { get; set; }

public bool IsCancelable { get; internal set; } = false;
Expand All @@ -73,6 +74,17 @@ public JobStatus Status
}
}

public string LinkType
{
get
{
if (Report?.DownloadImmediately == true)
{
return "Donwload file";
}
return "Report";
}
}
}

public enum JobStatus
Expand Down
2 changes: 1 addition & 1 deletion Src/WitsmlExplorer.Api/Models/Reports/BaseReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ public class BaseReport
public IEnumerable<object> ReportItems { get; init; }
public string WarningMessage { get; init; }
public bool DownloadImmediately { get; init; } = false;
public string ReportHeader { get; init; } = null;
public string ReportHeader { get; init; }
}
}
2 changes: 1 addition & 1 deletion Src/WitsmlExplorer.Api/Routes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static void ConfigureApi(this WebApplication app, IConfiguration configur
app.MapGet("/jobs/userjobinfo/{jobId}", JobHandler.GetUserJobInfo, useOAuth2);
app.MapGet("/jobs/alljobinfos", JobHandler.GetAllJobInfos, useOAuth2, AuthorizationPolicyRoles.ADMINORDEVELOPER);
app.MapPost("/jobs/cancel/{jobId}", JobHandler.CancelJob, useOAuth2);
app.MapGet("/jobs/getreportitems/{jobId}", JobHandler.GetReportItems, useOAuth2);
app.MapGet("/jobs/getreport/{jobId}", JobHandler.GetReport, useOAuth2);

app.MapGet("/credentials/authorize", AuthorizeHandler.Authorize, useOAuth2);
app.MapGet("/credentials/deauthorize", AuthorizeHandler.Deauthorize, useOAuth2);
Expand Down
28 changes: 5 additions & 23 deletions Src/WitsmlExplorer.Api/Services/JobCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface IJobCache
ICollection<JobInfo> GetJobInfosByUser(string username);
JobInfo GetJobInfoById(string jobId);
ICollection<JobInfo> GetAllJobInfos();
IEnumerable<object> GetReportItems(string jobId);
BaseReport GetReportByJobId(string jobId);
}

public class JobCache : IJobCache
Expand Down Expand Up @@ -53,8 +53,7 @@ public void CacheJob(JobInfo jobInfo)

public ICollection<JobInfo> GetJobInfosByUser(string username)
{
var allJobs = _jobs.Values.Where(job => job.Username == username).ToList();
return HandleBigData(allJobs);
return _jobs.Values.Where(job => job.Username == username).ToList();
}

public JobInfo GetJobInfoById(string jobId)
Expand All @@ -64,17 +63,15 @@ public JobInfo GetJobInfoById(string jobId)

public ICollection<JobInfo> GetAllJobInfos()
{
var allJobs = _jobs.Values.ToList();
return HandleBigData(allJobs);
return _jobs.Values.ToList();
}

public IEnumerable<object> GetReportItems(string jobId)
public BaseReport GetReportByJobId(string jobId)
{
var job = _jobs[jobId];
if (job != null)
{
var reportItems = job.Report?.ReportItems;
return reportItems;
return job.Report;
}
return null;
}
Expand Down Expand Up @@ -102,20 +99,5 @@ private void Cleanup()
}
_logger.LogInformation("JobCache cleanup finished, deleted: {deleted}, failed: {failed}, remaining: {remaining}", deleted, failed, _jobs.Count);
}

private static ICollection<JobInfo> HandleBigData(ICollection<JobInfo> allJobs)
{
var resultJobs = new List<JobInfo>();
foreach (var job in allJobs)
{
if (job.Report != null && job.Report.DownloadImmediately)
{
var copyJob = job with { Report = new BaseReport() { Title = job.Report?.Title, ReportHeader = job.Report?.ReportHeader, DownloadImmediately = true } };
resultJobs.Add(copyJob);
}
else { resultJobs.Add(job); }
}
return resultJobs;
}
}
}
1 change: 0 additions & 1 deletion Src/WitsmlExplorer.Api/Workers/DownloadAllLogDataWorker.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

Expand Down
1 change: 1 addition & 0 deletions Src/WitsmlExplorer.Frontend/__testUtils__/testUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ export function getJobInfo(overrides?: Partial<JobInfo>): JobInfo {
report: null,
progress: 0,
isCancelable: false,
linkType: "",
...overrides
};
}
Expand Down
29 changes: 12 additions & 17 deletions Src/WitsmlExplorer.Frontend/components/ContentViews/JobsView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { refreshJobInfoQuery } from "hooks/query/queryRefreshHelpers";
import { useGetJobInfo } from "hooks/query/useGetJobInfo";
import { useGetServers } from "hooks/query/useGetServers";
import useExport from "hooks/useExport";
import BaseReport from "models/reports/BaseReport";
import { Server } from "models/server";
import {
adminRole,
Expand Down Expand Up @@ -67,10 +66,13 @@ export const JobsView = (): React.ReactElement => {
});
};

const onClickReport = async (report: BaseReport, jobId: string) => {
const onClickReport = async (jobId: string) => {
const report = await JobService.getReport(jobId);
if (report.downloadImmediately === true) {
const reportItems = await JobService.getReportItems(jobId);
const reportProperties = generateReport(reportItems, report.reportHeader);
const reportProperties = generateReport(
report.reportItems,
report.reportHeader
);
exportData(
report.title,
reportProperties.exportColumns,
Expand Down Expand Up @@ -130,10 +132,6 @@ export const JobsView = (): React.ReactElement => {
wellName: jobInfo.wellName,
wellboreName: jobInfo.wellboreName,
objectName: jobInfo.objectName,
status:
jobInfo.progress && jobInfo.status === "Started"
? `${Math.round(jobInfo.progress * 100)}%`
: jobInfo.status,
startTime: formatDateString(
jobInfo.startTime,
timeZone,
Expand All @@ -146,15 +144,12 @@ export const JobsView = (): React.ReactElement => {
),
targetServer: serverUrlToName(servers, jobInfo.targetServer),
sourceServer: serverUrlToName(servers, jobInfo.sourceServer),
report: jobInfo.report ? (
<ReportButton
onClick={() => onClickReport(jobInfo.report, jobInfo.id)}
>
{jobInfo.report.downloadImmediately === true
? "Donwload file"
: "Report"}
</ReportButton>
) : null,
report:
jobInfo.status === "Finished" ? (
<ReportButton onClick={() => onClickReport(jobInfo.id)}>
{jobInfo.linkType}
</ReportButton>
) : null,
jobInfo: jobInfo
};
})
Expand Down
11 changes: 6 additions & 5 deletions Src/WitsmlExplorer.Frontend/components/Modals/ReportModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,15 @@ export const useGetReportOnJobFinished = (jobId: string): BaseReport => {
)
);
} else {
setReport(jobInfo.report);
if (jobInfo.report.downloadImmediately === true) {
const report = await JobService.getReport(jobId);
setReport(report);
if (report.downloadImmediately === true) {
const reportProperties = generateReport(
jobInfo.report.reportItems,
jobInfo.report.reportHeader
report.reportItems,
report.reportHeader
);
exportData(
jobInfo.report.title,
report.title,
reportProperties.exportColumns,
reportProperties.data
);
Expand Down
1 change: 1 addition & 0 deletions Src/WitsmlExplorer.Frontend/models/jobs/jobInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export default interface JobInfo {
report: BaseReport;
progress: number;
isCancelable: boolean;
linkType: string;
}
7 changes: 4 additions & 3 deletions Src/WitsmlExplorer.Frontend/services/jobService.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import JobInfo from "models/jobs/jobInfo";
import BaseReport from "models/reports/BaseReport";
import { Server } from "models/server";
import { ApiClient, throwError } from "services/apiClient";
import AuthorizationService from "services/authorizationService";
Expand Down Expand Up @@ -108,12 +109,12 @@ export default class JobService {
}
}

public static async getReportItems(
public static async getReport(
jobId: string,
abortSignal?: AbortSignal
): Promise<object[]> {
): Promise<BaseReport> {
const response = await ApiClient.get(
`/api/jobs/getreportitems/${jobId}`,
`/api/jobs/getreport/${jobId}`,
abortSignal
);
if (response.ok) {
Expand Down

0 comments on commit ed04c41

Please sign in to comment.