Skip to content

Commit

Permalink
get version by cr
Browse files Browse the repository at this point in the history
  • Loading branch information
c121914yu committed Jun 29, 2023
1 parent 61a9b27 commit 8811241
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 11 deletions.
2 changes: 2 additions & 0 deletions frontend/providers/dbprovider/src/api/platform.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { GET, POST, DELETE } from '@/services/request';
import type { Response as resourcePriceResponse } from '@/pages/api/platform/resourcePrice';
import type { Response as DBVersionMapType } from '@/pages/api/platform/getVersion';

export const getResourcePrice = () => GET<resourcePriceResponse>('/api/platform/resourcePrice');
export const getDBVersionMap = () => GET<DBVersionMapType>('/api/platform/getVersion');
9 changes: 2 additions & 7 deletions frontend/providers/dbprovider/src/constants/db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DBEditType, DBDetailType, PodDetailType, BackupStatusMapType } from '@/types/db';
import { DBEditType, DBDetailType, PodDetailType } from '@/types/db';
import { CpuSlideMarkList, MemorySlideMarkList } from './editApp';

export const crLabelKey = 'sealos-db-provider-cr';
Expand Down Expand Up @@ -151,11 +151,6 @@ export const DBTypeList = [
{ id: DBTypeEnum.mongodb, label: 'mongo' },
{ id: DBTypeEnum.mysql, label: 'mysql' }
];
export const DBVersionMap = {
[DBTypeEnum.postgresql]: [{ id: 'postgresql-14.8.0', label: 'postgresql-14.8.0' }],
[DBTypeEnum.mongodb]: [{ id: 'mongodb-5.0.14', label: 'mongodb-5.0.14' }],
[DBTypeEnum.mysql]: [{ id: 'ac-mysql-8.0.30', label: 'ac-mysql-8.0.30' }]
};
export const DBComponentNameMap = {
[DBTypeEnum.postgresql]: 'postgresql',
[DBTypeEnum.mongodb]: 'mongo',
Expand All @@ -169,7 +164,7 @@ export const DBBackupPolicyNameMap = {

export const defaultDBEditValue: DBEditType = {
dbType: DBTypeEnum.postgresql,
dbVersion: DBVersionMap[DBTypeEnum.postgresql][0].id,
dbVersion: '',
dbName: 'dbname',
replicas: 1,
cpu: CpuSlideMarkList[1].value,
Expand Down
4 changes: 3 additions & 1 deletion frontend/providers/dbprovider/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useLoading } from '@/hooks/useLoading';
import { useRouter } from 'next/router';
import { appWithTranslation, useTranslation } from 'next-i18next';
import { getLangStore, setLangStore } from '@/utils/cookieUtils';
import { getUserPrice } from '@/store/static';
import { getUserPrice, getDBVersion } from '@/store/static';

import 'nprogress/nprogress.css';
import 'react-day-picker/dist/style.css';
Expand Down Expand Up @@ -99,6 +99,8 @@ function App({ Component, pageProps, domain }: AppProps & { domain: string }) {
};

getUserPrice();
getDBVersion();

(async () => {
try {
const lang = await sealosApp.getLanguage();
Expand Down
30 changes: 30 additions & 0 deletions frontend/providers/dbprovider/src/pages/api/platform/getVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import * as yaml from 'js-yaml';
import { getK8s } from '@/services/backend/kubernetes';
import { jsonRes } from '@/services/backend/response';
import { DBTypeEnum } from '@/constants/db';

export type Response = Record<
`${DBTypeEnum}`,
{
id: string;
label: string;
}[]
>;

const DBVersionMap = {
[DBTypeEnum.postgresql]: [{ id: 'postgresql-14.8.0', label: 'postgresql-14.8.0' }],
[DBTypeEnum.mongodb]: [{ id: 'mongodb-5.0.14', label: 'mongodb-5.0.14' }],
[DBTypeEnum.mysql]: [{ id: 'ac-mysql-8.0.30', label: 'ac-mysql-8.0.30' }]
};

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
jsonRes(res, {
data: DBVersionMap
});
} catch (error) {
console.log(error);
jsonRes(res, { code: 500, message: 'get price error' });
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import type { DBEditType } from '@/types/db';
import { CpuSlideMarkList, MemorySlideMarkList } from '@/constants/editApp';
import Tabs from '@/components/Tabs';
import MySelect from '@/components/Select';
import { DBTypeList, DBVersionMap } from '@/constants/db';
import { DBTypeList } from '@/constants/db';
import { DBVersionMap } from '@/store/static';
import { useTranslation } from 'next-i18next';
import PriceBox from './PriceBox';
import { INSTALL_ACCOUNT } from '@/store/static';
Expand Down
6 changes: 5 additions & 1 deletion frontend/providers/dbprovider/src/pages/db/edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useGlobalStore } from '@/store/global';
import { serviceSideProps } from '@/utils/i18n';
import { useTranslation } from 'next-i18next';
import { adaptDBForm } from '@/utils/adapt';
import { DBVersionMap } from '@/store/static';
import Header from './components/Header';
import Form from './components/Form';
import Yaml from './components/Yaml';
Expand Down Expand Up @@ -52,7 +53,10 @@ const EditApp = ({ dbName, tabType }: { dbName?: string; tabType?: 'form' | 'yam

// form
const formHook = useForm<DBEditType>({
defaultValues: defaultDBEditValue
defaultValues: {
...defaultDBEditValue,
dbVersion: DBVersionMap.postgresql[0]?.id
}
});

// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
25 changes: 24 additions & 1 deletion frontend/providers/dbprovider/src/store/static.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { getResourcePrice } from '@/api/platform';
import { getResourcePrice, getDBVersionMap } from '@/api/platform';
import type { Response as resourcePriceResponse } from '@/pages/api/platform/resourcePrice';
import { DBTypeEnum } from '@/constants/db';
import type { Response as DBVersionMapType } from '@/pages/api/platform/getVersion';

export let SOURCE_PRICE: resourcePriceResponse = {
cpu: 0.067,
Expand All @@ -9,6 +11,13 @@ export let SOURCE_PRICE: resourcePriceResponse = {
export let INSTALL_ACCOUNT = false;

let retryGetPrice = 3;
let retryVersion = 3;

export let DBVersionMap: DBVersionMapType = {
[DBTypeEnum.postgresql]: [],
[DBTypeEnum.mongodb]: [],
[DBTypeEnum.mysql]: []
};

export const getUserPrice = async () => {
try {
Expand All @@ -24,3 +33,17 @@ export const getUserPrice = async () => {
}
}
};

export const getDBVersion = async () => {
try {
const res = await getDBVersionMap();
DBVersionMap = res;
} catch (err) {
retryVersion--;
if (retryVersion >= 0) {
setTimeout(() => {
getDBVersion();
}, 1000);
}
}
};

0 comments on commit 8811241

Please sign in to comment.