Skip to content

Commit cff1be3

Browse files
authored
Merge pull request #3315 from OpenNeuroOrg/feat/brain-initiative-resolver
feat(server): Add resolver to flag Brain Initiative datasets
2 parents 0ace60b + f8e62b3 commit cff1be3

File tree

10 files changed

+22112
-2
lines changed

10 files changed

+22112
-2
lines changed

.codespellrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Ref: https://github.com/codespell-project/codespell#using-a-config-file
22
[codespell]
3-
skip = .git,*.pdf,*.svg,go.sum,*.lock,.codespellrc,.yarn,node_modules,schema-validator.js,.pnp.cjs
3+
skip = .git,*.pdf,*.svg,go.sum,*.lock,.codespellrc,.yarn,node_modules,schema-validator.js,.pnp.cjs,funded_awards.json
44
check-hidden = true
55
ignore-regex = ^.{300,}$|/((.*\|){4,}.*\))|\b(afterAll)\b
66
ignore-words-list = chack

packages/openneuro-search/src/mappings/datasets-mapping.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"id": { "type": "keyword" },
44
"created": { "type": "date" },
55
"public": { "type": "boolean" },
6+
"brainInitiative": { "type": "boolean" },
67
"metadata": {
78
"properties": {
89
"datasetName": { "type": "keyword" },

packages/openneuro-search/src/query.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const INDEX_DATASET_FRAGMENT = gql`
99
id
1010
created
1111
public
12+
brainInitiative
1213
metadata {
1314
datasetName
1415
datasetUrl

packages/openneuro-server/src/cache/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ export enum CacheType {
1313
participantCount = "participantCount",
1414
snapshotDownload = "download",
1515
draftRevision = "revision",
16+
brainInitiative = "brainInitiative"
1617
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
funded_awards.json retrieved originally from https://braininitiative.nih.gov/sites/default/files/views_export/funded_awards/funded_awards.json

packages/openneuro-server/src/data/funded_awards.json

Lines changed: 22037 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { redis } from "../../libs/redis"
2+
import type { DatasetOrSnapshot } from "../../utils/datasetOrSnapshot"
3+
import { latestSnapshot } from "./snapshots"
4+
import { description } from "../../datalad/description"
5+
import Metadata from "../../models/metadata"
6+
import CacheItem, { CacheType } from "../../cache/item"
7+
import * as Sentry from "@sentry/node"
8+
import fundedAwards from "../../data/funded_awards.json"
9+
10+
const brainInitiativeMatch = new RegExp("brain.initiative", "i")
11+
12+
const brainInitiativeGrants = fundedAwards.map((award) =>
13+
award.field_project_number.replace(/[^a-zA-Z0-9]/g, "")
14+
)
15+
16+
/**
17+
* Check for any Brain Initiative metadata
18+
*/
19+
export const brainInitiative = async (
20+
dataset: DatasetOrSnapshot,
21+
_,
22+
context,
23+
): Promise<boolean> => {
24+
const cache = new CacheItem(
25+
redis,
26+
CacheType.brainInitiative,
27+
[dataset.id],
28+
86400,
29+
)
30+
return await cache.get(async () => {
31+
try {
32+
const metadata = await Metadata.findOne({ datasetId: dataset.id })
33+
if (metadata.grantFunderName.match(brainInitiativeMatch)) {
34+
return true
35+
} else {
36+
// Fetch snapshot if metadata didn't match
37+
const snapshot = await latestSnapshot(dataset, null, context)
38+
const snapshotDescription = await description(snapshot)
39+
for (const funding of snapshotDescription.Funding) {
40+
if (funding.match(brainInitiativeMatch)) {
41+
return true
42+
}
43+
}
44+
// Check for grant ids too - filter to only alphanumeric to improve matching across format differences
45+
const identifier = metadata.grantIdentifier.replace(/[^a-zA-Z0-9]/g, "")
46+
for (const grant of brainInitiativeGrants) {
47+
if (
48+
identifier.includes(grant)
49+
) {
50+
return true
51+
}
52+
for (const funding of snapshotDescription.Funding) {
53+
if (funding.replace(/[^a-zA-Z0-9]/g, "").includes(grant)) {
54+
return true
55+
}
56+
}
57+
}
58+
}
59+
return false
60+
} catch (_err) {
61+
Sentry.captureException(_err)
62+
return false
63+
}
64+
})
65+
}

packages/openneuro-server/src/graphql/resolvers/dataset.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { reviewers } from "./reviewer"
1919
import { getDatasetWorker } from "../../libs/datalad-service"
2020
import { getFileName } from "../../datalad/files"
2121
import { onBrainlife } from "./brainlife"
22+
import { brainInitiative } from "./brainInitiative"
2223
import { derivatives } from "./derivatives"
2324
import { promiseTimeout } from "../../utils/promiseTimeout"
2425
import semver from "semver"
@@ -301,6 +302,7 @@ const Dataset = {
301302
history,
302303
worker,
303304
reviewers,
305+
brainInitiative,
304306
}
305307

306308
export default Dataset

packages/openneuro-server/src/graphql/schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ export const typeDefs = `
410410
worker: String
411411
# Anonymous reviewers for this dataset
412412
reviewers: [DatasetReviewer]
413+
# Dataset belongs to Brain Initiative
414+
brainInitiative: Boolean
413415
}
414416
415417
type DatasetDerivatives {

packages/openneuro-server/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"module": "CommonJS"
1010
},
1111
"include": ["./src"],
12-
"files": ["./src/lerna.json"],
12+
"files": ["./src/lerna.json", "./src/data/funded_awards.json"],
1313
"references": [
1414
{ "path": "../openneuro-search" }
1515
]

0 commit comments

Comments
 (0)