add graph

This commit is contained in:
2025-09-23 03:04:20 +07:00
parent 9b7241031c
commit c144123cff
10 changed files with 1071 additions and 1486 deletions
+4
View File
@@ -63,3 +63,7 @@ func (s *Server) GiveApplications(ctx context.Context, req *proto.GiveApplicatio
func (s *Server) DownloadTeamsQrCodesFile(ctx context.Context, req *proto.DownloadTeamsQrCodesFileReq) (*proto.DownloadTeamsQrCodesFileRsp, error) {
return s.services.DownloadTeamsQrCodesFile(ctx, req)
}
func (s *Server) GetGraph(ctx context.Context, req *proto.GetGraphReq) (*proto.GetGraphRsp, error) {
return s.services.GetGraph(ctx, req)
}
+23
View File
@@ -200,6 +200,29 @@ func (s *Services) DownloadTeamsQrCodesFile(ctx context.Context, req *proto.Down
return &proto.DownloadTeamsQrCodesFileRsp{Result: b}, nil
}
func (s *Services) GetGraph(ctx context.Context, req *proto.GetGraphReq) (*proto.GetGraphRsp, error) {
graph := s.storyService.GetGraph(ctx)
nodes := make([]*proto.GetGraphRsp_Node, 0, len(graph.Nodes))
for _, node := range graph.Nodes {
nodes = append(nodes, &proto.GetGraphRsp_Node{
Id: node.ID,
Label: node.Label,
})
}
edges := make([]*proto.GetGraphRsp_Edge, 0, len(graph.Edges))
for _, edge := range graph.Edges {
edges = append(edges, &proto.GetGraphRsp_Edge{
From: edge.From,
To: edge.To,
Arrows: "to",
})
}
return &proto.GetGraphRsp{
Nodes: nodes,
Edges: edges,
}, nil
}
func (s *Services) getTeam(ctx context.Context) (*models.Team, error) {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
@@ -1,10 +1,12 @@
package story_service
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"regexp"
"strings"
)
@@ -37,6 +39,21 @@ type Place struct {
Applications []*Application `json:"applications"`
}
type Graph struct {
Nodes []*Node
Edges []*Edge
}
type Node struct {
ID int32
Label string
}
type Edge struct {
From int32
To int32
}
type Application struct {
Name string `json:"name"`
}
@@ -69,6 +86,8 @@ func (s *StoryService) GetPlace(code string) *Place {
code = clearCode(code)
for _, place := range s.story.Places {
if clearCode(place.Code) == code {
re := regexp.MustCompile(`\(\[[a-zA-Zа-яА-Я\d-]+\]\)`)
place.Text = re.ReplaceAllString(place.Text, "")
return place
}
}
@@ -79,6 +98,41 @@ func (s *StoryService) GetPlace(code string) *Place {
}
}
func (s *StoryService) GetGraph(ctx context.Context) *Graph {
m := make(map[string]int32, len(s.story.Places))
nodes := make([]*Node, 0, len(s.story.Places))
for i, place := range s.story.Places {
m[clearCode(place.Code)] = int32(i)
nodes = append(nodes, &Node{ID: int32(i), Label: place.Code})
}
edges := make([]*Edge, 0, len(s.story.Places)*3)
for _, place := range s.story.Places {
re := regexp.MustCompile(`\(\[[a-zA-Zа-яА-Я\d-]+\]\)`)
matches := re.FindAllString(place.Text, -1)
for _, match := range matches {
edges = append(edges, &Edge{
From: m[clearCode(place.Code)],
To: m[clearMatch(match)],
})
}
}
return &Graph{
Nodes: nodes,
Edges: edges,
}
}
func clearMatch(s string) string {
s = strings.TrimPrefix(s, "(")
s = strings.TrimPrefix(s, "[")
s = strings.TrimSuffix(s, ")")
s = strings.TrimSuffix(s, "]")
return clearCode(s)
}
func clearCode(code string) string {
code = strings.ToLower(code)
code = strings.TrimSpace(code)