Add GetDatasetGraph() API.
This commit is contained in:
parent
ee2c8e99fc
commit
10a825b5ef
|
@ -25,6 +25,13 @@ func (c *ApiController) GetDataset() {
|
|||
c.ServeJSON()
|
||||
}
|
||||
|
||||
func (c *ApiController) GetDatasetGraph() {
|
||||
id := c.Input().Get("id")
|
||||
|
||||
c.Data["json"] = object.GetDatasetGraph(id)
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
func (c *ApiController) UpdateDataset() {
|
||||
id := c.Input().Get("id")
|
||||
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
package object
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func GetDatasetGraph(id string) *Graph {
|
||||
dataset := GetDataset(id)
|
||||
if dataset == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
g := generateGraph(dataset.Vectors)
|
||||
return g
|
||||
}
|
||||
|
||||
func getDistance(v1 *Vector, v2 *Vector) float64 {
|
||||
res := 0.0
|
||||
for i := range v1.Data {
|
||||
res += (v1.Data[i] - v2.Data[i]) * (v1.Data[i] - v2.Data[i])
|
||||
}
|
||||
return math.Sqrt(res)
|
||||
}
|
||||
|
||||
func refineVectors(vectors []*Vector) []*Vector {
|
||||
res := []*Vector{}
|
||||
for _, vector := range vectors {
|
||||
if len(vector.Data) > 0 {
|
||||
res = append(res, vector)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func generateGraph(vectors []*Vector) *Graph {
|
||||
vectors = refineVectors(vectors)
|
||||
|
||||
g := newGraph()
|
||||
|
||||
for _, vector := range vectors {
|
||||
g.addNode(vector.Name, vector.Name, 10, "red", "")
|
||||
}
|
||||
|
||||
for i := 0; i < len(vectors); i++ {
|
||||
for j := i + 1; j < len(vectors); j++ {
|
||||
v1 := vectors[i]
|
||||
v2 := vectors[j]
|
||||
distance := getDistance(v1, v2)
|
||||
g.addLink(fmt.Sprintf("%s_%s", v1.Name, v2.Name), v1.Name, v2.Name, int(distance), "green", "")
|
||||
}
|
||||
}
|
||||
|
||||
return g
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package object
|
||||
|
||||
type Node struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Value int `json:"val"`
|
||||
Color string `json:"color"`
|
||||
Tag string `json:"tag"`
|
||||
}
|
||||
|
||||
func newNode(id string, name string, value int, color string, tag string) *Node {
|
||||
n := Node{}
|
||||
n.Id = id
|
||||
n.Name = name
|
||||
n.Value = value
|
||||
n.Color = color
|
||||
n.Tag = tag
|
||||
return &n
|
||||
}
|
||||
|
||||
type Link struct {
|
||||
Name string `json:"name"`
|
||||
Source string `json:"source"`
|
||||
Target string `json:"target"`
|
||||
Value int `json:"value"`
|
||||
Color string `json:"color"`
|
||||
Tag string `json:"tag"`
|
||||
}
|
||||
|
||||
func newLink(name string, source string, target string, value int, color string, tag string) *Link {
|
||||
l := Link{}
|
||||
l.Name = name
|
||||
l.Source = source
|
||||
l.Target = target
|
||||
l.Value = value
|
||||
l.Color = color
|
||||
l.Tag = tag
|
||||
return &l
|
||||
}
|
||||
|
||||
type Graph struct {
|
||||
Nodes []*Node `json:"nodes"`
|
||||
Links []*Link `json:"links"`
|
||||
}
|
||||
|
||||
func newGraph() *Graph {
|
||||
g := Graph{}
|
||||
return &g
|
||||
}
|
||||
|
||||
func (g *Graph) addNode(id string, name string, value int, color string, tag string) {
|
||||
n := newNode(id, name, value, color, tag)
|
||||
g.Nodes = append(g.Nodes, n)
|
||||
}
|
||||
|
||||
func (g *Graph) addLink(name string, source string, target string, value int, color string, tag string) {
|
||||
l := newLink(name, source, target, value, color, tag)
|
||||
g.Links = append(g.Links, l)
|
||||
}
|
|
@ -26,6 +26,7 @@ func initAPI() {
|
|||
beego.Router("/api/get-global-datasets", &controllers.ApiController{}, "GET:GetGlobalDatasets")
|
||||
beego.Router("/api/get-datasets", &controllers.ApiController{}, "GET:GetDatasets")
|
||||
beego.Router("/api/get-dataset", &controllers.ApiController{}, "GET:GetDataset")
|
||||
beego.Router("/api/get-dataset-graph", &controllers.ApiController{}, "GET:GetDatasetGraph")
|
||||
beego.Router("/api/update-dataset", &controllers.ApiController{}, "POST:UpdateDataset")
|
||||
beego.Router("/api/add-dataset", &controllers.ApiController{}, "POST:AddDataset")
|
||||
beego.Router("/api/delete-dataset", &controllers.ApiController{}, "POST:DeleteDataset")
|
||||
|
|
|
@ -1,18 +1,32 @@
|
|||
import React from "react";
|
||||
import * as DatasetBackend from "./backend/DatasetBackend";
|
||||
|
||||
class Dataset extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.state = {
|
||||
classes: props,
|
||||
datasetName: props.datasetName !== undefined ? props.datasetName : props.match.params.datasetName,
|
||||
graph: null,
|
||||
}
|
||||
}
|
||||
|
||||
componentWillMount() {
|
||||
this.getDatasetGraph();
|
||||
}
|
||||
|
||||
getDatasetGraph() {
|
||||
DatasetBackend.getDatasetGraph("admin", this.state.datasetName)
|
||||
.then((graph) => {
|
||||
this.setState({
|
||||
graph: graph,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
render()
|
||||
{
|
||||
const dataset = this.props.dataset;
|
||||
|
||||
return 111;
|
||||
return JSON.stringify(this.state.graph);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -101,7 +101,7 @@ class DatasetEditPage extends React.Component {
|
|||
{i18next.t("general:Preview")} :
|
||||
</Col>
|
||||
<Col span={22} >
|
||||
<Dataset dataset={this.state.dataset} />
|
||||
<Dataset dataset={this.state.dataset} datasetName={this.state.dataset.name} />
|
||||
</Col>
|
||||
</Row>
|
||||
</Card>
|
||||
|
|
|
@ -34,7 +34,11 @@ class HomePage extends React.Component {
|
|||
<Col span={!Setting.isMobile() ? 3 : 0}>
|
||||
</Col>
|
||||
<Col span={!Setting.isMobile() ? 18 : 24}>
|
||||
<Dataset dataset={this.state.dataset} />
|
||||
{
|
||||
(this.state.dataset === undefined || this.state.dataset === null) ? null : (
|
||||
<Dataset dataset={this.state.dataset} datasetName={this.state.dataset.name} />
|
||||
)
|
||||
}
|
||||
</Col>
|
||||
<Col span={!Setting.isMobile() ? 3 : 0}>
|
||||
</Col>
|
||||
|
|
|
@ -21,6 +21,13 @@ export function getDataset(owner, name) {
|
|||
}).then(res => res.json());
|
||||
}
|
||||
|
||||
export function getDatasetGraph(owner, name) {
|
||||
return fetch(`${Setting.ServerUrl}/api/get-dataset-graph?id=${owner}/${encodeURIComponent(name)}`, {
|
||||
method: "GET",
|
||||
credentials: "include"
|
||||
}).then(res => res.json());
|
||||
}
|
||||
|
||||
export function updateDataset(owner, name, dataset) {
|
||||
let newDataset = Setting.deepCopy(dataset);
|
||||
return fetch(`${Setting.ServerUrl}/api/update-dataset?id=${owner}/${encodeURIComponent(name)}`, {
|
||||
|
|
Loading…
Reference in New Issue