|
4 | 4 | import androidx.annotation.Nullable; |
5 | 5 | import androidx.annotation.WorkerThread; |
6 | 6 |
|
| 7 | +import org.openimis.imisclaims.network.exception.HttpException; |
7 | 8 | import org.openimis.imisclaims.network.request.BaseFHIRGetPaginatedRequest; |
8 | 9 | import org.openimis.imisclaims.network.response.PaginatedResponse; |
| 10 | +import org.openimis.imisclaims.tools.Log; |
9 | 11 |
|
10 | 12 | import java.util.ArrayList; |
11 | 13 | import java.util.Collection; |
| 14 | +import java.util.Collections; |
12 | 15 | import java.util.List; |
13 | 16 |
|
14 | 17 | public class PaginatedResponseUtils { |
| 18 | + private static final String LOG_TAG = "FHIR_SYNC_TOLERANCE"; |
15 | 19 |
|
16 | 20 | private PaginatedResponseUtils() { |
17 | 21 | throw new IllegalAccessError("This constructor is private"); |
@@ -50,10 +54,154 @@ public static <T, U> List<U> downloadAll( |
50 | 54 | return list; |
51 | 55 | } |
52 | 56 |
|
| 57 | + @NonNull |
| 58 | + @WorkerThread |
| 59 | + public static <T, U> DownloadResult<U> downloadAllSkipFailedPages( |
| 60 | + @NonNull String endpoint, |
| 61 | + @NonNull RequestExecutor<T> executor, |
| 62 | + @Nullable Mapper.Transformer<T, U> transformer, |
| 63 | + int maxPages, |
| 64 | + int maxConsecutiveFailures |
| 65 | + ) throws Exception { |
| 66 | + int page = 0; |
| 67 | + int consecutiveFailures = 0; |
| 68 | + boolean hasMore = true; |
| 69 | + boolean hasSkippedPages = false; |
| 70 | + List<U> list = new ArrayList<>(); |
| 71 | + List<Integer> skippedPages = new ArrayList<>(); |
| 72 | + List<Integer> recoveredPages = new ArrayList<>(); |
| 73 | + Mapper<T, U> mapper = transformer != null ? new Mapper<>(transformer) : null; |
| 74 | + |
| 75 | + while (hasMore && page < maxPages) { |
| 76 | + final int displayPage = page + 1; |
| 77 | + try { |
| 78 | + PaginatedResponse<T> response = executor.download(page); |
| 79 | + if (mapper != null) { |
| 80 | + list.addAll(mapper.map(response.getValue())); |
| 81 | + } else { |
| 82 | + list.addAll((Collection<? extends U>) response.getValue()); |
| 83 | + } |
| 84 | + hasMore = response.hasMore(); |
| 85 | + consecutiveFailures = 0; |
| 86 | + if (hasSkippedPages) { |
| 87 | + recoveredPages.add(displayPage); |
| 88 | + } |
| 89 | + } catch (Exception exception) { |
| 90 | + skippedPages.add(displayPage); |
| 91 | + hasSkippedPages = true; |
| 92 | + consecutiveFailures++; |
| 93 | + logSkippedPage(endpoint, displayPage, exception); |
| 94 | + if (consecutiveFailures >= maxConsecutiveFailures) { |
| 95 | + Log.w( |
| 96 | + LOG_TAG, |
| 97 | + String.format( |
| 98 | + "endpoint=%s action=stop reason=maxConsecutiveFailures reached skippedPages=%s", |
| 99 | + endpoint, |
| 100 | + skippedPages |
| 101 | + ) |
| 102 | + ); |
| 103 | + break; |
| 104 | + } |
| 105 | + } |
| 106 | + page++; |
| 107 | + } |
| 108 | + |
| 109 | + if (page >= maxPages && hasMore) { |
| 110 | + Log.w( |
| 111 | + LOG_TAG, |
| 112 | + String.format( |
| 113 | + "endpoint=%s action=stop reason=maxPages reached maxPages=%s skippedPages=%s", |
| 114 | + endpoint, |
| 115 | + maxPages, |
| 116 | + skippedPages |
| 117 | + ) |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + Log.i( |
| 122 | + LOG_TAG, |
| 123 | + String.format( |
| 124 | + "endpoint=%s summary skippedPages=%s recoveredPages=%s totalItems=%s", |
| 125 | + endpoint, |
| 126 | + skippedPages, |
| 127 | + recoveredPages, |
| 128 | + list.size() |
| 129 | + ) |
| 130 | + ); |
| 131 | + return new DownloadResult<>(list, skippedPages, recoveredPages); |
| 132 | + } |
| 133 | + |
| 134 | + private static void logSkippedPage(@NonNull String endpoint, int page, @NonNull Exception exception) { |
| 135 | + String errorType = exception.getClass().getSimpleName(); |
| 136 | + if (exception instanceof HttpException) { |
| 137 | + HttpException httpException = (HttpException) exception; |
| 138 | + Log.w( |
| 139 | + LOG_TAG, |
| 140 | + String.format( |
| 141 | + "endpoint=%s page=%s action=skip errorType=%s code=%s message=%s", |
| 142 | + endpoint, |
| 143 | + page, |
| 144 | + errorType, |
| 145 | + httpException.getCode(), |
| 146 | + exception.getMessage() |
| 147 | + ) |
| 148 | + ); |
| 149 | + } else { |
| 150 | + Log.w( |
| 151 | + LOG_TAG, |
| 152 | + String.format( |
| 153 | + "endpoint=%s page=%s action=skip errorType=%s message=%s", |
| 154 | + endpoint, |
| 155 | + page, |
| 156 | + errorType, |
| 157 | + exception.getMessage() |
| 158 | + ) |
| 159 | + ); |
| 160 | + } |
| 161 | + } |
| 162 | + |
53 | 163 | public interface RequestExecutor<T> { |
54 | 164 |
|
55 | 165 | @NonNull |
56 | 166 | @WorkerThread |
57 | 167 | PaginatedResponse<T> download(int page) throws Exception; |
58 | 168 | } |
| 169 | + |
| 170 | + public static class DownloadResult<U> { |
| 171 | + @NonNull |
| 172 | + private final List<U> items; |
| 173 | + @NonNull |
| 174 | + private final List<Integer> skippedPages; |
| 175 | + @NonNull |
| 176 | + private final List<Integer> recoveredPages; |
| 177 | + |
| 178 | + public DownloadResult( |
| 179 | + @NonNull List<U> items, |
| 180 | + @NonNull List<Integer> skippedPages, |
| 181 | + @NonNull List<Integer> recoveredPages |
| 182 | + ) { |
| 183 | + this.items = items; |
| 184 | + this.skippedPages = skippedPages; |
| 185 | + this.recoveredPages = recoveredPages; |
| 186 | + } |
| 187 | + |
| 188 | + @NonNull |
| 189 | + public List<U> getItems() { |
| 190 | + return items; |
| 191 | + } |
| 192 | + |
| 193 | + @NonNull |
| 194 | + public List<Integer> getSkippedPages() { |
| 195 | + return Collections.unmodifiableList(skippedPages); |
| 196 | + } |
| 197 | + |
| 198 | + @NonNull |
| 199 | + public List<Integer> getRecoveredPages() { |
| 200 | + return Collections.unmodifiableList(recoveredPages); |
| 201 | + } |
| 202 | + |
| 203 | + public boolean hasSkippedPages() { |
| 204 | + return !skippedPages.isEmpty(); |
| 205 | + } |
| 206 | + } |
59 | 207 | } |
0 commit comments