ctucx.git: oeffi-web

[nimlang] oeffisearch fork that works without javascript

commit e4115a0f7f7f5499363f2b2133a98c5325dab8f3
parent 59c8255ca0f93f338ef3bab4bbe6d6f1e0db90fc
Author: Leah (ctucx) <leah@ctu.cx>
Date: Thu, 21 Jan 2021 21:37:51 +0100

renderJourney: refactor code
1 file changed, 47 insertions(+), 76 deletions(-)
M
src/formaters/renderJourney.nim
|
123
++++++++++++++++++++++++++++++-------------------------------------------------
diff --git a/src/formaters/renderJourney.nim b/src/formaters/renderJourney.nim
@@ -2,46 +2,45 @@ import json, tables, options, math
 import ../types, ../cache_types
 import formaters
 
+template getPlannedOrPrognosedTime(obj: untyped): int64 =
+  if not obj.prognosedTime.isNone: 
+    obj.prognosedTime.get
+  else:
+    obj.plannedTime
+
+template stopGetPlannedOrPrognosedTime(obj: untyped): int64 =
+  if not obj.prognosedTime.isNone: 
+    obj.prognosedTime.get
+  elif not obj.plannedTime.isNone:
+    obj.plannedTime.get
+  else:
+    0
+
 proc renderJourney*(data: CacheObject, id: string): JsonNode = 
   result = newJObject()
   
   if not data.journeys.hasKey(id):
     raise newException(notFoundException, "Diese Reise konnte nicht gefunden werden.")
 
-  var journey = data.journeys[id]
+  let journey = data.journeys[id]
 
   let departureLeg    = journey.legs[0]
   let arrivalLeg      = journey.legs[journey.legs.len-1]
+  let departureTime   = getPlannedOrPrognosedTime(departureLeg.departure) 
+  let arrivalTime     = getPlannedOrPrognosedTime(arrivalLeg.arrival)
 
   var changes:          int64
   var changesDuration:  int64
-
-  var departureTime:    int64 
-  var arrivalTime:      int64
-
   var lastArrival:      LegPart 
 
-  if not departureLeg.departure.prognosedTime.isNone: 
-    departureTime = departureLeg.departure.prognosedTime.get
-  else:
-    departureTime = departureLeg.departure.plannedTime
-
-  if not arrivalLeg.arrival.prognosedTime.isNone: 
-    arrivalTime = arrivalLeg.arrival.prognosedTime.get
-  else:
-    arrivalTime = arrivalLeg.arrival.plannedTime
-
-  let duration = arrivalTime - departureTime
-
-
-  result.add("REQ_ID",   %data.reqId)
-  result.add("ID",       %id)
-  result.add("FROM",     %formatPoint(departureLeg.departure.point))
-  result.add("TO",       %formatPoint(arrivalLeg.arrival.point))
-  result.add("DATE",     %formatTime(departureTime, "date"))
-  result.add("DURATION", %formatDuration(duration))
-  result.add("CHANGES",  %0)
-  result.add("LEGS",     newJArray())
+  result["REQ_ID"]   = %data.reqId
+  result["ID"]       = %id
+  result["FROM"]     = %formatPoint(departureLeg.departure.point)
+  result["TO"]       = %formatPoint(arrivalLeg.arrival.point)
+  result["DATE"]     = %formatTime(departureTime, "date")
+  result["DURATION"] = %formatDuration(arrivalTime - departureTime)
+  result["CHANGES"]  = %0
+  result["LEGS"]     = newJArray()
 
   for leg in journey.legs:
     if leg.isWalking != false:

@@ -60,47 +59,27 @@ proc renderJourney*(data: CacheObject, id: string): JsonNode =
         })
 
     else:
-      var arrivalTime:   int64
-      var departureTime: int64
-      var duration:      int64
+      let arrivalTime     = getPlannedOrPrognosedTime(leg.arrival)
+      let departureTime   = getPlannedOrPrognosedTime(leg.departure)
 
-      if lastArrival.plannedTime != 0:
-        if not leg.departure.prognosedTime.isNone: 
-          departureTime  = leg.departure.prognosedTime.get
-        else:
-          departureTime  = leg.departure.plannedTime
-
-        if not lastArrival.prognosedTime.isNone: 
-          arrivalTime  = lastArrival.prognosedTime.get
-        else:
-          arrivalTime  = lastArrival.plannedTime
+      var legDuration:    int64
+      var additionalName: string
 
-        duration = departureTime - arrivalTime
+      # is this leg a change?
+      if lastArrival.plannedTime != 0:
+        let lastArrivalTime = getPlannedOrPrognosedTime(lastArrival)
 
         result["LEGS"].add(%* {
             "IS_CHANGE": true,
-            "DURATION":  formatDuration(duration)
+            "DURATION":  formatDuration(departureTime - lastArrivalTime)
           })
 
-      if not leg.departure.prognosedTime.isNone: 
-        departureTime  = leg.departure.prognosedTime.get
-      else:
-        departureTime  = leg.departure.plannedTime
-
-      if not leg.arrival.prognosedTime.isNone: 
-        arrivalTime  = leg.arrival.prognosedTime.get
-      else:
-        arrivalTime  = leg.arrival.plannedTime
-
-      duration = arrivalTime - departureTime
-
+      legDuration = arrivalTime - departureTime
 
       lastArrival = leg.arrival
-      inc(changes)
-
-      var line = leg.line.get.name
+      
       if not leg.line.get.additionalName.isNone():
-        line &= " (" & leg.line.get.additionalName.get & ")"
+        additionalName = leg.line.get.additionalName.get
     
       var remarks = newJArray()
       if not leg.remarks.isNone():

@@ -111,8 +90,8 @@ proc renderJourney*(data: CacheObject, id: string): JsonNode =
       var stopovers = newJArray()
       if not leg.stopovers.isNone():
         for stopover in leg.stopovers.get:
-          var departureTime:         int64 
-          var arrivalTime:           int64
+          let stopDepartureTime    = stopGetPlannedOrPrognosedTime(stopover.departure)
+          let stopArrivalTime      = stopGetPlannedOrPrognosedTime(stopover.arrival)
 
           var departureDelay:        string
           var arrivalDelay:          string

@@ -126,16 +105,6 @@ proc renderJourney*(data: CacheObject, id: string): JsonNode =
           var platform:              string
           var platformHasChanged:    bool
 
-          if not stopover.departure.prognosedTime.isNone: 
-            departureTime  = stopover.departure.prognosedTime.get
-          elif not stopover.departure.plannedTime.isNone:
-            departureTime  = stopover.departure.plannedTime.get
-
-          if not stopover.arrival.prognosedTime.isNone: 
-            arrivalTime  = stopover.arrival.prognosedTime.get
-          elif not stopover.arrival.plannedTime.isNone:
-            arrivalTime = stopover.arrival.plannedTime.get
-
           if stopover.departure.plannedTime.isSome and stopover.departure.prognosedTime.isSome:
             let delay = floorDiv((stopover.departure.prognosedTime.get - stopover.departure.plannedTime.get), 60)
 

@@ -145,7 +114,7 @@ proc renderJourney*(data: CacheObject, id: string): JsonNode =
             if delay > 0:
               departureDelay = "+"
 
-            departureDelay &= $delay
+            departureDelay & $delay
 
           if stopover.arrival.plannedTime.isSome and stopover.arrival.prognosedTime.isSome:
             let delay = floorDiv((stopover.arrival.prognosedTime.get - stopover.arrival.plannedTime.get), 60)

@@ -158,13 +127,13 @@ proc renderJourney*(data: CacheObject, id: string): JsonNode =
 
             arrivalDelay &= $delay
 
-          if departureTime > 0:
-            departureTimeFormated = formatTime(departureTime, "")
+          if stopArrivalTime > 0:
+            departureTimeFormated = formatTime(stopDepartureTime, "")
           else:
             departureTimeFormated = "-"
 
-          if arrivalTime > 0:
-            arrivalTimeFormated = formatTime(arrivalTime, "")
+          if stopArrivalTime > 0:
+            arrivalTimeFormated = formatTime(stopArrivalTime, "")
           else:
             arrivalTimeFormated = "-"
 

@@ -201,13 +170,15 @@ proc renderJourney*(data: CacheObject, id: string): JsonNode =
               "PLATFORM_HAS_CHANGED": platformHasChanged
             })
 
+      inc(changes)
 
       result["LEGS"].add(%* {
           "IS_VEHICLE":      true,
           "IS_CANCELLED":    leg.cancelled,
-          "LINE":            line,
+          "LINE":            leg.line.get.name,
+          "ADDITIOAL_NAME":  additionalName,
           "DIRECTION":       leg.direction.get,
-          "LEG_DURATION":    %formatDuration(duration),
+          "LEG_DURATION":    %formatDuration(legDuration),
           "ARRIVAL_POINT":   formatPoint(leg.arrival.point),
           "DEPARTURE_POINT": formatPoint(leg.departure.point),
           "STOPOVERS":       stopovers,