convert_departures.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // SPDX-FileCopyrightText: Adam Evyčędo
  2. //
  3. // SPDX-License-Identifier: AGPL-3.0-or-later
  4. package traffic
  5. import (
  6. "bufio"
  7. "context"
  8. "database/sql"
  9. "encoding/csv"
  10. "fmt"
  11. "io"
  12. "log"
  13. "os"
  14. "path/filepath"
  15. "strings"
  16. "git.sr.ht/~sircmpwn/go-bare"
  17. "github.com/lanrat/extsort"
  18. _ "github.com/mattn/go-sqlite3"
  19. )
  20. func readInputRoutesIndex(c feedConverter) (feedConverter, error) {
  21. index := map[string]int64{}
  22. path := c.TmpFeedPath
  23. err := forEachRow(filepath.Join(path, "routes.txt"), func(offset int64, fields map[string]int, record []string) error {
  24. routeID := record[fields["route_id"]]
  25. index[routeID] = offset
  26. return nil
  27. })
  28. c.routesInputIndex = index
  29. return c, err
  30. }
  31. func readInputStopsIndex(c feedConverter) (feedConverter, error) {
  32. index := map[string]int64{}
  33. path := c.TmpFeedPath
  34. forEachRow(filepath.Join(path, "stops.txt"), func(offset int64, fields map[string]int, record []string) error {
  35. stopID := record[fields["stop_id"]]
  36. index[stopID] = offset
  37. return nil
  38. })
  39. c.stopsInputIndex = index
  40. return c, nil
  41. }
  42. func readInputTripsIndex(c feedConverter) (feedConverter, error) {
  43. index := map[string]int64{}
  44. path := c.TmpFeedPath
  45. err := forEachRow(filepath.Join(path, "trips.txt"), func(offset int64, fields map[string]int, record []string) error {
  46. tripID := record[fields["trip_id"]]
  47. index[tripID] = offset
  48. return nil
  49. })
  50. c.tripsInputIndex = index
  51. return c, err
  52. }
  53. func convertDepartures(c feedConverter) (feedConverter, error) { // O(n:stop_times) ; (TmpFeedPath, tripsInputIndex, stopsInputIndex -- TripsOffsets:map[tripID]offset >> trips)
  54. path := c.TmpFeedPath
  55. cacheDir, err := os.UserCacheDir()
  56. if err != nil {
  57. return c, fmt.Errorf("while getting cache dir: %w", err)
  58. }
  59. db, err := sql.Open("sqlite3", filepath.Join(cacheDir, "turntable.db"))
  60. if err != nil {
  61. return c, fmt.Errorf("while opening db: %w", err)
  62. }
  63. _, err = db.Exec("create table change_options(stop_id text, line_name text, headsign text, primary key(stop_id, line_name, headsign))")
  64. if err != nil {
  65. return c, fmt.Errorf("while creating changeOptions table: %w", err)
  66. }
  67. tx, err := db.Begin()
  68. if err != nil {
  69. return c, fmt.Errorf("while beginning transaction: %w", err)
  70. }
  71. defer tx.Rollback()
  72. tripsOffsets := map[string]uint{}
  73. var outputOffset uint = 0
  74. previousTrip := ""
  75. firstDepartureTime := uint(0)
  76. trip := Trip{}
  77. result, err := os.Create(filepath.Join(path, "trips.bare"))
  78. if err != nil {
  79. return c, fmt.Errorf("while creating file: %w", err)
  80. }
  81. defer result.Close()
  82. tripsFile, err := os.Open(filepath.Join(path, "trips.txt"))
  83. if err != nil {
  84. return c, fmt.Errorf("while opening trips file: %w", err)
  85. }
  86. defer tripsFile.Close()
  87. // TODO unnecessary overhead, should parse single csv header line
  88. trips := csv.NewReader(tripsFile)
  89. tripsHeader, err := trips.Read()
  90. if err != nil {
  91. return c, fmt.Errorf("while reading trips header: %w", err)
  92. }
  93. tripsFields := map[string]int{}
  94. for i, headerField := range tripsHeader {
  95. tripsFields[headerField] = i
  96. }
  97. stopsFile, err := os.Open(filepath.Join(path, "stops.txt"))
  98. if err != nil {
  99. return c, fmt.Errorf("while opening stops file: %w", err)
  100. }
  101. defer stopsFile.Close()
  102. // TODO unnecessary overhead, should parse single csv header line
  103. stops := csv.NewReader(stopsFile)
  104. stopsHeader, err := stops.Read()
  105. if err != nil {
  106. return c, fmt.Errorf("while reading stops header: %w", err)
  107. }
  108. stopsFields := map[string]int{}
  109. for i, headerField := range stopsHeader {
  110. stopsFields[headerField] = i
  111. }
  112. routesFile, err := os.Open(filepath.Join(path, "routes.txt"))
  113. if err != nil {
  114. return c, fmt.Errorf("while opening routes file: %w", err)
  115. }
  116. defer routesFile.Close()
  117. // TODO unnecessary overhead, should parse single csv header line
  118. routes := csv.NewReader(routesFile)
  119. routesHeader, err := routes.Read()
  120. if err != nil {
  121. return c, fmt.Errorf("while reading routes header: %w", err)
  122. }
  123. routesFields := map[string]int{}
  124. for i, headerField := range routesHeader {
  125. routesFields[headerField] = i
  126. }
  127. tripsThroughStopFile, err := os.Create(filepath.Join(path, "tripsthroughstop.csv"))
  128. if err != nil {
  129. return c, fmt.Errorf("while creating tripsThroughStop file: %w", err)
  130. }
  131. defer tripsThroughStopFile.Close()
  132. defer tripsThroughStopFile.Sync()
  133. tripsThroughStop := csv.NewWriter(tripsThroughStopFile)
  134. defer tripsThroughStop.Flush()
  135. err = tripsThroughStop.Write([]string{"stop_id", "trip_id", "sequence"})
  136. if err != nil {
  137. return c, fmt.Errorf("while writing tripsThroughStop header: %w", err)
  138. }
  139. err = forEachRow(filepath.Join(path, "stop_times.txt"), func(offset int64, fields map[string]int, record []string) error {
  140. departure := Departure{}
  141. tripID := record[fields["trip_id"]]
  142. stopID := record[fields["stop_id"]]
  143. if previousTrip != tripID {
  144. if previousTrip != "" {
  145. tripsOffsets, outputOffset, err = finishTrip(trip, result, tripsOffsets, outputOffset)
  146. if err != nil {
  147. return fmt.Errorf("while finishing trip: %w", err)
  148. }
  149. }
  150. trip, err = beginTrip(tripsFile, c, tripID, len(tripsHeader), Trip{}, tripsFields)
  151. if err != nil {
  152. return fmt.Errorf("while beginning trip: %w", err)
  153. }
  154. firstDepartureTime = departure.Time
  155. }
  156. routeRecord, err := readCsvLine(routesFile, c.routesInputIndex[trip.LineID], len(routesHeader))
  157. if err != nil && err != io.EOF {
  158. return fmt.Errorf("while reading a routes record: %w", err)
  159. }
  160. lineName := c.Feed.Flags().LineName
  161. for _, template := range []string{"route_short_name", "route_long_name"} {
  162. lineName = strings.Replace(lineName, "{{"+template+"}}", routeRecord[routesFields[template]], -1)
  163. }
  164. /*headsign := translateFieldDefault(trip.Headsign, c.feedInfo.Language, c.defaultLanguage, c.translations)
  165. translatedHeadsigns := translateField(trip.Headsign, c.feedInfo.Language, c.defaultLanguage, c.translations)
  166. for _, translatedHeadsign := range translatedHeadsigns {
  167. changeOptionsRecord = append(changeOptionsRecord, translatedHeadsign.Language)
  168. changeOptionsRecord = append(changeOptionsRecord, translatedHeadsign.Value)
  169. }*/
  170. _, err = tx.Exec("insert into change_options values(?, ?, ?) on conflict(stop_id, line_name, headsign) do nothing", stopID, lineName, trip.Headsign)
  171. if err != nil {
  172. return fmt.Errorf("while writing changeOptions record: %w", err)
  173. }
  174. fmt.Sscanf(record[fields["stop_sequence"]], "%d", &departure.StopSequence)
  175. fmt.Sscanf(record[fields["pickup_type"]], "%d", &departure.Pickup)
  176. fmt.Sscanf(record[fields["drop_off_type"]], "%d", &departure.Dropoff)
  177. stopSequence := fmt.Sprintf("%d", departure.StopSequence)
  178. tripsThroughStopRecord := []string{stopID, tripID, stopSequence}
  179. err = tripsThroughStop.Write(tripsThroughStopRecord)
  180. if err != nil {
  181. return fmt.Errorf("while writing tripsThroughStop record: %w", err)
  182. }
  183. departureTime, err := parseDepartureTime(record[fields["arrival_time"]])
  184. if err != nil {
  185. return fmt.Errorf("while parsing arrival time: %w", err)
  186. }
  187. departure.Time = uint(departureTime)
  188. if _, ok := c.Headways[tripID]; ok {
  189. departure.Time -= firstDepartureTime
  190. }
  191. trip.Departures = append(trip.Departures, departure)
  192. previousTrip = tripID
  193. return nil
  194. })
  195. tripsOffsets, outputOffset, err = finishTrip(trip, result, tripsOffsets, outputOffset)
  196. if err != nil {
  197. return c, fmt.Errorf("while finishing trip: %w", err)
  198. }
  199. c.tripsOffsets = tripsOffsets
  200. tx.Commit()
  201. return c, err
  202. }
  203. func beginTrip(tripsFile *os.File, c feedConverter, tripID string, tripsHeaderLen int, trip Trip, tripsFields map[string]int) (Trip, error) {
  204. tripRecord, err := readCsvLine(tripsFile, c.tripsInputIndex[tripID], tripsHeaderLen)
  205. if err != nil && err != io.EOF {
  206. return trip, fmt.Errorf("while reading a trips record: %w", err)
  207. }
  208. trip.Id = tripID
  209. switch c.Feed.Flags().Headsign {
  210. case HeadsignTripHeadsing:
  211. trip.Headsign = tripRecord[tripsFields["trip_headsign"]]
  212. case HeadsignTripLastStop:
  213. // TODO test this case
  214. /*
  215. stopRecord, err := readCsvLine(stopsFile, c.stopsInputIndex[stopID], len(stopsHeader))
  216. if err != nil && err != io.EOF {
  217. return fmt.Errorf("while reading a stops record: %w", err)
  218. }
  219. trip.Headsign = stopRecord[stopsFields["stop_name"]]
  220. */
  221. }
  222. // TODO translated headsign(-s)
  223. if h, ok := c.Headways[trip.Id]; ok {
  224. trip.Headways = h
  225. }
  226. trip.ScheduleID = tripRecord[tripsFields["service_id"]]
  227. trip.LineID = tripRecord[tripsFields["route_id"]]
  228. fmt.Sscanf(tripRecord[tripsFields["direction_id"]], "%d", &trip.Direction)
  229. return trip, nil
  230. }
  231. func finishTrip(trip Trip, result io.Writer, tripsOffsets map[string]uint, outputOffset uint) (map[string]uint, uint, error) {
  232. trip.Departures[0].Ordinality = ORIGIN
  233. trip.Departures[len(trip.Departures)-1].Ordinality = TERMINUS
  234. bytes, err := bare.Marshal(&trip)
  235. if err != nil {
  236. return tripsOffsets, outputOffset, fmt.Errorf("while marshalling: %w", err)
  237. }
  238. b, err := result.Write(bytes)
  239. if err != nil {
  240. return tripsOffsets, outputOffset, fmt.Errorf("while writing: %w", err)
  241. }
  242. tripsOffsets[trip.Id] = outputOffset
  243. outputOffset += uint(b)
  244. return tripsOffsets, outputOffset, nil
  245. }
  246. func sortOutIndex(fileName string) error {
  247. file, err := os.Open(fileName)
  248. if err != nil {
  249. return fmt.Errorf("while opening file: %w", err)
  250. }
  251. defer file.Close()
  252. result, err := os.Create(fileName + "2")
  253. if err != nil {
  254. return fmt.Errorf("while creating file: %w", err)
  255. }
  256. defer result.Close()
  257. scanner := bufio.NewScanner(file)
  258. scanner.Scan()
  259. headerLine := scanner.Text()
  260. if err := scanner.Err(); err != nil {
  261. return fmt.Errorf("while scanning: %w", err)
  262. }
  263. inputChan := make(chan string)
  264. go func() {
  265. for scanner.Scan() {
  266. inputChan <- scanner.Text()
  267. }
  268. close(inputChan)
  269. }()
  270. sorter, outputChan, errChan := extsort.Strings(inputChan, nil)
  271. sorter.Sort(context.Background())
  272. result.WriteString(headerLine + "\n")
  273. for data := range outputChan {
  274. result.WriteString(data + "\n")
  275. }
  276. if err := <-errChan; err != nil {
  277. return fmt.Errorf("while sorting: %w", err)
  278. }
  279. result.Sync()
  280. result.Close()
  281. os.Rename(fileName, fileName+"_xxx")
  282. err = os.Rename(fileName+"2", fileName)
  283. if err != nil {
  284. return fmt.Errorf("while replacing file: %w", err)
  285. }
  286. return nil
  287. }
  288. func sortTripsThroughStop(c feedConverter) error {
  289. return sortOutIndex(filepath.Join(c.TmpFeedPath, "tripsthroughstop.csv"))
  290. }
  291. // TODO out to separate file
  292. func readCsvLine(r *os.File, offset int64, fields int) ([]string, error) {
  293. if offset != -1 {
  294. r.Seek(offset, io.SeekStart)
  295. }
  296. line := []byte{}
  297. for {
  298. b := make([]byte, 1)
  299. _, err := r.Read(b)
  300. if err != nil {
  301. if err == io.EOF {
  302. break
  303. }
  304. return []string{}, fmt.Errorf("while reading byte: %w", err)
  305. }
  306. if b[0] == '\n' {
  307. break
  308. }
  309. line = append(line, b[0])
  310. }
  311. if string(line) == "" {
  312. return []string{}, io.EOF
  313. }
  314. // TODO unnecessary overhead, should parse single csv line, expecting $fields fields
  315. csvReader := csv.NewReader(strings.NewReader(string(line)))
  316. csvReader.FieldsPerRecord = fields
  317. record, err := csvReader.Read()
  318. if err != nil {
  319. log.Printf("fields: %d\nline: %s\nrecord:%v\n", fields, string(line), record)
  320. return record, fmt.Errorf("while reading record: %w", err)
  321. }
  322. return record, nil
  323. }