ctucx.git: oeffi-web

[nimlang] oeffisearch fork that works without javascript

commit fbf90c9a14310015f58d018c6041bdd2b54bb3fa
parent e1bce1b772c3a6e4e09b6ad43298617399b9438f
Author: Leah (ctucx) <leah@ctu.cx>
Date: Fri, 22 Jan 2021 11:13:14 +0100

joruneysView: add traintypes to products
1 file changed, 40 insertions(+), 30 deletions(-)
M
src/formaters/renderJourneys.nim
|
70
++++++++++++++++++++++++++++++++++++++++------------------------------
diff --git a/src/formaters/renderJourneys.nim b/src/formaters/renderJourneys.nim
@@ -2,45 +2,43 @@ import json, tables, options, sequtils, strutils, algorithm, math
 import ../types, ../cache_types
 import formaters
 
+template getPlannedOrPrognosedTime(obj: untyped): int64 =
+  if not obj.prognosedTime.isNone: 
+    obj.prognosedTime.get
+  else:
+    obj.plannedTime
+
 proc renderJourneys*(data: CacheObject): JsonNode = 
   var data = data;
 
   result = newJObject()
   
-  result.add("REQ_ID", %data.reqId)
-  result.add("FROM", %formatPoint(data.params.fromPoint))
-  result.add("TO", %formatPoint(data.params.toPoint))
-  result.add("JOURNEYS", newJArray())
+  result["REQ_ID"]   = %data.reqId
+  result["FROM"]     = %formatPoint(data.params.fromPoint)
+  result["TO"]       = %formatPoint(data.params.toPoint)
+  result["JOURNEYS"] = newJArray()
 
   var journeys = toSeq(data.journeys.pairs).map(proc (i: (string, Journey)): (int, Journey) = (parseInt(i[0]), i[1])).sorted(proc (b, a: (int, Journey)): int = b[0] - a[0])
 
   for id, journey in items(journeys):
-    let departureLeg    = journey.legs[0]
-    let arrivalLeg      = journey.legs[journey.legs.len-1]
-
-    var products:              seq[string]
-    var changes:               int
-    var changesDuration:       int
-    var cancelled:             bool
+    let departureLeg     = journey.legs[0]
+    let arrivalLeg       = journey.legs[journey.legs.len-1]
 
-    var departureTime:         int64 
-    var arrivalTime:           int64
+    let departureTime    = getPlannedOrPrognosedTime(departureLeg.departure)
+    let arrivalTime      = getPlannedOrPrognosedTime(arrivalLeg.arrival)
 
-    var departureDelay:        string
-    var arrivalDelay:          string
+    var products         = initTable[string, seq[string]]()
+    var productsCombined:  seq[string]
 
-    var departureHasDelay:     bool
-    var arrivalHasDelay:       bool
+    var changes:           int
+    var changesDuration:   int
+    var cancelled:         bool
 
-    if not departureLeg.departure.prognosedTime.isNone: 
-      departureTime = departureLeg.departure.prognosedTime.get
-    else:
-      departureTime = departureLeg.departure.plannedTime
+    var departureDelay:    string
+    var arrivalDelay:      string
 
-    if not arrivalLeg.arrival.prognosedTime.isNone: 
-      arrivalTime = arrivalLeg.arrival.prognosedTime.get
-    else:
-      arrivalTime = arrivalLeg.arrival.plannedTime
+    var departureHasDelay: bool
+    var arrivalHasDelay:   bool
 
     if departureLeg.departure.prognosedTime.isSome:
       let delay = floorDiv((departureLeg.departure.prognosedTime.get - departureLeg.departure.plannedTime), 60)

@@ -64,8 +62,6 @@ proc renderJourneys*(data: CacheObject): JsonNode =
 
       arrivalDelay &= $delay
 
-    let duration = arrivalTime - departureTime
-
     for leg in journey.legs:
       if leg.cancelled != false:
         cancelled = true

@@ -76,9 +72,23 @@ proc renderJourneys*(data: CacheObject): JsonNode =
       inc(changes)
 
       if not products.contains(leg.line.get.productName):
-        products.add(leg.line.get.productName)
+        products[leg.line.get.productName] = newSeq[string]()
+
+      if leg.line.get.trainTypeShort.isSome():
+        products[leg.line.get.productName].add(leg.line.get.trainTypeShort.get)
+
+    for product, types in pairs(products):
+      let typesLen = types.len
+
+      if typesLen > 1:
+        productsCombined.add(product & " (" & types.join(", ") & ")")
+      elif typesLen == 1:
+        productsCombined.add(product & " " & types[0])
+      else:
+        productsCombined.add(product)
+
+    let productsString = productsCombined.join(", ")
 
-    let productsString = products.join(", ")
 
     result["JOURNEYS"].add(%* {
         "ID":                  id,

@@ -86,7 +96,7 @@ proc renderJourneys*(data: CacheObject): JsonNode =
         "ARRIVAL_HAS_DELAY":   arrivalHasDelay,
         "ARRIVAL_DELAY":       arrivalDelay,
         "DEPARTURE":           formatTime(departureTime, ""),
-        "DURATION":            formatDuration(duration),
+        "DURATION":            formatDuration(arrivalTime - departureTime),
         "DEPARTURE_HAS_DELAY": departureHasDelay,
         "DEPARTURE_DELAY":     departureDelay,
         "CHANGES_DUR":         changesDuration,