Skip to content

Commit

Permalink
Fix create discrete times bug (#7028)
Browse files Browse the repository at this point in the history
* Add a unit test that will fail.

* Update unit test.

* fix bugs.

* Fixed a bug and improved unit tests.

* Update doc.
  • Loading branch information
mwu2018 authored Jan 17, 2024
1 parent d0906be commit b1b8753
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 13 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
- Update `thredds-catalog-crawler` to `0.0.6`
- `WebMapServiceCatalogItem` will drop problematic query parameters from `url` when calling `GetCapabilities` (eg `"styles","srs","crs","format"`)
- Fixed regression causing explorer window not to display instructions when first opened.
- [The next improvement]
- Enable eslint for typescript: plugin:@typescript-eslint/eslint-recommended
- Fixed a bug where the search box was missing for small screen devices.
- Prevent user adding empty web url
- Fix bug where search results shown in `My Data` tab
- Fix bug in function createDiscreteTimesFromIsoSegments where it might create duplicate timestamps.
- [The next improvement]

#### 8.4.1 - 2023-12-08

Expand Down
5 changes: 5 additions & 0 deletions lib/Core/createDiscreteTimes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export default function createDiscreteTimesFromIsoSegments(
// Add intervals starting at start until:
// we go past the stop date, or
// we go past the max limit
//
// The stop date might be included if it is the same as the current.add(duration).
while (
current &&
current.isSameOrBefore(stop) &&
Expand All @@ -86,11 +88,14 @@ export default function createDiscreteTimesFromIsoSegments(
++count;
}

current.subtract(duration);

if (count >= maxRefreshIntervals) {
console.warn(
"Interval has more than the allowed number of discrete times. Consider setting `maxRefreshIntervals`."
);
} else if (!current.isSame(stop)) {
// Add stop date if it has not been added yet.
result.push({
time: formatMomentForWms(stop, duration),
tag: undefined
Expand Down
67 changes: 67 additions & 0 deletions test/Core/createDiscreteTimesSpec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Complete } from "../../lib/Core/TypeModifiers";
import createDiscreteTimesFromIsoSegments from "../../lib/Core/createDiscreteTimes";

describe("createDiscreteTimesFromIsoSegments", () => {
it("should not duplicate stop date", () => {
const result: Complete<{
time?: string;
tag?: string;
}>[] = [];
const value = "2018-02-07T00:00:00.000Z/2018-03-09T00:00:00.000Z/P15D";
const isoSegments = value.split("/");
const maxRefreshIntervals = 10;
createDiscreteTimesFromIsoSegments(
result,
isoSegments[0],
isoSegments[1],
isoSegments[2],
maxRefreshIntervals
);
expect(result.length).toBe(3);
expect(result[0].time).toBe("2018-02-07T00:00:00Z");
expect(result[1].time).toBe("2018-02-22T00:00:00Z");
expect(result[2].time).toBe("2018-03-09T00:00:00Z");
});

it("should include a stop date", () => {
const result: Complete<{
time?: string;
tag?: string;
}>[] = [];
const value = "2018-02-07T00:00:00.000Z/2018-03-10T00:00:00.000Z/P15D";
const isoSegments = value.split("/");
const maxRefreshIntervals = 10;
createDiscreteTimesFromIsoSegments(
result,
isoSegments[0],
isoSegments[1],
isoSegments[2],
maxRefreshIntervals
);
expect(result.length).toBe(4);
expect(result[0].time).toBe("2018-02-07T00:00:00Z");
expect(result[1].time).toBe("2018-02-22T00:00:00Z");
expect(result[2].time).toBe("2018-03-09T00:00:00Z");
expect(result[3].time).toBe("2018-03-10T00:00:00Z");
});

it("should limit time number to maxRefreshInterval", () => {
const result: Complete<{
time?: string;
tag?: string;
}>[] = [];
const value = "2018-02-07T00:00:00.000Z/2018-03-09T00:00:00.000Z/P15D";
const isoSegments = value.split("/");
const maxRefreshIntervals = 2;
createDiscreteTimesFromIsoSegments(
result,
isoSegments[0],
isoSegments[1],
isoSegments[2],
maxRefreshIntervals
);
expect(result.length).toBe(maxRefreshIntervals);
expect(result[0].time).toBe("2018-02-07T00:00:00Z");
expect(result[1].time).toBe("2018-02-22T00:00:00Z");
});
});
Loading

0 comments on commit b1b8753

Please sign in to comment.