getPage
    :: Env
    -> Text
    -> Distribution.Distribution
    -> Counter.Counter
    -> Counter.Counter
    -> Counter.Counter
    -> (Maybe Text, Maybe Text)
    -> IO PageResult
getPage env bucketName items_returned_distribution zero_items_counter non_zero_items_counter requests_counter (start,end) = do
    let request = listObjectsV (BucketName bucketName) & lStartAfter .~ start
    response <- runResourceT . runAWST env $ timeout 10 $ send request
    let objects = filterEnd end $ view lrsContents response
    let objectCount = length objects
    Distribution.add items_returned_distribution (fromIntegral objectCount)
    if objectCount == 0
        then Counter.inc zero_items_counter
        else Counter.inc non_zero_items_counter
    Counter.inc requests_counter
    let nextSegment =
            if view lrsIsTruncated response == Just False
                then Nothing
                else if Fold.null objects
                         then Nothing
                         else let start' =
                                      view (oKey . _ObjectKey) $ last objects
                              in Just (start', end)
    return $ (fmap objectToS3Object objects, nextSegment)
  where
    filterEnd :: Maybe Text -> [Object] -> [Object]
    filterEnd Nothing x = x
    filterEnd (Just endKey) x =
        filter
            (\o ->
                  view (oKey . _ObjectKey) o <= endKey)
            x
getPage
    :: Env
    -> Text
    -> Distribution.Distribution
    -> Counter.Counter
    -> Counter.Counter
    -> Counter.Counter
    -> (Maybe Text, Maybe Text)
    -> IO PageResult
getPage env bucketName items_returned_distribution zero_items_counter non_zero_items_counter requests_counter (start,end) = do
    let request = listObjectsV (BucketName bucketName) & lStartAfter .~ start
    response <- runResourceT . runAWST env $ timeout 10 $ send request
    let objects = filterEnd end $ view lrsContents response
    let objectCount = length objects
    Distribution.add items_returned_distribution (fromIntegral objectCount)
    if objectCount == 0
        then Counter.inc zero_items_counter
        else Counter.inc non_zero_items_counter
    Counter.inc requests_counter
    let nextSegment =
            if view lrsIsTruncated response == Just False
                then Nothing
                else if Fold.null objects
                         then Nothing
                         else let start' =
                                      view (oKey . _ObjectKey) $ last objects
                              in Just (start', end)
    return $ (fmap objectToS3Object objects, nextSegment)
  where
    filterEnd :: Maybe Text -> [Object] -> [Object]
    filterEnd Nothing x = x
    filterEnd (Just endKey) x =
        filter
            (\o ->
                  view (oKey . _ObjectKey) o <= endKey)
            x
getPage env bucketName items_returned_distribution zero_items_counter non_zero_items_counter requests_counter (start,end) = do
    let request = listObjectsV (BucketName bucketName) & lStartAfter .~ start
    response <- runResourceT . runAWST env $ timeout 10 $ send request
    let objects = filterEnd end $ view lrsContents response
    let objectCount = length objects
    Distribution.add items_returned_distribution (fromIntegral objectCount)
    if objectCount == 0
        then Counter.inc zero_items_counter
        else Counter.inc non_zero_items_counter
    Counter.inc requests_counter
    let nextSegment =
            if view lrsIsTruncated response == Just False
                then Nothing
                else if Fold.null objects
                         then Nothing
                         else let start' =
                                      view (oKey . _ObjectKey) $ last objects
                              in Just (start', end)
    return $ (fmap objectToS3Object objects, nextSegment)
  where
    filterEnd :: Maybe Text -> [Object] -> [Object]
    filterEnd Nothing x = x
    filterEnd (Just endKey) x =
        filter
            (\o ->
                  view (oKey . _ObjectKey) o <= endKey)
            x