-
Notifications
You must be signed in to change notification settings - Fork 4
/
elastic-notebook-index.js
86 lines (81 loc) · 3.07 KB
/
elastic-notebook-index.js
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
//const {Client} = require('@elastic/elasticsearch');
const { Client } = require('@opensearch-project/opensearch');
const configuration = require('./configuration.json');
const notebooksConfig = require('./notebooks.configuration.json');
const { Octokit } = require('octokit');
const { Indexer } = require('./lib/Indexer');
const { ROCrate } = require('ro-crate');
(async () => {
const client = await initElasticClient();
const repository = {};
const indexer = new Indexer({ configuration, repository, client });
const octokit = new Octokit({ auth: notebooksConfig.key });
const {
data: { login },
} = await octokit.rest.users.getAuthenticated();
console.log('Hello, %s', login);
const org = await octokit.request('GET /orgs/{org}/repos', {
org: notebooksConfig.org,
});
for (const repo of org.data || []) {
// const repoData = await octokit.request('GET /repos/{owner}/{repo}', {
// owner: notebooksConfig.org,
// repo: repo.name
// });
let roCrateFile;
try {
//This request only supports 1MB with no limits and 1-100MB with some limits, check doco
roCrateFile = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner: notebooksConfig.org,
repo: repo.name,
path: 'ro-crate-metadata.json', // Where are the ro-crates-metadata.json's?
});
} catch (e) {
console.log(`${repo.name} : ro-crate-metadata.json ${e.message}`);
}
if (roCrateFile && roCrateFile.data.encoding === 'base64') {
const buff = Buffer.from(roCrateFile.data.content, 'base64');
const base64data = buff.toString('utf-8');
//let jsonCrate;
try {
console.log(`Trying: ${repo.name} : ro-crate-metadata.json`);
const jsonCrate = JSON.parse(base64data);
const crate = new ROCrate(jsonCrate, { alwaysAsArray: true, resolveLinks: true });
for (const notebook of crate.utils.asArray(crate.rootDataset.hasPart)) {
await indexer.indexNotebook({
org: notebooksConfig.org,
repo,
notebookId: notebook['@id'],
crate,
binderUrl: notebooksConfig.binderUrl,
notebookRequest: { get: octokit.request, from: 'GET /repos/{owner}/{repo}/contents/{path}' },
});
}
} catch (e) {
console.log(e.message);
}
}
}
})();
async function initElasticClient() {
const elastic = configuration?.api?.elastic;
// Init elastic client
const client = new Client({
node: 'http://localhost:9200', //This is different from Oni since we are talking to it directly
});
// Configure mappings
console.log('Index Configuration');
console.log(JSON.stringify(elastic.indexConfiguration));
const index = elastic?.index || 'items';
await client.indices.putSettings({
index,
body: {
max_result_window: elastic.indexConfiguration?.max_result_window,
mapping: elastic.indexConfiguration?.mapping,
},
});
const settings = await client.indices.getSettings({ index });
console.log('Index Settings');
console.log(JSON.stringify(settings));
return client;
}