-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: loading remote boundaries, integration with formula dependency s…
…ystem
- Loading branch information
Showing
27 changed files
with
415 additions
and
141 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { waitFor } from "@testing-library/react" | ||
import { BoundaryManager, boundaryManager } from "./boundary-manager" | ||
|
||
const kStateBoundaries = "US_state_boundaries" | ||
|
||
describe("BoundaryManager", () => { | ||
it("works when initialized/empty", () => { | ||
expect(boundaryManager.boundaryKeys).toEqual([]) | ||
expect(boundaryManager.isBoundarySet()).toBe(false) | ||
expect(boundaryManager.isBoundarySet("foo")).toBe(false) | ||
expect(boundaryManager.hasBoundaryData()).toBe(false) | ||
expect(boundaryManager.hasBoundaryData("foo")).toBe(false) | ||
expect(boundaryManager.isBoundaryDataPending()).toBe(false) | ||
expect(boundaryManager.isBoundaryDataPending("foo")).toBe(false) | ||
expect(boundaryManager.getBoundaryData("foo", "bar")).toBeUndefined() | ||
}) | ||
|
||
it("fetches boundary data successfully", async () => { | ||
// mock the initial boundary specs request | ||
fetchMock.mockResponseOnce(JSON.stringify([ | ||
{ | ||
"name": kStateBoundaries, | ||
"format": "codap", | ||
"url": "US_State_Boundaries.codap" | ||
} | ||
]), { | ||
headers: { | ||
"content-type": "application/json" | ||
} | ||
}) | ||
|
||
const _boundaryManager = new BoundaryManager() | ||
// request has been submitted but response has not been received | ||
expect(_boundaryManager.boundaryKeys.length).toBe(0) | ||
await waitFor(() => { | ||
expect(_boundaryManager.boundaryKeys.length).toBe(1) | ||
}) | ||
expect(_boundaryManager.isBoundarySet(kStateBoundaries)).toBe(true) | ||
|
||
// mock the boundary data request | ||
const kAlaskaBoundaryGuid = 1234 | ||
const kAlaskaBoundaryData = { geometry: {}, coordinates: {}, features: {} } | ||
fetchMock.mockResponseOnce(JSON.stringify({ | ||
contexts: [{ | ||
collections: [ | ||
// [0] boundary collection | ||
{ | ||
cases: [{ | ||
guid: kAlaskaBoundaryGuid, | ||
values: { | ||
boundary: kAlaskaBoundaryData | ||
} | ||
}] | ||
}, | ||
// [1] key collection | ||
{ | ||
cases: [{ | ||
parent: kAlaskaBoundaryGuid, | ||
values: { | ||
key: "alaska" | ||
} | ||
}] | ||
} | ||
] | ||
}] | ||
})) | ||
// request the boundary data | ||
_boundaryManager.getBoundaryData(kStateBoundaries, "Alaska") | ||
// boundary data is pending | ||
expect(_boundaryManager.hasBoundaryData(kStateBoundaries)).toBe(false) | ||
expect(_boundaryManager.isBoundaryDataPending(kStateBoundaries)).toBe(true) | ||
await waitFor(() => { | ||
expect(_boundaryManager.hasBoundaryData(kStateBoundaries)).toBe(true) | ||
}) | ||
expect(_boundaryManager.isBoundaryDataPending(kStateBoundaries)).toBe(false) | ||
expect(_boundaryManager.getBoundaryData(kStateBoundaries, "Alaska")).toEqual(kAlaskaBoundaryData) | ||
expect(_boundaryManager.hasBoundaryDataError(kStateBoundaries)).toBe(false) | ||
}) | ||
|
||
it("logs error when failing to fetch boundary specs", () => { | ||
fetchMock.mockRejectOnce(new Error("not found")) | ||
|
||
jestSpyConsole("error", async spy => { | ||
const _boundaryManager = new BoundaryManager() | ||
await waitFor(() => { | ||
expect(spy).toHaveBeenCalledTimes(1) | ||
}) | ||
expect(_boundaryManager.boundaryKeys.length).toBe(0) | ||
}) | ||
}) | ||
|
||
it("logs error when failing to fetch boundary data", async () => { | ||
// mock the initial boundary specs request | ||
fetchMock.mockResponseOnce(JSON.stringify([ | ||
{ | ||
"name": kStateBoundaries, | ||
"format": "codap", | ||
"url": "US_State_Boundaries.codap" | ||
} | ||
]), { | ||
headers: { | ||
"content-type": "application/json" | ||
} | ||
}) | ||
|
||
const _boundaryManager = new BoundaryManager() | ||
await waitFor(() => { | ||
expect(_boundaryManager.boundaryKeys.length).toBe(1) | ||
}) | ||
|
||
fetchMock.mockRejectOnce(new Error("not found")) | ||
|
||
jestSpyConsole("error", async spy => { | ||
const boundaryData = _boundaryManager.getBoundaryData(kStateBoundaries, "Alaska") | ||
expect(boundaryData).toBeUndefined() | ||
expect(_boundaryManager.isBoundaryDataPending(kStateBoundaries)).toBe(true) | ||
await waitFor(() => { | ||
expect(spy).toHaveBeenCalledTimes(1) | ||
}) | ||
expect(_boundaryManager.isBoundaryDataPending(kStateBoundaries)).toBe(false) | ||
expect(_boundaryManager.hasBoundaryDataError(kStateBoundaries)).toBe(true) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { computed, makeObservable, observable } from "mobx" | ||
import { kBoundariesRootUrl, kBoundariesSpecUrl, BoundaryInfo, isBoundaryInfo } from "./boundary-types" | ||
|
||
export class BoundaryManager { | ||
@observable.shallow | ||
private boundaryMap = new Map<string, BoundaryInfo>() | ||
|
||
// separate observable for remote boundaries for observability | ||
@observable.shallow | ||
private remoteBoundaries = new Map<string, any>() | ||
|
||
constructor() { | ||
makeObservable(this) | ||
|
||
this.fetchBoundarySpecs() | ||
} | ||
|
||
@computed | ||
get boundaryKeys() { | ||
return Array.from(this.boundaryMap.keys()) | ||
} | ||
|
||
async fetchBoundarySpecs() { | ||
try { | ||
const response = await fetch(kBoundariesSpecUrl) | ||
|
||
if (response.ok && response.headers.get("content-type")?.includes("application/json")) { | ||
const boundariesSpecs = await response.json() | ||
|
||
boundariesSpecs.forEach((boundariesSpec: any) => { | ||
if (isBoundaryInfo(boundariesSpec)) { | ||
this.boundaryMap.set(boundariesSpec.name, boundariesSpec) | ||
} | ||
}) | ||
} | ||
} catch (error) { | ||
console.error("Error fetching boundary specs:", error) | ||
} | ||
} | ||
|
||
isBoundarySet(name?: string) { | ||
return !!name && typeof name === "string" && !!this.boundaryMap.get(name) | ||
} | ||
|
||
hasBoundaryData(name?: string) { | ||
return !!name && typeof name === "string" && !!this.remoteBoundaries.get(name) | ||
} | ||
|
||
hasBoundaryDataError(name?: string) { | ||
return !!name && typeof name === "string" && !!this.boundaryMap.get(name)?.error | ||
} | ||
|
||
isBoundaryDataPending(name?: string) { | ||
if (!name || !this.isBoundarySet(name)) return false | ||
return !!this.boundaryMap.get(name)?.promise && !this.hasBoundaryData(name) && !this.hasBoundaryDataError(name) | ||
} | ||
|
||
processBoundaryData(boundaryDocument: any) { | ||
const dataset = boundaryDocument.contexts[0] | ||
const boundaryCollection = dataset.collections[0] | ||
const boundaries: Record<number, string> = {} | ||
boundaryCollection.cases.forEach((aCase: any) => boundaries[aCase.guid] = aCase.values.boundary) | ||
|
||
const keyCollection = dataset.collections[1] | ||
const _boundaryMap: Record<string, string> = {} | ||
keyCollection.cases.forEach((aCase: any) => _boundaryMap[aCase.values.key] = boundaries[aCase.parent]) | ||
return _boundaryMap | ||
} | ||
|
||
getBoundaryData(boundarySet: string, boundaryKey: string) { | ||
if (!this.isBoundarySet(boundarySet)) return | ||
|
||
// Return the boundary data if it has already been fetched and cached | ||
const remoteBoundary = this.remoteBoundaries.get(boundarySet) | ||
if (remoteBoundary) return remoteBoundary[boundaryKey.toLowerCase()] | ||
|
||
// If the boundary info has not yet been fetched, fetch it and return a pending message | ||
const boundaryInfo = this.boundaryMap.get(boundarySet) | ||
if (boundaryInfo && !boundaryInfo.promise) { | ||
boundaryInfo.promise = (async () => { | ||
try { | ||
const boundaryResponse = await fetch(`${kBoundariesRootUrl}/${boundaryInfo.url}`) | ||
const boundary = await boundaryResponse.json() | ||
boundaryInfo.boundary = this.processBoundaryData(boundary) | ||
this.remoteBoundaries.set(boundarySet, boundaryInfo.boundary) | ||
return boundary | ||
} | ||
catch (error) { | ||
console.error(`Error fetching boundary data for "${boundarySet}":`, error) | ||
boundaryInfo.error = error | ||
} | ||
})() | ||
} | ||
|
||
} | ||
} | ||
|
||
export const boundaryManager = new BoundaryManager() |
2 changes: 1 addition & 1 deletion
2
v3/src/utilities/boundary-utils.test.ts → .../models/boundaries/boundary-types.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { codapResourcesUrl } from "../../constants" | ||
|
||
export const kBoundariesRootUrl = codapResourcesUrl("boundaries") | ||
export const kBoundariesSpecUrl = `${kBoundariesRootUrl}/default_boundary_specs.json` | ||
|
||
// TODO: localize this list properly | ||
export const kPolygonNames = ['boundary', 'boundaries', 'polygon', 'polygons', 'grenze', '境界', 'مرز'] | ||
|
||
export const boundaryObjectFromBoundaryValue = (iBoundaryValue: object | string) => { | ||
if (typeof iBoundaryValue === 'object') { | ||
return iBoundaryValue | ||
} else { | ||
try { | ||
return JSON.parse(iBoundaryValue) | ||
} catch (er) { | ||
return null | ||
} | ||
} | ||
} | ||
|
||
export const isBoundaryValue = (iValue: object | string): boolean => { | ||
const obj = boundaryObjectFromBoundaryValue(iValue) | ||
return obj != null && | ||
!!(obj.geometry || obj.coordinates || obj.features || | ||
obj.type === 'FeatureCollection' || obj.type === 'Feature' || obj.jsonBoundaryObject) | ||
} | ||
|
||
export interface BoundaryInfo { | ||
name: string | ||
format: string | ||
url: string | ||
promise?: Promise<unknown> | ||
boundary?: unknown | ||
error?: unknown | ||
} | ||
export function isBoundaryInfo(obj: any): obj is BoundaryInfo { | ||
return obj && typeof obj === "object" && "format" in obj && "name" in obj && "url" in obj | ||
} |
Oops, something went wrong.