-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiple-client-subscribe.ts
88 lines (78 loc) · 2.98 KB
/
multiple-client-subscribe.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
let ldfetch = require('ldfetch');
let ld_fetch = new ldfetch({});
const N3 = require('n3');
const parser = new N3.Parser();
const solid_pod_location = "https://n079-11.wall1.ilabt.imec.be:3000/pod1/";
const start_find_ldes_stream = Date.now();
async function main() {
const promises: Promise<any>[] = [];
const start_find_ldes_stream = Date.now();
for (let i = 0; i < 5; i++) {
promises.push(find_relevant_streams(solid_pod_location, ["wearable.acceleration.x", "wearable.acceleration.y", "wearable.acceleration.z"]));
}
await Promise.all(promises).then((streams) => {
if (streams) {
const end_find_ldes_stream = Date.now();
console.log(`time_to_find_ldes_stream,${end_find_ldes_stream - start_find_ldes_stream}\n`);
}
});
}
main();
export async function find_relevant_streams(solid_pod_url: string, interest_metrics: string[]): Promise<string[]> {
let relevant_streams: string[] = [];
if (if_exists_relevant_streams(solid_pod_url, interest_metrics)) {
try {
let public_type_index = await find_public_type_index(solid_pod_url);
const response = await ld_fetch.get(public_type_index);
let store = new N3.Store(await response.triples);
for (let quad of store) {
if (quad.predicate.value == "https://w3id.org/tree#view") {
relevant_streams.push(quad.object.value);
}
}
return relevant_streams;
}
catch (error) {
console.log(`Error: ${error}`);
return relevant_streams;
}
}
return relevant_streams;
}
export async function if_exists_relevant_streams(solid_pod_url: string, interest_metrics: string[]): Promise<boolean> {
try {
let public_type_index = await find_public_type_index(solid_pod_url);
const response = await ld_fetch.get(public_type_index);
let store = new N3.Store(await response.triples);
for (let quad of store) {
if (quad.predicate.value == "https://saref.etsi.org/core/relatesToProperty") {
if (interest_metrics.includes(quad.object.value)) {
return true;
}
}
}
return false;
}
catch (error) {
console.log(`Error: ${error}`);
return false;
}
}
export async function find_public_type_index(solid_pod_url: string): Promise<string> {
let profie_document = solid_pod_url + "/profile/card";
try {
const response = await ld_fetch.get(profie_document);
let store = new N3.Store(await response.triples);
for (let quad of store) {
if (quad.predicate.value == "http://www.w3.org/ns/solid/terms#publicTypeIndex") {
return quad.object.value;
};
}
console.log(`Could not find a public type index for ${solid_pod_url}`);
return '';
}
catch (error) {
console.log(`Error: ${error}`);
return '';
}
}