vincentlowe

Usingreduce

Jun 17th, 2021
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. %dw 2.0
  2. output application/dw
  3.  
  4. import dw::modules::ComplexData
  5.  
  6. var airportDetails =
  7. readUrl("classpath://airportInfoTiny.csv","application/csv")
  8. groupBy $.IATA
  9.  
  10. var keysToRemove = [
  11. "openFlightsAirportId",
  12. "type",
  13. "source"
  14. ]
  15.  
  16. fun listKeys(anObject : Object) =
  17. anObject pluck $$
  18.  
  19. fun flightsJoinedWithAirportInfo(payload) =
  20. payload.flights map ( (flight) ->
  21. flight ++
  22. {
  23. airportInfo:
  24. airportDetails[flight.destination][0]
  25. //--["openFlightsAirportId", "type", "source"]
  26. -- keysToRemove
  27. }
  28. )
  29.  
  30. fun flightsJoinedWithAirportInfoV2(payload) =
  31. payload.flights as Array map (flight, index) ->
  32. //"flight$(index)":
  33. flight mapObject (value,key) ->
  34. key match {
  35. case "destination" -> joinAirportDetails(flight)
  36. else -> { (key): value }
  37. }
  38.  
  39. fun joinAirportDetails(flight) =
  40. destination: (
  41. {airportCode: flight.destination}
  42. ++
  43. (
  44. filterDetailsKeys(
  45. airportDetails[flight.destination][0], keysToRemove
  46. )
  47. )
  48. )
  49.  
  50. fun filterDetailsKeys( detailsObject, keysArray ) =
  51. detailsObject mapObject ( (destValue, destKey) ->
  52. if ( keysArray contains (destKey as String) ) {}
  53. else { (destKey): destValue }
  54. )
  55.  
  56. //fun buildJsonObject( objectArray, parentTag) =
  57. fun buildJsonObject( objectArray: Array<Object>, parentTag: String ) =
  58. {(
  59. objectArray map (element, index) -> {
  60. ( parentTag ++ index ): element
  61. }
  62. )}
  63.  
  64. var joinedFlightsXml = ComplexData::buildXmlObject(
  65. flightsJoinedWithAirportInfoV2( payload ), "flight"
  66. )
  67.  
  68. fun formatDataStructure (anyInput) =
  69. anyInput match {
  70. case is Null -> ""
  71. case is String -> anyInput
  72. case is Array -> anyInput map formatDataStructure($)
  73. case is Object ->
  74. anyInput mapObject (v,k) -> (dw::core::Strings::dasherize(k)):formatDataStructure(v)
  75. case is Number -> anyInput as enrichedNumber
  76. else -> anyInput
  77. }
  78.  
  79. type enrichedNumber = String {format: "+#,##0.00;-#"}
  80.  
  81. fun deltaFlightsKpi (flightsInput:Array) =
  82. flightsInput reduce (flight,acc={
  83. delta: {count: 0, price: 0, revenue: 0}
  84. }) ->
  85. flight.airlineName match {
  86. case delta if(lower(delta) contains "del") ->
  87. delta: {
  88. count: acc.delta.count + 1,
  89. price: acc.delta.price + flight.price,
  90. revenue: acc.delta.revenue + flight.availableSeats * flight.price
  91. }
  92. else -> acc
  93. }
  94.  
  95. ---
  96. //formatDataStructure(joinedFlightsXml)
  97.  
  98. deltaFlightsKpi(flightsJoinedWithAirportInfoV2( payload ))
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
Advertisement
Add Comment
Please, Sign In to add comment