使用Enum.reduce Elixir增加日期,使用少量的miliseonds

时间:2017-08-30 07:02:30

标签: elixir ecto

我一直在使用Enum.reduce

Enum.reduce(email_array, {[], [], []}, fn email, {shares, share_requests, changes} = _acc ->

end)

我将在每个reducer上传递一个方法

CameraActivity.log_activity(caller, camera, "shared", %{with: email, ip: requester_ip}, done_at)

在done_at中,默认值为Ecto.DateTime.utc,但我想在每个Enum.reduce上增加此日期和时间。

我试图在Enum.reduce的最开头声明日期时间为done_at = Ecto.DateTime.utc,但我不知道我将如何在Enum.reduce中循环它,这样我可能会增加几个它的目的只是为了改变每次迭代的活动时间。任何帮助将不胜感激

Dogbert的更新

Enum.reduce(email_array, {[], [], []}, fn email, {shares, share_requests, changes} = _acc ->
  with {:found_user, sharee} <- ensure_user(email)
  do
    case CameraShare.create_share(camera, sharee, caller, params["rights"], params["message"]) do
      {:ok, camera_share} ->
        unless caller == sharee do
          send_email_notification(caller, camera, sharee.email, camera_share.message)
        end
        Camera.invalidate_user(sharee)
        Camera.invalidate_camera(camera)
        CameraActivity.log_activity(caller, camera, "shared", %{with: sharee.email, ip: requester_ip})
        {[camera_share | shares], share_requests, changes}
      {:error, changeset} ->
        {shares, share_requests, [attach_email_to_message(changeset, email) | changes]}
    end
  else
    {:not_found, email} ->
      case CameraShareRequest.create_share_request(camera, email, caller, params["rights"], params["message"]) do
        {:ok, camera_share_request} ->
          send_email_notification(caller, camera, email, camera_share_request.message, camera_share_request.key)
          CameraActivity.log_activity(caller, camera, "shared", %{with: email, ip: requester_ip})
          Intercom.intercom_activity(Application.get_env(:evercam_media, :create_intercom_user), get_user_model(email), get_user_agent(conn), requester_ip, "Shared-Non-Registered")
          {shares, [camera_share_request | share_requests], changes}
        {:error, changeset} ->
          {shares, share_requests, [attach_email_to_message(changeset, email) | changes]}
      end
  end
end)

1 个答案:

答案 0 :(得分:1)

您已在Enum.reduce/3来电中使用3元组作为累加器。您可以添加日期时间值作为元组中的第4项,并在每次迭代中递增它。例如,要将其递增5秒,您可以执行以下操作:

Enum.reduce(email_array, {[], [], [], Ecto.DateTime.utc}, fn email, {shares, share_requests, changes, datetime} = _acc ->
  # next_datetime is datetime + 5 seconds
  next_datetime = datetime |>
    Ecto.DateTime.to_erl |>
    :calendar.datetime_to_gregorian_seconds |>
    Kernel.+(5) |>
    :calendar.gregorian_seconds_to_datetime |>
    Ecto.DateTime.from_erl

  # `next_datetime` should be returned as the 4th value of each tuple below.
  # `datetime` can be used as the current value.

  with {:found_user, sharee} <- ensure_user(email)
  do
    case CameraShare.create_share(camera, sharee, caller, params["rights"], params["message"]) do
      {:ok, camera_share} ->
        ...
        {[camera_share | shares], share_requests, changes, next_datetime}
      {:error, changeset} ->
        {shares, share_requests, [attach_email_to_message(changeset, email) | changes], next_datetime}
    end
  else
    {:not_found, email} ->
      case CameraShareRequest.create_share_request(camera, email, caller, params["rights"], params["message"]) do
        {:ok, camera_share_request} ->
          ...
          {shares, [camera_share_request | share_requests], changes, next_datetime}
        {:error, changeset} ->
          {shares, share_requests, [attach_email_to_message(changeset, email) | changes], next_datetime}
      end
  end
end)