From 968b894f779f72c1a227acd660a3dfb06735a935 Mon Sep 17 00:00:00 2001 From: Kyle Johnson Date: Wed, 21 Aug 2024 18:02:38 +0100 Subject: [PATCH] feat: usage period filter (#4526) --- frontend/common/types/requests.ts | 16 ++ frontend/web/components/App.js | 20 --- frontend/web/components/OrganisationUsage.tsx | 154 +++++++++++++----- 3 files changed, 131 insertions(+), 59 deletions(-) diff --git a/frontend/common/types/requests.ts b/frontend/common/types/requests.ts index b97b023de8c4..c4e5935b0d21 100644 --- a/frontend/common/types/requests.ts +++ b/frontend/common/types/requests.ts @@ -22,6 +22,22 @@ export type PagedRequest = T & { } export type OAuthType = 'github' | 'saml' | 'google' export type PermissionLevel = 'organisation' | 'project' | 'environment' +export const billingPeriods = [ + { + label: 'Current billing period', + value: 'current_billing_period', + }, + { + label: 'Previous billing period', + value: 'previous_billing_period', + }, + { label: 'Last 90 days', value: '90_day_period' }, + { label: 'Last 30 days', value: undefined }, +] +export const freePeriods = [ + { label: 'Last 90 days', value: '90_day_period' }, + { label: 'Last 30 days', value: undefined }, +] export type CreateVersionFeatureState = { environmentId: number featureId: number diff --git a/frontend/web/components/App.js b/frontend/web/components/App.js index 26e352925e67..39e550569a38 100644 --- a/frontend/web/components/App.js +++ b/frontend/web/components/App.js @@ -16,11 +16,9 @@ import { Provider } from 'react-redux' import { getStore } from 'common/store' import { resolveAuthFlow } from '@datadog/ui-extensions-sdk' import ConfigProvider from 'common/providers/ConfigProvider' -import { getOrganisationUsage } from 'common/services/useOrganisationUsage' import Button from './base/forms/Button' import Icon from './Icon' import AccountStore from 'common/stores/account-store' -import InfoMessage from './InfoMessage' import OrganisationLimit from './OrganisationLimit' import GithubStar from './GithubStar' import Tooltip from './Tooltip' @@ -49,7 +47,6 @@ const App = class extends Component { } state = { - activeOrganisation: 0, asideIsVisible: !isMobile, lastEnvironmentId: '', lastProjectId: '', @@ -102,11 +99,9 @@ const App = class extends Component { } this.listenTo(OrganisationStore, 'change', () => this.forceUpdate()) this.listenTo(ProjectStore, 'change', () => this.forceUpdate()) - this.listenTo(AccountStore, 'change', this.getOrganisationUsage) if (AccountStore.model) { this.onLogin() } - this.getOrganisationUsage() window.addEventListener('scroll', this.handleScroll) const updateLastViewed = () => { AsyncStorage.getItem('lastEnv').then((res) => { @@ -123,21 +118,6 @@ const App = class extends Component { updateLastViewed() } - getOrganisationUsage = () => { - if ( - AccountStore.getOrganisation()?.id && - this.state.activeOrganisation !== AccountStore.getOrganisation().id - ) { - getOrganisationUsage(getStore(), { - organisationId: AccountStore.getOrganisation()?.id, - }).then((res) => { - this.setState({ - activeOrganisation: AccountStore.getOrganisation().id, - }) - }) - } - } - toggleDarkMode = () => { const newValue = !Utils.getFlagsmithHasFeature('dark_mode') flagsmith.setTrait('dark_mode', newValue) diff --git a/frontend/web/components/OrganisationUsage.tsx b/frontend/web/components/OrganisationUsage.tsx index 6f07a65937bd..10f3af853344 100644 --- a/frontend/web/components/OrganisationUsage.tsx +++ b/frontend/web/components/OrganisationUsage.tsx @@ -1,4 +1,4 @@ -import Utils from 'common/utils/utils' +import Utils, { planNames } from 'common/utils/utils' import React, { FC, useState } from 'react' import { Bar, @@ -19,6 +19,10 @@ import { ValueType, } from 'recharts/types/component/DefaultTooltipContent' import InfoMessage from './InfoMessage' +import { IonIcon } from '@ionic/react' +import { checkmarkSharp } from 'ionicons/icons' +import AccountStore from 'common/stores/account-store' +import { billingPeriods, freePeriods, Req } from 'common/types/requests' type OrganisationUsageType = { organisationId: string @@ -26,19 +30,28 @@ type OrganisationUsageType = { type LegendItemType = { title: string value: number + selection: string[] + onChange: (v: string) => void colour?: string } -const LegendItem: FC = ({ colour, title, value }) => { +const LegendItem: FC = ({ + colour, + onChange, + selection, + title, + value, +}) => { if (!value) { return null } return (

{Utils.numberWithCommas(value)}

- + onChange(title)}> {!!colour && ( = ({ colour, title, value }) => { height: 16, width: 16, }} - /> + > + {selection.includes(title) && ( + + )} + )} {title} @@ -57,9 +74,14 @@ const LegendItem: FC = ({ colour, title, value }) => { const OrganisationUsage: FC = ({ organisationId }) => { const [project, setProject] = useState() const [environment, setEnvironment] = useState() + const currentPlan = Utils.getPlanName(AccountStore.getActiveOrgPlan()) + const [billingPeriod, setBillingPeriod] = useState< + Req['getOrganisationUsage']['billing_period'] + >(currentPlan === planNames.free ? '90_day_period' : 'current_billing_period') const { data } = useGetOrganisationUsageQuery( { + billing_period: billingPeriod, environmentId: environment, organisationId, projectId: project, @@ -67,10 +89,33 @@ const OrganisationUsage: FC = ({ organisationId }) => { { skip: !organisationId }, ) const colours = ['#0AADDF', '#27AB95', '#FF9F43', '#EF4D56'] + const [selection, setSelection] = useState([ + 'Flags', + 'Identities', + 'Environment Document', + 'Traits', + ]) + const updateSelection = (key) => { + if (selection.includes(key)) { + setSelection(selection.filter((v) => v !== key)) + } else { + setSelection(selection.concat([key])) + } + } return data?.totals ? ( -
-
+
+
+ +