Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(server): Add resolver to flag Brain Initiative datasets #3315

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"id": { "type": "keyword" },
"created": { "type": "date" },
"public": { "type": "boolean" },
"brainInitiative": { "type": "boolean" },
"metadata": {
"properties": {
"datasetName": { "type": "keyword" },
Expand Down
1 change: 1 addition & 0 deletions packages/openneuro-search/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const INDEX_DATASET_FRAGMENT = gql`
id
created
public
brainInitiative
metadata {
datasetName
datasetUrl
Expand Down
1 change: 1 addition & 0 deletions packages/openneuro-server/src/cache/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export enum CacheType {
participantCount = "participantCount",
snapshotDownload = "download",
draftRevision = "revision",
brainInitiative = "brainInitiative"
}
41 changes: 41 additions & 0 deletions packages/openneuro-server/src/graphql/resolvers/brainInitiative.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { redis } from '../../libs/redis'
import type { DatasetOrSnapshot } from '../../utils/datasetOrSnapshot'
import { latestSnapshot } from './snapshots'
import { description } from '../../datalad/description'
import Metadata from '../../models/metadata'
import CacheItem, { CacheType } from '../../cache/item'
import * as Sentry from "@sentry/node"

const brainInitiativeMatch = new RegExp('brain.initiative', 'i')

/**
* Check for any Brain Initiative metadata
*/
export const brainInitiative = async (
dataset: DatasetOrSnapshot,
_,
context
): Promise<boolean> => {
const cache = new CacheItem(redis, CacheType.brainInitiative, [dataset.id], 7200)
return await cache.get(async () => {
try {
const metadata = await Metadata.findOne({ datasetId: dataset.id })
if (metadata.grantFunderName.match(brainInitiativeMatch)) {
return true
} else {
// Fetch snapshot if metadata didn't match
const snapshot = await latestSnapshot(dataset, null, context)
const snapshotDescription = await description(snapshot)
for (const funding of snapshotDescription.Funding) {
if (funding.match(brainInitiativeMatch)) {
return true
}
}
}
return false
} catch (_err) {
Sentry.captureException(_err)
return false
}
})
}

Check warning on line 41 in packages/openneuro-server/src/graphql/resolvers/brainInitiative.ts

View check run for this annotation

Codecov / codecov/patch

packages/openneuro-server/src/graphql/resolvers/brainInitiative.ts#L15-L41

Added lines #L15 - L41 were not covered by tests
2 changes: 2 additions & 0 deletions packages/openneuro-server/src/graphql/resolvers/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { reviewers } from "./reviewer"
import { getDatasetWorker } from "../../libs/datalad-service"
import { getFileName } from "../../datalad/files"
import { onBrainlife } from "./brainlife"
import { brainInitiative } from "./brainInitiative"
import { derivatives } from "./derivatives"
import { promiseTimeout } from "../../utils/promiseTimeout"
import semver from "semver"
Expand Down Expand Up @@ -301,6 +302,7 @@ const Dataset = {
history,
worker,
reviewers,
brainInitiative,
}

export default Dataset
2 changes: 2 additions & 0 deletions packages/openneuro-server/src/graphql/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@
worker: String
# Anonymous reviewers for this dataset
reviewers: [DatasetReviewer]
# Dataset belongs to Brain Initiative
brainInitiative: Boolean

Check warning on line 414 in packages/openneuro-server/src/graphql/schema.ts

View check run for this annotation

Codecov / codecov/patch

packages/openneuro-server/src/graphql/schema.ts#L413-L414

Added lines #L413 - L414 were not covered by tests
}

type DatasetDerivatives {
Expand Down
Loading