Use workaround for MATOMO bug with users by country
Resolves
https://gitlab.ids-mannheim.de/tfnd/IDS-Nutzungsdatenreport/-/issues/14
Change-Id: I72015a50d145fd503b69523ba0f770ddd66e421f
diff --git a/R/usercountry.R b/R/usercountry.R
index 0ea414c..62075e1 100644
--- a/R/usercountry.R
+++ b/R/usercountry.R
@@ -6,7 +6,7 @@
#'
#' @inheritParams matomoQuery
#'
-#' @importFrom stringr str_replace str_replace_all
+#' @importFrom stringr str_replace str_replace_all str_split
#'
#' @examples
#' \dontrun{
@@ -21,12 +21,31 @@
filter_limit = 100,
accessToken = getAccessToken(matomoUrl)
) {
- matomoQuery(matomoUrl = matomoUrl,
- siteId = siteId,
- period = period,
- date = str_replace_all(str_replace_all(date, " *UTC", ""), "-00", "-01"),
- removeFirst = FALSE,
- removeLast = FALSE,
- accessToken = accessToken,
- getMethod = "UserCountry.getCountry")
+ result <- list()
+ MAX_RETRIES <- 5
+ retry_count <- 0
+
+ while (length(result) == 0 && retry_count < MAX_RETRIES) {
+ result <- matomoQuery(matomoUrl = matomoUrl,
+ siteId = siteId,
+ period = period,
+ date = str_replace_all(str_replace_all(date, " *UTC", ""), "-00", "-01"),
+ removeFirst = FALSE,
+ removeLast = FALSE,
+ accessToken = accessToken,
+ getMethod = "UserCountry.getCountry",
+ ignoreEmptyResult = retry_count < MAX_RETRIES)
+
+ if (length(result) == 0 && retry_count < MAX_RETRIES) {
+ msg <- paste0("UserCountry.getCountry returned an empty list (known MATOMO bug). Changing date range from ", date, " to ")
+ retry_count <- retry_count + 1
+ startend <- str_split(date, ",", simplify = T)
+ l <- str_split(startend[1], "-", simplify = T)
+ l[3] <- sprintf("%02d", as.numeric(l[3]) + 1)
+ date <- paste0(paste0(l, collapse = "-"), ",", startend[2])
+ msg <- paste0(msg, date, "\n")
+ warning(msg)
+ }
+ }
+ return(result)
}