convert_departures.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. trip := Trip{}
  76. result, err := os.Create(filepath.Join(path, "trips.bare"))
  77. if err != nil {
  78. return c, fmt.Errorf("while creating file: %w", err)
  79. }
  80. defer result.Close()
  81. tripsFile, err := os.Open(filepath.Join(path, "trips.txt"))
  82. if err != nil {
  83. return c, fmt.Errorf("while opening trips file: %w", err)
  84. }
  85. defer tripsFile.Close()
  86. // TODO unnecessary overhead, should parse single csv header line
  87. trips := csv.NewReader(tripsFile)
  88. tripsHeader, err := trips.Read()
  89. if err != nil {
  90. return c, fmt.Errorf("while reading trips header: %w", err)
  91. }
  92. tripsFields := map[string]int{}
  93. for i, headerField := range tripsHeader {
  94. tripsFields[headerField] = i
  95. }
  96. stopsFile, err := os.Open(filepath.Join(path, "stops.txt"))
  97. if err != nil {
  98. return c, fmt.Errorf("while opening stops file: %w", err)
  99. }
  100. defer stopsFile.Close()
  101. // TODO unnecessary overhead, should parse single csv header line
  102. stops := csv.NewReader(stopsFile)
  103. stopsHeader, err := stops.Read()
  104. if err != nil {
  105. return c, fmt.Errorf("while reading stops header: %w", err)
  106. }
  107. stopsFields := map[string]int{}
  108. for i, headerField := range stopsHeader {
  109. stopsFields[headerField] = i
  110. }
  111. routesFile, err := os.Open(filepath.Join(path, "routes.txt"))
  112. if err != nil {
  113. return c, fmt.Errorf("while opening routes file: %w", err)
  114. }
  115. defer routesFile.Close()
  116. // TODO unnecessary overhead, should parse single csv header line
  117. routes := csv.NewReader(routesFile)
  118. routesHeader, err := routes.Read()
  119. if err != nil {
  120. return c, fmt.Errorf("while reading routes header: %w", err)
  121. }
  122. routesFields := map[string]int{}
  123. for i, headerField := range routesHeader {
  124. routesFields[headerField] = i
  125. }
  126. tripsThroughStopFile, err := os.Create(filepath.Join(path, "tripsthroughstop.csv"))
  127. if err != nil {
  128. return c, fmt.Errorf("while creating tripsThroughStop file: %w", err)
  129. }
  130. defer tripsThroughStopFile.Close()
  131. defer tripsThroughStopFile.Sync()
  132. tripsThroughStop := csv.NewWriter(tripsThroughStopFile)
  133. defer tripsThroughStop.Flush()
  134. err = tripsThroughStop.Write([]string{"stop_id", "trip_id", "sequence"})
  135. if err != nil {
  136. return c, fmt.Errorf("while writing tripsThroughStop header: %w", err)
  137. }
  138. err = forEachRow(filepath.Join(path, "stop_times.txt"), func(offset int64, fields map[string]int, record []string) error {
  139. departure := Departure{}
  140. tripID := record[fields["trip_id"]]
  141. stopID := record[fields["stop_id"]]
  142. if previousTrip != tripID {
  143. if previousTrip != "" {
  144. tripsOffsets, outputOffset, err = finishTrip(trip, result, tripsOffsets, outputOffset)
  145. if err != nil {
  146. return fmt.Errorf("while finishing trip: %w", err)
  147. }
  148. }
  149. trip, err = beginTrip(tripsFile, c, tripID, len(tripsHeader), Trip{}, tripsFields)
  150. if err != nil {
  151. return fmt.Errorf("while beginning trip: %w", err)
  152. }
  153. }
  154. routeRecord, err := readCsvLine(routesFile, c.routesInputIndex[trip.LineID], len(routesHeader))
  155. if err != nil && err != io.EOF {
  156. return fmt.Errorf("while reading a routes record: %w", err)
  157. }
  158. lineName := c.Feed.Flags().LineName
  159. for _, template := range []string{"route_short_name", "route_long_name"} {
  160. lineName = strings.Replace(lineName, "{{"+template+"}}", routeRecord[routesFields[template]], -1)
  161. }
  162. /*headsign := translateFieldDefault(trip.Headsign, c.feedInfo.Language, c.defaultLanguage, c.translations)
  163. translatedHeadsigns := translateField(trip.Headsign, c.feedInfo.Language, c.defaultLanguage, c.translations)
  164. for _, translatedHeadsign := range translatedHeadsigns {
  165. changeOptionsRecord = append(changeOptionsRecord, translatedHeadsign.Language)
  166. changeOptionsRecord = append(changeOptionsRecord, translatedHeadsign.Value)
  167. }*/
  168. _, err = tx.Exec("insert into change_options values(?, ?, ?) on conflict(stop_id, line_name, headsign) do nothing", stopID, lineName, trip.Headsign)
  169. if err != nil {
  170. return fmt.Errorf("while writing changeOptions record: %w", err)
  171. }
  172. fmt.Sscanf(record[fields["stop_sequence"]], "%d", &departure.StopSequence)
  173. fmt.Sscanf(record[fields["pickup_type"]], "%d", &departure.Pickup)
  174. fmt.Sscanf(record[fields["drop_off_type"]], "%d", &departure.Dropoff)
  175. stopSequence := fmt.Sprintf("%d", departure.StopSequence)
  176. tripsThroughStopRecord := []string{stopID, tripID, stopSequence}
  177. err = tripsThroughStop.Write(tripsThroughStopRecord)
  178. if err != nil {
  179. return fmt.Errorf("while writing tripsThroughStop record: %w", err)
  180. }
  181. departureTime, err := parseDepartureTime(record[fields["arrival_time"]])
  182. if err != nil {
  183. return fmt.Errorf("while parsing arrival time: %w", err)
  184. }
  185. departure.Time = uint(departureTime)
  186. trip.Departures = append(trip.Departures, departure)
  187. previousTrip = tripID
  188. return nil
  189. })
  190. tripsOffsets, outputOffset, err = finishTrip(trip, result, tripsOffsets, outputOffset)
  191. if err != nil {
  192. return c, fmt.Errorf("while finishing trip: %w", err)
  193. }
  194. c.tripsOffsets = tripsOffsets
  195. tx.Commit()
  196. return c, err
  197. }
  198. func beginTrip(tripsFile *os.File, c feedConverter, tripID string, tripsHeaderLen int, trip Trip, tripsFields map[string]int) (Trip, error) {
  199. tripRecord, err := readCsvLine(tripsFile, c.tripsInputIndex[tripID], tripsHeaderLen)
  200. if err != nil && err != io.EOF {
  201. return trip, fmt.Errorf("while reading a trips record: %w", err)
  202. }
  203. trip.Id = tripID
  204. switch c.Feed.Flags().Headsign {
  205. case HeadsignTripHeadsing:
  206. trip.Headsign = tripRecord[tripsFields["trip_headsign"]]
  207. case HeadsignTripLastStop:
  208. // TODO test this case
  209. /*
  210. stopRecord, err := readCsvLine(stopsFile, c.stopsInputIndex[stopID], len(stopsHeader))
  211. if err != nil && err != io.EOF {
  212. return fmt.Errorf("while reading a stops record: %w", err)
  213. }
  214. trip.Headsign = stopRecord[stopsFields["stop_name"]]
  215. */
  216. }
  217. // TODO translated headsign(-s)
  218. trip.ScheduleID = tripRecord[tripsFields["service_id"]]
  219. trip.LineID = tripRecord[tripsFields["route_id"]]
  220. fmt.Sscanf(tripRecord[tripsFields["direction_id"]], "%d", &trip.Direction)
  221. return trip, nil
  222. }
  223. func finishTrip(trip Trip, result io.Writer, tripsOffsets map[string]uint, outputOffset uint) (map[string]uint, uint, error) {
  224. bytes, err := bare.Marshal(&trip)
  225. if err != nil {
  226. return tripsOffsets, outputOffset, fmt.Errorf("while marshalling: %w", err)
  227. }
  228. b, err := result.Write(bytes)
  229. if err != nil {
  230. return tripsOffsets, outputOffset, fmt.Errorf("while writing: %w", err)
  231. }
  232. tripsOffsets[trip.Id] = outputOffset
  233. outputOffset += uint(b)
  234. return tripsOffsets, outputOffset, nil
  235. }
  236. func sortOutIndex(fileName string) error {
  237. file, err := os.Open(fileName)
  238. if err != nil {
  239. return fmt.Errorf("while opening file: %w", err)
  240. }
  241. defer file.Close()
  242. result, err := os.Create(fileName + "2")
  243. if err != nil {
  244. return fmt.Errorf("while creating file: %w", err)
  245. }
  246. defer result.Close()
  247. scanner := bufio.NewScanner(file)
  248. scanner.Scan()
  249. headerLine := scanner.Text()
  250. if err := scanner.Err(); err != nil {
  251. return fmt.Errorf("while scanning: %w", err)
  252. }
  253. inputChan := make(chan string)
  254. go func() {
  255. for scanner.Scan() {
  256. inputChan <- scanner.Text()
  257. }
  258. close(inputChan)
  259. }()
  260. sorter, outputChan, errChan := extsort.Strings(inputChan, nil)
  261. sorter.Sort(context.Background())
  262. result.WriteString(headerLine + "\n")
  263. for data := range outputChan {
  264. result.WriteString(data + "\n")
  265. }
  266. if err := <-errChan; err != nil {
  267. return fmt.Errorf("while sorting: %w", err)
  268. }
  269. result.Sync()
  270. result.Close()
  271. os.Rename(fileName, fileName+"_xxx")
  272. err = os.Rename(fileName+"2", fileName)
  273. if err != nil {
  274. return fmt.Errorf("while replacing file: %w", err)
  275. }
  276. return nil
  277. }
  278. func sortTripsThroughStop(c feedConverter) error {
  279. return sortOutIndex(filepath.Join(c.TmpFeedPath, "tripsthroughstop.csv"))
  280. }
  281. // TODO out to separate file
  282. func readCsvLine(r *os.File, offset int64, fields int) ([]string, error) {
  283. if offset != -1 {
  284. r.Seek(offset, io.SeekStart)
  285. }
  286. line := []byte{}
  287. for {
  288. b := make([]byte, 1)
  289. _, err := r.Read(b)
  290. if err != nil {
  291. if err == io.EOF {
  292. break
  293. }
  294. return []string{}, fmt.Errorf("while reading byte: %w", err)
  295. }
  296. if b[0] == '\n' {
  297. break
  298. }
  299. line = append(line, b[0])
  300. }
  301. if string(line) == "" {
  302. return []string{}, io.EOF
  303. }
  304. // TODO unnecessary overhead, should parse single csv line, expecting $fields fields
  305. csvReader := csv.NewReader(strings.NewReader(string(line)))
  306. csvReader.FieldsPerRecord = fields
  307. record, err := csvReader.Read()
  308. if err != nil {
  309. log.Printf("fields: %d\nline: %s\nrecord:%v\n", fields, string(line), record)
  310. return record, fmt.Errorf("while reading record: %w", err)
  311. }
  312. return record, nil
  313. }