Открыть боковую панель
Медведев Антон Павлович
universo-react
Коммиты
ee2520ec
Не подтверждена
Коммит
ee2520ec
создал
Янв 16, 2024
по автору
DarkSky
Просмотр файлов
feat: add query quota of workspace (#5603)
владелец
4f4d057a
Изменения
9
Скрыть пробелы
Построчно
Рядом
packages/backend/server/src/modules/quota/index.ts
Просмотр файла @
ee2520ec
...
...
@@ -21,4 +21,4 @@ export class QuotaModule {}
export
{
QuotaManagementService
,
QuotaService
};
export
{
Quota_FreePlanV1
,
Quota_ProPlanV1
,
Quotas
}
from
'
./schema
'
;
export
{
QuotaType
}
from
'
./types
'
;
export
{
QuotaQueryType
,
QuotaType
}
from
'
./types
'
;
packages/backend/server/src/modules/quota/storage.ts
Просмотр файла @
ee2520ec
...
...
@@ -3,6 +3,7 @@ import { Injectable, NotFoundException } from '@nestjs/common';
import
{
WorkspaceBlobStorage
}
from
'
../storage
'
;
import
{
PermissionService
}
from
'
../workspaces/permission
'
;
import
{
QuotaService
}
from
'
./service
'
;
import
{
QuotaQueryType
}
from
'
./types
'
;
@
Injectable
()
export
class
QuotaManagementService
{
...
...
@@ -40,21 +41,21 @@ export class QuotaManagementService {
// get workspace's owner quota and total size of used
// quota was apply to owner's account
async
getWorkspaceUsage
(
workspaceId
:
string
)
{
async
getWorkspaceUsage
(
workspaceId
:
string
)
:
Promise
<
QuotaQueryType
>
{
const
{
user
:
owner
}
=
await
this
.
permissions
.
getWorkspaceOwner
(
workspaceId
);
if
(
!
owner
)
throw
new
NotFoundException
(
'
Workspace owner not found
'
);
const
{
storageQuota
,
blobLimit
}
=
await
this
.
getUserQuota
(
owner
.
id
);
// get all workspaces size of owner used
const
us
ag
eSize
=
await
this
.
getUserUsage
(
owner
.
id
);
const
use
d
Size
=
await
this
.
getUserUsage
(
owner
.
id
);
return
{
quota
:
storageQuota
,
size
:
usageSize
,
limit
:
blobLimit
};
return
{
storageQuota
,
usedSize
,
blobLimit
};
}
async
checkBlobQuota
(
workspaceId
:
string
,
size
:
number
)
{
const
{
quota
,
size
:
usage
Size
}
=
const
{
storageQuota
,
used
Size
}
=
await
this
.
getWorkspaceUsage
(
workspaceId
);
return
q
uota
-
(
size
+
us
ag
eSize
);
return
storageQ
uota
-
(
size
+
use
d
Size
);
}
}
packages/backend/server/src/modules/quota/types.ts
Просмотр файла @
ee2520ec
import
{
Field
,
Int
,
ObjectType
}
from
'
@nestjs/graphql
'
;
import
{
z
}
from
'
zod
'
;
import
{
commonFeatureSchema
,
FeatureKind
}
from
'
../features
'
;
...
...
@@ -37,6 +38,20 @@ export const QuotaSchema = commonFeatureSchema
export
type
Quota
=
z
.
infer
<
typeof
QuotaSchema
>
;
/// ======== query types ========
@
ObjectType
()
export
class
QuotaQueryType
{
@
Field
(()
=>
Int
)
storageQuota
!
:
number
;
@
Field
(()
=>
Int
)
usedSize
!
:
number
;
@
Field
(()
=>
Int
)
blobLimit
!
:
number
;
}
/// ======== utils ========
export
function
formatSize
(
bytes
:
number
,
decimals
:
number
=
2
):
string
{
...
...
packages/backend/server/src/modules/workspaces/resolvers/blob.ts
Просмотр файла @
ee2520ec
...
...
@@ -128,7 +128,7 @@ export class WorkspaceBlobResolver {
Permission
.
Write
);
const
{
quota
,
size
,
l
imit
}
=
const
{
storageQuota
,
usedSize
,
blobL
imit
}
=
await
this
.
quota
.
getWorkspaceUsage
(
workspaceId
);
const
unlimited
=
await
this
.
feature
.
hasWorkspaceFeature
(
...
...
@@ -137,7 +137,7 @@ export class WorkspaceBlobResolver {
);
const
checkExceeded
=
(
recvSize
:
number
)
=>
{
if
(
!
q
uota
)
{
if
(
!
storageQ
uota
)
{
throw
new
GraphQLError
(
'
cannot find user quota
'
,
{
extensions
:
{
status
:
HttpStatus
[
HttpStatus
.
FORBIDDEN
],
...
...
@@ -145,13 +145,15 @@ export class WorkspaceBlobResolver {
},
});
}
const
total
=
s
ize
+
recvSize
;
const
total
=
usedS
ize
+
recvSize
;
// only skip total storage check if workspace has unlimited feature
if
(
total
>
quota
&&
!
unlimited
)
{
this
.
logger
.
log
(
`storage size limit exceeded:
${
total
}
>
${
quota
}
`
);
if
(
total
>
storageQuota
&&
!
unlimited
)
{
this
.
logger
.
log
(
`storage size limit exceeded:
${
total
}
>
${
storageQuota
}
`
);
return
true
;
}
else
if
(
recvSize
>
l
imit
)
{
this
.
logger
.
log
(
`blob size limit exceeded:
${
recvSize
}
>
${
l
imit
}
`
);
}
else
if
(
recvSize
>
blobL
imit
)
{
this
.
logger
.
log
(
`blob size limit exceeded:
${
recvSize
}
>
${
blobL
imit
}
`
);
return
true
;
}
else
{
return
false
;
...
...
packages/backend/server/src/modules/workspaces/resolvers/workspace.ts
Просмотр файла @
ee2520ec
...
...
@@ -31,7 +31,7 @@ import {
import
{
Auth
,
CurrentUser
,
Public
}
from
'
../../auth
'
;
import
{
AuthService
}
from
'
../../auth/service
'
;
import
{
FeatureManagementService
,
FeatureType
}
from
'
../../features
'
;
import
{
QuotaManagementService
}
from
'
../../quota
'
;
import
{
QuotaManagementService
,
QuotaQueryType
}
from
'
../../quota
'
;
import
{
WorkspaceBlobStorage
}
from
'
../../storage
'
;
import
{
UsersService
,
UserType
}
from
'
../../users
'
;
import
{
PermissionService
}
from
'
../permission
'
;
...
...
@@ -149,6 +149,15 @@ export class WorkspaceResolver {
}));
}
@
ResolveField
(()
=>
QuotaQueryType
,
{
name
:
'
quota
'
,
description
:
'
quota of workspace
'
,
complexity
:
2
,
})
workspaceQuota
(@
Parent
()
workspace
:
WorkspaceType
)
{
return
this
.
quota
.
getWorkspaceUsage
(
workspace
.
id
);
}
@
Query
(()
=>
Boolean
,
{
description
:
'
Get is owner of workspace
'
,
complexity
:
2
,
...
...
packages/backend/server/src/schema.gql
Просмотр файла @
ee2520ec
...
...
@@ -131,6 +131,9 @@ type WorkspaceType {
"""Owner of workspace"""
owner: UserType!
"""quota of workspace"""
quota: QuotaQueryType!
"""Available features of workspace"""
availableFeatures: [FeatureType!]!
...
...
@@ -183,6 +186,12 @@ type InvitationType {
invitee
:
UserType
!
}
type
QuotaQueryType
{
storageQuota
:
Int
!
usedSize
:
Int
!
blobLimit
:
Int
!
}
type
TokenType
{
token
:
String
!
refresh
:
String
!
...
...
packages/frontend/graphql/src/graphql/index.ts
Просмотр файла @
ee2520ec
...
...
@@ -880,3 +880,20 @@ mutation acceptInviteByInviteId($workspaceId: String!, $inviteId: String!, $send
)
}`
,
};
export
const
workspaceQuotaQuery
=
{
id
:
'
workspaceQuotaQuery
'
as
const
,
operationName
:
'
workspaceQuota
'
,
definitionName
:
'
workspace
'
,
containsFile
:
false
,
query
:
`
query workspaceQuota($id: String!) {
workspace(id: $id) {
quota {
storageQuota
usedSize
blobLimit
}
}
}`
,
};
packages/frontend/graphql/src/graphql/workspace-quota.gql
0 → 100644
Просмотр файла @
ee2520ec
query
workspaceQuota
(
$id
:
String
!)
{
workspace
(
id
:
$id
)
{
quota
{
storageQuota
usedSize
blobLimit
}
}
}
packages/frontend/graphql/src/schema.ts
Просмотр файла @
ee2520ec
...
...
@@ -827,6 +827,23 @@ export type AcceptInviteByInviteIdMutation = {
acceptInviteById
:
boolean
;
};
export
type
WorkspaceQuotaQueryVariables
=
Exact
<
{
id
:
Scalars
[
'
String
'
][
'
input
'
];
}
>
;
export
type
WorkspaceQuotaQuery
=
{
__typename
?:
'
Query
'
;
workspace
:
{
__typename
?:
'
WorkspaceType
'
;
quota
:
{
__typename
?:
'
QuotaQueryType
'
;
storageQuota
:
number
;
usedSize
:
number
;
blobLimit
:
number
;
};
};
};
export
type
Queries
=
|
{
name
:
'
checkBlobSizesQuery
'
;
...
...
@@ -957,6 +974,11 @@ export type Queries =
name
:
'
listWorkspaceFeaturesQuery
'
;
variables
:
ListWorkspaceFeaturesQueryVariables
;
response
:
ListWorkspaceFeaturesQuery
;
}
|
{
name
:
'
workspaceQuotaQuery
'
;
variables
:
WorkspaceQuotaQueryVariables
;
response
:
WorkspaceQuotaQuery
;
};
export
type
Mutations
=
...
...
Редактирование
Предварительный просмотр
Поддерживает Markdown
0%
Попробовать снова
или
прикрепить новый файл
.
Отмена
You are about to add
0
people
to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Отмена
Пожалуйста,
зарегистрируйтесь
или
войдите
чтобы прокомментировать