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 alert message in the FE when exceeded the API usage #4027

Merged
1 change: 1 addition & 0 deletions frontend/common/services/useOrganisationUsage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const organisationUsageService = service
query.organisationId
}/usage-data/?${Utils.toParam({
environment_id: query.environmentId,
period: query.period,
project_id: query.projectId,
})}`,
}
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 @@ -59,6 +59,7 @@ export type Req = {
organisationId: string
projectId?: string
environmentId?: string
period?: string
Copy link
Member

Choose a reason for hiding this comment

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

This type should be more specific

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

}
deleteIdentity: {
id: string
Expand Down
2 changes: 1 addition & 1 deletion frontend/common/types/responses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ export type Res = {
rolesPermissionUsers: PagedResponse<RolePermissionUser>
createRolePermissionGroup: RolePermissionGroup
rolePermissionGroup: PagedResponse<RolePermissionGroup>
getSubscriptionMetadata: { id: string }
getSubscriptionMetadata: { id: string; max_api_calls: number }
environment: Environment
metadataModelFieldList: PagedResponse<MetadataModelField>
metadataModelField: MetadataModelField
Expand Down
3 changes: 3 additions & 0 deletions frontend/web/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,9 @@ const App = class extends Component {
{user && (
<OrganisationLimit
id={AccountStore.getOrganisation()?.id}
organisationPlan={
AccountStore.getOrganisation()?.subscription.plan
}
/>
)}
{user && showBanner && (
Expand Down
1 change: 1 addition & 0 deletions frontend/web/components/ButterBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const ButterBar: React.FC<ButterBarProps> = ({ billingStatus, projectId }) => {
{ skip: !projectId },
)
const processingRef = useRef(false)

const checkProcessing = useCallback(
(processing: FeatureImport | undefined) => {
if (processing) {
Expand Down
10 changes: 10 additions & 0 deletions frontend/web/components/ErrorMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ export default class ErrorMessage extends PureComponent {
.join('<br/>'),
}}
/>
) : this.props.exceeded ? (
Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't pollute generic components.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

New component Created

<>
Your organisation has exceeded its API usage quota{' '}
<b>{`(${error}).`}</b>{' '}
{Utils.getPlanName(this.props.organisationPlan) === 'Free' ? (
<b>Please upgrade your plan to continue receiving service.</b>
) : (
<b>Automated billing for the overages may apply.</b>
)}
</>
Copy link
Contributor

Choose a reason for hiding this comment

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

The bolding of the error amount looks weird because the end of the sentence extends into the next one. I recommend doing it like this:

Suggested change
<>
Your organisation has exceeded its API usage quota{' '}
<b>{`(${error}).`}</b>{' '}
{Utils.getPlanName(this.props.organisationPlan) === 'Free' ? (
<b>Please upgrade your plan to continue receiving service.</b>
) : (
<b>Automated billing for the overages may apply.</b>
)}
</>
<>
Your organisation has exceeded its API usage quota{' '}
{`(${error}).`}{' '}
{Utils.getPlanName(this.props.organisationPlan) === 'Free' ? (
<b>Please upgrade your plan to continue receiving service.</b>
) : (
<b>Automated billing for the overages may apply.</b>
)}
</>

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

) : (
error
)}
Expand Down
32 changes: 20 additions & 12 deletions frontend/web/components/OrganisationLimit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,33 @@ import { useGetOrganisationUsageQuery } from 'common/services/useOrganisationUsa

type OrganisationLimitType = {
id: string
organisationPlan: string
}

const OrganisationLimit: FC<OrganisationLimitType> = ({ id }) => {
const { data: totalApiCalls } = useGetOrganisationUsageQuery(
{
organisationId: id,
},
{ skip: !id },
)
const OrganisationLimit: FC<OrganisationLimitType> = ({
id,
organisationPlan,
}) => {
let body = { organisationId: id }
if (Utils.getPlanName(organisationPlan) !== 'free') {
body = { ...body, ...{ period: 'current_billing_period' } }
}

const { data: totalApiCalls } = useGetOrganisationUsageQuery(body, {
Copy link
Member

@kyle-ssg kyle-ssg Jun 14, 2024

Choose a reason for hiding this comment

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

Can’t this just go into the new component ? It may as well be just self contained / can be added anywhere

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not quite sure I understand your suggestion. Are you suggesting using this hook in the new QuotaExceededMessagecomponent?

Copy link
Member

@kyle-ssg kyle-ssg Jun 17, 2024

Choose a reason for hiding this comment

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

Yes or just move the QuotaExceededMessagecomponent to this, it doesn't look like it's necessary

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

skip: !id,
})
const { data: maxApiCalls } = useGetSubscriptionMetadataQuery({ id })
const maxApiCallsPercentage = Utils.calculateRemainingLimitsPercentage(
totalApiCalls?.totals.total,
maxApiCalls?.max_api_calls,
70,
).percentage

const alertMaxApiCallsText = `You have used ${Format.shortenNumber(
const errorMessageText = `${Format.shortenNumber(
totalApiCalls?.totals.total,
)}/${Format.shortenNumber(
maxApiCalls?.max_api_calls,
)} of your allowed requests.`
)}/${Format.shortenNumber(maxApiCalls?.max_api_calls)}`

const alertMaxApiCallsText = `You have used ${errorMessageText} of your allowed requests.`

return (
<Row>
Expand All @@ -43,9 +49,11 @@ const OrganisationLimit: FC<OrganisationLimitType> = ({ id }) => {
) : (
maxApiCallsPercentage >= 100 && (
<ErrorMessage
error={alertMaxApiCallsText}
error={errorMessageText}
organisationPlan={organisationPlan}
errorMessageClass={'announcement'}
enabledButton
exceeded
/>
)
))}
Expand Down