github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/web/elm/src/Views/BuildDuration.elm (about)

     1  module Views.BuildDuration exposing (show, view)
     2  
     3  import Concourse
     4  import DateFormat
     5  import Duration exposing (Duration)
     6  import Html exposing (Html)
     7  import Html.Attributes exposing (class, title)
     8  import Time
     9  
    10  
    11  view : Time.Zone -> Concourse.BuildDuration -> Time.Posix -> Html a
    12  view timeZone duration now =
    13      Html.table [ class "dictionary build-duration" ] <|
    14          case ( duration.startedAt, duration.finishedAt ) of
    15              ( Nothing, Nothing ) ->
    16                  [ pendingLabel "pending" ]
    17  
    18              ( Just startedAt, Nothing ) ->
    19                  [ labeledDate
    20                      { label = "started"
    21                      , timeZone = timeZone
    22                      }
    23                      now
    24                      startedAt
    25                  ]
    26  
    27              ( Nothing, Just finishedAt ) ->
    28                  [ labeledDate
    29                      { label = "finished"
    30                      , timeZone = timeZone
    31                      }
    32                      now
    33                      finishedAt
    34                  ]
    35  
    36              ( Just startedAt, Just finishedAt ) ->
    37                  let
    38                      durationElmIssue =
    39                          -- https://github.com/elm-lang/elm-compiler/issues/1223
    40                          Duration.between startedAt finishedAt
    41                  in
    42                  [ labeledDate
    43                      { label = "started"
    44                      , timeZone = timeZone
    45                      }
    46                      now
    47                      startedAt
    48                  , labeledDate
    49                      { label = "finished"
    50                      , timeZone = timeZone
    51                      }
    52                      now
    53                      finishedAt
    54                  , labeledDuration "duration" durationElmIssue
    55                  ]
    56  
    57  
    58  show : Time.Posix -> Concourse.BuildDuration -> String
    59  show now =
    60      .startedAt
    61          >> Maybe.map ((\a -> Duration.between a now) >> Duration.format)
    62          >> Maybe.withDefault ""
    63  
    64  
    65  labeledDate :
    66      { label : String, timeZone : Time.Zone }
    67      -> Time.Posix
    68      -> Time.Posix
    69      -> Html a
    70  labeledDate { label, timeZone } now date =
    71      let
    72          ago =
    73              Duration.between date now
    74  
    75          verboseDate =
    76              DateFormat.format
    77                  [ DateFormat.monthNameAbbreviated
    78                  , DateFormat.text " "
    79                  , DateFormat.dayOfMonthNumber
    80                  , DateFormat.text " "
    81                  , DateFormat.yearNumber
    82                  , DateFormat.text " "
    83                  , DateFormat.hourFixed
    84                  , DateFormat.text ":"
    85                  , DateFormat.minuteFixed
    86                  , DateFormat.text ":"
    87                  , DateFormat.secondFixed
    88                  , DateFormat.text " "
    89                  , DateFormat.amPmUppercase
    90                  ]
    91                  timeZone
    92                  date
    93  
    94          relativeDate =
    95              Duration.format ago ++ " ago"
    96      in
    97      if ago < 24 * 60 * 60 * 1000 then
    98          Html.tr []
    99              [ Html.td [ class "dict-key" ] [ Html.text label ]
   100              , Html.td
   101                  [ title verboseDate, class "dict-value" ]
   102                  [ Html.span [] [ Html.text relativeDate ] ]
   103              ]
   104  
   105      else
   106          Html.tr []
   107              [ Html.td [ class "dict-key" ] [ Html.text label ]
   108              , Html.td
   109                  [ title relativeDate, class "dict-value" ]
   110                  [ Html.span [] [ Html.text verboseDate ] ]
   111              ]
   112  
   113  
   114  labeledDuration : String -> Duration -> Html a
   115  labeledDuration label duration =
   116      Html.tr []
   117          [ Html.td [ class "dict-key" ] [ Html.text label ]
   118          , Html.td [ class "dict-value" ] [ Html.span [] [ Html.text (Duration.format duration) ] ]
   119          ]
   120  
   121  
   122  pendingLabel : String -> Html a
   123  pendingLabel label =
   124      Html.tr []
   125          [ Html.td [ class "dict-key" ] [ Html.text label ]
   126          , Html.td [ class "dict-value" ] []
   127          ]