refactor: Update parsing of flight schedule data for improved accuracy and functionality
This commit is contained in:
+23
-25
@@ -29,25 +29,27 @@ type FlightSchedule struct {
|
||||
// Example: "ILS(0)"
|
||||
ILS string `json:"ils,omitempty"`
|
||||
|
||||
// ICAO code for the departure airport.
|
||||
// Example: "ZBTJ"
|
||||
DepartureAirport string `json:"departure_airport"`
|
||||
// // ICAO code for the departure airport.
|
||||
// // Example: "ZBTJ"
|
||||
// DepartureAirport string `json:"departure_airport"`
|
||||
|
||||
// Scheduled departure time of the flight. This field may be formatted as HHMM or include date information.
|
||||
// Example: "1845"
|
||||
DepartureTime string `json:"departure_time,omitempty"`
|
||||
// // Scheduled departure time of the flight. This field may be formatted as HHMM or include date information.
|
||||
// // Example: "1845"
|
||||
// DepartureTime string `json:"departure_time,omitempty"`
|
||||
|
||||
// Additional schedule information. This field may include special instructions or additional scheduling details.
|
||||
// Example: "SI:TSN1845"
|
||||
ScheduleInfo string `json:"schedule_info,omitempty"`
|
||||
// // Additional schedule information. This field may include special instructions or additional scheduling details.
|
||||
// // Example: "SI:TSN1845"
|
||||
// ScheduleInfo string `json:"schedule_info,omitempty"`
|
||||
|
||||
// Estimated or scheduled arrival time of the flight. This field may be formatted as HHMM or include date information.
|
||||
// Example: "2100"
|
||||
ArrivalTime string `json:"arrival_time,omitempty"`
|
||||
// // Estimated or scheduled arrival time of the flight. This field may be formatted as HHMM or include date information.
|
||||
// // Example: "2100"
|
||||
// ArrivalTime string `json:"arrival_time,omitempty"`
|
||||
|
||||
// ICAO code for the arrival airport.
|
||||
// Example: "ZSPD"
|
||||
ArrivalAirport string `json:"arrival_airport"`
|
||||
// // ICAO code for the arrival airport.
|
||||
// // Example: "ZSPD"
|
||||
// ArrivalAirport string `json:"arrival_airport"`
|
||||
|
||||
Waypoints []WayPoint `json:"waypoints"`
|
||||
|
||||
// Additional comments or remarks about the flight schedule. This field may include any relevant notes or observations.
|
||||
// Example: "Special cargo handling required"
|
||||
@@ -57,6 +59,12 @@ type FlightSchedule struct {
|
||||
Reference string `json:"reference,omitempty"`
|
||||
}
|
||||
|
||||
type WayPoint struct {
|
||||
ArrivalTime string
|
||||
Airport string
|
||||
DepartureTime string
|
||||
}
|
||||
|
||||
func (f *FlightSchedule) Validate() error {
|
||||
if f.Date == "" {
|
||||
return fmt.Errorf("date is required")
|
||||
@@ -67,15 +75,5 @@ func (f *FlightSchedule) Validate() error {
|
||||
if f.AircraftReg == "" {
|
||||
return fmt.Errorf("aircraft registration is required")
|
||||
}
|
||||
if f.DepartureAirport == "" {
|
||||
return fmt.Errorf("departure airport is required")
|
||||
}
|
||||
if f.ArrivalAirport == "" {
|
||||
return fmt.Errorf("arrival airport is required")
|
||||
}
|
||||
|
||||
if f.DepartureTime == "" && f.ArrivalTime == "" {
|
||||
return fmt.Errorf("either departure time or arrival time is required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,21 +2,19 @@ package parsers
|
||||
|
||||
import (
|
||||
"caatsm/internal/domain"
|
||||
"caatsm/pkg/utils"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
// DepartureCode = "dep"
|
||||
// DepartureTime = "dep_time"
|
||||
// ArrivalCode = "arr"
|
||||
AirportCode = "airport"
|
||||
Date = "date"
|
||||
Task = "task"
|
||||
IndexPattern = `(?P<idx>\(?L?[0-9]+\)?\.?)`
|
||||
DatePattern = `\s?(?P<date>\d{2}\w{3})`
|
||||
TaskPattern = `\s?(?P<task>[A-Z]\/[A-Z])`
|
||||
WaypointPattern = `\s?(?P<arr_time>\d{4}(\(\d{2}\w{3}\))?)\/?(?P<airport>\w{3})\/?(?P<dep_time>\d{4}(\(\d{2}\w{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}\))?)?$`
|
||||
FlightNumberPattern = `\s?(?P<number>[0-9A-Z][0-9A-Z]\d{3,5})`
|
||||
RegisterPattern = `\s?(?P<reg>B\d{4})`
|
||||
)
|
||||
@@ -38,31 +36,43 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
func FindWaypoints(message string) map[string]string {
|
||||
func ExtractWaypoint(message string) *domain.WayPoint {
|
||||
matches := WaypointExpression.FindStringSubmatch(message)
|
||||
if matches == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
result := make(map[string]string)
|
||||
data := make(map[string]string)
|
||||
for i, name := range WaypointExpression.SubexpNames() {
|
||||
if i != 0 && name != "" {
|
||||
result[name] = matches[i]
|
||||
data[name] = matches[i]
|
||||
}
|
||||
}
|
||||
result := &domain.WayPoint{
|
||||
ArrivalTime: data[ArrivalTime],
|
||||
Airport: data[AirportCode],
|
||||
DepartureTime: data[DepartureTime],
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func ParseLine(line string) *domain.FlightSchedule {
|
||||
log := utils.GetSugaredLogger()
|
||||
cleanLine := strings.TrimSpace(line)
|
||||
words := strings.Split(cleanLine, " ")
|
||||
var flightSchedule = &domain.FlightSchedule{}
|
||||
var flightSchedule = &domain.FlightSchedule{
|
||||
Reference: line,
|
||||
}
|
||||
var data map[string]string
|
||||
|
||||
if indexData := parse(words[0], IndexExpression); indexData != nil {
|
||||
flightSchedule.Index = indexData[Index]
|
||||
words = words[1:]
|
||||
}
|
||||
|
||||
// Define the parsing strategy
|
||||
parseStrategy := []string{
|
||||
Index,
|
||||
Task,
|
||||
Date,
|
||||
FlightNumber,
|
||||
@@ -71,38 +81,81 @@ func ParseLine(line string) *domain.FlightSchedule {
|
||||
|
||||
// Track parsed fields to avoid re-parsing
|
||||
parsed := make(map[string]bool)
|
||||
var maxParsed int
|
||||
|
||||
// Parse each word in the line
|
||||
for i, word := range words {
|
||||
if i > 0 {
|
||||
parsed[Index] = true
|
||||
}
|
||||
// Check if all fields have been parsed
|
||||
for _, name := range parseStrategy {
|
||||
// Skip if already parsed
|
||||
if parsed[name] {
|
||||
continue
|
||||
}
|
||||
// Parse the word
|
||||
if data = parse(word, parserMap[name]); data != nil {
|
||||
|
||||
// Update the flight schedule
|
||||
switch name {
|
||||
case Index:
|
||||
flightSchedule.Index = data[Index]
|
||||
parsed[Index] = true
|
||||
case Task:
|
||||
flightSchedule.Task = data[Task]
|
||||
parsed[Task] = true
|
||||
maxParsed = i
|
||||
case Date:
|
||||
flightSchedule.Date = data[Date]
|
||||
parsed[Date] = true
|
||||
maxParsed = i
|
||||
case FlightNumber:
|
||||
flightSchedule.FlightNumber = data[FlightNumber]
|
||||
parsed[FlightNumber] = true
|
||||
maxParsed = i
|
||||
case Register:
|
||||
flightSchedule.AircraftReg = data[Register]
|
||||
parsed[Register] = true
|
||||
maxParsed = i
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
if maxParsed+1 < len(words) {
|
||||
flightSchedule.Waypoints = parseWaypoints(words[maxParsed+1:])
|
||||
} else {
|
||||
log.Warn("No waypoints found")
|
||||
flightSchedule.Comments = "No waypoints found"
|
||||
}
|
||||
|
||||
return flightSchedule
|
||||
}
|
||||
|
||||
func parseWaypoints(points []string) []domain.WayPoint {
|
||||
log := utils.GetSugaredLogger()
|
||||
if len(points) == 0 {
|
||||
log.Warn("No waypoints found")
|
||||
return nil
|
||||
}
|
||||
|
||||
//find first waypoint
|
||||
var realWaypoints []string
|
||||
for i, point := range points {
|
||||
if parse(point, WaypointExpression) != nil {
|
||||
realWaypoints = points[i:]
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(realWaypoints) == 0 {
|
||||
log.Warn("No waypoints found")
|
||||
return nil
|
||||
}
|
||||
var waypoints []domain.WayPoint
|
||||
for _, point := range realWaypoints {
|
||||
log.Debugf("Parsing waypoint: %s", point)
|
||||
if waypoint := ExtractWaypoint(point); waypoint != nil {
|
||||
log.Debugf("Waypoint: %v", waypoint)
|
||||
waypoints = append(waypoints, *waypoint)
|
||||
} else {
|
||||
log.Warnf("Failed to parse waypoint: %s", point)
|
||||
}
|
||||
}
|
||||
log.Debugf("Found %d waypoints", len(waypoints))
|
||||
return waypoints
|
||||
}
|
||||
|
||||
@@ -105,16 +105,16 @@ var _ = Describe("Schedule Parser", func() {
|
||||
Describe("FindWaypoints", func() {
|
||||
It("should return the correct waypoints based on the message", func() {
|
||||
message := "1845(11JUN)TSN/2100"
|
||||
waypoints := FindWaypoints(message)
|
||||
Expect(waypoints).NotTo(BeNil())
|
||||
Expect(waypoints[ArrivalTime]).To(Equal("1845(11JUN)"))
|
||||
Expect(waypoints[AirportCode]).To(Equal("TSN"))
|
||||
Expect(waypoints[DepartureTime]).To(Equal("2100"))
|
||||
waypoint := ExtractWaypoint(message)
|
||||
Expect(waypoint).NotTo(BeNil())
|
||||
Expect(waypoint.ArrivalTime).To(Equal("1845(11JUN)"))
|
||||
Expect(waypoint.Airport).To(Equal("TSN"))
|
||||
Expect(waypoint.DepartureTime).To(Equal("2100"))
|
||||
})
|
||||
|
||||
It("should return nil if no waypoints are found", func() {
|
||||
message := "1845TSN"
|
||||
waypoints := FindWaypoints(message)
|
||||
message := "18451TSN"
|
||||
waypoints := ExtractWaypoint(message)
|
||||
Expect(waypoints).To(BeNil())
|
||||
})
|
||||
})
|
||||
@@ -130,6 +130,10 @@ var _ = Describe("Schedule Parser", func() {
|
||||
// Expect(schedule.Task).To(Equal("1/1"))
|
||||
Expect(schedule.FlightNumber).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"))
|
||||
// Expect(schedule.PassengerConfig).To(Equal("1/1"))
|
||||
// Expect(schedule.ILS).To(Equal("ILS (00)"))
|
||||
// Expect(schedule.DepartureAirport).To(Equal("TSN"))
|
||||
|
||||
Reference in New Issue
Block a user