Skip to content

Commit

Permalink
Merge pull request #10 from tokenguardio/fix/dashboard-elements-are-a…
Browse files Browse the repository at this point in the history
…dded-properly

fix database info and groupby operation
  • Loading branch information
rrozek authored Mar 18, 2024
2 parents 30f5c0d + a31cdd5 commit 931db09
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
26 changes: 18 additions & 8 deletions src/components/db-api/databaseInfo.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export const getAllDatabases = async (req: Request, res: Response) => {

export const getAllSchemas = async (req: Request, res: Response) => {
try {
const response = await axios.get(`${API_BASE_URL}/schemas`);
const { dbname } = req.params;
const response = await axios.get(
`${API_BASE_URL}/database/${encodeURIComponent(dbname)}/schemas`,
);
res.status(httpStatus.OK).send({ data: response.data });
} catch (error) {
console.error('Error fetching schemas:', error);
Expand All @@ -32,7 +35,10 @@ export const getAllSchemas = async (req: Request, res: Response) => {

export const getAllTables = async (req: Request, res: Response) => {
try {
const response = await axios.get(`${API_BASE_URL}/tables`);
const { dbname } = req.params;
const response = await axios.get(
`${API_BASE_URL}/database/${encodeURIComponent(dbname)}/tables`,
);
res.status(httpStatus.OK).send({ data: response.data });
} catch (error) {
console.error('Error fetching tables:', error);
Expand All @@ -44,11 +50,13 @@ export const getAllTables = async (req: Request, res: Response) => {

export const getTableColumns = async (req: Request, res: Response) => {
try {
const { schemaName, tableName } = req.params;
const { dbname, schema, table } = req.params;
const response = await axios.get(
`${API_BASE_URL}/tables/${encodeURIComponent(
schemaName,
)}/${encodeURIComponent(tableName)}/columns`,
`${API_BASE_URL}/database/${encodeURIComponent(
dbname,
)}/tables/${encodeURIComponent(schema)}/${encodeURIComponent(
table,
)}/columns`,
);

const processedColumns = response.data.map((column) => {
Expand Down Expand Up @@ -89,7 +97,7 @@ export const getTableColumns = async (req: Request, res: Response) => {

export const generateChartData = async (req: Request, res: Response) => {
try {
const { schema, table } = req.params;
const { dbname, schema, table } = req.params;
const { dimension, measures, differential, filters } = req.body;

logger.debug('measures', measures);
Expand All @@ -107,7 +115,9 @@ export const generateChartData = async (req: Request, res: Response) => {
parsedFilters,
});

const url = `${API_BASE_URL}/group-by-operation/${schema}/${table}`;
const url = `${API_BASE_URL}/group-by-operation/${encodeURIComponent(
dbname,
)}/${encodeURIComponent(schema)}/${encodeURIComponent(table)}`;
const response = await axios.post(url, payload);

const mappedData = chartDataGenerator.formatChartDataResponse(
Expand Down
8 changes: 4 additions & 4 deletions src/components/db-api/databaseInfo.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ import {
const router: Router = Router();

router.get('/database-info/databases', getAllDatabases);
router.get('/database-info/schemas', getAllSchemas);
router.get('/database-info/tables', getAllTables);
router.get('/database-info/:dbname/schemas', getAllSchemas);
router.get('/database-info/:dbname/tables', getAllTables);
router.get(
'/database-info/tables/:schemaName/:tableName/columns',
'/database-info/:dbname/tables/:schema/:table/columns',
validate(getTableColumnsValidation),
getTableColumns,
);
router.post(
'/database-info/generate-chart-data/:schema/:table',
'/database-info/generate-chart-data/:dbname/:schema/:table',
validate(generateChartDataValidation),
generateChartData,
);
Expand Down
6 changes: 4 additions & 2 deletions src/components/db-api/databaseInfo.validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { ValidSchema } from '../../middleware/joiValidate';

export const getTableColumnsValidation: ValidSchema = {
params: Joi.object({
schemaName: Joi.string().required(),
tableName: Joi.string().required(),
dbname: Joi.string().required(),
schema: Joi.string().required(),
table: Joi.string().required(),
}),
};

Expand All @@ -28,6 +29,7 @@ const filterColumnSchema = Joi.object({

export const generateChartDataValidation: ValidSchema = {
params: Joi.object({
dbname: Joi.string().required(),
schema: Joi.string().required(),
table: Joi.string().required(),
}),
Expand Down

0 comments on commit 931db09

Please sign in to comment.