Skip to content

Commit

Permalink
添加删除全部子节点及修改问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostg00 committed Sep 19, 2019
1 parent 99760e7 commit 8eb04d2
Showing 4 changed files with 17,960 additions and 102 deletions.
94 changes: 53 additions & 41 deletions src/renderer/pages/home/index.tsx
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ import {
Tooltip,
Tree
} from "antd";
import { AnyAction, Dispatch } from "redux";
import { Dispatch } from "redux";
import { StateType } from "@/pages/home/model";
import { AntTreeNode, TreeProps } from "antd/es/tree";
import { TreeNodeNormal } from "antd/es/tree/Tree";
@@ -44,7 +44,7 @@ const IconFont = Icon.createFromIconfontCN({

interface HomeProps {
home: StateType;
dispatch: Dispatch<AnyAction>;
dispatch: Dispatch;
}

interface CreateNodeFormProps extends FormComponentProps {
@@ -88,7 +88,7 @@ const CreateNodeForm = Form.create<CreateNodeFormProps>({ name: "from" })(
let logArr: string[] = [];

function Home(props: HomeProps) {
const { dispatch, home } = props;
const { dispatch } = props;

const [url, setUrl] = useState(
localStorage.getItem("connectionString") || "127.0.0.1:2181"
@@ -103,6 +103,7 @@ function Home(props: HomeProps) {
const [nodePath, setNodePath] = useState("");
const [nodeName, setNodeName] = useState("");
const [nodeData, setNodeData] = useState("");
const [nodeStat, setNodeStat] = useState([]);
const [nodeACL, setNodeACL] = useState<ZkACL>(new ZkACL("", "", ""));
const [createNodeVisible, setCreateNodeVisible] = useState(false);
const [formRef, setFormRef] = useState<any>();
@@ -127,30 +128,33 @@ function Home(props: HomeProps) {
type: "home/connect",
payload: { connectionString: url },
callback() {
dispatch({
type: "home/getChildren",
payload: { path: "/" },
callback(data: string[]) {
setTreeData(
data.map(item => {
return { title: item, key: `/${item}` };
})
);
},
event(event: Event) {
// console.log("refreshTreeNode", event);
logEvent.emit("log", event);
// if (event.getType() == 4) {
// refreshTreeNode(path, node, true, resolve);
// }
}
});
refreshRootTreeNode();
setNodePath("/");
localStorage.setItem("connectionString", url);
message.success("连接成功");
}
});
};

const refreshRootTreeNode = () => {
let event: any = (event: Event) => {
logEvent.emit("log", event);
refreshRootTreeNode();
};
dispatch({
type: "home/getChildren",
payload: { path: "/" },
callback(data: string[]) {
setTreeData(
data.map(item => {
return { title: item, key: `/${item}` };
})
);
},
event
});
};

const close = () => {
dispatch({
type: "home/close",
@@ -187,7 +191,6 @@ function Home(props: HomeProps) {
let event: any = null;
if (watcher) {
event = (event: Event) => {
// console.log("refreshTreeNode", event);
logEvent.emit("log", event);
if (event.getType() == 4) {
refreshTreeNode(path, node, true, resolve);
@@ -302,23 +305,32 @@ function Home(props: HomeProps) {

const onSelectTree: TreeProps["onSelect"] = (selectedKeys, e) => {
setSelectedKeys(selectedKeys);
setNodeName(e.node.props.title as string);
const path = e.node.props.eventKey as string;
setNodePath(path);
dispatch({
type: "home/getData",
payload: { path },
callback(nodeData: string) {
setNodeData(nodeData);
}
});
dispatch({
type: "home/getACL",
payload: { path },
callback(nodeACL: ZkACL) {
setNodeACL(nodeACL);
}
});
if (selectedKeys.length === 0) {
setNodeName("");
setNodePath("/");
setNodeData("");
setNodeStat([]);
setNodeACL(new ZkACL("", "", ""));
} else {
setNodeName(e.node.props.title as string);
const path = e.node.props.eventKey as string;
setNodePath(path);
dispatch({
type: "home/getData",
payload: { path },
callback(data: [string, []]) {
setNodeData(data[0]);
setNodeStat(data[1]);
}
});
dispatch({
type: "home/getACL",
payload: { path },
callback(nodeACL: ZkACL) {
setNodeACL(nodeACL);
}
});
}
};

const onSetData = () => {
@@ -335,7 +347,7 @@ function Home(props: HomeProps) {
const { form } = formRef.props;
form.validateFields((err: any, values: any) => {
if (err) return;
let path = `${nodePath}/${values.nodeName}`;
let path = `${nodePath}${nodePath === "/" ? "" : "/"}${values.nodeName}`;
dispatch({
type: "home/create",
payload: {
@@ -564,7 +576,7 @@ function Home(props: HomeProps) {
>
<Table
columns={columns}
dataSource={home.nodeStat}
dataSource={nodeStat}
rowKey={"name"}
size={"small"}
pagination={false}
15 changes: 4 additions & 11 deletions src/renderer/pages/home/model.ts
Original file line number Diff line number Diff line change
@@ -5,9 +5,7 @@ import logEvent from "../../utils/LogEvent";

let zkClient = new ZkClient();

export interface StateType {
nodeStat: [];
}
export interface StateType {}

const event = (event: Event) => {
// console.log("getData", event);
@@ -16,7 +14,7 @@ const event = (event: Event) => {

const model: ModelType<StateType> = {
namespace: "home",
state: { nodeStat: [] },
state: {},

effects: {
*connect({ payload, callback }, { call, put }) {
@@ -60,7 +58,7 @@ const model: ModelType<StateType> = {
type: "getDataReducer",
payload: data
});
callback && callback(data[0]);
callback && callback(data);
},
*setData({ payload, callback }, { call, put }) {
yield call([zkClient, zkClient.setData], payload.path, payload.data);
@@ -75,12 +73,7 @@ const model: ModelType<StateType> = {
callback && callback(data);
}
},
// @ts-ignore
reducers: {
getDataReducer(state, { payload }) {
return { ...state, nodeStat: payload[1] };
}
}
reducers: {}
};

export default model;
100 changes: 50 additions & 50 deletions src/renderer/utils/ZkClient.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Client, Event, Stat } from "node-zookeeper-client";
import { MomentInput } from "moment";
import logEvent from "./LogEvent";
import { Client, Event, Stat } from 'node-zookeeper-client';
import { MomentInput } from 'moment';
import logEvent from './LogEvent';

const moment = require("moment");
const { Buffer } = window.require("buffer");
const nodeZookeeperClient = window.require("node-zookeeper-client");
const moment = require('moment');
const { Buffer } = window.require('buffer');
const nodeZookeeperClient = window.require('node-zookeeper-client');

class ZkClient {
client?: any;
@@ -13,11 +13,11 @@ class ZkClient {
if (this.client) return;
const promise = new Promise<Client>(resolve => {
let client = nodeZookeeperClient.createClient(connectionString) as Client;
client.once("connected", () => {
client.once('connected', () => {
resolve(client);
});
client.on("state", state => {
logEvent.emit("log", state);
client.on('state', state => {
logEvent.emit('log', state);
});
client.connect();
});
@@ -49,7 +49,7 @@ class ZkClient {

async remove(path: string) {
return new Promise(resolve => {
this.client.remove(path, (e: any) => {
this.client.removeRecursive(path, (e: any) => {
resolve();
});
});
@@ -97,94 +97,94 @@ class ZkClient {
}

const hexString = (longBuffer: any) => {
return (longBuffer as Buffer).toString("hex");
return (longBuffer as Buffer).toString('hex');
};

const int64 = (longBuffer: any) => {
const hexString = (longBuffer as Buffer).toString("hex");
const hexString = (longBuffer as Buffer).toString('hex');
return parseInt(hexString, 16);
};

const format = (inp: MomentInput) => {
return moment(inp).format("YYYY-MM-DD HH:mm:ss");
return moment(inp).format('YYYY-MM-DD HH:mm:ss');
};

const extracted = (stat: Stat) => {
return [
{
name: "cZxid",
description: "这个值是当前会话事物创建产生ID",
name: 'cZxid',
description: '这个值是当前会话事物创建产生ID',
value: int64(stat.czxid),
realValue: `0x${hexString(stat.czxid).replace(/0+/, "")}`
realValue: `0x${hexString(stat.czxid).replace(/0+/, '')}`,
},
{
name: "ctime",
description: "创建时间",
name: 'ctime',
description: '创建时间',
value: format(int64(stat.ctime)),
realValue: `${new Date(int64(stat.ctime)).toString()}(${int64(
stat.ctime
)})`
stat.ctime,
)})`,
},
{
name: "mZxid",
description: "最近更新节点的事物ID",
name: 'mZxid',
description: '最近更新节点的事物ID',
value: int64(stat.mzxid),
realValue: `0x${hexString(stat.mzxid).replace(/0+/, "")}`
realValue: `0x${hexString(stat.mzxid).replace(/0+/, '')}`,
},
{
name: "mtime",
description: "最近修改时间",
name: 'mtime',
description: '最近修改时间',
value: format(int64(stat.mtime)),
realValue: `${new Date(int64(stat.mtime)).toString()}(${int64(
stat.mtime
)})`
stat.mtime,
)})`,
},
{
name: "pZxid",
description: "该节点的子节点最后修改的事物ID",
name: 'pZxid',
description: '该节点的子节点最后修改的事物ID',
value: int64(stat.pzxid),
realValue: `0x${hexString(stat.pzxid).replace(/0+/, "")}`
realValue: `0x${hexString(stat.pzxid).replace(/0+/, '')}`,
},
{
name: "cversion",
description: "子节点的版本号",
name: 'cversion',
description: '子节点的版本号',
value: stat.cversion,
realValue: stat.cversion
realValue: stat.cversion,
},
{
name: "dataVersion",
description: "数据的版本号",
name: 'dataVersion',
description: '数据的版本号',
value: stat.version,
realValue: stat.version
realValue: stat.version,
},
{
name: "aclVersion",
description: "acl的版本号",
name: 'aclVersion',
description: 'acl的版本号',
value: stat.aversion,
realValue: stat.aversion
realValue: stat.aversion,
},
{
name: "ephemeralOwner",
description: "创建此临时节点的会话ID",
name: 'ephemeralOwner',
description: '创建此临时节点的会话ID',
value: int64(stat.ephemeralOwner),
realValue: `0x${
int64(stat.ephemeralOwner) != 0
? hexString(stat.ephemeralOwner).replace(/0+/, "")
? hexString(stat.ephemeralOwner).replace(/0+/, '')
: 0
}`
}`,
},
{
name: "dataLength",
description: "数据长度",
name: 'dataLength',
description: '数据长度',
value: stat.dataLength,
realValue: stat.dataLength
realValue: stat.dataLength,
},
{
name: "numChildren",
description: "子节点的个数",
name: 'numChildren',
description: '子节点的个数',
value: stat.numChildren,
realValue: stat.numChildren
}
realValue: stat.numChildren,
},
];
};

Loading

0 comments on commit 8eb04d2

Please sign in to comment.