|
| 1 | +#' Sync config sources with MongoDB |
| 2 | +#' |
| 3 | +#' Compares the sources listed in the config object |
| 4 | +#' with the MongoDB "sources" collection. |
| 5 | +#' Adds new sources, marks removed ones, and if a source with an existing |
| 6 | +#' ID has changed (name or URL), it raises a warning, marks the old entry |
| 7 | +#' as removed, and inserts the updated version. |
| 8 | +#' |
| 9 | +#' @param config The loaded configuration list (as from config.yaml) |
| 10 | +#' @export |
| 11 | +sync_sources_with_mongo <- function(config) { |
| 12 | + |
| 13 | + con <- credentials(collection="sources") |
| 14 | + |
| 15 | + config_sources <- map_df(config$sources, function(src) { |
| 16 | + tibble( |
| 17 | + source_id = as.character(src$id), |
| 18 | + source_name = as.character(src$name), |
| 19 | + source_url = as.character(src$url) |
| 20 | + ) |
| 21 | + }) |
| 22 | + |
| 23 | + # read current MongoDB sources |
| 24 | + mongo_sources <- tryCatch({ |
| 25 | + con$find('{}', fields = '{"_id":0}') |
| 26 | + }, error = function(e) { |
| 27 | + stop("Failed to fetch data from MongoDB 'sources' collection: ", e$message, call. = FALSE) |
| 28 | + }) |
| 29 | + |
| 30 | + now <- Sys.time() |
| 31 | + |
| 32 | + # detect updated sources (same id, changed name or url) |
| 33 | + updated_sources <- inner_join(config_sources, mongo_sources, by = "source_id") %>% |
| 34 | + filter( |
| 35 | + .data$source_name.x != .data$source_name.y | |
| 36 | + .data$source_url.x != .data$source_url.y |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | + if (nrow(updated_sources) > 0) { |
| 41 | + |
| 42 | + msg <- glue( |
| 43 | + "Source update detected for {nrow(updated_sources)} source(s).\n", |
| 44 | + "The following sources have the same source_id but different name or URL:\n\n", |
| 45 | + paste0( |
| 46 | + "- source_id: ", updated_sources$source_id, "\n", |
| 47 | + " old name: ", updated_sources$source_name.y, "\n", |
| 48 | + " new name: ", updated_sources$source_name.x, "\n", |
| 49 | + " old url: ", updated_sources$source_url.y, "\n", |
| 50 | + " new url: ", updated_sources$source_url.x, |
| 51 | + collapse = "\n\n" |
| 52 | + ), |
| 53 | + "\n\nPlease update your config file. A changed source should use a NEW source_id." |
| 54 | + ) |
| 55 | + |
| 56 | + flog.error(msg) |
| 57 | + stop(msg, call. = FALSE) |
| 58 | + } else { |
| 59 | + flog.debug("No updated sources found.") |
| 60 | + } |
| 61 | + |
| 62 | + # detect new sources |
| 63 | + new_sources <- anti_join(config_sources, mongo_sources, by = "source_id") |
| 64 | + |
| 65 | + if (nrow(new_sources) > 0) { |
| 66 | + new_sources <- mutate( |
| 67 | + new_sources, |
| 68 | + date_added = now, |
| 69 | + date_removed = as.POSIXct(NA, tz = "UTC") |
| 70 | + ) |
| 71 | + con$insert(new_sources) |
| 72 | + flog.info( |
| 73 | + glue("Inserted {nrow(new_sources)} new source(s): {paste(new_sources$source_name, collapse = ', ')}") |
| 74 | + ) |
| 75 | + } else { |
| 76 | + flog.debug("No new sources found.") |
| 77 | + } |
| 78 | + |
| 79 | + # detect removed sources |
| 80 | + removed_sources <- anti_join(mongo_sources, config_sources, by = "source_id") %>% |
| 81 | + filter(is.na(.data$date_removed)) |
| 82 | + |
| 83 | + if (nrow(removed_sources) > 0) { |
| 84 | + for (sid in removed_sources$source_id) { |
| 85 | + con$update( |
| 86 | + query = paste0('{"source_id": "', sid, '"}'), |
| 87 | + update = paste0('{"$set": {"date_removed": {"$date": "', format(now, "%Y-%m-%dT%H:%M:%S%z"), '"}}}') |
| 88 | + ) |
| 89 | + } |
| 90 | + flog.info( |
| 91 | + glue("Marked {nrow(removed_sources)} source(s) as removed: {paste(removed_sources$source_id, collapse = ', ')}") |
| 92 | + ) |
| 93 | + } else { |
| 94 | + flog.debug("No removed sources found.") |
| 95 | + } |
| 96 | + |
| 97 | + flog.info("MongoDB sync complete.") |
| 98 | +} |
0 commit comments