Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
akageun committed Jul 25, 2024
1 parent 1050868 commit bc56c91
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
import com.datastax.oss.driver.api.querybuilder.QueryBuilder;
import com.datastax.oss.driver.api.querybuilder.select.Select;
import com.datastax.oss.driver.api.querybuilder.select.SelectFrom;
import kr.hakdang.cassdio.core.domain.cluster.BaseClusterCommander;
import kr.hakdang.cassdio.core.domain.cluster.ClusterUtils;
Expand All @@ -12,11 +13,14 @@
import kr.hakdang.cassdio.core.domain.cluster.keyspace.CassdioColumnDefinition;
import kr.hakdang.cassdio.core.domain.cluster.keyspace.table.CassandraSystemTable;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.List;

import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.bindMarker;
import static java.util.Collections.emptyList;

/**
* ClusterTableColumnCommander
Expand All @@ -29,13 +33,22 @@
public class ClusterTableColumnCommander extends BaseClusterCommander {

public CqlSessionSelectResults columnList(CqlSession session, String keyspace, String table) {
return columnList(session, keyspace, table, emptyList());
}

public CqlSessionSelectResults columnList(CqlSession session, String keyspace, String table, List<String> columnList) {
SimpleStatement statement;

statement = getColumnTable(session, keyspace)
Select select = getColumnTable(session, keyspace)
.all()
.whereColumn(CassandraSystemTablesColumn.TABLES_KEYSPACE_NAME.getColumnName()).isEqualTo(bindMarker())
.whereColumn(CassandraSystemTablesColumn.TABLES_TABLE_NAME.getColumnName()).isEqualTo(bindMarker())
.build(keyspace, table)
.whereColumn(CassandraSystemTablesColumn.TABLES_TABLE_NAME.getColumnName()).isEqualTo(bindMarker());

// if (CollectionUtils.isNotEmpty(columnList)) {
// select.whereColumn("column_name").in(columnList.stream().map(info -> bindMarker()).toList());
// }

statement = select.build(keyspace, table)
.setTimeout(Duration.ofSeconds(3));

ResultSet resultSet = session.execute(statement);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package kr.hakdang.cassdio.web.config;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.HandlerMapping;

import java.util.Map;

/**
* ApiInterceptor
*
* @author akageun
* @since 2024-07-25
*/
@Slf4j
@Component
public class ApiInterceptor implements HandlerInterceptor {

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//Map<String, String> pathVariables = (Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
return HandlerInterceptor.super.preHandle(request, response, handler);
}

@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package kr.hakdang.cassdio.web.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;
Expand All @@ -18,6 +20,14 @@
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

private final ApiInterceptor apiInterceptor;

public WebMvcConfig(
ApiInterceptor apiInterceptor
) {
this.apiInterceptor = apiInterceptor;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
Expand All @@ -37,4 +47,10 @@ protected Resource getResource(String resourcePath, Resource location) throws IO
}
});
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(apiInterceptor)
.addPathPatterns("/api/**").excludePathPatterns("/static/**");
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import {useEffect} from "react";
import {Modal} from "react-bootstrap";
import {Modal, OverlayTrigger, Tooltip} from "react-bootstrap";

const TableRowDetailModal = (props) => {

const show = props.show;
const handleClose = props.handleClose;
const TableRowDetailModal = ({show, handleClose, rowDetailView, convertedRowHeader}) => {

useEffect(() => {
//show component
Expand All @@ -22,7 +19,44 @@ const TableRowDetailModal = (props) => {
<Modal.Title>Row Detail</Modal.Title>
</Modal.Header>
<Modal.Body>
Row Detail
<table className="table table-sm table-hover">
<tbody>
{
convertedRowHeader && convertedRowHeader.map((info, infoIndex) => {
return (
<tr>
<th className={"text-center"} key={`resultHeader${infoIndex}`}
scope="col">
<OverlayTrigger placement="bottom" overlay={
<Tooltip id="tooltip">
{info.column_name} ({info.type})
</Tooltip>
}>
<span style={{cursor: "pointer"}}>
{
info.kind === 'partition_key' &&
<i className="bi bi-p-square me-1"></i>
}
{
info.kind === 'clustering' &&
<i className="bi bi-c-square me-1"></i>
}
{info.column_name}
</span>
</OverlayTrigger>
</th>
<td className={"text-center text-break"} key={`resultItem${infoIndex}`}>
{rowDetailView[info.column_name]}
</td>
<td>
<a className={"btn btn-sm btn-outline-secondary"}>COPY</a>
</td>
</tr>
)
})
}
</tbody>
</table>
</Modal.Body>
<Modal.Footer>
<button className={"btn btn-sm btn-outline-secondary"} onClick={handleClose}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import DataRowItem from "../../data-row-item";
import ClusterBreadcrumb from "pages/cluster/components/cluster-breadcrumb";
import useTable from "pages/cluster/hooks/useTable";
import TableRowDetailModal from "./detail-modal/table-row-detail-modal";
import {CassdioUtils} from "../../../../../utils/cassdioUtils";

const TableHome = (props) => {

Expand All @@ -30,6 +29,7 @@ const TableHome = (props) => {
const [showRowDetail, setShowRowDetail] = useState(false);
const [tableName, setTableName] = useState('');
const [moreQueryLoading, setMoreQueryLoading] = useState(false)
const [rowDetailView, setRowDetailView] = useState(null);

useEffect(() => {
//show component
Expand Down Expand Up @@ -111,28 +111,33 @@ const TableHome = (props) => {
</div>
</div>

{
CassdioUtils.columnListSorting(queryResult.columnList.rows)
}

{/*TODO : 위치 변경*/}
<div className="table-responsive small">
<table className="table table-sm table-fixed table-lock-height table-hover">
<thead>
<tr className={"table-dark"}>
<th className={"text-center"} scope="col">#</th>
{
queryResult.rowHeader.map((info, infoIndex) => {
queryResult.convertedRowHeader.map((info, infoIndex) => {
return (
<th className={"text-center text-truncate"} key={`resultHeader${infoIndex}`}
scope="col">
<OverlayTrigger placement="bottom" overlay={
<Tooltip id="tooltip">
{info.columnName} <br/>
(type : {info.type})
{info.column_name} ({info.type})
</Tooltip>
}>
<span style={{cursor: "pointer"}}>{info.columnName}</span>
<span style={{cursor: "pointer"}}>
{
info.kind === 'partition_key' &&
<i className="bi bi-p-square me-1"></i>
}
{
info.kind === 'clustering' &&
<i className="bi bi-c-square me-1"></i>
}
{info.column_name}
</span>
</OverlayTrigger>
</th>
)
Expand All @@ -144,7 +149,7 @@ const TableHome = (props) => {
{
queryResult.rows.length <= 0 ? <>
<tr>
<td className={"text-center"} colSpan={queryResult.rowHeader.length + 1}>
<td className={"text-center"} colSpan={queryResult.convertedRowHeader.length + 1}>
No Data
</td>
</tr>
Expand All @@ -155,7 +160,10 @@ const TableHome = (props) => {
<td className={"text-center"}>
<div className="btn-group btn-group-sm">
<button type="button" className={"btn btn-sm btn-outline-primary"}
onClick={() => setShowRowDetail(true)}>
onClick={() => {
setRowDetailView(row);
setShowRowDetail(true);
}}>
<i className="bi bi-book"></i>
</button>
{/*UI 구성 등 오래걸릴 듯*/}
Expand All @@ -170,11 +178,11 @@ const TableHome = (props) => {
</div>
</td>
{
queryResult.rowHeader.map((info, infoIndex) => {
queryResult.convertedRowHeader.map((info, infoIndex) => {
return (
<td className={"text-center text-break text-truncate"}
key={`resultItem${infoIndex}`}>
<DataRowItem data={row[info.columnName]}/>
<DataRowItem data={row[info.column_name]}/>
</td>
)
})
Expand Down Expand Up @@ -241,8 +249,10 @@ const TableHome = (props) => {
/>
}
{
showRowDetail && <TableRowDetailModal
showRowDetail && rowDetailView && <TableRowDetailModal
show={showRowDetail}
rowDetailView={rowDetailView}
convertedRowHeader={queryResult.convertedRowHeader}
handleClose={() => setShowRowDetail(false)}
/>
}
Expand Down
14 changes: 4 additions & 10 deletions cassdio-web/src/main/webapp/src/pages/cluster/hooks/useTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {toast} from "react-toastify";
import {useNavigate, useParams} from "react-router-dom";
import {useState} from "react";
import useCassdio from "commons/hooks/useCassdio";
import {CassdioUtils} from "../../../utils/cassdioUtils";
import {CassdioUtils} from "utils/cassdioUtils";

export default function useTable() {
const navigate = useNavigate();
Expand Down Expand Up @@ -54,6 +54,7 @@ export default function useTable() {
rows: [],
rowHeader: [],
columnList: [],
convertedRowHeader: [],
};


Expand Down Expand Up @@ -81,18 +82,11 @@ export default function useTable() {
}).then((response) => {
setNextCursor(response.data.result.nextCursor)

const tempRowHeader = response.data.result.rowHeader;

CassdioUtils.rowHeaderSorting(response.data.result.columnList, tempRowHeader)

const sortedColumnList = CassdioUtils.columnListSorting(
response.data.result.columnList
);

setQueryResult({
rows: [...queryResult.rows, ...response.data.result.rows],
rowHeader: response.data.result.rowHeader,
columnList: sortedColumnList,
columnList: response.data.result.columnList,
convertedRowHeader: CassdioUtils.convertRowHeader(response.data.result.columnList, response.data.result.rowHeader),
})
}).catch((error) => {
errorCatch(error);
Expand Down
44 changes: 41 additions & 3 deletions cassdio-web/src/main/webapp/src/utils/cassdioUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,50 @@ export const CassdioUtils = {
return data;
},

rowHeaderSorting: (columnList, rowHeader) => {
//const sortedColumnList = this.columnListSorting(columnList);
convertRowHeader: (columnList, rowHeader) => {
const convertedMap = CassdioUtils.columnListSortValueMap(columnList);

const result = []
for (const header of rowHeader) {
console.log("header ", header)
const key = CassdioUtils.makeRowKey(header.keyspace, header.table, header.columnName)

const convertedColumnInfo = convertedMap[key];
result.push(convertedColumnInfo)

}

return result;
},

columnListSortValueMap: (columnList) => {
if (!columnList || columnList.rows.length <= 0) {
return [];
}

const rows = columnList.rows;

const kindSort = {
'partition_key': 0,
'clustering': 1,
'regular': 2,
'unknown': 3,
}

const temp = {}

for (const row of rows) {

temp[CassdioUtils.makeRowKey(row.keyspace_name, row.table_name, row.column_name)] = {
...row,
sortValue: `${kindSort[row.kind]}-${row.position}`
}
}

return temp;
},

makeRowKey: (keyspace, table, column) => {
return `${keyspace}.${table}.${column}`
},

columnListSorting: (columnList) => {
Expand Down

0 comments on commit bc56c91

Please sign in to comment.