Skip to content

Commit

Permalink
fix: limit the psa request listing and return right count
Browse files Browse the repository at this point in the history
  • Loading branch information
flea89 authored Feb 1, 2022
1 parent f8967a8 commit 67d71e8
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 145 deletions.
11 changes: 8 additions & 3 deletions packages/api/test/pin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ describe('Pinning APIs endpoints', () => {

it('filters pins created before a date', async () => {
const opts = new URLSearchParams({
before: '2021-07-01T00:00:00.000000Z'
before: '2021-07-01T00:00:00.000000Z',
status: 'failed,queued,pinning,pinned'
})
const url = new URL(`${baseUrl}?${opts}`).toString()
const res = await fetch(
Expand All @@ -289,12 +290,14 @@ describe('Pinning APIs endpoints', () => {
assert(res, 'Server responded')
assert(res.ok, 'Server response is ok')
const data = await res.json()
assert.strictEqual(data.results.length, 1)
assert.strictEqual(data.count, 1)
})

it('filters pins created after a date', async () => {
const opts = new URLSearchParams({
after: '2021-07-15T00:00:00.000000Z'
after: '2021-07-15T00:00:00.000000Z',
status: 'failed,queued,pinning,pinned'
})
const url = new URL(`${baseUrl}?${opts}`).toString()
const res = await fetch(
Expand All @@ -309,12 +312,14 @@ describe('Pinning APIs endpoints', () => {
assert(res, 'Server responded')
assert(res.ok, 'Server response is ok')
const data = await res.json()
assert.strictEqual(data.results.length, 1)
assert.strictEqual(data.count, 1)
})

it('limits the number of pins returned for this user and includes the total', async () => {
const opts = new URLSearchParams({
limit: '3'
limit: '3',
status: 'failed,queued,pinning,pinned'
})
const url = new URL(`${baseUrl}?${opts}`).toString()
const res = await fetch(
Expand Down
76 changes: 37 additions & 39 deletions packages/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -881,7 +881,7 @@ export class DBClient {
* Get a filtered list of pin requests for a user
*
* @param {string} authKey
* @param {import('./db-client-types').ListPsaPinRequestOptions} opts
* @param {import('./db-client-types').ListPsaPinRequestOptions} [opts]
* @return {Promise<import('./db-client-types').ListPsaPinRequestResults> }> }
*/
async listPsaPinRequests (authKey, opts = {}) {
Expand All @@ -890,63 +890,61 @@ export class DBClient {

let query = this._client
.from(psaPinRequestTableName)
.select(listPinsQuery)
.select(listPinsQuery, {
count: 'exact'
})
.eq('auth_key_id', authKey)
.is('deleted_at', null)
.limit(limit)
.order('inserted_at', { ascending: false })

if (!Object.keys(opts).length) {
if (!opts.cid && !opts.name && !opts.statuses) {
query = query.eq('content.pins.status', 'Pinned')
} else {
if (opts.statuses) {
query = query.in('content.pins.status', opts.statuses)
}
}

if (opts.cid) {
query = query.in('source_cid', opts.cid)
}
if (opts.statuses) {
query = query.in('content.pins.status', opts.statuses)
}

if (opts.name) {
switch (match) {
case 'exact':
query = query.like('name', `${opts.name}`)
break
case 'iexact':
query = query.ilike('name', `${opts.name}`)
break
case 'partial':
query = query.like('name', `%${opts.name}%`)
break
case 'ipartial':
query = query.ilike('name', `%${opts.name}%`)
break
}
}
if (opts.cid) {
query = query.in('source_cid', opts.cid)
}

if (opts.before) {
query = query.lte('inserted_at', opts.before)
if (opts.name) {
switch (match) {
case 'exact':
query = query.like('name', `${opts.name}`)
break
case 'iexact':
query = query.ilike('name', `${opts.name}`)
break
case 'partial':
query = query.like('name', `%${opts.name}%`)
break
case 'ipartial':
query = query.ilike('name', `%${opts.name}%`)
break
}
}

if (opts.after) {
query = query.gte('inserted_at', opts.after)
}
if (opts.before) {
query = query.lte('inserted_at', opts.before)
}

if (opts.after) {
query = query.gte('inserted_at', opts.after)
}

// TODO(https://github.com/web3-storage/web3.storage/issues/798): filter by meta is missing

/** @type {{ data: Array<import('./db-client-types').PsaPinRequestItem>, error: Error }} */
const { data, error } = (await query)
/** @type {{ data: Array<import('./db-client-types').PsaPinRequestItem>, count: number, error: PostgrestError }} */
const { data, count, error } = (await query)

if (error) {
throw new DBError(error)
}

const count = data.length

// TODO(https://github.com/web3-storage/web3.storage/issues/804): Not limiting the query might cause
// performance issues if a user created lots of requests with a token. We should improve this.
const pinRequests = data.slice(0, limit)
const pins = pinRequests.map(pinRequest => normalizePsaPinRequest(pinRequest))
const pins = data.map(pinRequest => normalizePsaPinRequest(pinRequest))

return {
count,
Expand Down
Loading

0 comments on commit 67d71e8

Please sign in to comment.