Skip to content

Commit e6de2b4

Browse files
committed
HARMONY-1995: Refactored code.
1 parent 7b16e5f commit e6de2b4

File tree

6 files changed

+12
-72
lines changed

6 files changed

+12
-72
lines changed

services/harmony/app/frontends/stac.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ILengthAwarePagination } from 'knex-paginate';
22
import { Job, JobForDisplay, JobStatus } from '../models/job';
3-
import { allWorkItemsNoData } from '../models/work-item';
43
import { keysToLowerCase } from '../util/object';
54
import isUUID from '../util/uuid';
65
import { getRequestRoot } from '../util/url';
@@ -50,7 +49,7 @@ async function handleStacRequest(
5049
});
5150

5251
if ([JobStatus.SUCCESSFUL, JobStatus.COMPLETE_WITH_ERRORS].includes(job.status)) {
53-
if (stacDataLinks.length) {
52+
if (stacDataLinks.length > 0) {
5453
job.links = stacDataLinks;
5554
const urlRoot = getRequestRoot(req);
5655
// default to s3 links
@@ -70,18 +69,13 @@ async function handleStacRequest(
7069
throw new RequestValidationError('The requested paging parameters were out of bounds');
7170
}
7271
} else if (job.status === JobStatus.SUCCESSFUL) {
73-
let allItemsAreNoData = false;
74-
await db.transaction(async (tx) => {
75-
allItemsAreNoData = await allWorkItemsNoData(tx, jobId);
76-
});
77-
78-
if (allItemsAreNoData) {
72+
if (req.params.itemIndex) {
73+
throw new NotFoundError(`Service did not provide STAC items for job ${jobId}`);
74+
} else {
7975
const urlRoot = getRequestRoot(req);
8076
const lType = linkType || 's3';
8177
const serializedJob = job.serialize(urlRoot, lType);
8278
res.json(callback(serializedJob, pagination));
83-
} else {
84-
throw new NotFoundError(`Service did not provide STAC items for job ${jobId}`);
8579
}
8680
} else {
8781
throw new NotFoundError(`Service did not provide STAC items for job ${jobId}`);

services/harmony/app/models/services/base-service.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import _ from 'lodash';
22
import { Logger } from 'winston';
33
import { v4 as uuid } from 'uuid';
4-
import WorkItem, { allWorkItemsNoData } from '../work-item';
4+
import WorkItem from '../work-item';
55
import WorkflowStep from '../workflow-steps';
66
import InvocationResult from './invocation-result';
77
import { Job, JobStatus, statesToDefaultMessages } from '../job';
@@ -335,16 +335,10 @@ export default abstract class BaseService<ServiceParamType> {
335335
const links = job.getRelatedLinks('data');
336336
if (links.length === 1) {
337337
result = { redirect: links[0].href };
338+
} else if (links.length === 0) {
339+
result = { redirect: `/jobs/${jobID}`, headers: {} };
338340
} else {
339-
let allItemsAreNoData = false;
340-
await db.transaction(async (tx) => {
341-
allItemsAreNoData = await allWorkItemsNoData(tx, jobID);
342-
});
343-
if (allItemsAreNoData) {
344-
result = { redirect: `/jobs/${jobID}`, headers: {} };
345-
} else {
346-
result = { error: `The backend service provided ${links.length} outputs when 1 was required`, statusCode: 500 };
347-
}
341+
result = { error: `The backend service provided ${links.length} outputs when 1 was required`, statusCode: 500 };
348342
}
349343
}
350344
} catch (e) {

services/harmony/app/models/work-item.ts

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -749,33 +749,6 @@ export async function getScrollIdForJob(
749749
return null;
750750
}
751751

752-
/**
753-
* Return true if all work items of the given job have NoData warning
754-
*
755-
* @param tx - transaction to use for the query
756-
* @param jobID - the job ID
757-
* @returns true or false
758-
*/
759-
export async function allWorkItemsNoData(
760-
tx: Transaction,
761-
jobID: string,
762-
): Promise < boolean > {
763-
const workflowStepIndexResults = await tx(WorkItem.table)
764-
.max('workflowStepIndex as maxIndex')
765-
.where({ jobID });
766-
767-
const workflowStepIndex = workflowStepIndexResults[0].maxIndex;
768-
769-
const data = await tx(WorkItem.table)
770-
.where({
771-
jobID,
772-
workflowStepIndex,
773-
})
774-
.whereNot('message_category', 'nodata');
775-
776-
return data.length === 0;
777-
}
778-
779752
/**
780753
* Returns the sum of the work item sizes for all work items for the provided jobID.
781754
* @param tx - the transaction to use for querying

services/harmony/test/backend-callbacks.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { expect } from 'chai';
33
import request from 'supertest';
44
import { HTTPError } from 'superagent';
55
import { Job, JobStatus } from '../app/models/job';
6-
import { hookAllWorkItemsNoData } from './helpers/work-items';
76
import JobLink from '../app/models/job-link';
87
import { truncateAll } from './helpers/db';
98
import hookServersStartStop from './helpers/servers';
@@ -20,7 +19,6 @@ describe('Backend Callbacks', function () {
2019
const href = 'https://example.com/foo';
2120

2221
hookServersStartStop();
23-
hookAllWorkItemsNoData();
2422

2523
beforeEach(truncateAll);
2624
after(truncateAll);
@@ -34,8 +32,8 @@ describe('Backend Callbacks', function () {
3432
hookCallbackEach((r) => r.query({ status: 'successful', httpBackend: 'true' }), true);
3533

3634
it('sends a synchronous failure explaining that there were no results', async function () {
37-
expect(this.userResp.statusCode).to.equal(500);
38-
expect(this.userResp.text).to.include('The backend service provided 0 outputs when 1 was required');
35+
expect(this.userResp.statusCode).to.eql(303);
36+
expect(this.userResp.headers.location).to.include('/jobs/');
3937
});
4038
});
4139

services/harmony/test/helpers/work-items.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { Application } from 'express';
22
import { afterEach, beforeEach } from 'mocha';
3-
import { stub, SinonStub } from 'sinon';
43
import request, { Test } from 'supertest';
54
import _ from 'lodash';
6-
import WorkItem, * as wi from '../../app/models/work-item';
5+
import WorkItem from '../../app/models/work-item';
76
import db, { Transaction } from '../../app/util/db';
87
import { truncateAll } from './db';
98
import { hookBackendRequest } from './hooks';
@@ -294,15 +293,3 @@ export async function fakeServiceStacOutput(
294293

295294
}
296295
}
297-
298-
/**
299-
* This function stubs the allWorkItemsNoData function for testing purposes
300-
**/
301-
export function hookAllWorkItemsNoData(): void {
302-
before(function () {
303-
stub(wi, 'allWorkItemsNoData').callsFake(async () => false);
304-
});
305-
after(function () {
306-
(wi.allWorkItemsNoData as SinonStub).restore();
307-
});
308-
}

services/harmony/test/stac/stac-item.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { expect } from 'chai';
22
import { describe, it, before } from 'mocha';
33
import { v4 as uuid } from 'uuid';
44
import itReturnsTheExpectedStacResponse from '../helpers/stac-item';
5-
import { hookAllWorkItemsNoData } from '../helpers/work-items';
65
import { buildJob } from '../helpers/jobs';
76
import hookServersStartStop from '../helpers/servers';
87
import { hookTransaction } from '../helpers/db';
@@ -93,17 +92,12 @@ const completedNonStacJob = buildJob({
9392
message: 'it is done',
9493
progress: 100,
9594
numInputGranules: 12,
96-
links: [{
97-
href: 'http://example.com',
98-
type: 'application/octet-stream',
99-
rel: 'data',
100-
}],
95+
links: [],
10196
request: 'http://example.com/harmony?job=completedJob',
10297
});
10398

10499
describe('STAC item route', function () {
105100
hookServersStartStop({ USE_EDL_CLIENT_APP: true });
106-
hookAllWorkItemsNoData();
107101
hookTransaction();
108102
before(async function () {
109103
await runningJob.save(this.trx);

0 commit comments

Comments
 (0)