Skip to content

Commit

Permalink
chore: rename pinned to psaPinned (#1268)
Browse files Browse the repository at this point in the history
Co-authored-by: Alan Shaw <alan.shaw@protocol.ai>
  • Loading branch information
flea89 and alanshaw committed May 20, 2022
1 parent e103152 commit aeae342
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 24 deletions.
2 changes: 1 addition & 1 deletion packages/api/test/user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('GET /user/account', () => {
assert(res.ok)
const data = await res.json()
assert.strictEqual(data.usedStorage.uploaded, 32000)
assert.strictEqual(data.usedStorage.pinned, 10000)
assert.strictEqual(data.usedStorage.psaPinned, 10000)
})
})

Expand Down
4 changes: 2 additions & 2 deletions packages/db/db-client-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,5 +334,5 @@ export type NameItem = {

export type UsedStorage = {
uploaded: number,
pinned: number
}
psaPinned: number
}
3 changes: 2 additions & 1 deletion packages/db/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import type {
ContentInput,
ListPsaPinRequestOptions,
ListPsaPinRequestResults,
UsedStorage
} from './db-client-types'

export { gql }
Expand All @@ -38,7 +39,7 @@ export class DBClient {
getMetricsValue (key: string): Promise<{ total: number }>
upsertUser (user: UpsertUserInput): Promise<UpsertUserOutput>
getUser (issuer: string): Promise<UserOutput>
getUsedStorage (userId: number): Promise<{ uploaded: number, pinned: number }>
getUsedStorage (userId: number): Promise<UsedStorage>
createUpload (data: CreateUploadInput): Promise<CreateUploadOutput>
getUpload (cid: string, userId: number): Promise<UploadItemOutput>
listUploads (userId: number, opts?: ListUploadsOptions): Promise<UploadItemOutput[]>
Expand Down
4 changes: 2 additions & 2 deletions packages/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export class DBClient {
* @returns {Promise<import('./db-client-types').UsedStorage>}
*/
async getUsedStorage (userId) {
/** @type {{ data: { uploaded: string, pinned: string }, error: PostgrestError }} */
/** @type {{ data: { uploaded: string, psa_pinned: string }, error: PostgrestError }} */
const { data, error } = await this._client.rpc('user_used_storage', { query_user_id: userId }).single()

if (error) {
Expand All @@ -206,7 +206,7 @@ export class DBClient {

return {
uploaded: parseTextToNumber(data.uploaded),
pinned: parseTextToNumber(data.pinned)
psaPinned: parseTextToNumber(data.psa_pinned)
}
}

Expand Down
8 changes: 4 additions & 4 deletions packages/db/postgres/functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,14 @@ BEGIN
END
$$;

CREATE TYPE used_storage AS (uploaded TEXT, pinned TEXT);
CREATE TYPE stored_bytes AS (uploaded TEXT, psa_pinned TEXT);

CREATE OR REPLACE FUNCTION user_used_storage(query_user_id BIGINT)
RETURNS used_storage
RETURNS stored_bytes
LANGUAGE plpgsql
AS
$$
DECLARE used_storage used_storage;
DECLARE used_storage stored_bytes;
BEGIN
SELECT COALESCE(SUM(c.dag_size), 0)
INTO used_storage.uploaded::TEXT
Expand All @@ -300,7 +300,7 @@ BEGIN
GROUP BY psa_pr.content_cid,
c.dag_size
) AS pinned_content), 0)
INTO used_storage.pinned::TEXT;
INTO used_storage.psa_pinned::TEXT;

return used_storage;
END
Expand Down
39 changes: 39 additions & 0 deletions packages/db/postgres/migrations/008-user-used-storage-update.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
DROP TYPE used_storage;
CREATE TYPE stored_bytes AS (uploaded TEXT, psa_pinned TEXT);

DROP FUNCTION user_used_storage(bigint);

CREATE OR REPLACE FUNCTION user_used_storage(query_user_id BIGINT)
RETURNS stored_bytes
LANGUAGE plpgsql
AS
$$
DECLARE used_storage stored_bytes;
BEGIN
SELECT COALESCE(SUM(c.dag_size), 0)
INTO used_storage.uploaded::TEXT
FROM upload u
JOIN content c ON c.cid = u.content_cid
WHERE u.user_id = query_user_id::BIGINT
AND u.deleted_at is null;

SELECT COALESCE((
SELECT SUM(dag_size)
FROM (
SELECT psa_pr.content_cid,
c.dag_size
FROM psa_pin_request psa_pr
JOIN content c ON c.cid = psa_pr.content_cid
JOIN pin p ON p.content_cid = psa_pr.content_cid
JOIN auth_key a ON a.id = psa_pr.auth_key_id
WHERE a.user_id = query_user_id::BIGINT
AND psa_pr.deleted_at is null
AND p.status = 'Pinned'
GROUP BY psa_pr.content_cid,
c.dag_size
) AS pinned_content), 0)
INTO used_storage.psa_pinned::TEXT;

return used_storage;
END
$$;
4 changes: 2 additions & 2 deletions packages/db/test/pinning.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ describe('Pin Request', () => {

it('sums pinned size for unique CIDs in used storage', async () => {
let usedStorage = await client.getUsedStorage(user._id)
assert.strictEqual(usedStorage.pinned, dagSize1, 'used storage for pinned')
assert.strictEqual(usedStorage.psaPinned, dagSize1, 'used storage for pinned')

await client.createPsaPinRequest({
sourceCid: cids[1],
Expand All @@ -189,7 +189,7 @@ describe('Pin Request', () => {
})

usedStorage = await client.getUsedStorage(user._id)
assert.strictEqual(usedStorage.pinned, dagSize1 * 2, 'used storage for pinned')
assert.strictEqual(usedStorage.psaPinned, dagSize1 * 2, 'used storage for pinned')
})
})

Expand Down
2 changes: 1 addition & 1 deletion packages/db/test/user.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ describe('user operations', () => {

const emptyUsedStorage = await client.getUsedStorage(user._id)
assert.strictEqual(emptyUsedStorage.uploaded, 0, 'empty used storage for uploaded')
assert.strictEqual(emptyUsedStorage.pinned, 0, 'empty used storage for pinned')
assert.strictEqual(emptyUsedStorage.psaPinned, 0, 'empty used storage for pinned')

// Create Upload 1
const cid1 = 'bafybeiczsscdsbs7ffqz55asqdf3smv6klcw3gofszvwlyarci47fgf111'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const StorageManager = ({ className = '', content }) => {
storageData: { data, isLoading },
} = useUser();
const uploaded = useMemo(() => data?.usedStorage?.uploaded || 0, [data]);
const pinned = useMemo(() => data?.usedStorage?.pinned || 0, [data]);
const psaPinned = useMemo(() => data?.usedStorage?.psaPinned || 0, [data]);
const [componentInViewport, setComponentInViewport] = useState(false);
const storageManagerRef = useRef(/** @type {HTMLDivElement | null} */ (null));

Expand All @@ -51,22 +51,22 @@ const StorageManager = ({ className = '', content }) => {
maxSpaceLabel: content.tiers[0].max_space_label,
unlockLabel: content.tiers[0].unlock_label,
percentUploaded: (uploaded / tebibyte) * 100,
percentPinned: (pinned / tebibyte) * 100,
percentPinned: (psaPinned / tebibyte) * 100,
},
[StorageTiers.TIER_2]: {
maxSpaceLabel: content.tiers[1].max_space_label,
unlockLabel: content.tiers[1].unlock_label,
percentUploaded: (uploaded / (tebibyte * 10)) * 100,
percentPinned: (pinned / (tebibyte * 10)) * 100,
percentPinned: (psaPinned / (tebibyte * 10)) * 100,
},
[StorageTiers.TIER_3]: {
maxSpaceLabel: `${Math.floor(uploaded + pinned / (tebibyte * 10) + 1) + content.tiers[2].max_space_label}`,
maxSpaceLabel: `${Math.floor(uploaded + psaPinned / (tebibyte * 10) + 1) + content.tiers[2].max_space_label}`,
// every increment of 10 changes the amount of space used
percentUploaded: ((uploaded % (tebibyte * 10)) / (tebibyte * 10)) * 100,
percentPinned: ((pinned % (tebibyte * 10)) / (tebibyte * 10)) * 100,
percentPinned: ((psaPinned % (tebibyte * 10)) / (tebibyte * 10)) * 100,
},
}[storageTier]),
[storageTier, uploaded, pinned, content.tiers]
[storageTier, uploaded, psaPinned, content.tiers]
);

useEffect(() => {
Expand Down Expand Up @@ -121,7 +121,7 @@ const StorageManager = ({ className = '', content }) => {
{/* Used storage in GB */}
<span className="storage-label">{content.heading}</span>:{' '}
<span className="storage-number">
{filesz(uploaded + pinned, {
{filesz(uploaded + psaPinned, {
base: 2,
standard: 'iec',
})}
Expand Down Expand Up @@ -150,7 +150,7 @@ const StorageManager = ({ className = '', content }) => {
</Button>
)}
</div>
<div className={clsx('storage-manager-legend', uploaded > 0 || pinned > 0 ? '' : 'no-margin')}>
<div className={clsx('storage-manager-legend', uploaded > 0 || psaPinned > 0 ? '' : 'no-margin')}>
{uploaded > 0 ? (
<div className="sml-uploaded">
<span className="legend-label">{content.legend.uploaded}&nbsp;</span>
Expand All @@ -160,10 +160,10 @@ const StorageManager = ({ className = '', content }) => {
})}
</div>
) : null}
{pinned > 0 ? (
{psaPinned > 0 ? (
<div className="sml-pinned">
<span className="legend-label">{content.legend.pinned}&nbsp;</span>
{filesz(pinned, {
{filesz(psaPinned, {
base: 2,
standard: 'iec',
})}
Expand Down
2 changes: 1 addition & 1 deletion packages/website/components/contexts/userContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { useAuthorization } from './authorizationContext.js';
* @property {string} publicAddress
* @property {string} created
* @property {string} updated
* @property {import('react-query').UseQueryResult<{usedStorage: {uploaded: number, pinned: number}}>} storageData
* @property {import('react-query').UseQueryResult<{usedStorage: {uploaded: number, psaPinned: number}}>} storageData
* @property {string} isLoadingStorage
*/

Expand Down

0 comments on commit aeae342

Please sign in to comment.