Reset and regenerate a report


Use this R example to reset a report and wait for it to be regenerated before requesting cached exports.

CONJOINTLY_TOKEN <- "YOUR_TOKEN" # Get a token from https://run.conjoint.ly/utilities/tokens

resetReport <- function(experiment_id, attempts = 40, TOKEN = CONJOINTLY_TOKEN) {

  require(httr)

  headers <- add_headers(
    `Authorization` = paste("Bearer", TOKEN),
    `Content-Type` = "application/json",
    `Accept` = "application/json",
    `X-Requested-With` = "XMLHttpRequest"
  )

  cat('Resetting the report...\n')
  resetRequest <- POST(
    paste0("https://api.conjoint.ly/api/report/", experiment_id, "/reset"),
    headers
  )
  stop_for_status(resetRequest)

  cat('Requesting regeneration of the report...\n')
  generateRequest <- POST(
    paste0("https://api.conjoint.ly/api/report/", experiment_id, "/generate"),
    headers
  ) |> content("parsed", encoding = "UTF-8")

  jobId <- generateRequest$data$id
  if (is.null(jobId)) {
    stop("Job ID is null")
  }

  # Wait for the job to be completed (repeat up to `attempts` times with a 2-second delay):
  for (i in seq_len(attempts)) {
    jobRequest <- GET(
      paste0("https://api.conjoint.ly/api/jobs/", jobId, "/get"),
      headers
    ) |> content("parsed", encoding = "UTF-8")

    if (jobRequest$data$status %in% c('completed', 'done')) {
      cat('Report has been reset and regenerated.\n')
      return(invisible(jobId))
    }

    cat('Waiting for the report to be regenerated...\n')
    Sys.sleep(2)
  }

  stop("Report was not regenerated in time")
}

resetReport(experiment_id) # Then re-request the export to get fresh data