forked from OpenNeuroOrg/openneuro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnapshots.ts
More file actions
306 lines (280 loc) · 8.49 KB
/
Copy pathsnapshots.ts
File metadata and controls
306 lines (280 loc) · 8.49 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
* Get snapshots from datalad-service tags
*/
import * as Sentry from "@sentry/node"
import request from "superagent"
import { getRedis, getRedlock } from "../libs/redis"
import CacheItem, { CacheType } from "../cache/item"
import config from "../config"
import { snapshotCreationComparison } from "../utils/snapshots"
import { createDraftDoi } from "../libs/doi/index"
import { assembleMetadata } from "../libs/doi/metadata"
import Doi from "../models/doi"
import { getFiles } from "./files"
import { generateDataladCookie } from "../libs/authentication/jwt"
import notifications from "../libs/notifications"
import Dataset from "../models/dataset"
import Snapshot from "../models/snapshot"
import type { SnapshotDocument } from "../models/snapshot"
import { updateDatasetRevision } from "./draft"
import { getDatasetWorker } from "../libs/datalad-service"
import { createEvent, updateEvent } from "../libs/events"
import { queueIndexDataset } from "../queues/producer-methods"
const lockSnapshot = (datasetId, tag) => {
return getRedlock().lock(
`openneuro:create-snapshot-lock:${datasetId}:${tag}`,
1800000,
)
}
const createSnapshotMetadata = (datasetId, tag, hexsha, created) => {
return Snapshot.updateOne(
{ datasetId: datasetId, tag: tag },
{
$set: {
datasetId: datasetId,
tag: tag,
hexsha: hexsha,
created: created,
},
},
{ upsert: true },
)
}
const createIfNotExistsDoi = async (
datasetId,
tag,
descriptionFieldUpdates,
) => {
if (!config.doi.username || !config.doi.password) return
// Skip if DOI already exists for this snapshot
const existing = await Doi.findOne({ datasetId, snapshotId: tag })
if (existing) {
descriptionFieldUpdates["DatasetDOI"] = `doi:${existing.doi}`
return
}
try {
const attributes = await assembleMetadata(datasetId, tag, "HEAD")
const doi = await createDraftDoi(attributes)
// Persist to MongoDB
await Doi.updateOne(
{ datasetId, snapshotId: tag },
{ $set: { doi, state: "draft" } },
{ upsert: true },
)
descriptionFieldUpdates["DatasetDOI"] = `doi:${doi}`
} catch (err) {
Sentry.captureException(err)
// eslint-disable-next-line no-console
console.error(err)
throw new Error(`DOI minting failed: ${err.message}`)
}
}
const postSnapshot = async (
user,
createSnapshotUrl,
descriptionFieldUpdates,
snapshotChanges,
) => {
// Create snapshot once DOI is ready
const response = await request
.post(createSnapshotUrl)
.send({
description_fields: descriptionFieldUpdates,
snapshot_changes: snapshotChanges,
})
.set("Accept", "application/json")
.set("Cookie", generateDataladCookie(config)(user))
return response.body
}
/**
* Get a list of all snapshot tags available for a dataset
*
* This is equivalent to `git tag` on the repository
*
* @param {string} datasetId Dataset accession number
* @returns {Promise<import('../models/snapshot').SnapshotDocument[]>}
*/
export const getSnapshots = async (datasetId): Promise<SnapshotDocument[]> => {
const dataset = await Dataset.findOne({ id: datasetId })
if (!dataset) return null
const cache = new CacheItem(
getRedis(),
CacheType.snapshot,
[datasetId],
432000,
)
return cache.get(() => {
const url = `${getDatasetWorker(datasetId)}/datasets/${datasetId}/snapshots`
return request
.get(url)
.set("Accept", "application/json")
.then(({ body: { snapshots } }) => {
return snapshots.sort(snapshotCreationComparison)
})
})
}
const announceNewSnapshot = async (snapshot, datasetId, user) => {
if (snapshot.files) {
notifications.snapshotCreated(datasetId, snapshot, user) // send snapshot notification to subscribers
}
}
/**
* Snapshot the current working tree for a dataset
* @param {String} datasetId - Dataset ID string
* @param {String} tag - Snapshot identifier and git tag
* @param {Object} user - User object that has made the snapshot request
* @param {Object} descriptionFieldUpdates - Key/value pairs to update dataset_description.json
* @param {Array<string>} snapshotChanges - Array of changes to inject into CHANGES file
* @returns {Promise<Snapshot>} - resolves when tag is created
*/
export const createSnapshot = async (
datasetId,
tag,
user,
descriptionFieldUpdates = {},
snapshotChanges = [],
) => {
const snapshotCache = new CacheItem(getRedis(), CacheType.snapshot, [
datasetId,
tag,
])
// lock snapshot id to prevent upload/update conflicts
const snapshotLock = await lockSnapshot(datasetId, tag)
try {
// Create a version attempt event
const event = await createEvent(datasetId, user.id, {
type: "versioned",
version: tag,
})
await createIfNotExistsDoi(datasetId, tag, descriptionFieldUpdates)
const createSnapshotUrl = `${
getDatasetWorker(
datasetId,
)
}/datasets/${datasetId}/snapshots/${tag}`
const snapshot = await postSnapshot(
user,
createSnapshotUrl,
descriptionFieldUpdates,
snapshotChanges,
)
snapshot.created = new Date()
snapshot.files = await getFiles(datasetId, tag)
await Promise.all([
// Update the draft status in datasets collection in case any changes were made (DOI, License)
updateDatasetRevision(datasetId),
// Update metadata in snapshots collection
createSnapshotMetadata(datasetId, tag, snapshot.hexsha, snapshot.created),
// Trigger an async update for the name field (cache for sorting)
// Dynamic import breaks circular dependency: datalad/snapshots → resolvers/dataset
import("../graphql/resolvers/dataset").then((m) =>
m.updateDatasetName(datasetId)
),
])
const snapshotListCache = new CacheItem(getRedis(), CacheType.snapshot, [
datasetId,
])
await snapshotListCache.drop()
// Version is created here and event is updated
await updateEvent(event)
// Immediate indexing for new snapshots
queueIndexDataset(datasetId)
announceNewSnapshot(snapshot, datasetId, user)
return snapshot
} catch (err) {
// delete the keys if any step fails
// this avoids inconsistent cache state after failures
snapshotCache.drop()
return err
} finally {
snapshotLock.unlock()
}
}
export const deleteSnapshot = (datasetId, tag) => {
const url = `${
getDatasetWorker(
datasetId,
)
}/datasets/${datasetId}/snapshots/${tag}`
return request.del(url).then(async ({ body }) => {
const snapshotCache = new CacheItem(getRedis(), CacheType.snapshot, [
datasetId,
tag,
])
await snapshotCache.drop()
const snapshotListCache = new CacheItem(getRedis(), CacheType.snapshot, [
datasetId,
])
await snapshotListCache.drop()
return body
})
}
/**
* Get the contents of a snapshot (files, git metadata) from datalad-service
* @param {string} datasetId Dataset accession number
* @param {string} commitRef Tag name to retrieve
* @returns {Promise<import('../models/snapshot').SnapshotDocument>}
*/
export const getSnapshot = (
datasetId,
commitRef,
): Promise<SnapshotDocument> => {
const url = `${
getDatasetWorker(
datasetId,
)
}/datasets/${datasetId}/snapshots/${commitRef}`
const cache = new CacheItem(
getRedis(),
CacheType.snapshot,
[datasetId, commitRef],
432000,
)
return cache.get(() =>
request
.get(url)
.set("Accept", "application/json")
.then(({ body }) => body)
)
}
/**
* Get the hexsha for a snapshot from the datasetId and tag
*
* Returns null for snapshots which do not exist
*
* @param {string} datasetId
* @param {string} tag
*/
export const getSnapshotHexsha = (datasetId, tag) => {
return Snapshot.findOne({ datasetId, tag }, { hexsha: true })
.exec()
.then((result) => (result ? result.hexsha : null))
}
/**
* Get Public Snapshots
*
* Returns the most recent snapshots of all publicly available datasets
*/
export const getPublicSnapshots = () => {
// query all publicly available dataset
return Dataset.find({ public: true }, "id")
.exec()
.then((datasets) => {
const datasetIds = datasets.map((dataset) => dataset.id)
return Snapshot.aggregate([
{ $match: { datasetId: { $in: datasetIds } } },
{ $sort: { created: -1 } },
{
$group: {
_id: "$datasetId",
snapshots: { $push: "$$ROOT" },
},
},
{
$replaceRoot: {
newRoot: { $arrayElemAt: ["$snapshots", 0] },
},
},
]).exec()
})
}