@openneuro/search 4.6.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 The Board of Trustees of the Leland Stanford Junior University
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/dist/auth.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ /** Create a dataset:indexing scoped token for search indexing */
2
+ export declare function indexingToken(): string;
3
+ //# sourceMappingURL=auth.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAEA,iEAAiE;AACjE,wBAAgB,aAAa,IAAI,MAAM,CAQtC"}
package/dist/auth.js ADDED
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.indexingToken = void 0;
7
+ const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8
+ /** Create a dataset:indexing scoped token for search indexing */
9
+ function indexingToken() {
10
+ return jsonwebtoken_1.default.sign({
11
+ scopes: ['dataset:indexing'],
12
+ }, process.env.JWT_SECRET || process.env.JEST_WORKER_ID, { expiresIn: 60 * 60 * 3 });
13
+ }
14
+ exports.indexingToken = indexingToken;
@@ -0,0 +1,5 @@
1
+ import { DatasetsIndex } from './indexes/datasets';
2
+ import { DatasetQueryResult, indexDataset, indexQuery, queryForIndex } from './query';
3
+ import { indexingToken } from './auth';
4
+ export { DatasetsIndex, DatasetQueryResult, indexDataset, indexQuery, queryForIndex, indexingToken, };
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AAClD,OAAO,EACL,kBAAkB,EAClB,YAAY,EACZ,UAAU,EACV,aAAa,EACd,MAAM,SAAS,CAAA;AAChB,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAA;AACtC,OAAO,EACL,aAAa,EACb,kBAAkB,EAClB,YAAY,EACZ,UAAU,EACV,aAAa,EACb,aAAa,GACd,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.indexingToken = exports.queryForIndex = exports.indexQuery = exports.indexDataset = exports.DatasetsIndex = void 0;
4
+ const datasets_1 = require("./indexes/datasets");
5
+ Object.defineProperty(exports, "DatasetsIndex", { enumerable: true, get: function () { return datasets_1.DatasetsIndex; } });
6
+ const query_1 = require("./query");
7
+ Object.defineProperty(exports, "indexDataset", { enumerable: true, get: function () { return query_1.indexDataset; } });
8
+ Object.defineProperty(exports, "indexQuery", { enumerable: true, get: function () { return query_1.indexQuery; } });
9
+ Object.defineProperty(exports, "queryForIndex", { enumerable: true, get: function () { return query_1.queryForIndex; } });
10
+ const auth_1 = require("./auth");
11
+ Object.defineProperty(exports, "indexingToken", { enumerable: true, get: function () { return auth_1.indexingToken; } });
@@ -0,0 +1,7 @@
1
+ interface IndexDefinition {
2
+ name: string;
3
+ mapping: Record<string, unknown>;
4
+ }
5
+ export declare const DatasetsIndex: IndexDefinition;
6
+ export default DatasetsIndex;
7
+ //# sourceMappingURL=datasets.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"datasets.d.ts","sourceRoot":"","sources":["../../src/indexes/datasets.ts"],"names":[],"mappings":"AAGA,UAAU,eAAe;IACvB,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;CACjC;AAED,eAAO,MAAM,aAAa,EAAE,eAG3B,CAAA;AAED,eAAe,aAAa,CAAA"}
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.DatasetsIndex = void 0;
7
+ const datasets_mapping_json_1 = __importDefault(require("../mappings/datasets-mapping.json"));
8
+ const names_1 = require("../names");
9
+ exports.DatasetsIndex = {
10
+ name: (0, names_1.elasticMappingName)('datasets', datasets_mapping_json_1.default),
11
+ mapping: datasets_mapping_json_1.default,
12
+ };
13
+ exports.default = exports.DatasetsIndex;
@@ -0,0 +1,100 @@
1
+ {
2
+ "properties": {
3
+ "id": { "type": "keyword" },
4
+ "created": { "type": "date" },
5
+ "public": { "type": "boolean" },
6
+ "metadata": {
7
+ "properties": {
8
+ "datasetName": { "type": "keyword" },
9
+ "datasetUrl": { "type": "keyword" },
10
+ "dataProcessed": { "type": "boolean" },
11
+ "firstSnapshotCreatedAt": { "type": "date" },
12
+ "latestSnapshotCreatedAt": { "type": "date" },
13
+ "ages": { "type": "float" },
14
+ "modalities": { "type": "keyword" },
15
+ "datasetId": { "type": "keyword" },
16
+ "dxStatus": { "type": "keyword" },
17
+ "trialCount": { "type": "long" },
18
+ "tasksCompleted": { "type": "keyword" },
19
+ "studyDesign": { "type": "keyword" },
20
+ "studyDomain": { "type": "keyword" },
21
+ "studyLongitudinal": { "type": "keyword" },
22
+ "species": { "type": "keyword" },
23
+ "associatedPaperDOI": { "type": "keyword" },
24
+ "openneuroPaperDOI": { "type": "keyword" },
25
+ "seniorAuthor": { "type": "keyword" },
26
+ "grantFunderName": { "type": "keyword" },
27
+ "grantIdentifier": { "type": "keyword" }
28
+ }
29
+ },
30
+ "latestSnapshot": {
31
+ "properties": {
32
+ "id": { "type": "keyword" },
33
+ "tag": { "type": "keyword" },
34
+ "summary": {
35
+ "properties": {
36
+ "tasks": { "type": "keyword" },
37
+ "modalities": { "type": "keyword" },
38
+ "secondaryModalities": { "type": "keyword" },
39
+ "subjects": { "type": "keyword" },
40
+ "subjectMetadata": {
41
+ "properties": {
42
+ "participantId": { "type": "keyword" },
43
+ "group": { "type": "keyword" },
44
+ "sex": { "type": "keyword" },
45
+ "age": { "type": "integer" }
46
+ }
47
+ },
48
+ "pet": {
49
+ "properties": {
50
+ "BodyPart": { "type": "keyword" },
51
+ "ScannerManufacturer": { "type": "keyword" },
52
+ "ScannerManufacturersModelName": { "type": "keyword" },
53
+ "TracerName": { "type": "keyword" },
54
+ "TracerRadionuclide": { "type": "keyword" }
55
+ }
56
+ }
57
+ }
58
+ },
59
+ "description": {
60
+ "properties": {
61
+ "Name": { "type": "text" },
62
+ "Authors": { "type": "text" },
63
+ "SeniorAuthor": { "type": "text" }
64
+ }
65
+ },
66
+ "readme": {
67
+ "type": "text"
68
+ }
69
+ }
70
+ },
71
+ "draft": {
72
+ "properties": {
73
+ "issues": {
74
+ "properties": {
75
+ "severity": { "type": "keyword" }
76
+ }
77
+ }
78
+ }
79
+ },
80
+ "permissions": {
81
+ "properties": {
82
+ "userPermissions": {
83
+ "properties": {
84
+ "level": { "type": "keyword" },
85
+ "user": {
86
+ "properties": {
87
+ "id": { "type": "keyword" }
88
+ }
89
+ }
90
+ }
91
+ }
92
+ }
93
+ },
94
+ "analytics": {
95
+ "properties": {
96
+ "downloads": { "type": "integer" }
97
+ }
98
+ }
99
+ }
100
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Hash mappping objects to provide a stable index name if the mapping is unchanged
3
+ * @param mapping ElasticSearch mapping object
4
+ */
5
+ export declare const hashElasticMapping: (mapping: Record<string, unknown>) => string;
6
+ export declare const elasticMappingName: (name: string, mapping: Record<string, unknown>) => string;
7
+ //# sourceMappingURL=names.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"names.d.ts","sourceRoot":"","sources":["../src/names.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,eAAO,MAAM,kBAAkB,YAAa,OAAO,MAAM,EAAE,OAAO,CAAC,KAAG,MACG,CAAA;AAEzE,eAAO,MAAM,kBAAkB,SACvB,MAAM,WACH,OAAO,MAAM,EAAE,OAAO,CAAC,KAC/B,MAAkD,CAAA"}
package/dist/names.js ADDED
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.elasticMappingName = exports.hashElasticMapping = void 0;
7
+ const crypto_1 = __importDefault(require("crypto"));
8
+ /**
9
+ * Hash mappping objects to provide a stable index name if the mapping is unchanged
10
+ * @param mapping ElasticSearch mapping object
11
+ */
12
+ const hashElasticMapping = (mapping) => crypto_1.default.createHash('sha1').update(JSON.stringify(mapping)).digest('hex');
13
+ exports.hashElasticMapping = hashElasticMapping;
14
+ const elasticMappingName = (name, mapping) => `${name}-${(0, exports.hashElasticMapping)(mapping)}`;
15
+ exports.elasticMappingName = elasticMappingName;
@@ -0,0 +1,29 @@
1
+ import { Client as ElasticClient, RequestParams, ApiResponse } from '@elastic/elasticsearch';
2
+ import { ApolloClient, NormalizedCacheObject } from '@apollo/client';
3
+ export declare const INDEX_DATASET_FRAGMENT: import("@apollo/client").DocumentNode;
4
+ export declare const indexDatasetQuery: import("@apollo/client").DocumentNode;
5
+ export declare const indexQuery: import("@apollo/client").DocumentNode;
6
+ export interface DatasetQueryResult {
7
+ id: string;
8
+ metadata: Record<string, any>;
9
+ latestSnapshot: Record<string, any>;
10
+ __typename: string;
11
+ }
12
+ /**
13
+ * Convert from GraphQL dataset object to RequestParams.Index documents
14
+ * TODO: Use generated GraphQL typing
15
+ * @param datasetObj GraphQL dataset object from searchDatasets query
16
+ */
17
+ export declare function extractDatasetDocument(datasetObj: DatasetQueryResult): RequestParams.Index;
18
+ /**
19
+ * Query a single dataset for indexing
20
+ * @param datasetId Accession number
21
+ */
22
+ export declare function queryForIndex(apolloClient: ApolloClient<NormalizedCacheObject>, datasetId: string): Promise<import("@apollo/client").ApolloQueryResult<any>>;
23
+ /**
24
+ * Index one dataset (latest snapshot)
25
+ * @param elasticClient Elastic client to submit index data
26
+ * @param datasetObj OpenNeuro GraphQL dataset object
27
+ */
28
+ export declare function indexDataset(elasticClient: ElasticClient, datasetObj: DatasetQueryResult): Promise<ApiResponse>;
29
+ //# sourceMappingURL=query.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"query.d.ts","sourceRoot":"","sources":["../src/query.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,IAAI,aAAa,EAEvB,aAAa,EAEb,WAAW,EACZ,MAAM,wBAAwB,CAAA;AAC/B,OAAO,EAAE,YAAY,EAAO,qBAAqB,EAAE,MAAM,gBAAgB,CAAA;AAGzE,eAAO,MAAM,sBAAsB,uCA0ElC,CAAA;AAED,eAAO,MAAM,iBAAiB,uCAO7B,CAAA;AAED,eAAO,MAAM,UAAU,uCAoBtB,CAAA;AAGD,MAAM,WAAW,kBAAkB;IACjC,EAAE,EAAE,MAAM,CAAA;IACV,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IAC7B,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IACnC,UAAU,EAAE,MAAM,CAAA;CACnB;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,CACpC,UAAU,EAAE,kBAAkB,GAC7B,aAAa,CAAC,KAAK,CASrB;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAC3B,YAAY,EAAE,YAAY,CAAC,qBAAqB,CAAC,EACjD,SAAS,EAAE,MAAM,4DAOlB;AAED;;;;GAIG;AACH,wBAAsB,YAAY,CAChC,aAAa,EAAE,aAAa,EAC5B,UAAU,EAAE,kBAAkB,GAC7B,OAAO,CAAC,WAAW,CAAC,CAUtB"}
package/dist/query.js ADDED
@@ -0,0 +1,153 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.indexDataset = exports.queryForIndex = exports.extractDatasetDocument = exports.indexQuery = exports.indexDatasetQuery = exports.INDEX_DATASET_FRAGMENT = void 0;
4
+ const client_1 = require("@apollo/client");
5
+ const datasets_1 = require("./indexes/datasets");
6
+ exports.INDEX_DATASET_FRAGMENT = (0, client_1.gql) `
7
+ fragment DatasetIndex on Dataset {
8
+ id
9
+ created
10
+ public
11
+ metadata {
12
+ datasetName
13
+ datasetUrl
14
+ dataProcessed
15
+ firstSnapshotCreatedAt
16
+ latestSnapshotCreatedAt
17
+ ages
18
+ modalities
19
+ datasetId
20
+ dxStatus
21
+ trialCount
22
+ tasksCompleted
23
+ studyDesign
24
+ studyDomain
25
+ studyLongitudinal
26
+ dataProcessed
27
+ species
28
+ associatedPaperDOI
29
+ openneuroPaperDOI
30
+ seniorAuthor
31
+ grantFunderName
32
+ grantIdentifier
33
+ }
34
+ latestSnapshot {
35
+ id
36
+ tag
37
+ description {
38
+ Name
39
+ Authors
40
+ SeniorAuthor
41
+ }
42
+ summary {
43
+ tasks
44
+ modalities
45
+ secondaryModalities
46
+ subjectMetadata {
47
+ participantId
48
+ group
49
+ sex
50
+ age
51
+ }
52
+ subjects
53
+ pet {
54
+ BodyPart
55
+ ScannerManufacturer
56
+ ScannerManufacturersModelName
57
+ TracerName
58
+ TracerRadionuclide
59
+ }
60
+ }
61
+ readme
62
+ }
63
+ draft {
64
+ issues {
65
+ severity
66
+ }
67
+ }
68
+ permissions {
69
+ userPermissions {
70
+ level
71
+ user {
72
+ id
73
+ }
74
+ }
75
+ }
76
+ analytics {
77
+ downloads
78
+ }
79
+ }
80
+ `;
81
+ exports.indexDatasetQuery = (0, client_1.gql) `
82
+ query dataset($datasetId: ID!) {
83
+ dataset(id: $datasetId) {
84
+ ...DatasetIndex
85
+ }
86
+ }
87
+ ${exports.INDEX_DATASET_FRAGMENT}
88
+ `;
89
+ exports.indexQuery = (0, client_1.gql) `
90
+ query getDatasets(
91
+ $cursor: String
92
+ $orderBy: DatasetSort = { created: descending }
93
+ $filterBy: DatasetFilter = {}
94
+ ) {
95
+ datasets(first: 5, after: $cursor, orderBy: $orderBy, filterBy: $filterBy) {
96
+ edges {
97
+ node {
98
+ ...DatasetIndex
99
+ }
100
+ }
101
+ pageInfo {
102
+ endCursor
103
+ hasNextPage
104
+ count
105
+ }
106
+ }
107
+ }
108
+ ${exports.INDEX_DATASET_FRAGMENT}
109
+ `;
110
+ /**
111
+ * Convert from GraphQL dataset object to RequestParams.Index documents
112
+ * TODO: Use generated GraphQL typing
113
+ * @param datasetObj GraphQL dataset object from searchDatasets query
114
+ */
115
+ function extractDatasetDocument(datasetObj) {
116
+ const dataset = {
117
+ index: datasets_1.DatasetsIndex.name,
118
+ id: datasetObj.id,
119
+ body: {
120
+ ...datasetObj,
121
+ },
122
+ };
123
+ return dataset;
124
+ }
125
+ exports.extractDatasetDocument = extractDatasetDocument;
126
+ /**
127
+ * Query a single dataset for indexing
128
+ * @param datasetId Accession number
129
+ */
130
+ function queryForIndex(apolloClient, datasetId) {
131
+ return apolloClient.query({
132
+ query: exports.indexDatasetQuery,
133
+ variables: { datasetId },
134
+ errorPolicy: 'all',
135
+ });
136
+ }
137
+ exports.queryForIndex = queryForIndex;
138
+ /**
139
+ * Index one dataset (latest snapshot)
140
+ * @param elasticClient Elastic client to submit index data
141
+ * @param datasetObj OpenNeuro GraphQL dataset object
142
+ */
143
+ async function indexDataset(elasticClient, datasetObj) {
144
+ try {
145
+ console.log(`Indexing "${datasetObj.id}"`);
146
+ const response = await elasticClient.index(extractDatasetDocument(datasetObj));
147
+ return response;
148
+ }
149
+ catch (err) {
150
+ console.dir(err);
151
+ }
152
+ }
153
+ exports.indexDataset = indexDataset;
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@openneuro/search",
3
+ "version": "4.6.0-alpha.0",
4
+ "description": "OpenNeuro search client functions.",
5
+ "main": "dist/index.js",
6
+ "browser": "src/index.ts",
7
+ "exports": {
8
+ "import": "./src/index.js",
9
+ "require": "./dist/index.js"
10
+ },
11
+ "repository": "git@github.com:OpenNeuroOrg/openneuro.git",
12
+ "author": "Stanford Center for Reproducible Neuroscience",
13
+ "license": "MIT",
14
+ "scripts": {
15
+ "build": "tsc -b"
16
+ },
17
+ "dependencies": {
18
+ "@apollo/client": "3.4.17",
19
+ "@elastic/elasticsearch": "7.15.0",
20
+ "jsonwebtoken": "^8.5.1"
21
+ },
22
+ "devDependencies": {
23
+ "@babel/runtime-corejs3": "^7.13.10",
24
+ "@types/jsonwebtoken": "^8",
25
+ "@types/node": "16.11.13",
26
+ "core-js": "^3.10.1"
27
+ },
28
+ "jest": {
29
+ "testEnvironment": "node"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "gitHead": "ce2db27f750c2614e9cf2f0461add04ca8c3cb48"
35
+ }
@@ -0,0 +1,50 @@
1
+ import { extractDatasetDocument } from '../query'
2
+
3
+ describe('indexDatasets', () => {
4
+ describe('extractDatasetDocument', () => {
5
+ it('filters __typename fields', () => {
6
+ expect(
7
+ extractDatasetDocument({
8
+ id: 'ds000005',
9
+ metadata: {
10
+ species: 'Human',
11
+ },
12
+ latestSnapshot: {
13
+ id: 'ds000005:1.0.0',
14
+ tag: '1.0.0',
15
+ name: 'a test dataset',
16
+ readme: 'a longer description of this dataset',
17
+ description: {
18
+ Name: 'a test dataset',
19
+ Authors: ['J. Doe', 'A. Nonymous'],
20
+ },
21
+ __typename: 'Snapshot',
22
+ },
23
+ __typename: 'Dataset',
24
+ }),
25
+ ).toEqual(
26
+ expect.objectContaining({
27
+ id: 'ds000005',
28
+ body: {
29
+ id: 'ds000005',
30
+ metadata: {
31
+ species: 'Human',
32
+ },
33
+ latestSnapshot: {
34
+ id: 'ds000005:1.0.0',
35
+ tag: '1.0.0',
36
+ name: 'a test dataset',
37
+ readme: 'a longer description of this dataset',
38
+ description: {
39
+ Name: 'a test dataset',
40
+ Authors: ['J. Doe', 'A. Nonymous'],
41
+ },
42
+ __typename: 'Snapshot',
43
+ },
44
+ __typename: 'Dataset',
45
+ },
46
+ }),
47
+ )
48
+ })
49
+ })
50
+ })
@@ -0,0 +1,37 @@
1
+ import { hashElasticMapping } from '../names'
2
+
3
+ const simpleMapping = {
4
+ properties: {
5
+ id: { type: 'keyword' },
6
+ created: { type: 'date' },
7
+ },
8
+ }
9
+
10
+ describe('createIndices', () => {
11
+ describe('hashElasticMapping()', () => {
12
+ it('works with an empty object', () => {
13
+ expect(hashElasticMapping({})).toEqual(
14
+ 'bf21a9e8fbc5a3846fb05b4fa0859e0917b2202f',
15
+ )
16
+ })
17
+ it('works with a simple mapping', () => {
18
+ expect(hashElasticMapping(simpleMapping)).toEqual(
19
+ '21d2497ac57a2c5e2f258946c8efc2fcdb9221d2',
20
+ )
21
+ })
22
+ it('returns only alphanumeric values', () => {
23
+ expect(hashElasticMapping(simpleMapping)).toMatch(/^[a-z0-9]+$/)
24
+ })
25
+ it('produces different hashes for objects with same byte length', () => {
26
+ const similarMapping = {
27
+ properties: {
28
+ id: { type: 'keyword' },
29
+ treated: { type: 'date' },
30
+ },
31
+ }
32
+ expect(hashElasticMapping(simpleMapping)).not.toEqual(
33
+ hashElasticMapping(similarMapping),
34
+ )
35
+ })
36
+ })
37
+ })
package/src/auth.ts ADDED
@@ -0,0 +1,12 @@
1
+ import jwt from 'jsonwebtoken'
2
+
3
+ /** Create a dataset:indexing scoped token for search indexing */
4
+ export function indexingToken(): string {
5
+ return jwt.sign(
6
+ {
7
+ scopes: ['dataset:indexing'],
8
+ },
9
+ process.env.JWT_SECRET || process.env.JEST_WORKER_ID,
10
+ { expiresIn: 60 * 60 * 3 }, // 3 hours
11
+ )
12
+ }
package/src/index.ts ADDED
@@ -0,0 +1,16 @@
1
+ import { DatasetsIndex } from './indexes/datasets'
2
+ import {
3
+ DatasetQueryResult,
4
+ indexDataset,
5
+ indexQuery,
6
+ queryForIndex,
7
+ } from './query'
8
+ import { indexingToken } from './auth'
9
+ export {
10
+ DatasetsIndex,
11
+ DatasetQueryResult,
12
+ indexDataset,
13
+ indexQuery,
14
+ queryForIndex,
15
+ indexingToken,
16
+ }
@@ -0,0 +1,14 @@
1
+ import datasetsMapping from '../mappings/datasets-mapping.json'
2
+ import { elasticMappingName } from '../names'
3
+
4
+ interface IndexDefinition {
5
+ name: string
6
+ mapping: Record<string, unknown>
7
+ }
8
+
9
+ export const DatasetsIndex: IndexDefinition = {
10
+ name: elasticMappingName('datasets', datasetsMapping),
11
+ mapping: datasetsMapping,
12
+ }
13
+
14
+ export default DatasetsIndex
@@ -0,0 +1,100 @@
1
+ {
2
+ "properties": {
3
+ "id": { "type": "keyword" },
4
+ "created": { "type": "date" },
5
+ "public": { "type": "boolean" },
6
+ "metadata": {
7
+ "properties": {
8
+ "datasetName": { "type": "keyword" },
9
+ "datasetUrl": { "type": "keyword" },
10
+ "dataProcessed": { "type": "boolean" },
11
+ "firstSnapshotCreatedAt": { "type": "date" },
12
+ "latestSnapshotCreatedAt": { "type": "date" },
13
+ "ages": { "type": "float" },
14
+ "modalities": { "type": "keyword" },
15
+ "datasetId": { "type": "keyword" },
16
+ "dxStatus": { "type": "keyword" },
17
+ "trialCount": { "type": "long" },
18
+ "tasksCompleted": { "type": "keyword" },
19
+ "studyDesign": { "type": "keyword" },
20
+ "studyDomain": { "type": "keyword" },
21
+ "studyLongitudinal": { "type": "keyword" },
22
+ "species": { "type": "keyword" },
23
+ "associatedPaperDOI": { "type": "keyword" },
24
+ "openneuroPaperDOI": { "type": "keyword" },
25
+ "seniorAuthor": { "type": "keyword" },
26
+ "grantFunderName": { "type": "keyword" },
27
+ "grantIdentifier": { "type": "keyword" }
28
+ }
29
+ },
30
+ "latestSnapshot": {
31
+ "properties": {
32
+ "id": { "type": "keyword" },
33
+ "tag": { "type": "keyword" },
34
+ "summary": {
35
+ "properties": {
36
+ "tasks": { "type": "keyword" },
37
+ "modalities": { "type": "keyword" },
38
+ "secondaryModalities": { "type": "keyword" },
39
+ "subjects": { "type": "keyword" },
40
+ "subjectMetadata": {
41
+ "properties": {
42
+ "participantId": { "type": "keyword" },
43
+ "group": { "type": "keyword" },
44
+ "sex": { "type": "keyword" },
45
+ "age": { "type": "integer" }
46
+ }
47
+ },
48
+ "pet": {
49
+ "properties": {
50
+ "BodyPart": { "type": "keyword" },
51
+ "ScannerManufacturer": { "type": "keyword" },
52
+ "ScannerManufacturersModelName": { "type": "keyword" },
53
+ "TracerName": { "type": "keyword" },
54
+ "TracerRadionuclide": { "type": "keyword" }
55
+ }
56
+ }
57
+ }
58
+ },
59
+ "description": {
60
+ "properties": {
61
+ "Name": { "type": "text" },
62
+ "Authors": { "type": "text" },
63
+ "SeniorAuthor": { "type": "text" }
64
+ }
65
+ },
66
+ "readme": {
67
+ "type": "text"
68
+ }
69
+ }
70
+ },
71
+ "draft": {
72
+ "properties": {
73
+ "issues": {
74
+ "properties": {
75
+ "severity": { "type": "keyword" }
76
+ }
77
+ }
78
+ }
79
+ },
80
+ "permissions": {
81
+ "properties": {
82
+ "userPermissions": {
83
+ "properties": {
84
+ "level": { "type": "keyword" },
85
+ "user": {
86
+ "properties": {
87
+ "id": { "type": "keyword" }
88
+ }
89
+ }
90
+ }
91
+ }
92
+ }
93
+ },
94
+ "analytics": {
95
+ "properties": {
96
+ "downloads": { "type": "integer" }
97
+ }
98
+ }
99
+ }
100
+ }
package/src/names.ts ADDED
@@ -0,0 +1,13 @@
1
+ import crypto from 'crypto'
2
+
3
+ /**
4
+ * Hash mappping objects to provide a stable index name if the mapping is unchanged
5
+ * @param mapping ElasticSearch mapping object
6
+ */
7
+ export const hashElasticMapping = (mapping: Record<string, unknown>): string =>
8
+ crypto.createHash('sha1').update(JSON.stringify(mapping)).digest('hex')
9
+
10
+ export const elasticMappingName = (
11
+ name: string,
12
+ mapping: Record<string, unknown>,
13
+ ): string => `${name}-${hashElasticMapping(mapping)}`
package/src/query.ts ADDED
@@ -0,0 +1,177 @@
1
+ import {
2
+ Client as ElasticClient,
3
+ // Object that contains the type definitions of every API method
4
+ RequestParams,
5
+ // Interface of the generic API response
6
+ ApiResponse,
7
+ } from '@elastic/elasticsearch'
8
+ import { ApolloClient, gql, NormalizedCacheObject } from '@apollo/client'
9
+ import { DatasetsIndex } from './indexes/datasets'
10
+
11
+ export const INDEX_DATASET_FRAGMENT = gql`
12
+ fragment DatasetIndex on Dataset {
13
+ id
14
+ created
15
+ public
16
+ metadata {
17
+ datasetName
18
+ datasetUrl
19
+ dataProcessed
20
+ firstSnapshotCreatedAt
21
+ latestSnapshotCreatedAt
22
+ ages
23
+ modalities
24
+ datasetId
25
+ dxStatus
26
+ trialCount
27
+ tasksCompleted
28
+ studyDesign
29
+ studyDomain
30
+ studyLongitudinal
31
+ dataProcessed
32
+ species
33
+ associatedPaperDOI
34
+ openneuroPaperDOI
35
+ seniorAuthor
36
+ grantFunderName
37
+ grantIdentifier
38
+ }
39
+ latestSnapshot {
40
+ id
41
+ tag
42
+ description {
43
+ Name
44
+ Authors
45
+ SeniorAuthor
46
+ }
47
+ summary {
48
+ tasks
49
+ modalities
50
+ secondaryModalities
51
+ subjectMetadata {
52
+ participantId
53
+ group
54
+ sex
55
+ age
56
+ }
57
+ subjects
58
+ pet {
59
+ BodyPart
60
+ ScannerManufacturer
61
+ ScannerManufacturersModelName
62
+ TracerName
63
+ TracerRadionuclide
64
+ }
65
+ }
66
+ readme
67
+ }
68
+ draft {
69
+ issues {
70
+ severity
71
+ }
72
+ }
73
+ permissions {
74
+ userPermissions {
75
+ level
76
+ user {
77
+ id
78
+ }
79
+ }
80
+ }
81
+ analytics {
82
+ downloads
83
+ }
84
+ }
85
+ `
86
+
87
+ export const indexDatasetQuery = gql`
88
+ query dataset($datasetId: ID!) {
89
+ dataset(id: $datasetId) {
90
+ ...DatasetIndex
91
+ }
92
+ }
93
+ ${INDEX_DATASET_FRAGMENT}
94
+ `
95
+
96
+ export const indexQuery = gql`
97
+ query getDatasets(
98
+ $cursor: String
99
+ $orderBy: DatasetSort = { created: descending }
100
+ $filterBy: DatasetFilter = {}
101
+ ) {
102
+ datasets(first: 5, after: $cursor, orderBy: $orderBy, filterBy: $filterBy) {
103
+ edges {
104
+ node {
105
+ ...DatasetIndex
106
+ }
107
+ }
108
+ pageInfo {
109
+ endCursor
110
+ hasNextPage
111
+ count
112
+ }
113
+ }
114
+ }
115
+ ${INDEX_DATASET_FRAGMENT}
116
+ `
117
+
118
+ // TODO: This would be better to generate from the GraphQL schema
119
+ export interface DatasetQueryResult {
120
+ id: string
121
+ metadata: Record<string, any>
122
+ latestSnapshot: Record<string, any>
123
+ __typename: string
124
+ }
125
+
126
+ /**
127
+ * Convert from GraphQL dataset object to RequestParams.Index documents
128
+ * TODO: Use generated GraphQL typing
129
+ * @param datasetObj GraphQL dataset object from searchDatasets query
130
+ */
131
+ export function extractDatasetDocument(
132
+ datasetObj: DatasetQueryResult,
133
+ ): RequestParams.Index {
134
+ const dataset: RequestParams.Index = {
135
+ index: DatasetsIndex.name,
136
+ id: datasetObj.id,
137
+ body: {
138
+ ...datasetObj,
139
+ },
140
+ }
141
+ return dataset
142
+ }
143
+
144
+ /**
145
+ * Query a single dataset for indexing
146
+ * @param datasetId Accession number
147
+ */
148
+ export function queryForIndex(
149
+ apolloClient: ApolloClient<NormalizedCacheObject>,
150
+ datasetId: string,
151
+ ) {
152
+ return apolloClient.query({
153
+ query: indexDatasetQuery,
154
+ variables: { datasetId },
155
+ errorPolicy: 'all',
156
+ })
157
+ }
158
+
159
+ /**
160
+ * Index one dataset (latest snapshot)
161
+ * @param elasticClient Elastic client to submit index data
162
+ * @param datasetObj OpenNeuro GraphQL dataset object
163
+ */
164
+ export async function indexDataset(
165
+ elasticClient: ElasticClient,
166
+ datasetObj: DatasetQueryResult,
167
+ ): Promise<ApiResponse> {
168
+ try {
169
+ console.log(`Indexing "${datasetObj.id}"`)
170
+ const response: ApiResponse = await elasticClient.index(
171
+ extractDatasetDocument(datasetObj),
172
+ )
173
+ return response
174
+ } catch (err) {
175
+ console.dir(err)
176
+ }
177
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "compilerOptions": {
3
+ "rootDir": "./src",
4
+ "outDir": "./dist",
5
+ "tsBuildInfoFile": "../../.build-cache/search.tsbuildinfo"
6
+ },
7
+ "extends": "../../tsconfig.json",
8
+ "include": ["./src"],
9
+ "exclude": ["node_modules", "**/*.spec.ts"],
10
+ "files": ["./src/mappings/datasets-mapping.json"],
11
+ "references": [{ "path": "../openneuro-client" }]
12
+ }