refactor: Update parsing of flight schedule data for improved accuracy and functionality

This commit is contained in:
windyboy
2024-07-31 16:04:52 +08:00
parent 34ea30b594
commit 2a1dc1eebe
4 changed files with 256 additions and 41 deletions
+1
View File
@@ -122,4 +122,5 @@ tasks:
- echo " task lint - Lint the code" - echo " task lint - Lint the code"
- echo " task schema - Download the GraphQL schema from Hasura server" - echo " task schema - Download the GraphQL schema from Hasura server"
- echo " task gen - Generate code using genqlient" - echo " task gen - Generate code using genqlient"
- echo " task upgrade - Upgrade go dependencies"
- echo " task help - Show this help message" - echo " task help - Show this help message"
+2 -2
View File
@@ -110,7 +110,7 @@ func (bp *BodyParser) Parse() (string, interface{}, error) {
// data := extractData(match, p.Expression) // data := extractData(match, p.Expression)
// return bp.createBodyData(data) // return bp.createBodyData(data)
// } // }
if data := parse(bp.body, p.Expression); data != nil { if data := extract(bp.body, p.Expression); data != nil {
return bp.createBodyData(data) return bp.createBodyData(data)
} }
} }
@@ -129,7 +129,7 @@ func findCategory(body string) string {
return "" return ""
} }
func parse(data string, exp *regexp.Regexp) map[string]string { func extract(data string, exp *regexp.Regexp) map[string]string {
match := exp.FindStringSubmatch(data) match := exp.FindStringSubmatch(data)
if len(match) > 0 { if len(match) > 0 {
return extractData(match, exp) return extractData(match, exp)
+130 -7
View File
@@ -7,11 +7,19 @@ import (
"strings" "strings"
) )
type LineParser struct {
Airlines []string
MinLen int
WaypointStart int
Fields map[int]string
}
const ( const (
AirportCode = "airport" AirportCode = "airport"
Date = "date" Date = "date"
Task = "task" Task = "task"
IndexPattern = `^(?P<idx>\(?L?[0-9]+\)?\.?)$` // Index = "idx"
IndexPattern = `^(?P<idx>\(?L?[0-9]+\)?:?\.?)$`
DatePattern = `^(?P<date>\d{2}\w{3})$` DatePattern = `^(?P<date>\d{2}\w{3})$`
TaskPattern = `^(?P<task>[A-Z]\/[A-Z])$` TaskPattern = `^(?P<task>[A-Z]\/[A-Z])$`
WaypointPattern = `^(SI:)?(?P<arr_time>\d{4}(\(\d{2}[A-Z]{3}\))?)?\/?(?P<airport>[A-Z]{3})\/?(?P<dep_time>\d{4}(\(\d{2}[A-Z]{3}\))?)?$` WaypointPattern = `^(SI:)?(?P<arr_time>\d{4}(\(\d{2}[A-Z]{3}\))?)?\/?(?P<airport>[A-Z]{3})\/?(?P<dep_time>\d{4}(\(\d{2}[A-Z]{3}\))?)?$`
@@ -34,6 +42,57 @@ var (
FlightNumber: FlightNumberExpression, FlightNumber: FlightNumberExpression,
Register: RegisterExpression, Register: RegisterExpression,
} }
parserDef = &[]LineParser{
{
/**
* 上航(FM)解析
* 解析参考
* W/Z FM9134 B2688 1/1ILS (00) TSN0100 SHA
* W/Z FM9133 B2688 1/1ILS (00) SHA0340 TSN
*/
Airlines: []string{"FM"},
MinLen: 6,
WaypointStart: 5,
Fields: map[int]string{
0: Task,
1: FlightNumber,
2: Register,
},
},
{
/**
* 解析厦航(MF)计划
* 01) MF8193 B5595 ILS(8) HGH1100 1305TSN
* 02) MF8194 B5595 ILS(8) TSN1355 1550HGH
*/
Airlines: []string{"MF"},
MinLen: 5,
WaypointStart: 4,
Fields: map[int]string{
0: Index,
1: FlightNumber,
2: Register,
},
},
{
/**
* 解析奥凯(8X)计划
* 计划参考:
*L1: 29OCT BK2735 B2863 ILS IS (3/6) TSN2350(28OCT) HAK
*L2: 29OCT BK2735 B2863 ILS IS (3/6) HAK0435 NKG
*/
Airlines: []string{"8X"},
MinLen: 9,
WaypointStart: 7,
Fields: map[int]string{
0: Index,
1: Date,
2: FlightNumber,
3: Register,
},
},
}
) )
func ExtractWaypoint(message string) *domain.WayPoint { func ExtractWaypoint(message string) *domain.WayPoint {
@@ -57,6 +116,70 @@ func ExtractWaypoint(message string) *domain.WayPoint {
return result return result
} }
func FindDef(code string) *LineParser {
// fmt.Printf("Finding definition for %s\n", code)
// fmt.Println("ParserDef: ", parserDef)
for _, def := range *parserDef {
for _, airline := range def.Airlines {
if airline == code {
return &def
}
}
}
return nil
}
func standardizeSpaces(s string) string {
return strings.Join(strings.Fields(s), " ")
}
func ParseWithDef(line string, parserDef *LineParser) *domain.ScheduleLine {
log := utils.GetSugaredLogger()
cleanLine := standardizeSpaces(strings.TrimSpace(line))
words := strings.Split(cleanLine, " ")
var flightSchedule = &domain.ScheduleLine{
Reference: line,
}
// var result map[string]string
if parserDef == nil {
log.Warnf("No definition found: %s", line)
flightSchedule.Comments = "No definition found [" + line + "] "
return flightSchedule
}
if len(words) < parserDef.MinLen {
log.Warnf("Line too short: %s", line)
flightSchedule.Comments = "Line too short [" + line + "] "
return flightSchedule
}
for i, field := range parserDef.Fields {
// log.Debugf("Parsing field %v -> %s", i, field)
data := extract(words[i], parserMap[field])
if data != nil {
switch field {
case Index:
flightSchedule.Index = data[Index]
case Date:
flightSchedule.Date = data[Date]
case Task:
flightSchedule.Task = data[Task]
case FlightNumber:
flightSchedule.FlightNumber = append(flightSchedule.FlightNumber, data[FlightNumber])
case Register:
flightSchedule.AircraftReg = data[Register]
}
}
}
if len(words) > parserDef.WaypointStart {
flightSchedule.Waypoints = parseWaypoints(words[parserDef.WaypointStart:])
} else {
log.Warn("No waypoints found")
flightSchedule.Comments = "No waypoints found"
}
return flightSchedule
}
func ParseLine(line string) *domain.ScheduleLine { func ParseLine(line string) *domain.ScheduleLine {
log := utils.GetSugaredLogger() log := utils.GetSugaredLogger()
cleanLine := strings.TrimSpace(line) cleanLine := strings.TrimSpace(line)
@@ -66,7 +189,7 @@ func ParseLine(line string) *domain.ScheduleLine {
} }
var data map[string]string var data map[string]string
if indexData := parse(words[0], IndexExpression); indexData != nil { if indexData := extract(words[0], IndexExpression); indexData != nil {
flightSchedule.Index = indexData[Index] flightSchedule.Index = indexData[Index]
words = words[1:] words = words[1:]
} }
@@ -92,7 +215,7 @@ func ParseLine(line string) *domain.ScheduleLine {
continue continue
} }
// Parse the word // Parse the word
if data = parse(word, parserMap[name]); data != nil { if data = extract(word, parserMap[name]); data != nil {
// Update the flight schedule // Update the flight schedule
switch name { switch name {
case Task: case Task:
@@ -138,7 +261,7 @@ func parseWaypoints(points []string) []domain.WayPoint {
//find first waypoint //find first waypoint
var realWaypoints []string var realWaypoints []string
for i, point := range points { for i, point := range points {
if parse(point, WaypointExpression) != nil { if extract(point, WaypointExpression) != nil {
realWaypoints = points[i:] realWaypoints = points[i:]
break break
} }
@@ -149,14 +272,14 @@ func parseWaypoints(points []string) []domain.WayPoint {
} }
var waypoints []domain.WayPoint var waypoints []domain.WayPoint
for _, point := range realWaypoints { for _, point := range realWaypoints {
log.Debugf("Parsing waypoint: %s", point) // log.Debugf("Parsing waypoint: %s", point)
if waypoint := ExtractWaypoint(point); waypoint != nil { if waypoint := ExtractWaypoint(point); waypoint != nil {
log.Debugf("Waypoint: %v", waypoint) // log.Debugf("Waypoint: %v", waypoint)
waypoints = append(waypoints, *waypoint) waypoints = append(waypoints, *waypoint)
} else { } else {
log.Warnf("Failed to parse waypoint: %s", point) log.Warnf("Failed to parse waypoint: %s", point)
} }
} }
log.Debugf("Found %d waypoints", len(waypoints)) // log.Debugf("Found %d waypoints", len(waypoints))
return waypoints return waypoints
} }
+118 -27
View File
@@ -9,54 +9,54 @@ var _ = Describe("Schedule Parser", func() {
Describe("Index Parser", func() { Describe("Index Parser", func() {
Context("parse : 83.", func() { Context("parse : 83.", func() {
message := "83."
data := parse(message, IndexExpression)
It("should return a valid index", func() { It("should return a valid index", func() {
message := "83."
data := extract(message, IndexExpression)
Expect(data).NotTo(BeNil()) Expect(data).NotTo(BeNil())
Expect(data[Index]).To(Equal("83.")) Expect(data[Index]).To(Equal("83."))
}) })
}) })
Context("parse : (21)", func() { Context("parse : (21)", func() {
message := "(21)"
data := parse(message, IndexExpression)
It("should return a valid index", func() { It("should return a valid index", func() {
message := "(21)"
data := extract(message, IndexExpression)
Expect(data).NotTo(BeNil()) Expect(data).NotTo(BeNil())
Expect(data[Index]).To(Equal("(21)")) Expect(data[Index]).To(Equal("(21)"))
}) })
}) })
Context("parse : L59", func() { Context("parse : L59", func() {
message := "L59"
data := parse(message, IndexExpression)
It("should return a valid index", func() { It("should return a valid index", func() {
message := "L59"
data := extract(message, IndexExpression)
Expect(data).NotTo(BeNil()) Expect(data).NotTo(BeNil())
Expect(data[Index]).To(Equal("L59")) Expect(data[Index]).To(Equal("L59"))
}) })
}) })
Context("parse : (205)", func() { Context("parse : (205)", func() {
message := "(205)"
data := parse(message, IndexExpression)
It("should return a valid index", func() { It("should return a valid index", func() {
message := "(205)"
data := extract(message, IndexExpression)
Expect(data).NotTo(BeNil()) Expect(data).NotTo(BeNil())
Expect(data[Index]).To(Equal("(205)")) Expect(data[Index]).To(Equal("(205)"))
}) })
}) })
Context("parse : L01", func() { Context("parse : L01", func() {
message := "L01"
data := parse(message, IndexExpression)
It("should return a valid index", func() { It("should return a valid index", func() {
message := "L01"
data := extract(message, IndexExpression)
Expect(data).NotTo(BeNil()) Expect(data).NotTo(BeNil())
Expect(data[Index]).To(Equal("L01")) Expect(data[Index]).To(Equal("L01"))
}) })
}) })
Context("parse : 01)", func() { Context("parse : 01)", func() {
message := "01)"
data := parse(message, IndexExpression)
It("should return a valid index", func() { It("should return a valid index", func() {
message := "01)"
data := extract(message, IndexExpression)
Expect(data).NotTo(BeNil()) Expect(data).NotTo(BeNil())
Expect(data[Index]).To(Equal("01)")) Expect(data[Index]).To(Equal("01)"))
}) })
@@ -66,42 +66,53 @@ var _ = Describe("Schedule Parser", func() {
Describe("Flight Number Parser", func() { Describe("Flight Number Parser", func() {
Context("parse : FM9134", func() { Context("parse : FM9134", func() {
message := "FM9134"
data := parse(message, FlightNumberExpression)
It("should return a valid flight number", func() { It("should return a valid flight number", func() {
message := "FM9134"
data := extract(message, FlightNumberExpression)
Expect(data).NotTo(BeNil()) Expect(data).NotTo(BeNil())
Expect(data[FlightNumber]).To(Equal("FM9134")) Expect(data[FlightNumber]).To(Equal("FM9134"))
}) })
}) })
Context("parse : Y87969", func() { Context("parse : Y87969", func() {
message := "Y87969"
data := parse(message, FlightNumberExpression)
It("should return a valid flight number", func() { It("should return a valid flight number", func() {
message := "Y87969"
data := extract(message, FlightNumberExpression)
Expect(data).NotTo(BeNil()) Expect(data).NotTo(BeNil())
Expect(data[FlightNumber]).To(Equal("Y87969")) Expect(data[FlightNumber]).To(Equal("Y87969"))
}) })
}) })
Context("parse : CK261", func() { Context("parse : CK261", func() {
message := "CK261"
data := parse(message, FlightNumberExpression)
It("should return a valid flight number", func() { It("should return a valid flight number", func() {
message := "CK261"
data := extract(message, FlightNumberExpression)
Expect(data).NotTo(BeNil()) Expect(data).NotTo(BeNil())
Expect(data[FlightNumber]).To(Equal("CK261")) Expect(data[FlightNumber]).To(Equal("CK261"))
}) })
}) })
Context("parse : 9C8812", func() { Context("parse : 9C8812", func() {
message := "9C8812"
data := parse(message, FlightNumberExpression)
It("should return a valid flight number", func() { It("should return a valid flight number", func() {
message := "9C8812"
data := extract(message, FlightNumberExpression)
Expect(data).NotTo(BeNil()) Expect(data).NotTo(BeNil())
Expect(data[FlightNumber]).To(Equal("9C8812")) Expect(data[FlightNumber]).To(Equal("9C8812"))
}) })
}) })
}) })
Describe("Schedule Date Parser", func() {
Context("parse : 29OCT", func() {
It("should return a valid date", func() {
message := "29OCT"
data := extract(message, DateExpression)
Expect(data).NotTo(BeNil())
Expect(data[Date]).To(Equal("29OCT"))
})
})
})
Describe("FindWaypoints", func() { Describe("FindWaypoints", func() {
It("should return the correct waypoints based on the message", func() { It("should return the correct waypoints based on the message", func() {
message := "1845(11JUN)TSN/2100" message := "1845(11JUN)TSN/2100"
@@ -121,26 +132,106 @@ var _ = Describe("Schedule Parser", func() {
Describe("Parsing one line of schedule", func() { Describe("Parsing one line of schedule", func() {
Context("parse : W/Z FM9134 B2688 1/1ILS (00) TSN0100 SHA", func() { Context("parse : W/Z FM9134 B2688 1/1ILS (00) TSN0100 SHA", func() {
It("should return a valid schedule", func() {
lineText := "W/Z FM9134 B2688 1/1ILS (00) TSN0100 SHA" lineText := "W/Z FM9134 B2688 1/1ILS (00) TSN0100 SHA"
schedule := ParseLine(lineText) schedule := ParseLine(lineText)
It("should return a valid schedule", func() {
Expect(schedule).NotTo(BeNil()) Expect(schedule).NotTo(BeNil())
Expect(schedule.Task).To(Equal("W/Z")) Expect(schedule.Task).To(Equal("W/Z"))
// Expect(schedule.Date).To(Equal("TSN0100")) // Expect(schedule.Date).To(Equal("TSN0100"))
// Expect(schedule.Task).To(Equal("1/1")) // Expect(schedule.Task).To(Equal("1/1"))
Expect(schedule.FlightNumber).To(Equal("FM9134")) Expect(schedule.FlightNumber[0]).To(Equal("FM9134"))
Expect(schedule.AircraftReg).To(Equal("B2688")) Expect(schedule.AircraftReg).To(Equal("B2688"))
Expect(len(schedule.Waypoints)).To(Equal(2)) Expect(len(schedule.Waypoints)).To(Equal(2))
Expect(schedule.Waypoints[0].Airport).To(Equal("TSN")) Expect(schedule.Waypoints[0].Airport).To(Equal("TSN"))
Expect(schedule.Waypoints[0].DepartureTime).To(Equal("0100")) Expect(schedule.Waypoints[0].DepartureTime).To(Equal("0100"))
Expect(schedule.Waypoints[1].Airport).To(Equal("SHA")) Expect(schedule.Waypoints[1].Airport).To(Equal("SHA"))
// Expect(schedule.PassengerConfig).To(Equal("1/1"))
// Expect(schedule.ILS).To(Equal("ILS (00)"))
// Expect(schedule.DepartureAirport).To(Equal("TSN"))
// Expect(schedule.DepartureTime).To(Equal("0100"))
// Expect(schedule.ScheduleInfo).To(Equal("SHA"))
}) })
}) })
}) })
})
var _ = Describe("FindDef", func() {
Context("find def for MF", func() {
It("should return a valid def", func() {
def := FindDef("MF")
Expect(def).NotTo(BeNil())
Expect(def.Airlines).To(ContainElement("MF"))
})
})
Context("find def for FM", func() {
It("should return a valid def", func() {
def := FindDef("FM")
Expect(def).NotTo(BeNil())
Expect(def.Airlines).To(ContainElement("FM"))
})
})
Context("find def for CK", func() {
It("should return a valid def", func() {
def := FindDef("CK")
Expect(def).To(BeNil())
})
})
})
var _ = Describe("Parse Line with PreDef", func() {
Context("FM", func() {
It("W/Z FM9134 B2688 1/1ILS (00) TSN0100 SHA", func() {
lineText := "W/Z FM9134 B2688 1/1ILS (00) TSN0100 SHA"
def := FindDef("FM")
schedule := ParseWithDef(lineText, def)
Expect(schedule).NotTo(BeNil())
Expect(schedule.Task).To(Equal("W/Z"))
Expect(len(schedule.FlightNumber)).To(Equal(1))
Expect(schedule.FlightNumber[0]).To(Equal("FM9134"))
Expect(schedule.AircraftReg).To(Equal("B2688"))
Expect(len(schedule.Waypoints)).To(Equal(2))
Expect(schedule.Waypoints[0].Airport).To(Equal("TSN"))
Expect(schedule.Waypoints[0].DepartureTime).To(Equal("0100"))
Expect(schedule.Waypoints[1].Airport).To(Equal("SHA"))
})
})
Context("MF", func() {
It("01) MF8193 B5595 ILS(8) HGH1100 1305TSN", func() {
lineText := "01) MF8193 B5595 ILS(8) HGH1100 1305TSN"
def := FindDef("MF")
Expect(def).NotTo(BeNil())
schedule := ParseWithDef(lineText, def)
Expect(schedule).NotTo(BeNil())
Expect(schedule.Index).To(Equal("01)"))
Expect(len(schedule.FlightNumber)).To(Equal(1))
Expect(schedule.FlightNumber[0]).To(Equal("MF8193"))
Expect(schedule.AircraftReg).To(Equal("B5595"))
Expect(len(schedule.Waypoints)).To(Equal(2))
Expect(schedule.Waypoints[0].Airport).To(Equal("HGH"))
Expect(schedule.Waypoints[0].DepartureTime).To(Equal("1100"))
Expect(schedule.Waypoints[1].Airport).To(Equal("TSN"))
Expect(schedule.Waypoints[1].ArrivalTime).To(Equal("1305"))
})
})
Context("8X", func() {
It("L1: 29OCT BK2735 B2863 ILS IS (3/6) TSN2350(28OCT) HAK", func() {
lineText := "L1: 29OCT BK2735 B2863 ILS IS (3/6) TSN2350(28OCT) HAK"
def := FindDef("8X")
Expect(def).NotTo(BeNil())
schedule := ParseWithDef(lineText, def)
Expect(schedule).NotTo(BeNil())
Expect(schedule.Index).To(Equal("L1:"))
Expect(schedule.Date).To(Equal("29OCT"))
Expect(len(schedule.FlightNumber)).To(Equal(1))
Expect(schedule.FlightNumber[0]).To(Equal("BK2735"))
Expect(schedule.AircraftReg).To(Equal("B2863"))
Expect(len(schedule.Waypoints)).To(Equal(2))
Expect(schedule.Waypoints[0].Airport).To(Equal("TSN"))
Expect(schedule.Waypoints[0].DepartureTime).To(Equal("2350(28OCT)"))
Expect(schedule.Waypoints[1].Airport).To(Equal("HAK"))
})
})
}) })