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: add timestamps to /status/:cid response #89

Merged
merged 4 commits into from
Jul 15, 2021
Merged
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
14 changes: 12 additions & 2 deletions packages/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ date: Mon, 14 Jun 2021 08:30:56 GMT
content-length: 564692
```

### `GET /root/:cid/deals`
### `GET /status/:cid`

Get filecoin deals info. (_TBD_) We need to define what info we want here to find the right status.
Get pinning status and filecoin deals info for a CID.

```console
$ curl 'http://127.0.0.1:8787/status/bafybeidwfngv7n5y7ydbzotrwl3gohgr2lv2g7vn6xggwcjzrf5emknrki' -s | jq
{
"cid": "bafybeidwfngv7n5y7ydbzotrwl3gohgr2lv2g7vn6xggwcjzrf5emknrki",
"created": "2021-07-14T19:55:49.409306Z",
"dagSize": null,
"pins": [],
"deals": []
}
24 changes: 16 additions & 8 deletions packages/api/src/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export async function statusGet (request, env) {
const result = await env.db.query(
gql`query FindContentByCid($cid: String!) {
findContentByCid(cid: $cid) {
created
dagSize
batchEntries {
data {
Expand All @@ -37,8 +38,10 @@ export async function statusGet (request, env) {
data {
miner
chainDealId
activation
status
activation
created
updated
}
}
}
Expand All @@ -47,6 +50,7 @@ export async function statusGet (request, env) {
pins {
data {
status
updated
location {
peerId
peerName
Expand All @@ -58,16 +62,15 @@ export async function statusGet (request, env) {
}
`, { cid })

const { findContentByCid: raw } = result.data
const { dagSize } = raw
const { findContentByCid: raw } = result

if (raw.pins.data.length === 0 && raw.batchEntries.data.length === 0) {
if (!raw) {
return notFound()
}

const pins = raw.pins.data
.filter(({ status }) => PIN_STATUS.has(status))
.map(({ status, location }) => ({ status, ...location }))
.map(({ status, updated, location }) => ({ status, updated, ...location }))

const deals = raw.batchEntries.data.map(({ dataModelSelector, batch }) => {
const { pieceCid, cid: dataCid, deals } = batch
Expand All @@ -81,19 +84,24 @@ export async function statusGet (request, env) {
}
return deals.data
.filter(({ status }) => DEAL_STATUS.has(status))
.map(({ chainDealId: dealId, miner, activation, status }) => ({
.map(({ chainDealId: dealId, miner, status, activation, created, updated }) => ({
dealId,
miner,
status,
activation,
pieceCid,
dataCid,
dataModelSelector
dataModelSelector,
activation,
created,
updated
}))
}).reduce((a, b) => a.concat(b), []) // flatten array of arrays.

const { dagSize, created } = raw

const status = {
cid,
created,
dagSize,
pins,
deals
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
{
"data": {
"findContentByCid": {
"created": "2021-07-14T19:27:14.934572Z",
"dagSize": 101,
"batchEntries": {
"data": []
},
"pins": {
"data": [{
"status": "Pinned",
"updated": "2021-07-14T19:27:14.934572Z",
"location": {
"peerId": "12D3KooWR1Js",
"peerName": "who?",
"region": "where?"
}
}]
}
}
}
}
}
2 changes: 2 additions & 0 deletions packages/api/test/fixtures/find-content-by-cid-no-deal.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"data": {
"findContentByCid": {
"created": "2021-07-14T19:27:14.934572Z",
"dagSize": 101,
"batchEntries": {
"data": [{
Expand All @@ -17,6 +18,7 @@
"pins": {
"data": [{
"status": "Pinned",
"updated": "2021-07-14T19:27:14.934572Z",
"location": {
"peerId": "12D3KooWR1Js",
"peerName": "who?",
Expand Down
10 changes: 1 addition & 9 deletions packages/api/test/fixtures/find-content-by-cid-unknown.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
{
"data": {
"findContentByCid": {
"dagSize": null,
"batchEntries": {
"data": []
},
"pins": {
"data": []
}
}
"findContentByCid": null
}
}
6 changes: 5 additions & 1 deletion packages/api/test/fixtures/find-content-by-cid.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"data": {
"findContentByCid": {
"created": "2021-07-14T19:27:14.934572Z",
"dagSize": 101,
"batchEntries": {
"data": [{
Expand All @@ -12,8 +13,10 @@
"data" : [ {
"miner": "f99",
"chainDealId": 12345,
"status": "Active",
"activation": "<iso timestamp>",
"status": "Active"
"created": "2021-07-14T19:27:14.934572Z",
"updated": "2021-07-14T19:27:14.934572Z"
}]
}
}
Expand All @@ -22,6 +25,7 @@
"pins": {
"data": [{
"status": "Pinned",
"updated": "2021-07-14T19:27:14.934572Z",
"location": {
"peerId": "12D3KooWR1Js",
"peerName": "who?",
Expand Down
7 changes: 5 additions & 2 deletions packages/api/test/fixtures/status.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cid": "testcid",
"created": "2021-07-14T19:27:14.934572Z",
"dagSize": 101,
"pins": [{
"peerId": "12D3KooWR1Js",
Expand All @@ -11,9 +12,11 @@
"dealId": 12345,
"miner": "f99",
"status": "Active",
"activation": "<iso timestamp>",
"pieceCid": "baga",
"dataCid": "bafy",
"dataModelSelector": "Links/0/Links"
"dataModelSelector": "Links/0/Links",
"activation": "<iso timestamp>",
"created": "2021-07-14T19:27:14.934572Z",
"updated": "2021-07-14T19:27:14.934572Z"
}]
}
8 changes: 4 additions & 4 deletions packages/api/test/mocks/db/post_graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ module.exports = ({ body }) => {

if (body.query.includes('findContentByCid')) {
if (body.variables.cid === 'unknown') {
return gqlOkResponse(require('../../fixtures/find-content-by-cid-unknown.json'))
return gqlResponse(200, require('../../fixtures/find-content-by-cid-unknown.json'))
}
if (body.variables.cid === 'nobatch') {
return gqlOkResponse(require('../../fixtures/find-content-by-cid-no-batch.json'))
return gqlResponse(200, require('../../fixtures/find-content-by-cid-no-batch.json'))
}
if (body.variables.cid === 'nodeal') {
return gqlOkResponse(require('../../fixtures/find-content-by-cid-no-deal.json'))
return gqlResponse(200, require('../../fixtures/find-content-by-cid-no-deal.json'))
}
return gqlOkResponse(require('../../fixtures/find-content-by-cid.json'))
return gqlResponse(200, require('../../fixtures/find-content-by-cid.json'))
}

return gqlResponse(400, {
Expand Down
20 changes: 14 additions & 6 deletions packages/api/test/status.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,29 @@ describe('GET /status/:cid', () => {
it('get pin and deal status', async () => {
const cid = 'testcid'
const res = await fetch(new URL(`status/${cid}`, endpoint))
assert(res.ok)
assert(res.ok, `${JSON.stringify(res)}`)
const json = await res.json()
assert.deepStrictEqual(json, {
cid: 'testcid',
created: '2021-07-14T19:27:14.934572Z',
dagSize: 101,
pins: [{
peerId: '12D3KooWR1Js',
peerName: 'who?',
region: 'where?',
status: 'Pinned'
status: 'Pinned',
updated: '2021-07-14T19:27:14.934572Z'
}],
deals: [{
dealId: 12345,
miner: 'f99',
status: 'Active',
activation: '<iso timestamp>',
pieceCid: 'baga',
dataCid: 'bafy',
dataModelSelector: 'Links/0/Links'
dataModelSelector: 'Links/0/Links',
activation: '<iso timestamp>',
created: '2021-07-14T19:27:14.934572Z',
updated: '2021-07-14T19:27:14.934572Z'
}]
})
})
Expand All @@ -36,12 +40,14 @@ describe('GET /status/:cid', () => {
const json = await res.json()
assert.deepStrictEqual(json, {
cid,
created: '2021-07-14T19:27:14.934572Z',
dagSize: 101,
pins: [{
peerId: '12D3KooWR1Js',
peerName: 'who?',
region: 'where?',
status: 'Pinned'
status: 'Pinned',
updated: '2021-07-14T19:27:14.934572Z'
}],
deals: [{
status: 'Queued',
Expand All @@ -59,12 +65,14 @@ describe('GET /status/:cid', () => {
const json = await res.json()
assert.deepStrictEqual(json, {
cid,
created: '2021-07-14T19:27:14.934572Z',
dagSize: 101,
pins: [{
peerId: '12D3KooWR1Js',
peerName: 'who?',
region: 'where?',
status: 'Pinned'
status: 'Pinned',
updated: '2021-07-14T19:27:14.934572Z'
}],
deals: []
})
Expand Down