@curless/hotel-storefront 0.84.8 → 0.86.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (229) hide show
  1. package/dist/config.d.ts +35 -2
  2. package/dist/config.d.ts.map +1 -1
  3. package/dist/config.js.map +1 -1
  4. package/dist/huazhu/ari-sync.d.ts +93 -6
  5. package/dist/huazhu/ari-sync.d.ts.map +1 -1
  6. package/dist/huazhu/ari-sync.js +303 -28
  7. package/dist/huazhu/ari-sync.js.map +1 -1
  8. package/dist/huazhu/backend.d.ts +9 -0
  9. package/dist/huazhu/backend.d.ts.map +1 -1
  10. package/dist/huazhu/backend.js +62 -22
  11. package/dist/huazhu/backend.js.map +1 -1
  12. package/dist/huazhu/catalog.d.ts +58 -1
  13. package/dist/huazhu/catalog.d.ts.map +1 -1
  14. package/dist/huazhu/catalog.js +318 -15
  15. package/dist/huazhu/catalog.js.map +1 -1
  16. package/dist/huazhu/live-asks.d.ts +63 -0
  17. package/dist/huazhu/live-asks.d.ts.map +1 -0
  18. package/dist/huazhu/live-asks.js +127 -0
  19. package/dist/huazhu/live-asks.js.map +1 -0
  20. package/dist/huazhu/make.d.ts.map +1 -1
  21. package/dist/huazhu/make.js +50 -4
  22. package/dist/huazhu/make.js.map +1 -1
  23. package/dist/server.d.ts +1 -1
  24. package/dist/server.js +1 -1
  25. package/dist/tools/accept-terms.d.ts +4 -0
  26. package/dist/tools/accept-terms.d.ts.map +1 -0
  27. package/dist/tools/accept-terms.js +26 -0
  28. package/dist/tools/accept-terms.js.map +1 -0
  29. package/dist/tools/add-booking-addon.d.ts +4 -0
  30. package/dist/tools/add-booking-addon.d.ts.map +1 -0
  31. package/dist/tools/add-booking-addon.js +44 -0
  32. package/dist/tools/add-booking-addon.js.map +1 -0
  33. package/dist/tools/add-to-trip.d.ts +4 -0
  34. package/dist/tools/add-to-trip.d.ts.map +1 -0
  35. package/dist/tools/add-to-trip.js +49 -0
  36. package/dist/tools/add-to-trip.js.map +1 -0
  37. package/dist/tools/all-tools.d.ts +1342 -0
  38. package/dist/tools/all-tools.d.ts.map +1 -0
  39. package/dist/tools/all-tools.js +954 -0
  40. package/dist/tools/all-tools.js.map +1 -0
  41. package/dist/tools/answer.d.ts +3 -0
  42. package/dist/tools/answer.d.ts.map +1 -0
  43. package/dist/tools/answer.js +104 -0
  44. package/dist/tools/answer.js.map +1 -0
  45. package/dist/tools/args.d.ts +149 -0
  46. package/dist/tools/args.d.ts.map +1 -0
  47. package/dist/tools/args.js +176 -0
  48. package/dist/tools/args.js.map +1 -0
  49. package/dist/tools/base-output-schemas.d.ts +3 -0
  50. package/dist/tools/base-output-schemas.d.ts.map +1 -0
  51. package/dist/tools/base-output-schemas.js +380 -0
  52. package/dist/tools/base-output-schemas.js.map +1 -0
  53. package/dist/tools/book-picked.d.ts +4 -0
  54. package/dist/tools/book-picked.d.ts.map +1 -0
  55. package/dist/tools/book-picked.js +14 -0
  56. package/dist/tools/book-picked.js.map +1 -0
  57. package/dist/tools/booking-answer.d.ts +24 -0
  58. package/dist/tools/booking-answer.d.ts.map +1 -0
  59. package/dist/tools/booking-answer.js +485 -0
  60. package/dist/tools/booking-answer.js.map +1 -0
  61. package/dist/tools/buy-shop-item.d.ts +4 -0
  62. package/dist/tools/buy-shop-item.d.ts.map +1 -0
  63. package/dist/tools/buy-shop-item.js +45 -0
  64. package/dist/tools/buy-shop-item.js.map +1 -0
  65. package/dist/tools/call.d.ts +11 -0
  66. package/dist/tools/call.d.ts.map +1 -0
  67. package/dist/tools/call.js +2 -0
  68. package/dist/tools/call.js.map +1 -0
  69. package/dist/tools/cancel-facts.d.ts +281 -0
  70. package/dist/tools/cancel-facts.d.ts.map +1 -0
  71. package/dist/tools/cancel-facts.js +309 -0
  72. package/dist/tools/cancel-facts.js.map +1 -0
  73. package/dist/tools/cancel.d.ts +4 -0
  74. package/dist/tools/cancel.d.ts.map +1 -0
  75. package/dist/tools/cancel.js +364 -0
  76. package/dist/tools/cancel.js.map +1 -0
  77. package/dist/tools/card-addons.d.ts +4 -0
  78. package/dist/tools/card-addons.d.ts.map +1 -0
  79. package/dist/tools/card-addons.js +76 -0
  80. package/dist/tools/card-addons.js.map +1 -0
  81. package/dist/tools/change-booking.d.ts +4 -0
  82. package/dist/tools/change-booking.d.ts.map +1 -0
  83. package/dist/tools/change-booking.js +145 -0
  84. package/dist/tools/change-booking.js.map +1 -0
  85. package/dist/tools/clear-trip.d.ts +4 -0
  86. package/dist/tools/clear-trip.d.ts.map +1 -0
  87. package/dist/tools/clear-trip.js +10 -0
  88. package/dist/tools/clear-trip.js.map +1 -0
  89. package/dist/tools/compare.d.ts +4 -0
  90. package/dist/tools/compare.d.ts.map +1 -0
  91. package/dist/tools/compare.js +114 -0
  92. package/dist/tools/compare.js.map +1 -0
  93. package/dist/tools/current-trip.d.ts +70 -0
  94. package/dist/tools/current-trip.d.ts.map +1 -0
  95. package/dist/tools/current-trip.js +261 -0
  96. package/dist/tools/current-trip.js.map +1 -0
  97. package/dist/tools/deps.d.ts +229 -0
  98. package/dist/tools/deps.d.ts.map +1 -0
  99. package/dist/tools/deps.js +2 -0
  100. package/dist/tools/deps.js.map +1 -0
  101. package/dist/tools/follow-ups.d.ts +120 -0
  102. package/dist/tools/follow-ups.d.ts.map +1 -0
  103. package/dist/tools/follow-ups.js +369 -0
  104. package/dist/tools/follow-ups.js.map +1 -0
  105. package/dist/tools/forget-preferences.d.ts +4 -0
  106. package/dist/tools/forget-preferences.d.ts.map +1 -0
  107. package/dist/tools/forget-preferences.js +24 -0
  108. package/dist/tools/forget-preferences.js.map +1 -0
  109. package/dist/tools/get-hotel.d.ts +4 -0
  110. package/dist/tools/get-hotel.d.ts.map +1 -0
  111. package/dist/tools/get-hotel.js +324 -0
  112. package/dist/tools/get-hotel.js.map +1 -0
  113. package/dist/tools/hotel-choices.d.ts +132 -0
  114. package/dist/tools/hotel-choices.d.ts.map +1 -0
  115. package/dist/tools/hotel-choices.js +558 -0
  116. package/dist/tools/hotel-choices.js.map +1 -0
  117. package/dist/tools/itinerary.d.ts +4 -0
  118. package/dist/tools/itinerary.d.ts.map +1 -0
  119. package/dist/tools/itinerary.js +148 -0
  120. package/dist/tools/itinerary.js.map +1 -0
  121. package/dist/tools/list-booking-addons.d.ts +4 -0
  122. package/dist/tools/list-booking-addons.d.ts.map +1 -0
  123. package/dist/tools/list-booking-addons.js +21 -0
  124. package/dist/tools/list-booking-addons.js.map +1 -0
  125. package/dist/tools/list-bookings.d.ts +4 -0
  126. package/dist/tools/list-bookings.d.ts.map +1 -0
  127. package/dist/tools/list-bookings.js +119 -0
  128. package/dist/tools/list-bookings.js.map +1 -0
  129. package/dist/tools/list-extras.d.ts +4 -0
  130. package/dist/tools/list-extras.d.ts.map +1 -0
  131. package/dist/tools/list-extras.js +90 -0
  132. package/dist/tools/list-extras.js.map +1 -0
  133. package/dist/tools/list-hotels.d.ts +4 -0
  134. package/dist/tools/list-hotels.d.ts.map +1 -0
  135. package/dist/tools/list-hotels.js +891 -0
  136. package/dist/tools/list-hotels.js.map +1 -0
  137. package/dist/tools/list-shop-items.d.ts +4 -0
  138. package/dist/tools/list-shop-items.d.ts.map +1 -0
  139. package/dist/tools/list-shop-items.js +34 -0
  140. package/dist/tools/list-shop-items.js.map +1 -0
  141. package/dist/tools/list-shop-orders.d.ts +4 -0
  142. package/dist/tools/list-shop-orders.d.ts.map +1 -0
  143. package/dist/tools/list-shop-orders.js +22 -0
  144. package/dist/tools/list-shop-orders.js.map +1 -0
  145. package/dist/tools/look-up.d.ts +4 -0
  146. package/dist/tools/look-up.d.ts.map +1 -0
  147. package/dist/tools/look-up.js +170 -0
  148. package/dist/tools/look-up.js.map +1 -0
  149. package/dist/tools/output-schemas.d.ts +645 -0
  150. package/dist/tools/output-schemas.d.ts.map +1 -0
  151. package/dist/tools/output-schemas.js +503 -0
  152. package/dist/tools/output-schemas.js.map +1 -0
  153. package/dist/tools/paid-lines.d.ts +718 -0
  154. package/dist/tools/paid-lines.d.ts.map +1 -0
  155. package/dist/tools/paid-lines.js +359 -0
  156. package/dist/tools/paid-lines.js.map +1 -0
  157. package/dist/tools/pay-with-union-pay.d.ts +4 -0
  158. package/dist/tools/pay-with-union-pay.d.ts.map +1 -0
  159. package/dist/tools/pay-with-union-pay.js +112 -0
  160. package/dist/tools/pay-with-union-pay.js.map +1 -0
  161. package/dist/tools/payment-words.d.ts +269 -0
  162. package/dist/tools/payment-words.d.ts.map +1 -0
  163. package/dist/tools/payment-words.js +365 -0
  164. package/dist/tools/payment-words.js.map +1 -0
  165. package/dist/tools/pick-room.d.ts +4 -0
  166. package/dist/tools/pick-room.d.ts.map +1 -0
  167. package/dist/tools/pick-room.js +23 -0
  168. package/dist/tools/pick-room.js.map +1 -0
  169. package/dist/tools/policies.d.ts +4 -0
  170. package/dist/tools/policies.d.ts.map +1 -0
  171. package/dist/tools/policies.js +74 -0
  172. package/dist/tools/policies.js.map +1 -0
  173. package/dist/tools/pricing.d.ts +55 -0
  174. package/dist/tools/pricing.d.ts.map +1 -0
  175. package/dist/tools/pricing.js +260 -0
  176. package/dist/tools/pricing.js.map +1 -0
  177. package/dist/tools/put-room-line.d.ts +12 -0
  178. package/dist/tools/put-room-line.d.ts.map +1 -0
  179. package/dist/tools/put-room-line.js +314 -0
  180. package/dist/tools/put-room-line.js.map +1 -0
  181. package/dist/tools/quote-card.d.ts +234 -0
  182. package/dist/tools/quote-card.d.ts.map +1 -0
  183. package/dist/tools/quote-card.js +375 -0
  184. package/dist/tools/quote-card.js.map +1 -0
  185. package/dist/tools/refund-booking.d.ts +4 -0
  186. package/dist/tools/refund-booking.d.ts.map +1 -0
  187. package/dist/tools/refund-booking.js +112 -0
  188. package/dist/tools/refund-booking.js.map +1 -0
  189. package/dist/tools/request-booking.d.ts +4 -0
  190. package/dist/tools/request-booking.d.ts.map +1 -0
  191. package/dist/tools/request-booking.js +5 -0
  192. package/dist/tools/request-booking.js.map +1 -0
  193. package/dist/tools/request-words.d.ts +238 -0
  194. package/dist/tools/request-words.d.ts.map +1 -0
  195. package/dist/tools/request-words.js +487 -0
  196. package/dist/tools/request-words.js.map +1 -0
  197. package/dist/tools/room-choices.d.ts +64 -0
  198. package/dist/tools/room-choices.d.ts.map +1 -0
  199. package/dist/tools/room-choices.js +348 -0
  200. package/dist/tools/room-choices.js.map +1 -0
  201. package/dist/tools/session-notes.d.ts +164 -0
  202. package/dist/tools/session-notes.d.ts.map +1 -0
  203. package/dist/tools/session-notes.js +412 -0
  204. package/dist/tools/session-notes.js.map +1 -0
  205. package/dist/tools/sign-in.d.ts +4 -0
  206. package/dist/tools/sign-in.d.ts.map +1 -0
  207. package/dist/tools/sign-in.js +72 -0
  208. package/dist/tools/sign-in.js.map +1 -0
  209. package/dist/tools/sign-out.d.ts +4 -0
  210. package/dist/tools/sign-out.d.ts.map +1 -0
  211. package/dist/tools/sign-out.js +13 -0
  212. package/dist/tools/sign-out.js.map +1 -0
  213. package/dist/tools/terms-answer.d.ts +114 -0
  214. package/dist/tools/terms-answer.d.ts.map +1 -0
  215. package/dist/tools/terms-answer.js +475 -0
  216. package/dist/tools/terms-answer.js.map +1 -0
  217. package/dist/tools/tool-set.d.ts +1474 -0
  218. package/dist/tools/tool-set.d.ts.map +1 -0
  219. package/dist/tools/tool-set.js +497 -0
  220. package/dist/tools/tool-set.js.map +1 -0
  221. package/dist/tools/view-trip.d.ts +4 -0
  222. package/dist/tools/view-trip.d.ts.map +1 -0
  223. package/dist/tools/view-trip.js +7 -0
  224. package/dist/tools/view-trip.js.map +1 -0
  225. package/dist/tools.d.ts +13 -2089
  226. package/dist/tools.d.ts.map +1 -1
  227. package/dist/tools.js +11 -10685
  228. package/dist/tools.js.map +1 -1
  229. package/package.json +1 -1
@@ -43,8 +43,34 @@ export const SYNC_WINDOWS = [
43
43
  ];
44
44
  /** What a hotel sells (`/ari/products`: rooms x rates, how many days ahead) - as 0.77.4 kept it. */
45
45
  export const SYNC_PRODUCTS_EVERY_MS = 6 * HOUR;
46
- /** One job (a window's offers + inventory, or a hotel's products) starts this long after the last, at the soonest. */
47
- export const SYNC_GAP_MS = 5_000;
46
+ /**
47
+ * Jobs a minute at most (a job is a window's offers + inventory - two calls - or a hotel's products -
48
+ * one): the default, kept as it was until Huazhu answers how many calls its price interface takes
49
+ * (question 42, docs/hotel-connectors.md). `data.api.ariSync.maxPerMinute` sets it (0.85.0).
50
+ */
51
+ export const SYNC_DEFAULT_PER_MINUTE = 12;
52
+ /** One job (a window's offers + inventory, or a hotel's products) starts this long after the last, at the soonest - at the default budget. */
53
+ export const SYNC_GAP_MS = 60_000 / SYNC_DEFAULT_PER_MINUTE;
54
+ /** The most a minute may be set to: past this the limit on calls to Huazhu (2 a second, `OUTBOUND_DEFAULTS`) is what answers. */
55
+ export const SYNC_MAX_PER_MINUTE = 60;
56
+ /**
57
+ * How many hotels the copy keeps in memory at once; the rest stay on disk and are read when a
58
+ * guest looks at them or a job pulls them (0.85.0: one hotel's file is ~240 KB for 24 products, 400
59
+ * of them were 250 MB of heap).
60
+ */
61
+ export const SYNC_RESIDENT_HOTELS = 120;
62
+ /** The hotels the schedule covers are chosen again this often (and when the catalog changes). */
63
+ export const SYNC_PLAN_EVERY_MS = 5 * 60_000;
64
+ /** Jobs a day that the schedule plans to use; the rest of the budget is for retries and `soon` pulls. */
65
+ export const SYNC_PLAN_SHARE = 0.9;
66
+ /** A hotel looked at within this long is "hot": its first pulls go before the other hotels'. */
67
+ export const SYNC_HOT_MS = 24 * HOUR;
68
+ /** The `seen` list is written at most this often, and keeps hotels seen within this long. */
69
+ const SEEN_SAVE_EVERY_MS = 5 * 60_000;
70
+ const SEEN_KEEP_MS = 14 * 24 * HOUR;
71
+ const SEEN_KEEP_MAX = 5_000;
72
+ /** How far the far windows (31 days on) and what a hotel sells are stretched when the budget cannot cover every hotel. */
73
+ export const SYNC_STRETCH = 2;
48
74
  /** A job our own limit on calls did not send at all is tried again this long after. */
49
75
  export const SYNC_NOT_SENT_RETRY_MS = 60_000;
50
76
  /**
@@ -62,7 +88,10 @@ const LOOK_AT_LEAST_EVERY_MS = 60_000;
62
88
  */
63
89
  export const syncServedForMs = (everyMs) => 2 * everyMs + HOUR;
64
90
  const KINDS = ['P', 'W0', 'W1', 'W2', 'W3'];
91
+ /** The rule's own period for a job - never shorter than this, whatever the budget. */
65
92
  const everyOf = (k) => k === 'P' ? SYNC_PRODUCTS_EVERY_MS : SYNC_WINDOWS[windowIndex(k)].everyMs;
93
+ /** Jobs a day, per hotel, with the far windows and the products stretched `stretch` times (the nearest week never is). */
94
+ export const jobsPerDay = (stretch) => KINDS.reduce((n, k) => n + (24 * HOUR) / (everyOf(k) * (k === 'W0' ? 1 : stretch)), 0);
66
95
  const windowIndex = (k) => Number(k.slice(1));
67
96
  /** Where in its period a hotel's job falls, past the hotel's own place: its windows do not start together. */
68
97
  const SHIFT = { W0: 0, W1: 0.2, W2: 0.4, W3: 0.6, P: 0.8 };
@@ -131,6 +160,23 @@ export class AriSync {
131
160
  /** Jobs not sent at all, and when to try them again. */
132
161
  retryAt = new Map();
133
162
  passes = new Map();
163
+ /** When a guest last looked at each hotel (0.85.0): what the schedule covers first. */
164
+ seenAt = new Map();
165
+ seenSavedAt = 0;
166
+ seenDirty = false;
167
+ /** The hotels the schedule pulls, sorted - all of them when the budget allows (`replan`). */
168
+ covered = [];
169
+ coverIndex = new Map();
170
+ stretch = 1;
171
+ plannedAt;
172
+ plannedFor;
173
+ /** Hotels with their chunks in memory, least recently used first. */
174
+ resident = new Set();
175
+ /** The hotel whose job is running: its chunks stay in memory until it has written them. */
176
+ busy;
177
+ seenFailing = false;
178
+ gapMs;
179
+ maxPerMinute;
134
180
  lastFull = new Map();
135
181
  ranked = [];
136
182
  rankOf = new Map();
@@ -154,7 +200,11 @@ export class AriSync {
154
200
  fellBackCount = 0;
155
201
  constructor(opts) {
156
202
  this.opts = opts;
203
+ const per = opts.maxPerMinute ?? SYNC_DEFAULT_PER_MINUTE;
204
+ this.maxPerMinute = Math.min(SYNC_MAX_PER_MINUTE, Math.max(1, per));
205
+ this.gapMs = Math.ceil(60_000 / this.maxPerMinute);
157
206
  this.load();
207
+ this.loadSeen();
158
208
  }
159
209
  log(line) {
160
210
  (this.opts.log ?? console.log)(`[huazhu sync] ${line}`);
@@ -167,7 +217,7 @@ export class AriSync {
167
217
  */
168
218
  ari(hotelId, start, end) {
169
219
  const k = this.kept.get(hotelId);
170
- if (!k?.chunks.length || end < start)
220
+ if (!k || end < start || !this.chunksLoaded(hotelId, k).length)
171
221
  return undefined;
172
222
  const t = this.opts.now();
173
223
  const owners = [];
@@ -177,7 +227,7 @@ export class AriSync {
177
227
  if (c.start <= d && d <= c.end && (!best || c.at > best.at))
178
228
  best = c;
179
229
  const w = best ? SYNC_WINDOWS[best.w] : undefined;
180
- if (!best || !w || t - best.at > syncServedForMs(w.everyMs))
230
+ if (!best || !w || t - best.at > syncServedForMs(w.everyMs * this.stretchOf(best.w)))
181
231
  return undefined;
182
232
  owners.push(best);
183
233
  }
@@ -205,7 +255,7 @@ export class AriSync {
205
255
  /** What a hotel sells and how far ahead, from the copy - or undefined when it holds none fresh enough. */
206
256
  products(hotelId) {
207
257
  const p = this.kept.get(hotelId)?.products;
208
- if (!p || this.opts.now() - p.at > syncServedForMs(SYNC_PRODUCTS_EVERY_MS))
258
+ if (!p || this.opts.now() - p.at > syncServedForMs(SYNC_PRODUCTS_EVERY_MS * this.stretch))
209
259
  return undefined;
210
260
  return { value: p.value, sellDays: p.sellDays };
211
261
  }
@@ -216,6 +266,27 @@ export class AriSync {
216
266
  else
217
267
  this.fellBackCount += 1;
218
268
  }
269
+ /**
270
+ * A guest looked at this hotel (a list tile, its page, a comparison, a plan, a booking): what the
271
+ * schedule covers first when the budget cannot cover every hotel. Kept across restarts.
272
+ */
273
+ seen(hotelId) {
274
+ if (!SAFE_ID.test(hotelId))
275
+ return;
276
+ const t = this.opts.now();
277
+ // A look a minute is as good as every look; no write for the rest.
278
+ if (t - (this.seenAt.get(hotelId) ?? 0) < 60_000)
279
+ return;
280
+ this.seenAt.set(hotelId, t);
281
+ this.seenDirty = true;
282
+ }
283
+ /** How many periods a window's pulls are stretched (the nearest week never is). */
284
+ stretchOf(w) {
285
+ return w === 0 ? 1 : this.stretch;
286
+ }
287
+ every(k) {
288
+ return everyOf(k) * (k === 'W0' ? 1 : this.stretch);
289
+ }
219
290
  /**
220
291
  * Huazhu's prebook (or booking) said a stay's price moved, or no room is left: the windows the
221
292
  * stay is in are asked at the first time the rule allows - a period after they were last asked
@@ -259,9 +330,13 @@ export class AriSync {
259
330
  for (const id of [...this.kept.keys()])
260
331
  if (!this.rankOf.has(id))
261
332
  this.drop(id);
333
+ for (const id of [...this.seenAt.keys()])
334
+ if (!this.rankOf.has(id))
335
+ this.seenAt.delete(id);
262
336
  }
263
337
  drop(hotelId) {
264
338
  this.kept.delete(hotelId);
339
+ this.resident.delete(hotelId);
265
340
  for (const k of KINDS) {
266
341
  this.soonJobs.delete(`${hotelId}|${k}`);
267
342
  this.retryAt.delete(`${hotelId}|${k}`);
@@ -273,11 +348,69 @@ export class AriSync {
273
348
  // Gone from the schedule either way.
274
349
  }
275
350
  }
276
- /** The job's own times: its place in the hotels, and its window, of each period. */
351
+ /**
352
+ * The hotels the schedule pulls, chosen again every `SYNC_PLAN_EVERY_MS` and when the catalog
353
+ * changes (0.85.0, robin 11-26: 15,000 hotels, and a budget of calls that holds 380).
354
+ *
355
+ * The budget is `maxPerMinute` jobs a minute, `SYNC_PLAN_SHARE` of it planned. A hotel at Huazhu's
356
+ * own pace is `jobsPerDay(1)` = 45 jobs a day. If every hotel fits, every hotel is pulled, as ever.
357
+ * If not, the far windows and what a hotel sells are pulled `SYNC_STRETCH` times less often
358
+ * (never more often than the rule: a longer period is allowed, and a copy that old is served for
359
+ * twice its own period) and the hotels covered are those guests looked at most recently, then
360
+ * those already in the copy, then in id order. The others are not pulled - a copy older than its
361
+ * time would only be dropped when asked for - and their prices are asked when a guest looks (a
362
+ * capped few at a time, backend.ts); a hotel looked at comes into the schedule at the next plan.
363
+ */
364
+ replan(t) {
365
+ this.plannedAt = t;
366
+ this.plannedFor = this.hotelsAt;
367
+ const all = this.hotels();
368
+ const cap = this.maxPerMinute * 60 * 24 * SYNC_PLAN_SHARE;
369
+ // Stretched once the hotels do not fit; back only when they fit with a margin (no flapping at the edge).
370
+ const fits1 = all.length * jobsPerDay(1) <= cap * (this.stretch > 1 ? 0.85 : 1);
371
+ this.stretch = fits1 ? 1 : SYNC_STRETCH;
372
+ const room = Math.max(1, Math.floor(cap / jobsPerDay(this.stretch)));
373
+ let chosen = all;
374
+ if (all.length > room) {
375
+ const hasCopy = (id) => {
376
+ const k = this.kept.get(id);
377
+ return k && (k.ok.W0 !== undefined || k.ok.P !== undefined) ? 1 : 0;
378
+ };
379
+ chosen = [...all]
380
+ .sort((a, b) => (this.seenAt.get(b) ?? 0) - (this.seenAt.get(a) ?? 0) ||
381
+ hasCopy(b) - hasCopy(a) ||
382
+ (this.rankOf.get(a) ?? 0) - (this.rankOf.get(b) ?? 0))
383
+ .slice(0, room)
384
+ .sort();
385
+ }
386
+ const before = this.covered.length;
387
+ this.covered = chosen;
388
+ this.coverIndex = new Map(chosen.map((id, i) => [id, i]));
389
+ if (before !== chosen.length && (before > 0 || chosen.length < all.length))
390
+ this.log(`budget ${this.maxPerMinute} jobs a minute: covering ${chosen.length} of ${all.length} hotels${this.stretch > 1 ? `, far windows and products every ${this.stretch}x their period` : ''}`);
391
+ // A hotel left out for longer than its copy could be served is dropped from the copy (its file too).
392
+ const oldest = syncServedForMs(24 * HOUR * this.stretch);
393
+ for (const [id, k] of [...this.kept]) {
394
+ if (this.coverIndex.has(id))
395
+ continue;
396
+ // ⚠️ THE LAST ASK, ANSWERED OR NOT (`tried`): a hotel that never answered was asked all the same,
397
+ // and forgetting that asks it again at once when it comes back - sooner than the rule.
398
+ const last = Math.max(0, ...KINDS.map((x) => Math.max(k.ok[x] ?? 0, k.tried[x] ?? 0)));
399
+ if (t - last > oldest)
400
+ this.drop(id);
401
+ }
402
+ }
403
+ ensurePlan(t) {
404
+ if (this.plannedAt === undefined ||
405
+ this.plannedFor !== this.hotelsAt ||
406
+ t - this.plannedAt >= SYNC_PLAN_EVERY_MS)
407
+ this.replan(t);
408
+ }
409
+ /** The job's own times: its place in the hotels covered, and its window, of each period. */
277
410
  phaseOf(hotelId, k) {
278
- const n = Math.max(1, this.ranked.length);
279
- const rank = this.rankOf.get(hotelId) ?? 0;
280
- return ((rank + SHIFT[k]) / n) * everyOf(k);
411
+ const n = Math.max(1, this.covered.length);
412
+ const rank = this.coverIndex.get(hotelId) ?? 0;
413
+ return ((rank + SHIFT[k]) / n) * this.every(k);
281
414
  }
282
415
  /**
283
416
  * When a job is due. Never asked: at the start (the first pull goes in turn, `pick`). Asked
@@ -293,8 +426,10 @@ export class AriSync {
293
426
  const last = kept?.tried[k];
294
427
  if (last === undefined)
295
428
  return { due: Math.max(this.startedAt ?? t, retry), first: true };
296
- const every = everyOf(k);
297
- const earliest = last + every;
429
+ // ⚠️ THE RULE'S OWN PERIOD IS THE SHORTEST (`everyOf`); a stretched one (budget short) is longer.
430
+ const rule = everyOf(k);
431
+ const every = this.every(k);
432
+ const earliest = last + rule;
298
433
  if (this.soonJobs.has(key))
299
434
  return { due: Math.max(earliest, retry), first: false };
300
435
  // ⚠️ A FAILED ASK IS ASKED AGAIN AT THE FIRST TIME THE RULE ALLOWS (0.80.2), not at the job's
@@ -306,16 +441,22 @@ export class AriSync {
306
441
  if (ok === undefined || ok < last)
307
442
  return { due: Math.max(earliest, retry), first: false };
308
443
  const phase = this.phaseOf(hotelId, k);
309
- const slot = phase + Math.ceil((earliest - every * SYNC_SLOT_GRACE - phase) / every) * every;
310
- return { due: Math.max(slot, earliest, retry), first: false };
444
+ const slot = phase + Math.ceil((last + every - every * SYNC_SLOT_GRACE - phase) / every) * every;
445
+ return { due: Math.max(slot, last + every, retry), first: false };
311
446
  }
312
- /** The job to run at `t`: the first pulls in turn, then the one due longest. */
447
+ /** The job to run at `t`: the first pulls in turn (hot hotels' first), then the one due longest. */
313
448
  pick(t) {
314
449
  let best;
315
450
  const before = (a, b) => {
316
451
  if (a.first !== b.first)
317
452
  return a.first;
318
453
  if (a.first) {
454
+ // A hotel guests looked at in the last day is pulled before the rest - whatever its window.
455
+ if (a.hot !== b.hot)
456
+ return a.hot;
457
+ // ...the one looked at last, first - all of its jobs, then the next one's.
458
+ if (a.hot && a.seen !== b.seen)
459
+ return a.seen > b.seen;
319
460
  if (STAGE[a.k] !== STAGE[b.k])
320
461
  return STAGE[a.k] < STAGE[b.k];
321
462
  const ra = this.rankOf.get(a.hotelId) ?? 0;
@@ -324,17 +465,21 @@ export class AriSync {
324
465
  return ra < rb;
325
466
  return ORDER[a.k] < ORDER[b.k];
326
467
  }
327
- return a.due < b.due;
468
+ // Overdue by the most first; the nearer window first when they are as late.
469
+ return a.due !== b.due ? a.due < b.due : ORDER[a.k] < ORDER[b.k];
328
470
  };
329
- for (const hotelId of this.ranked)
471
+ for (const hotelId of this.covered) {
472
+ const seen = this.seenAt.get(hotelId) ?? Number.NEGATIVE_INFINITY;
473
+ const hot = t - seen <= SYNC_HOT_MS;
330
474
  for (const k of KINDS) {
331
475
  const { due, first } = this.dueOf(hotelId, k, t);
332
476
  if (due > t)
333
477
  continue;
334
- const c = { hotelId, k, due, first };
478
+ const c = { hotelId, k, due, first, hot, seen };
335
479
  if (!best || before(c, best))
336
480
  best = c;
337
481
  }
482
+ }
338
483
  return best ? { hotelId: best.hotelId, k: best.k } : undefined;
339
484
  }
340
485
  /** When `tick` will next have a job to start - for a test's clock, and nothing else. */
@@ -345,26 +490,30 @@ export class AriSync {
345
490
  if (hotels.length === 0)
346
491
  return undefined;
347
492
  const t = this.opts.now();
493
+ this.ensurePlan(t);
348
494
  let min = Number.POSITIVE_INFINITY;
349
- for (const hotelId of hotels)
495
+ for (const hotelId of this.covered)
350
496
  for (const k of KINDS)
351
497
  min = Math.min(min, this.dueOf(hotelId, k, t).due);
352
498
  return Math.max(min, this.nextStartAt, this.opts.pausedUntil());
353
499
  }
354
500
  /**
355
501
  * One look at the schedule: starts the job most due, if one is and the last started at least
356
- * `SYNC_GAP_MS` ago, and waits for it. Nothing while a job runs, while price calls are held
357
- * after a 流控, or before the catalog has hotels. Answers whether a job ran.
502
+ * the budget's gap ago (`maxPerMinute`; 5 s at the default), and waits for it. Nothing while a
503
+ * job runs, while price calls are held after a 流控, or before the catalog has hotels. Answers
504
+ * whether a job ran.
358
505
  */
359
506
  async tick() {
360
507
  if (this.running || this.stopped)
361
508
  return false;
362
509
  const t = this.opts.now();
510
+ this.saveSeen(t);
363
511
  if (t < this.nextStartAt || t < this.opts.pausedUntil())
364
512
  return false;
365
513
  if (this.hotels().length === 0)
366
514
  return false;
367
515
  this.reconcile();
516
+ this.ensurePlan(t);
368
517
  if (this.startedAt === undefined) {
369
518
  this.startedAt = t;
370
519
  for (const k of KINDS)
@@ -374,7 +523,7 @@ export class AriSync {
374
523
  if (!job)
375
524
  return false;
376
525
  this.running = true;
377
- this.nextStartAt = t + SYNC_GAP_MS;
526
+ this.nextStartAt = t + this.gapMs;
378
527
  const run = this.run(job.hotelId, job.k).finally(() => {
379
528
  this.running = false;
380
529
  });
@@ -390,7 +539,7 @@ export class AriSync {
390
539
  start() {
391
540
  if (this.timer || this.stopped)
392
541
  return;
393
- this.log(`on: ${SYNC_WINDOWS.map((w) => `${w.name} ${w.from}-${w.to} days every ${w.everyMs / HOUR} h`).join(', ')}, products every ${SYNC_PRODUCTS_EVERY_MS / HOUR} h; ${this.kept.size} hotel(s) in the copy at ${this.opts.dir}`);
542
+ this.log(`on: ${SYNC_WINDOWS.map((w) => `${w.name} ${w.from}-${w.to} days every ${w.everyMs / HOUR} h`).join(', ')}, products every ${SYNC_PRODUCTS_EVERY_MS / HOUR} h, at most ${this.maxPerMinute} jobs a minute; ${this.kept.size} hotel(s) in the copy at ${this.opts.dir}`);
394
543
  this.lookLater(0);
395
544
  }
396
545
  lookLater(ms) {
@@ -418,16 +567,62 @@ export class AriSync {
418
567
  clearTimeout(this.timer);
419
568
  this.timer = undefined;
420
569
  await this.current?.catch(() => { });
570
+ this.saveSeen(this.opts.now(), true);
421
571
  }
422
572
  // ----------------------------------------------------------------- jobs ----
423
573
  keptOf(hotelId) {
424
574
  let k = this.kept.get(hotelId);
425
575
  if (!k) {
426
- k = { tried: {}, ok: {}, why: {}, chunks: [], bytes: 0 };
576
+ k = { tried: {}, ok: {}, why: {}, chunks: [], bytes: 0, loaded: true };
427
577
  this.kept.set(hotelId, k);
428
578
  }
429
579
  return k;
430
580
  }
581
+ /**
582
+ * A hotel's chunks, in memory: read from its file when they are not (the copy holds
583
+ * `SYNC_RESIDENT_HOTELS` hotels' at a time, the least recently used given up first). A file that
584
+ * cannot be read leaves the hotel as if never pulled - its jobs come due as at a first start.
585
+ */
586
+ chunksLoaded(hotelId, k) {
587
+ if (!k.loaded) {
588
+ try {
589
+ const raw = readFileSync(this.fileOf(hotelId), 'utf8');
590
+ const f = JSON.parse(raw);
591
+ k.chunks = Array.isArray(f.chunks) ? f.chunks.filter(isChunk) : [];
592
+ k.bytes = Buffer.byteLength(raw);
593
+ k.loaded = true;
594
+ }
595
+ catch (err) {
596
+ // ⚠️ NO FILE IS EMPTY; A FILE THAT CANNOT BE READ IS NOT (0.85.0 review): left unloaded, and never
597
+ // written over - what is on the disk may be all the prices there are.
598
+ if (err?.code === 'ENOENT') {
599
+ k.chunks = [];
600
+ k.loaded = true;
601
+ }
602
+ else {
603
+ k.chunks = [];
604
+ return k.chunks;
605
+ }
606
+ }
607
+ }
608
+ this.resident.delete(hotelId);
609
+ this.resident.add(hotelId);
610
+ const most = Math.max(1, this.opts.residentHotels ?? SYNC_RESIDENT_HOTELS);
611
+ for (const id of this.resident) {
612
+ if (this.resident.size <= most)
613
+ break;
614
+ // ⚠️ NEVER THE HOTEL A JOB IS PULLING: its chunks are what the job writes back (0.85.0 review).
615
+ if (id === hotelId || id === this.busy)
616
+ continue;
617
+ const other = this.kept.get(id);
618
+ this.resident.delete(id);
619
+ if (other) {
620
+ other.chunks = [];
621
+ other.loaded = false;
622
+ }
623
+ }
624
+ return k.chunks;
625
+ }
431
626
  sentNow(n) {
432
627
  const t = this.opts.now();
433
628
  for (let i = 0; i < n; i += 1)
@@ -444,6 +639,23 @@ export class AriSync {
444
639
  async run(hotelId, k) {
445
640
  const key = `${hotelId}|${k}`;
446
641
  const kept = this.keptOf(hotelId);
642
+ this.busy = hotelId;
643
+ try {
644
+ await this.runLoaded(hotelId, k, key, kept);
645
+ }
646
+ finally {
647
+ this.busy = undefined;
648
+ }
649
+ }
650
+ async runLoaded(hotelId, k, key, kept) {
651
+ this.chunksLoaded(hotelId, kept);
652
+ if (!kept.loaded) {
653
+ // The hotel's file is there and cannot be read: nothing is pulled over it, and nothing was asked.
654
+ if (!this.retryAt.has(key))
655
+ this.log(`${hotelId} ${labelOf(k)}: its file cannot be read; not pulled over it, tried again every minute`);
656
+ this.retryAt.set(key, this.opts.now() + SYNC_NOT_SENT_RETRY_MS);
657
+ return;
658
+ }
447
659
  let failure;
448
660
  let sent = 0;
449
661
  /** The dates a window asked for, for the log: " 2027-01-20..2027-02-18". */
@@ -573,8 +785,8 @@ export class AriSync {
573
785
  pass.ok.add(hotelId);
574
786
  else
575
787
  pass.ok.delete(hotelId);
576
- const hotels = this.hotels();
577
- if (!hotels.every((id) => pass.tried.has(id)))
788
+ const hotels = this.covered;
789
+ if (hotels.length === 0 || !hotels.every((id) => pass.tried.has(id)))
578
790
  return;
579
791
  const done = hotels.filter((id) => pass.ok.has(id)).length;
580
792
  this.log(`${labelOf(k)} ${done}/${hotels.length} hotels in ${Math.round((t - pass.startedAt) / 60_000)} min, ${pass.flow} 流控`);
@@ -590,6 +802,11 @@ export class AriSync {
590
802
  const kept = this.kept.get(hotelId);
591
803
  if (!kept)
592
804
  return;
805
+ // ⚠️ NEVER WRITTEN WITHOUT ITS CHUNKS: read back first, and left alone if they cannot be.
806
+ if (!kept.loaded)
807
+ this.chunksLoaded(hotelId, kept);
808
+ if (!kept.loaded)
809
+ return;
593
810
  const file = {
594
811
  version: 1,
595
812
  hotelId,
@@ -650,8 +867,10 @@ export class AriSync {
650
867
  ok: timesOf(f.ok),
651
868
  why: whysOf(f.why),
652
869
  ...(isProducts(f.products) ? { products: f.products } : {}),
653
- chunks: Array.isArray(f.chunks) ? f.chunks.filter(isChunk) : [],
870
+ // Read again when wanted (`chunksLoaded`): 400 hotels' chunks were 250 MB of heap.
871
+ chunks: [],
654
872
  bytes: Buffer.byteLength(raw),
873
+ loaded: false,
655
874
  });
656
875
  }
657
876
  catch {
@@ -659,12 +878,56 @@ export class AriSync {
659
878
  }
660
879
  }
661
880
  }
881
+ /** The hotels guests looked at, kept beside the copy (`_seen.json`, which no hotel's file is named). */
882
+ seenFile() {
883
+ return join(this.opts.dir, '_seen.json');
884
+ }
885
+ loadSeen() {
886
+ try {
887
+ const f = JSON.parse(readFileSync(this.seenFile(), 'utf8'));
888
+ for (const [id, at] of Object.entries(f.seen ?? {}))
889
+ if (SAFE_ID.test(id) && typeof at === 'number' && Number.isFinite(at))
890
+ this.seenAt.set(id, at);
891
+ }
892
+ catch {
893
+ // Nothing seen yet.
894
+ }
895
+ }
896
+ /** Written when something changed, at most every five minutes (and once more at `stop`). */
897
+ saveSeen(t, force = false) {
898
+ if (!this.seenDirty || (!force && t - this.seenSavedAt < SEEN_SAVE_EVERY_MS))
899
+ return;
900
+ const keep = [...this.seenAt.entries()]
901
+ .filter(([, at]) => t - at <= SEEN_KEEP_MS)
902
+ .sort((a, b) => b[1] - a[1])
903
+ .slice(0, SEEN_KEEP_MAX);
904
+ try {
905
+ mkdirSync(this.opts.dir, { recursive: true });
906
+ const tmp = `${this.seenFile()}.${process.pid}.tmp`;
907
+ writeFileSync(tmp, JSON.stringify({ version: 1, seen: Object.fromEntries(keep) }));
908
+ renameSync(tmp, this.seenFile());
909
+ this.seenSavedAt = t;
910
+ this.seenDirty = false;
911
+ this.seenFailing = false;
912
+ }
913
+ catch (err) {
914
+ // Kept in memory and tried again at the next look; only a restart would forget it.
915
+ this.seenSavedAt = t - SEEN_SAVE_EVERY_MS + 60_000;
916
+ if (!this.seenFailing)
917
+ this.log(`the hotels guests looked at could not be written: ${err.message}`);
918
+ this.seenFailing = true;
919
+ }
920
+ }
662
921
  // --------------------------------------------------------------- health ----
663
922
  health() {
664
923
  const t = this.opts.now();
665
- const hotels = this.hotels();
924
+ const all = this.hotels();
925
+ // The hotels the schedule covers (all of them, unless the budget is short - see `replan`).
926
+ if (all.length && this.plannedAt === undefined)
927
+ this.replan(t);
928
+ const hotels = this.covered;
666
929
  const of = (k) => {
667
- const every = everyOf(k);
930
+ const every = this.every(k);
668
931
  let due = 0;
669
932
  let done = 0;
670
933
  let never = 0;
@@ -706,6 +969,18 @@ export class AriSync {
706
969
  return {
707
970
  on: true,
708
971
  hotels: hotels.length,
972
+ // The budget of calls and what it covers (0.85.0): `jobsPerDayWanted` is what every hotel in the
973
+ // catalog would take at Huazhu's own pace, `jobsPerDayBudget` what the budget gives.
974
+ budget: {
975
+ maxPerMinute: this.maxPerMinute,
976
+ hotelsInCatalog: all.length,
977
+ hotelsCovered: hotels.length,
978
+ farWindowsStretch: this.stretch,
979
+ jobsPerDayWanted: Math.round(all.length * jobsPerDay(1)),
980
+ jobsPerDayBudget: this.maxPerMinute * 60 * 24,
981
+ hotelsSeenLast24h: [...this.seenAt.values()].filter((at) => t - at <= SYNC_HOT_MS).length,
982
+ hotelsInMemory: this.resident.size,
983
+ },
709
984
  windows: { W0: of('W0'), W1: of('W1'), W2: of('W2'), W3: of('W3') },
710
985
  products: of('P'),
711
986
  callsLastHour: this.sent.length,