First, if you have 5 sets of data it would be better to model each as a remote data. You can take a look remotedata-http for more information. This assumes that you might want to display partial data (data from some of the 5 endpoints as it arrives). If you want an all or nothing approach, you might also look into Http.task and Task.sequence.
Now, on to the filtering. This logic looks to me like it belongs in the backend BUT, if you don’t have access to the backend and there is no way to call it with some timestamp parameter, you can still do the filtering in the decoding step.
import Json.Decode as Json
type alias Timestamp =
{ timestamp : Int
, value : Int
}
oneHour : Int
oneHour = 3600000
timestampDecoder : Decoder Timestamp
timestampDecoder =
Json.map2 Timestamp
(Json.field "timestamp" Json.int)
(Json.field "value" Json.int)
oneHourFilter : Posix -> List Timestamp -> List Timestamp
oneHourFilter now timestamps =
List.filter (\t -> now + oneHour > t.timestamp ) timestamps
timestampListDecoder : Int -> Decoder (List Timestamp)
timestampListDecoder now =
Json.list timestampDecoder
|> Json.map (oneHourFilter now)
You would then get the current time and call the endpoints using (timestampListDecoder now)
getTimestamps : Int -> Cmd Msg
getTimestamps now =
Http.get
{ url = "https://your-host.com/api/some-timestamps"
, expect = Http.expectJson GotTimestamps (timestampListDecoder now)
}