Skip to content

Commit

Permalink
implement initial version of search
Browse files Browse the repository at this point in the history
  • Loading branch information
its-felix committed May 4, 2024
1 parent 324a50e commit 1e6dfdd
Show file tree
Hide file tree
Showing 8 changed files with 747 additions and 9 deletions.
3 changes: 3 additions & 0 deletions go/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ func main() {
c.Response().WriteHeader(http.StatusOK)
return search.ExportConnectionsImage(c.Response(), conns, graphviz.PNG)

case "reactflow":
return c.JSON(http.StatusOK, search.ExportConnectionsReactflow(conns))

default:
c.Response().Header().Set(echo.HeaderContentType, echo.MIMETextPlainCharsetUTF8)
c.Response().WriteHeader(http.StatusOK)
Expand Down
62 changes: 62 additions & 0 deletions go/api/search/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ import (
"time"
)

type Reactflow struct {
Nodes []Node `json:"nodes"`
Edges []Edge `json:"edges"`
}

type Node struct {
Id int `json:"id"`
X int `json:"x"`
Y int `json:"y"`
Label string `json:"label"`
}

type Edge struct {
Source int `json:"source"`
Target int `json:"target"`
Label string `json:"label"`
}

func ExportConnectionsText(w io.Writer, conns []Connection) error {
_, err := printConnections(w, conns, time.Time{}, 0)
return err
Expand Down Expand Up @@ -101,3 +119,47 @@ func buildGraph(parent *common.Flight, conns []Connection, graph *cgraph.Graph,

return nil
}

func ExportConnectionsReactflow(conns []Connection) Reactflow {
var rf Reactflow
buildReactFlow(conns, &rf, 0, 0, nil, make(map[*common.Flight]int))
return rf
}

func buildReactFlow(conns []Connection, rf *Reactflow, id, y int, parent *common.Flight, lookup map[*common.Flight]int) int {
const xIncr = 200
const yIncr = 100

x := 0

for _, conn := range conns {
var nodeId int
var ok bool

if nodeId, ok = lookup[conn.Flight]; !ok {
nodeId = id
lookup[conn.Flight] = nodeId
rf.Nodes = append(rf.Nodes, Node{
Id: nodeId,
X: x,
Y: y,
Label: fmt.Sprintf("%s\n%s-%s\n%s", conn.Flight.Number().String(), conn.Flight.DepartureAirport, conn.Flight.ArrivalAirport, conn.Flight.AircraftType),
})

id++
}

if parent != nil {
rf.Edges = append(rf.Edges, Edge{
Source: lookup[parent],
Target: nodeId,
Label: conn.Flight.DepartureTime.Sub(parent.ArrivalTime).String(),
})
}

id = buildReactFlow(conn.Outgoing, rf, id, y+yIncr, conn.Flight, lookup)
x += xIncr
}

return id
}
Loading

0 comments on commit 1e6dfdd

Please sign in to comment.