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: project usage limits #3313

Merged
merged 9 commits into from
Feb 15, 2024
17 changes: 17 additions & 0 deletions frontend/common/services/useProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export const projectService = service
.enhanceEndpoints({ addTagTypes: ['Project'] })
.injectEndpoints({
endpoints: (builder) => ({
getProject: builder.query<Res['project'], Req['getProject']>({
providesTags: (res) => [{ id: res?.id, type: 'Project' }],
query: (query: Req['getProject']) => ({
url: `projects/${query.id}`,
}),
}),
getProjects: builder.query<Res['projects'], Req['getProjects']>({
providesTags: [{ id: 'LIST', type: 'Project' }],
query: (data) => ({
Expand All @@ -28,9 +34,20 @@ export async function getProjects(
store.dispatch(projectService.util.getRunningQueriesThunk()),
)
}
export async function getProject(
store: any,
data: Req['getProject'],
options?: Parameters<typeof projectService.endpoints.getProject.initiate>[1],
) {
store.dispatch(projectService.endpoints.getProject.initiate(data, options))
return Promise.all(
store.dispatch(projectService.util.getRunningQueriesThunk()),
)
}
// END OF FUNCTION_EXPORTS

export const {
useGetProjectQuery,
useGetProjectsQuery,
// END OF EXPORTS
} = projectService
Expand Down
1 change: 1 addition & 0 deletions frontend/common/types/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,6 @@ export type Req = {
orgId: string
}
getAuditLogItem: { id: string }
getProject: { id: string }
// END OF TYPES
}
1 change: 1 addition & 0 deletions frontend/common/types/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ export type Res = {
auditLogs: PagedResponse<AuditLogItem>
organisations: PagedResponse<Organisation>
projects: ProjectSummary[]
project: Project
environments: PagedResponse<Environment>
organisationUsage: {
totals: {
Expand Down
82 changes: 82 additions & 0 deletions frontend/web/components/ProjectUsage.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { FC, useEffect, useState } from 'react'
import Project from 'common/project'
import InfoMessage from './InfoMessage'
import { useGetProjectQuery } from 'common/services/useProject'
import { useGetEnvironmentsQuery } from 'common/services/useEnvironment'
import { Environment } from 'common/types/responses'
const _data = require('common/data/base/_data')

type ProjectUsageType = {
projectId: string
}

const ProjectUsage: FC<ProjectUsageType> = ({ projectId }) => {
const { data: project } = useGetProjectQuery({ id: projectId })
const { data: environments } = useGetEnvironmentsQuery({ projectId })

const [segmentOverridesUsage, setSegmentOverridesUsage] = useState<number>(0)

const getSegmentOverridesUsage = (apiKeys: string[]) => {
apiKeys.forEach((apiKey) => {
_data
.get(`${Project.api}environments/${apiKey}`)
.then((res: Environment) => {
setSegmentOverridesUsage(
(prev) => prev + (res.total_segment_overrides || 0),
)
})
})
}

useEffect(() => {
if (environments) {
const apiKeys = environments.results.map((env) => env.api_key)
getSegmentOverridesUsage(apiKeys)
}
}, [environments])

return (
<div className='mt-4'>
<Flex>
<Row>
<InfoMessage>
In order to ensure consistent performance, Flagsmith has the
following usage limits.{' '}
<a
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to ensure consistent performance, Flagsmith has the following usage limits. <a ...>Check the Docs for more details

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

target='_blank'
href='https://docs.flagsmith.com/system-administration/system-limits'
rel='noreferrer'
>
<strong>Check the Docs for more details.</strong>
</a>
</InfoMessage>
</Row>
<Row className='mb-2'>
<h5 className='mb-0'>Project Usage</h5>
</Row>
<Flex className='gap-2'>
<p className='m-0 fs-small fw-normal'>
Segments:{' '}
<span className='fw-bold'>
{project?.total_features}/{project?.max_features_allowed}
</span>
</p>
<p className='m-0 fs-small fw-normal'>
Features:{' '}
<span className='fw-bold'>
{project?.total_segments}/{project?.max_segments_allowed}
</span>
</p>
<p className='m-0 fs-small fw-normal'>
Segment Overrides:{' '}
<span className='fw-bold'>
{segmentOverridesUsage}/{project?.max_segment_overrides_allowed}
</span>
</p>
</Flex>
</Flex>
</div>
)
}

export default ProjectUsage
6 changes: 6 additions & 0 deletions frontend/web/components/pages/ProjectSettingsPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { getRoles } from 'common/services/useRole'
import { getRolesProjectPermissions } from 'common/services/useRolePermission'
import AccountStore from 'common/stores/account-store'
import ImportPage from './ImportPage'
import ProjectUsage from 'components/ProjectUsage'

const ProjectSettingsPage = class extends Component {
static displayName = 'ProjectSettingsPage'
Expand Down Expand Up @@ -478,6 +479,11 @@ const ProjectSettingsPage = class extends Component {
</form>
</div>
</TabItem>
<TabItem tabLabel='Usage'>
<ProjectUsage
projectId={this.props.match.params.projectId}
/>
</TabItem>
<TabItem tabLabel='Permissions'>
<EditPermissions
onSaveUser={() => {
Expand Down
Loading