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

Fix prefix updater #544

Merged
merged 4 commits into from
Aug 12, 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: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- **Improved** performance of styling sidebar panels when many node & edge types
exist ([#542](https://github.com/aws/graph-explorer/pull/542))
- **Fixed** issue with upper case characters in RDF URIs
([#544](https://github.com/aws/graph-explorer/pull/544))

## Release 1.9.0

Expand Down
28 changes: 16 additions & 12 deletions packages/graph-explorer/src/hooks/usePrefixesUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,29 @@ const usePrefixesUpdater = () => {
const setSchema = useSetRecoilState(schemaAtom);
const { enqueueNotification } = useNotification();

const supportsPrefixes = config?.connection?.queryEngine === "sparql";
const existingPrefixes = config?.schema?.prefixes;
const activeConfigId = config?.id;

return useCallback(
(ids: Array<string>) => {
if (config?.connection?.queryEngine !== "sparql" || ids.length === 0) {
if (supportsPrefixes || ids.length === 0) {
return;
}

const genPrefixes = generatePrefixes(ids, config?.schema?.prefixes);
const genPrefixes = generatePrefixes(ids, existingPrefixes);
if (!genPrefixes?.length) {
return;
}

setSchema(prevSchemaMap => {
if (!config?.id) {
if (!activeConfigId) {
return prevSchemaMap;
}

const updatedSchema = new Map(prevSchemaMap);
const schema = updatedSchema.get(config.id);
updatedSchema.set(config.id, {
const schema = updatedSchema.get(activeConfigId);
updatedSchema.set(activeConfigId, {
// Update prefixes does not affect to sync last update date
lastUpdate: schema?.lastUpdate,
vertices: schema?.vertices || [],
Expand All @@ -40,11 +44,11 @@ const usePrefixesUpdater = () => {
return updatedSchema;
});

const oldPrefixesSize = config?.schema?.prefixes?.length || 0;
const newPrefixesSize = genPrefixes.length || 0;
const oldPrefixesSize = existingPrefixes?.length ?? 0;
const newPrefixesSize = genPrefixes.length;
if (
genPrefixes.length &&
config?.schema?.prefixes?.length !== genPrefixes.length
existingPrefixes?.length !== genPrefixes.length
) {
const addedCount = newPrefixesSize - oldPrefixesSize;
enqueueNotification({
Expand All @@ -59,11 +63,11 @@ const usePrefixesUpdater = () => {
}
},
[
config?.id,
config?.connection?.queryEngine,
config?.schema?.prefixes,
enqueueNotification,
supportsPrefixes,
existingPrefixes,
setSchema,
activeConfigId,
enqueueNotification,
]
);
};
Expand Down
56 changes: 50 additions & 6 deletions packages/graph-explorer/src/utils/generatePrefixes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ describe("generatePrefixes", () => {
[
{ prefix: "owl", uri: "https://www.w3.org/2002/07/owl#" },
{ prefix: "dbr", uri: "https://dbpedia.org/resource/" },
{
__inferred: true,
prefix: "loc-r",
uri: "http://www.example.com/location/resource#",
__matches: new Set([
"http://www.example.com/location/resource#London",
"http://www.example.com/location/resource#Manchester",
]),
},
]
);

Expand All @@ -102,25 +111,60 @@ describe("generatePrefixes", () => {
__matches: new Set(["https://dbpedia.org/resource/Qualifying_Rounds"]),
});
expect(updatedPrefixes?.[2]).toEqual({
__inferred: true,
uri: "http://www.example.com/location/resource#",
prefix: "loc-r",
__matches: new Set([
"http://www.example.com/location/resource#London",
"http://www.example.com/location/resource#Manchester",
]),
});
expect(updatedPrefixes?.[3]).toEqual({
__inferred: true,
uri: "http://www.example.com/soccer/ontology/",
prefix: "soc",
__matches: new Set(["http://www.example.com/soccer/ontology/League"]),
});
expect(updatedPrefixes?.[3]).toEqual({
expect(updatedPrefixes?.[4]).toEqual({
__inferred: true,
uri: "http://www.example.com/soccer/resource#",
prefix: "soc-r",
__matches: new Set(["http://www.example.com/soccer/resource#EPL"]),
});
expect(updatedPrefixes?.[4]).toEqual({
});

it("Should update existing prefixes when casing doesn't match", () => {
const updatedPrefixes = generatePrefixes(
[
"http://SecretSpyOrg/entity/quantity",
"http://SecretSpyOrg/entity/other",
"http://SecretSpyOrg/data/hasText",
],
[
{
__inferred: true,
prefix: "ent",
uri: "http://secretspyorg/entity/",
__matches: new Set(["http://SecretSpyOrg/entity/quantity"]),
},
]
);

expect(updatedPrefixes).toHaveLength(2);
expect(updatedPrefixes?.[0]).toEqual({
__inferred: true,
uri: "http://www.example.com/location/resource#",
prefix: "loc-r",
uri: "http://secretspyorg/entity/",
prefix: "ent",
__matches: new Set([
"http://www.example.com/location/resource#London",
"http://www.example.com/location/resource#Manchester",
"http://SecretSpyOrg/entity/quantity",
"http://SecretSpyOrg/entity/other",
]),
});
expect(updatedPrefixes?.[1]).toEqual({
__inferred: true,
uri: "http://secretspyorg/data/",
prefix: "dat",
__matches: new Set(["http://SecretSpyOrg/data/hasText"]),
});
});
});
6 changes: 4 additions & 2 deletions packages/graph-explorer/src/utils/generatePrefixes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,18 @@ const generatePrefixes = (
const updatedPrefixes: PrefixTypeConfig[] = cloneDeep(currentPrefixes);
uris.forEach(uri => {
const existInCommon = cPrefixes.some(prefixConfig => {
return !!uri.match(new RegExp(`^${prefixConfig.uri}`));
return !!uri.match(new RegExp(`^${prefixConfig.uri}`, "i"));
});
if (existInCommon) {
return;
}

const existPrefixIndex = updatedPrefixes.findIndex(prefixConfig => {
return !!uri.match(new RegExp(`^${prefixConfig.uri}`));
return !!uri.match(new RegExp(`^${prefixConfig.uri}`, "i"));
});

if (existPrefixIndex === -1) {
// Create a new prefix entry
try {
const url = new URL(uri);
let newPrefix: PrefixTypeConfig;
Expand All @@ -120,6 +121,7 @@ const generatePrefixes = (
// Catching wrong URLs and skip them
}
} else {
// Update existing prefix entry
if (!updatedPrefixes[existPrefixIndex].__matches) {
updatedPrefixes[existPrefixIndex].__matches = new Set();
}
Expand Down
8 changes: 4 additions & 4 deletions packages/graph-explorer/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default {
return;
}
optionalParams.length > 0
? console.debug(message, optionalParams)
? console.debug(message, ...optionalParams)
: console.debug(message);
},
/** Calls `console.log` if the app is running in DEV mode. */
Expand All @@ -29,19 +29,19 @@ export default {
return;
}
optionalParams.length > 0
? console.log(message, optionalParams)
? console.log(message, ...optionalParams)
: console.log(message);
},
/** Calls `console.warn`. */
warn(message?: any, ...optionalParams: any[]) {
optionalParams.length > 0
? console.warn(message, optionalParams)
? console.warn(message, ...optionalParams)
: console.warn(message);
},
/** Calls `console.error`. */
error(message?: any, ...optionalParams: any[]) {
optionalParams.length > 0
? console.error(message, optionalParams)
? console.error(message, ...optionalParams)
: console.error(message);
},
};
Loading