-
- fault_rate = fault_rate()
- put_stat(:fault_rate, fault_rate)
- fault_rate_allowance = Config.get([:populate_hashtags_table, :fault_rate_allowance], 0)
-
- cond do
- fault_rate == 0 ->
- set_complete()
-
- is_float(fault_rate) and fault_rate <= fault_rate_allowance ->
- message = """
- Done with fault rate of #{fault_rate} which doesn't exceed #{fault_rate_allowance}.
- Putting data migration to manual fix mode. Check `retry_failed/0`.
- """
-
- Logger.warn("#{__MODULE__}: #{message}")
- update_status(:manual, message)
- on_complete(data_migration())
-
- true ->
- message = "Too many failures. Check data_migration_failed_ids records / `retry_failed/0`."
- Logger.error("#{__MODULE__}: #{message}")
- update_status(:failed, message)
- end
-
- persist_state()
- {:noreply, state}
- end
-
- def fault_rate do
- with failures_count when is_integer(failures_count) <- failures_count() do
- failures_count / Enum.max([get_stat(:affected_count, 0), 1])
- else
- _ -> :error
- end
- end
-
- defp records_per_second do
- get_stat(:iteration_processed_count, 0) / Enum.max([running_time(), 1])
- end
-
- defp running_time do
- NaiveDateTime.diff(NaiveDateTime.utc_now(), get_stat(:started_at, NaiveDateTime.utc_now()))
- end
-
- @hashtags_objects_cleanup_query """
- DELETE FROM hashtags_objects WHERE object_id IN
- (SELECT DISTINCT objects.id FROM objects
- JOIN hashtags_objects ON hashtags_objects.object_id = objects.id LEFT JOIN activities
- ON COALESCE(activities.data->'object'->>'id', activities.data->>'object') =
- (objects.data->>'id')
- AND activities.data->>'type' = 'Create'
- WHERE activities.id IS NULL);
- """
-
- @hashtags_cleanup_query """
- DELETE FROM hashtags WHERE id IN
- (SELECT hashtags.id FROM hashtags
- LEFT OUTER JOIN hashtags_objects
- ON hashtags_objects.hashtag_id = hashtags.id
- WHERE hashtags_objects.hashtag_id IS NULL);
- """
-
- @doc """
- Deletes `hashtags_objects` for legacy objects not asoociated with Create activity.
- Also deletes unreferenced `hashtags` records (might occur after deletion of `hashtags_objects`).
- """
- def delete_non_create_activities_hashtags do
- {:ok, %{num_rows: hashtags_objects_count}} =
- Repo.query(@hashtags_objects_cleanup_query, [], timeout: :infinity)
-
- {:ok, %{num_rows: hashtags_count}} =
- Repo.query(@hashtags_cleanup_query, [], timeout: :infinity)
-
- {:ok, hashtags_objects_count, hashtags_count}