Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CMR-10122: Use EntryId returned by CMR for ID in CMR-STAC #359

Merged
merged 6 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/__tests__/providerCatalog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ describe("GET /:provider", () => {
count: mockCollections.length,
cursor: "foundCursor",
items: mockCollections.map((coll) => ({
id: `${coll.shortName}_${coll.version}`,
id: `${coll.id}`,
title: coll.title ?? faker.random.words(4),
})),
});
Expand Down
129 changes: 124 additions & 5 deletions src/domains/__tests__/collections.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,7 @@ describe("collectionsToStac", () => {

// Check if all expected fields are present and correctly populated
expect(stacCollection).to.have.property("type", "Collection");
expect(stacCollection).to.have.property(
"id",
`${mockCollection.shortName}_${mockCollection.version}`
);
expect(stacCollection).to.have.property("id", `${mockCollection.entryId}`);
expect(stacCollection).to.have.property("title", mockCollection.title);
expect(stacCollection).to.have.property("description", mockCollection.description);
expect(stacCollection).to.have.property("stac_version", "1.0.0");
Expand Down Expand Up @@ -313,8 +310,130 @@ describe("collectionsToStac", () => {
]);
expect(stacCollection.summaries).to.have.property("instruments");
expect(stacCollection.summaries?.instruments).to.deep.equal([
mockCollection.platforms[0].instruments[0].shortName,
mockCollection.platforms?.[0]?.instruments?.[0]?.shortName,
]);
});
});
describe("when processing platform and instruments", () => {
it("should correctly handle a collection with multiple Platforms and Instruments", () => {
const [mockCollection] = generateCollections(1);
mockCollection.platforms = [
{
type: "Earth Observation Satellites",
shortName: "Terra",
longName: "Earth Observing System, Terra (AM-1)",
instruments: [
{
shortName: "ASTER",
longName: "Advanced Spaceborne Thermal Emission and Reflection Radiometer",
},
{
shortName: "MODIS",
longName: "Moderate-Resolution Imaging Spectroradiometer",
},
],
},
];
const stacCollection = collectionToStac(mockCollection);
expect(stacCollection.summaries).to.have.property("platform");
expect(stacCollection.summaries?.platform).to.deep.equal(["Terra"]);
expect(stacCollection.summaries).to.have.property("instruments");
expect(stacCollection.summaries?.instruments).to.deep.equal(["ASTER", "MODIS"]);
});

it("should handle a collection with platforms but no instruments", () => {
const [mockCollection] = generateCollections(1);
mockCollection.platforms = [
{
type: "Earth Observation Satellites",
shortName: "Terra",
longName: "Earth Observing System, Terra (AM-1)",
},
];
const stacCollection = collectionToStac(mockCollection);

expect(stacCollection.summaries).to.have.property("platform");
expect(stacCollection.summaries?.platform).to.deep.equal(["Terra"]);
expect(stacCollection.summaries).to.have.property("instruments");
expect(stacCollection.summaries?.instruments).to.deep.equal(["Not Provided"]);
});

it("should handle a collection with some platforms having instruments and others not", () => {
const [mockCollection] = generateCollections(1);
mockCollection.platforms = [
{
type: "Earth Observation Satellites",
shortName: "Terra",
longName: "Earth Observing System, Terra (AM-1)",
instruments: [
{
shortName: "ASTER",
longName: "Advanced Spaceborne Thermal Emission and Reflection Radiometer",
},
],
},
{
type: "Earth Observation Satellites",
shortName: "SPOT-4",
longName: "Systeme Probatoire Pour l'Observation de la Terre-4",
},
{
type: "Earth Observation Satellites",
shortName: "SPOT-5",
longName: "Systeme Probatoire Pour l'Observation de la Terre-5",
instruments: [
{
shortName: "VEGETATION-2",
longName: "VEGETATION INSTRUMENT 2 (SPOT 5)",
},
{
shortName: "VEGETATION-3",
longName: "VEGETATION INSTRUMENT 3 (SPOT 3)",
},
],
},
];

const stacCollection = collectionToStac(mockCollection);

expect(stacCollection.summaries).to.have.property("platform");
expect(stacCollection.summaries?.platform).to.deep.equal(["Terra", "SPOT-4", "SPOT-5"]);
expect(stacCollection.summaries).to.have.property("instruments");
expect(stacCollection.summaries?.instruments).to.deep.equal([
"ASTER",
"VEGETATION-2",
"VEGETATION-3",
]);
});

it("should handle a collection with empty instruments arrays", () => {
const [mockCollection] = generateCollections(1);
mockCollection.platforms = [
{
type: "Earth Observation Satellites",
shortName: "Terra",
longName: "Earth Observing System, Terra (AM-1)",
instruments: [
{
shortName: "ASTER",
longName: "Advanced Spaceborne Thermal Emission and Reflection Radiometer",
},
],
},
{
type: "Earth Observation Satellites",
shortName: "SPOT-4",
longName: "Systeme Probatoire Pour l'Observation de la Terre-4",
instruments: [],
},
];

const stacCollection = collectionToStac(mockCollection);

expect(stacCollection.summaries).to.have.property("platform");
expect(stacCollection.summaries?.platform).to.deep.equal(["Terra", "SPOT-4"]);
expect(stacCollection.summaries).to.have.property("instruments");
expect(stacCollection.summaries?.instruments).to.deep.equal(["ASTER"]);
});
});
});
6 changes: 3 additions & 3 deletions src/domains/__tests__/items.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("granuleToStac", () => {
start_datetime: "2009-09-14T00:00:00.000Z",
end_datetime: "2010-09-14T00:00:00.000Z",
},
collection: "short_1",
collection: "TEST_COLLECTION_1",
links: [
{
href: "undefined/search/concepts/G000000000-TEST_PROV.json",
Expand Down Expand Up @@ -129,7 +129,7 @@ describe("granuleToStac", () => {
start_datetime: "2009-09-14T00:00:00.000Z",
end_datetime: "2010-09-14T00:00:00.000Z",
},
collection: "short_1",
collection: "TEST_COLLECTION_1",
links: [
{
href: "undefined/search/concepts/G000000000-TEST_PROV.json",
Expand Down Expand Up @@ -212,7 +212,7 @@ describe("granuleToStac", () => {
start_datetime: "2009-09-14T00:00:00.000Z",
end_datetime: "2010-09-14T00:00:00.000Z",
},
collection: "short_1",
collection: "TEST_COLLECTION_1",
links: [
{
href: "undefined/search/concepts/G000000000-TEST_PROV.json",
Expand Down
6 changes: 3 additions & 3 deletions src/domains/__tests__/stac.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,8 @@ describe("sortByToSortKeys", () => {
[
{ input: "properties.eo:cloud_cover", output: ["cloudCover"] },
{ input: "-properties.eo:cloud_cover", output: ["-cloudCover"] },
{ input: "id", output: ["shortName"] },
{ input: "-id", output: ["-shortName"] },
{ input: "id", output: ["entryId"] },
{ input: "-id", output: ["-entryId"] },
{ input: "title", output: ["entryTitle"] },
{ input: "-title", output: ["-entryTitle"] },
{ input: "someOtherField", output: ["someOtherField"] },
Expand All @@ -439,7 +439,7 @@ describe("sortByToSortKeys", () => {
{ field: "id", direction: "asc" },
{ field: "title", direction: "desc" },
];
expect(sortByToSortKeys(input)).to.deep.equal(["-cloudCover", "shortName", "-entryTitle"]);
expect(sortByToSortKeys(input)).to.deep.equal(["-cloudCover", "entryId", "-entryTitle"]);
});
});
});
Expand Down
45 changes: 22 additions & 23 deletions src/domains/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,18 @@ const collectionsQuery = gql`
conceptId
description: abstract
directDistributionInformation
entryId
lines
platforms
points
polygons
platforms
provider
relatedUrls
scienceKeywords
shortName
timeEnd
timeStart
title
useConstraints
version
}
}
}
Expand All @@ -56,9 +55,8 @@ const collectionIdsQuery = gql`
cursor
items {
conceptId
shortName
entryId
title
version
}
}
}
Expand Down Expand Up @@ -188,11 +186,19 @@ const createSummaries = (collection: Collection): Summaries => {
instruments: string[];
}

return platforms.reduce<Summaries>(
const summaries = platforms.reduce<Summaries>(
(summaries, platform) => {
const { platform: currPlatforms, instruments: currInstruments } = summaries;
const { instruments, shortName } = platform;

// If instruments is not present, return early with only the platform added
if (!instruments) {
return {
platform: [...currPlatforms, shortName],
instruments: currInstruments,
};
}

return {
platform: [...currPlatforms, shortName],
instruments: [
Expand All @@ -203,6 +209,12 @@ const createSummaries = (collection: Collection): Summaries => {
},
{ platform: [], instruments: [] }
);

if (summaries.instruments.length === 0) {
summaries.instruments = ["Not Provided"];
}

return summaries;
};

const generateProviders = (collection: Collection) => [
Expand All @@ -220,21 +232,20 @@ const generateProviders = (collection: Collection) => [
* Convert a GraphQL collection item into a STACCollection.
*/
export const collectionToStac = (collection: Collection): STACCollection => {
const { description, title } = collection;
const { entryId, description, title } = collection;

const { license, licenseLink } = extractLicense(collection);

const assets = extractAssets(collection);
const extent = createExtent(collection);
const id = collectionToId(collection);
const keywords = createKeywords(collection);
const links = generateCollectionLinks(collection, [licenseLink]);
const provider = generateProviders(collection);
const summaries = createSummaries(collection);

return {
type: "Collection",
id,
id: entryId,
title,
description,
stac_version: STAC_VERSION,
Expand Down Expand Up @@ -290,20 +301,9 @@ export const getCollections = async (
return { cursor, count, items: collections as STACCollection[] };
};

/**
* Return a STAC ID for a given collection.
* STAC ID should correspond to a CMR entry_id
* TODO: handle this type of situation ~> 10.3334/cdiac/otg.vos_alligatorhope_1999-2001_Not applicable as entry_id
*/
export const collectionToId = (collection: { shortName: string; version?: string | null }) => {
const { shortName, version } = collection;

return version ? `${shortName}_${version}` : shortName;
};

const attachId = (collection: { shortName: string; version?: string | null }) => ({
const attachId = (collection: { entryId: string }) => ({
...collection,
id: collectionToId(collection),
id: collection.entryId,
});

/**
Expand Down Expand Up @@ -339,7 +339,6 @@ export const getCollectionIds = async (
count,
items: collectionIds,
} = await paginateQuery(collectionIdsQuery, params, opts, collectionIdsHandler);

return { cursor, count, items: collectionIds as { id: string; title: string }[] };
};

Expand Down
6 changes: 2 additions & 4 deletions src/domains/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { cmrSpatialToExtent } from "./bounding-box";
import { cmrSpatialToGeoJSONGeometry } from "./geojson";
import { mergeMaybe, stacContext } from "../utils";
import { extractAssets, paginateQuery } from "./stac";
import { collectionToId } from "./collections";
import { ItemNotFound } from "../models/errors";

const STAC_VERSION = process.env.STAC_VERSION ?? "1.0.0";
Expand All @@ -26,8 +25,7 @@ const granulesQuery = gql`
conceptId
collection {
conceptId
shortName
version
entryId
title
}
cloudCover
Expand Down Expand Up @@ -216,7 +214,7 @@ export const granuleToStac = (granule: Granule): STACItem => {

return {
...item,
collection: collectionToId(granule.collection),
collection: granule.collection.entryId,
};
};

Expand Down
2 changes: 1 addition & 1 deletion src/domains/stac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ export const sortByToSortKeys = (sortBys?: string | SortObject[] | string[]): st
if (fieldName.match(/^eo:cloud_cover$/i)) {
mappedField = "cloudCover";
} else if (fieldName.match(/^id$/i)) {
mappedField = "shortName";
mappedField = "entryId";
} else if (fieldName.match(/^title$/i)) {
mappedField = "entryTitle";
} else {
Expand Down
5 changes: 2 additions & 3 deletions src/models/GraphQLModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export type Platform = {
type: string;
shortName: string;
longName: string;
instruments: Instrument[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to fix the issue where if the instrument field was null it was returning an error. It doesn't need comments or anything just pointing it out to reviewers since we chatted about that over DM.

We should add in the test session an assertion to make sure that is fixed

instruments?: Instrument[];
};

export type ScienceKeywords = {
Expand All @@ -164,8 +164,7 @@ export type DirectDistributionInformation = {

export type CollectionBase = {
conceptId: string;
version: string;
shortName: string;
entryId: string;
title: string;
};

Expand Down
Loading
Loading