Skip to content

Commit

Permalink
加载文件树
Browse files Browse the repository at this point in the history
  • Loading branch information
lvyahui8 committed Oct 1, 2024
1 parent eb1f7bb commit b1fcf80
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 47 deletions.
33 changes: 24 additions & 9 deletions ellyn_agent/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"github.com/lvyahui8/ellyn/ellyn_common/asserts"
"github.com/lvyahui8/ellyn/ellyn_common/collections"
"net/http"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -100,14 +101,7 @@ func trafficDetail(writer http.ResponseWriter, request *http.Request) {
}

func sourceFile(writer http.ResponseWriter, request *http.Request) {
_, _ = writer.Write(readCode(0))
}

func readCode(fileId uint32) []byte {
bytes, err := targetSources.ReadFile(
filepath.ToSlash(filepath.Join(SourcesDir, files[fileId].RelativePath)) + SourcesFileExt)
asserts.IsNil(err)
return bytes
_, _ = writer.Write(readCode(uint32(queryVal[int](request, "id"))))
}

func nodeDetail(writer http.ResponseWriter, request *http.Request) {
Expand All @@ -127,6 +121,26 @@ func nodeDetail(writer http.ResponseWriter, request *http.Request) {
})
}

func sourceTree(writer http.ResponseWriter, request *http.Request) {
tree := collections.NewSourceTree()

for _, f := range files {
n := tree.Add(f.RelativePath, strconv.Itoa(int(f.FileId)))
n.IsLeaf = true
}
responseJson(writer, tree.Root())
}

/// API结束

// readCode 读取指定代码id文件
func readCode(fileId uint32) []byte {
bytes, err := targetSources.ReadFile(
filepath.ToSlash(filepath.Join(SourcesDir, files[fileId].RelativePath)) + SourcesFileExt)
asserts.IsNil(err)
return bytes
}

// queryVal 工具方法
func queryVal[T string | int | uint](request *http.Request, key string) T {
query := request.URL.Query()
Expand Down Expand Up @@ -167,8 +181,9 @@ func newServer() {
register("/meta/methods", metaMethods)
register("/traffic/list", trafficList)
register("/traffic/detail", trafficDetail)
register("/source/0", sourceFile)
register("/source/file", sourceFile)
register("/node/detail", nodeDetail)
register("/source/tree", sourceTree)

err := http.ListenAndServe(":19898", nil)
if err != nil {
Expand Down
52 changes: 52 additions & 0 deletions ellyn_common/collections/source_tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package collections

import (
"path/filepath"
"strings"
)

type SourceTree struct {
nodeMap map[string]*SourceNode
root []*SourceNode
}

func NewSourceTree() *SourceTree {
return &SourceTree{
nodeMap: make(map[string]*SourceNode),
}
}

func (st *SourceTree) Root() []*SourceNode {
return st.root
}

func (st *SourceTree) Add(path string, key string) *SourceNode {
path = strings.Trim(filepath.ToSlash(path), "/")
n := st.nodeMap[path]
if n != nil {
return n
}
items := strings.Split(path, "/")
n = &SourceNode{
Title: items[len(items)-1],
Key: key,
}

if len(items) >= 2 {
pPath := strings.Join(items[0:len(items)-1], "/")
parent := st.Add(pPath, pPath)
parent.Children = append(parent.Children, n)
} else {
st.root = append(st.root, n)
}

st.nodeMap[path] = n
return n
}

type SourceNode struct {
Title string `json:"title"`
Key string `json:"key"`
Children []*SourceNode `json:"children"`
IsLeaf bool `json:"isLeaf"`
}
24 changes: 24 additions & 0 deletions ellyn_common/collections/source_tree_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package collections

import (
"github.com/stretchr/testify/require"
"testing"
)

func TestSourceTree1(t *testing.T) {
tree := NewSourceTree()
tree.Add("./a/b/c.txt", "1")
tree.Add("./a/d.txt", "2")
tree.Add("./a/b/h.txt", "3")
tree.Add("./x/m.txt", "4")
require.Equal(t, 1, len(tree.Root()))
}

func TestSourceTree2(t *testing.T) {
tree := NewSourceTree()
tree.Add("a/b/c.txt", "1")
tree.Add("a/d.txt", "2")
tree.Add("a/b/h.txt", "3")
tree.Add("x/m.txt", "4")
require.Equal(t, 2, len(tree.Root()))
}
76 changes: 39 additions & 37 deletions viewer/src/GlobalCovered.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,54 @@ import {StreamLanguage} from "@codemirror/language";
import {go} from "@codemirror/legacy-modes/mode/go";
import {whiteLight} from "@uiw/codemirror-theme-white";
import {ProDescriptions} from "@ant-design/pro-components";
import {useEffect, useState} from "react";
import axios from "axios";

const { DirectoryTree } = Tree;

const treeData = [
{
title: 'parent 0',
key: '0-0',
children: [
{
title: 'leaf 0-0',
key: '0-0-0',
isLeaf: true,
},
{
title: 'leaf 0-1',
key: '0-0-1',
isLeaf: true,
},
],
},
{
title: 'parent 1',
key: '0-1',
children: [
{
title: 'leaf 1-0',
key: '0-1-0',
isLeaf: true,
},
{
title: 'leaf 1-1',
key: '0-1-1',
isLeaf: true,
},
],
},
];


const GlobalCovered = () => {
const [data, setData] = useState([])

const [code, setCode] = useState("")

const loadCode = function (id) {
axios.get('http://localhost:19898/source/file?id=' + id)
.then(resp => {
setCode(resp.data)
})
.catch(err => {
console.log(err)
})
}

const onSelect = (keys, info) => {
console.log('Trigger Select', keys, info);
loadCode(keys[0])
};
const onExpand = (keys, info) => {
console.log('Trigger Expand', keys, info);
};



useEffect(() => {
axios.get('http://localhost:19898/source/tree')
.then(resp => {
setData(resp.data)
for (let i = 0; i < resp.data.length; i++) {
let n = resp.data[i]
if (n.isLeaf) {
loadCode(n.key)
break
}
}
})
.catch(err => {
console.log(err)
})
},[])

return (
<>
<Row>
Expand All @@ -75,11 +77,11 @@ const GlobalCovered = () => {
defaultExpandAll
onSelect={onSelect}
onExpand={onExpand}
treeData={treeData}
treeData={data}
/>
</Col>
<Col span={20}>
<CodeMirror value={"test"} height="600px" extensions={[StreamLanguage.define(go)]} theme={whiteLight} editable={false}/>;
<CodeMirror value={code} height="600px" extensions={[StreamLanguage.define(go)]} theme={whiteLight} editable={false}/>;
</Col>
</Row>
</>
Expand Down
2 changes: 1 addition & 1 deletion viewer/src/NodeDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { classname } from '@uiw/codemirror-extensions-classname';
const themeConf = EditorView.baseTheme({
// '&dark .target-line': { backgroundColor: 'yellow' },
'&light .covered-line': { backgroundColor: 'lightgreen' },
'&light .uncovered-line': { backgroundColor: '#ff4d4f' },
'&light .uncovered-line': { backgroundColor: '#ff8b8d' },
'.cm-content' : {fontFamily : "consolas, Monaco, Lucida Console, monospace"},
});

Expand Down

0 comments on commit b1fcf80

Please sign in to comment.