Skip to content

Commit

Permalink
Merge pull request #3174 from OpenNeuroOrg/3173-fix-draft-time-caching
Browse files Browse the repository at this point in the history
Resolve draft modified time from commit history instead of cache
  • Loading branch information
nellh authored Oct 24, 2024
2 parents b165334 + 9897641 commit 7c03e93
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
17 changes: 14 additions & 3 deletions packages/openneuro-server/src/datalad/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,26 @@ export const createDataset = async (
}
}

interface WorkerDraftFields {
// Commit id hash
ref: string
// Commit tree ref
tree: string
// Commit message
message: string
// Commit author time
modified: string
}

/**
* Return the latest commit
* @param {string} id Dataset accession number
*/
export const getDraftHead = async (id) => {
export const getDraftHead = async (id): Promise<WorkerDraftFields> => {
const draftRes = await request
.get(`${getDatasetWorker(id)}/datasets/${id}/draft`)
.set("Accept", "application/json")
return draftRes.body.hexsha
return draftRes.body
}

/**
Expand All @@ -87,7 +98,7 @@ export const getDataset = async (id) => {
const dataset = await Dataset.findOne({ id }).lean()
return {
...dataset,
revision: await getDraftHead(id),
revision: (await getDraftHead(id)).ref,
}
}

Expand Down
13 changes: 8 additions & 5 deletions packages/openneuro-server/src/graphql/resolvers/dataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,14 @@ const worker = (obj) => getDatasetWorker(obj.id)
*/
const Dataset = {
uploader: (ds) => user(ds, { id: ds.uploader }),
draft: async (obj) => ({
id: obj.id,
revision: await datalad.getDraftHead(obj.id),
modified: obj.modified,
}),
draft: async (obj) => {
const draftHead = await datalad.getDraftHead(obj.id)
return {
id: obj.id,
revision: draftHead.ref,
modified: draftHead.modified,
}
},
snapshots,
latestSnapshot,
analytics,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ export const latestSnapshot = async (obj, _, context) => {
// In the case where there are no real snapshots, return most recent commit as snapshot
return await snapshot(
obj,
{ datasetId: obj.id, tag: await getDraftHead(obj.id) },
{ datasetId: obj.id, tag: (await getDraftHead(obj.id)).ref },
context,
)
}
Expand Down
8 changes: 6 additions & 2 deletions services/datalad/datalad_service/handlers/draft.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import os

import falcon
Expand All @@ -19,8 +20,11 @@ async def on_get(self, req, resp, dataset):
if dataset and os.path.exists(dataset_path):
repo = pygit2.Repository(dataset_path)
commit = repo.revparse_single('HEAD')
resp.media = {'hexsha': str(commit.id),
'tree': str(commit.tree_id)}
resp.media = {'ref': str(commit.id),
'hexsha': str(commit.id), # Deprecate 'hexsha' but retain for now
'tree': str(commit.tree_id),
'message': str(commit.message),
'modified': datetime.datetime.fromtimestamp(commit.author.time).isoformat() + 'Z'}
resp.status = falcon.HTTP_OK
else:
resp.status = falcon.HTTP_NOT_FOUND
Expand Down

0 comments on commit 7c03e93

Please sign in to comment.