Commit 066500e1 authored by Laurent Wouters's avatar Laurent Wouters
Browse files

[fix] Fixing problem with resources with multiple alternative URIs

parent 68a230d4ef49
......@@ -257,19 +257,20 @@ class LDBrowserImpl implements LDBrowser {
data.resource.uris.forEach(
(uri: string) => (self.cacheStores[uri] = store)
);
let sources = getSourcesFor(data);
let count = sources.length;
let onFinished = () => {
count--;
if (count == 0) resolve(store);
};
sources.forEach((series: DocumentSource[]) => {
let allSources = getSourcesFor(data);
Object.keys(allSources).forEach((url: string) => {
let sources = allSources[url];
self
.loadRdfStoreFromSources(series, store)
.then(onFinished)
.catch(onFinished);
.loadRdfStoreFromSources(sources, store)
.then((loaded: number) => {
// register that the number of loaded triples come from this url
data.observations[url].totalLoaded += loaded;
})
.catch((reason: any) => {
// do nothing
});
});
resolve(store);
}
);
}
......@@ -282,20 +283,17 @@ class LDBrowserImpl implements LDBrowser {
private loadRdfStoreFromSources(
sources: DocumentSource[],
store: $rdf.Formula
): Promise<$rdf.Formula> {
): Promise<number> {
let self = this;
return new Promise(
(
resolve: (store: $rdf.Formula) => void,
reject: (reason: any) => void
) => {
(resolve: (loaded: number) => void, reject: (reason: any) => void) => {
let index = 0;
let tryNextSource = function() {
if (index >= sources.length) return reject(null);
self
.loadRdfStoreFromSource(sources[index], store)
.then((store: $rdf.Formula) => {
resolve(store);
.then((loaded: number) => {
resolve(loaded);
})
.catch((reason: any) => {
index++;
......@@ -316,15 +314,13 @@ class LDBrowserImpl implements LDBrowser {
private loadRdfStoreFromSource(
source: DocumentSource,
store: $rdf.Formula
): Promise<$rdf.Formula> {
): Promise<number> {
return new Promise(
(
resolve: (store: $rdf.Formula) => void,
reject: (reason: any) => void
) => {
(resolve: (loaded: number) => void, reject: (reason: any) => void) => {
fetchDocument(source)
.then((value: RawContent) => {
try {
let before = store.statements.length;
$rdf.parse(
value.content,
store,
......@@ -332,7 +328,7 @@ class LDBrowserImpl implements LDBrowser {
source.contentType,
undefined
);
resolve(store);
resolve(store.statements.length - before);
} catch (err) {
reject(err);
}
......@@ -378,8 +374,19 @@ class LDBrowserImpl implements LDBrowser {
* Executes the user request of navigating to a new resource
* @param resource The selected resource
*/
private onRequestNavigateTo(resouce: application.Resource) {
this.navigateTo(resouce.uri);
private async onRequestNavigateTo(resource: application.Resource) {
let data = await this.resolveResourceData(resource);
// look for the URI with the maximum number of incoming statements
let maxUri = null;
let maxValue = -1;
Object.keys(data.observations).forEach((url: string) => {
let observations = data.observations[url];
if (observations.totalLoaded > maxValue) {
maxUri = url;
maxValue = observations.totalLoaded;
}
});
this.navigateTo(maxUri != null ? maxUri : resource.uri);
}
/**
......
......@@ -54,6 +54,31 @@ export class ConfigCurrentTab extends React.Component<
this.props.data.command.isAutomatic ||
this.props.data.command.selectedTopic == null;
let topic = selectPrimaryTopic(this.props.data);
let allSources = getSourcesFor(this.props.data);
let renderedSources = Object.keys(allSources).map(
(url: string, index: number) => {
let series = allSources[url];
return (
<div key={"series-" + index} style={{ marginTop: "10px" }}>
{series.map((source: DocumentSource, index: number) => (
<div key={"source-" + index} className="row">
<div className="col-12">
{index > 0 ? (
<span title="Fallback source">
{"\u00A0\u00A0\u00A0\u00A0\u21B3"}
</span>
) : (
<span title="Series head">{"\u25BA"}</span>
)}
{source.name}
</div>
</div>
))}
</div>
);
}
);
return (
<div className="container-fluid">
<div className="row">
......@@ -206,26 +231,7 @@ export class ConfigCurrentTab extends React.Component<
<div className="row">
<div className="col-12">Detected sources of data</div>
</div>
{getSourcesFor(this.props.data).map(
(series: DocumentSource[], index: number) => (
<div key={"series-" + index} style={{ marginTop: "10px" }}>
{series.map((source: DocumentSource, index: number) => (
<div key={"source-" + index} className="row">
<div className="col-12">
{index > 0 ? (
<span title="Fallback source">
{"\u00A0\u00A0\u00A0\u00A0\u21B3"}
</span>
) : (
<span title="Series head">{"\u25BA"}</span>
)}
{source.name}
</div>
</div>
))}
</div>
)
)}
{renderedSources}
<hr />
<div className="row">
<div className="col-12">Supplementary resources</div>
......
......@@ -522,7 +522,7 @@ function compareSources(x: DocumentSource, y: DocumentSource): number {
export const NO_DATA = new DocumentSourceNone();
/**
* The observations made for a specific documents
* The observations made for a specific document
*/
export class DocumentObservations {
constructor() {
......@@ -531,6 +531,7 @@ export class DocumentObservations {
this.primaryTopic = "";
this.preemptable = false;
this.negotiated = null;
this.totalLoaded = 0;
}
/**
* The document's url
......@@ -552,6 +553,10 @@ export class DocumentObservations {
* Dataset that can be obtained at the document's location through HTTP content negotiation
*/
negotiated: DocumentSourceLinked;
/**
* The total number of statements loaded from this document
*/
totalLoaded: number;
}
/**
......@@ -583,7 +588,8 @@ export function observeContent(
url: observable.url,
sources: [source],
preemptable: true,
negotiated: null
negotiated: null,
totalLoaded: 0
};
return observations;
} else {
......@@ -615,7 +621,8 @@ export function observeContent(
url: observable.url,
sources: sources,
preemptable: MIME.hasOwnProperty(observable.contentType),
negotiated: negotiated != undefined ? negotiated : null
negotiated: negotiated != undefined ? negotiated : null,
totalLoaded: 0
};
return observations;
}
......@@ -687,13 +694,17 @@ export class ResourceData {
* Gets the sources of data for the specified resource
* @param data The current data about a resource
*/
export function getSourcesFor(data: ResourceData): DocumentSource[][] {
return Object.keys(data.observations).map((url: string) => {
export function getSourcesFor(
data: ResourceData
): { [uri: string]: DocumentSource[] } {
let result: { [url: string]: DocumentSource[] } = {};
Object.keys(data.observations).forEach((url: string) => {
let observations = data.observations[url];
return observations.sources
result[url] = observations.sources
.filter((source: DocumentSource) => source != NO_DATA)
.sort(compareSources);
});
return result;
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment