ctucx.git: oeffisearch

[nimlang] fast and simple tripplanner

commit d4aae8d34f201630a2b377575a1a4e8b690ce079
parent b065c3695b54f261f5ace3f8d69fb53af4767bb3
Author: Milan Pässler <me@pbb.lc>
Date: Fri, 7 Feb 2020 18:29:03 +0100

backend/hafas: only parse lines that are used
5 files changed, 36 insertions(+), 44 deletions(-)
diff --git a/src/backend/hafas/api/refresh_journey.nim b/src/backend/hafas/api/refresh_journey.nim
@@ -1,9 +1,5 @@
 import ../types
-import ../parse/remark
-import ../parse/point
-import ../parse/operator
-import ../parse/journey
-import ../parse/line
+import ../parse/journeys_response
 import ../util
 import json
 import asyncdispatch

@@ -25,10 +21,4 @@ proc refreshJourney*(params: RefreshJourneyParams): Future[Journey] {.async.} =
   }
 
   let data = await request(req)
-  let points = map(data["res"]["common"]["locL"].getElems(), parsePoint)
-  let operators = map(data["res"]["common"]["opL"].getElems(), parseOperator)
-  let remarks = map(data["res"]["common"]["remL"].getElems(), parseRemark)
-  let lines = map(data["res"]["common"]["prodL"].getElems(), mkParseLine(operators))
-  let common = CommonData(points: points, operators: operators, remarks: remarks, lines: lines)
-
-  return common.mkParseJourney()(data["res"]["outConL"][0])
+  return parseJourneysResponse(data, true).journeys[0]
diff --git a/src/backend/hafas/parse/journeys_response.nim b/src/backend/hafas/parse/journeys_response.nim
@@ -8,14 +8,15 @@ import json
 import sequtils
 import strutils
 
-proc parseJourneysResponse*(data: JsonNode): JourneysResponse =
+proc parseJourneysResponse*(data: JsonNode, isRefresh: bool = false): JourneysResponse =
   let points = map(data["res"]["common"]["locL"].getElems(), parsePoint)
   let operators = map(data["res"]["common"]["opL"].getElems(), parseOperator)
   let remarks = map(data["res"]["common"]["remL"].getElems(), parseRemark)
-  let lines = map(data["res"]["common"]["prodL"].getElems(), mkParseLine(operators))
+  let lines = data["res"]["common"]["prodL"]
   let timestamp = parseInt(data["res"]["planrtTS"].getStr())
   let common = CommonData(points: points, operators: operators, remarks: remarks, lines: lines, timestamp: timestamp)
 
   result.journeys = data["res"]["outConL"].getElems().map(mkParseJourney(common))
-  result.earlierRef = data["res"]["outCtxScrB"].getStr()
-  result.laterRef = data["res"]["outCtxScrF"].getStr()
+  if not isRefresh:
+    result.earlierRef = data["res"]["outCtxScrB"].getStr()
+    result.laterRef = data["res"]["outCtxScrF"].getStr()
diff --git a/src/backend/hafas/parse/leg.nim b/src/backend/hafas/parse/leg.nim
@@ -2,6 +2,7 @@ import ../types
 import ./stopover
 import ./msg
 import ./date
+import ./line
 import json
 import options
 

@@ -30,7 +31,7 @@ proc mkParseLeg*(common: CommonData): proc =
     if typeStr == "JNY":
       result.direction = some(l{"jny"}{"dirTxt"}.getStr())
       result.tripId = some(l{"jny"}{"jid"}.getStr())
-      result.line = common.lines[l{"jny"}{"prodX"}.getInt()]
+      result.line = common.parseLine(l["jny"]["prodX"].getInt())
 
       let stopovers = l{"jny"}{"stopL"}.getElems()
       if stopovers.len > 0:
diff --git a/src/backend/hafas/parse/line.nim b/src/backend/hafas/parse/line.nim
@@ -4,38 +4,37 @@ import ./products
 import json
 import options
 
-proc mkParseLine*(commonOperators: seq[Operator]): proc =
-  proc parseLine(l: JsonNode): Option[Line] =
-    # unparsable
-    if l{"cls"}.getInt == 0:
-      return options.none(Line)
+proc parseLine*(common: CommonData, i: int): Option[Line] =
+  let l = common.lines[i]
 
-    let line = l.to(HafasProd)
-    var res = Line()
+  # unparsable
+  if l{"cls"}.getInt == 0:
+    return options.none(Line)
 
-    res.name = line.name
-    res.product = parseProduct(line.cls)
-    res.tripNum = line.prodCtx.num
-    res.productName = line.prodCtx.catOut
-    res.fullProductName = line.prodCtx.catOutL
-    res.id = slug(line.prodCtx.lineId.get(line.name))
+  let line = l.to(HafasProd)
+  var res = Line()
 
-    if line.opX.isSome:
-      res.operator = some(commonOperators[line.opX.get])
+  res.name = line.name
+  res.product = parseProduct(line.cls)
+  res.tripNum = line.prodCtx.num
+  res.productName = line.prodCtx.catOut
+  res.fullProductName = line.prodCtx.catOutL
+  res.id = slug(line.prodCtx.lineId.get(line.name))
 
-    # DB
+  if line.opX.isSome:
+    res.operator = some(common.operators[line.opX.get])
 
-    if line.nameS.isSome and (res.product == bus or res.product == tram or res.product == ferry):
-      res.name = line.nameS.get
+  # DB
 
-    if line.addName.isSome:
-      # swap name and addName
-      res.additionalName = some(res.name)
-      res.name = line.addName.get
+  if line.nameS.isSome and (res.product == bus or res.product == tram or res.product == ferry):
+    res.name = line.nameS.get
 
-    # End DB
+  if line.addName.isSome:
+    # swap name and addName
+    res.additionalName = some(res.name)
+    res.name = line.addName.get
 
-    res.mode = MODES[int(res.product)]
-    return some(res)
+  # End DB
 
-  return parseLine
+  res.mode = MODES[int(res.product)]
+  return some(res)
diff --git a/src/backend/hafas/types.nim b/src/backend/hafas/types.nim
@@ -1,10 +1,11 @@
 import ../../types
 import options
 export types
+import json
 
 type
   CommonData* = object
-    lines*:       seq[Option[Line]]
+    lines*:       JsonNode
     remarks*:     seq[Remark]
     operators*:   seq[Operator]
     points*:      seq[Point]