Skip to content

Commit d25ea85

Browse files
StockInfoProviderDigiKey: show remaining queries
1 parent db6fddc commit d25ea85

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

src/util/stock_info_provider_digikey.cpp

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
#include "util/util.hpp"
77
#include "util/win32_undef.hpp"
88
#include "preferences/preferences.hpp"
9+
#include "util/str_util.hpp"
910
#include <list>
1011
#include <iomanip>
12+
#include <atomic>
1113

1214
namespace horizon {
1315

@@ -71,6 +73,18 @@ std::string StockInfoProviderDigiKey::update_tokens_from_response(SQLite::Databa
7173
return access_token;
7274
}
7375

76+
static int remaining_from_headers(const HTTP::Client::ResponseHeaders &headers)
77+
{
78+
for (const auto &it : headers) {
79+
if (it.find("X-RateLimit-Remaining:") == 0) {
80+
std::string s = it.substr(22);
81+
trim(s);
82+
return std::stoi(s);
83+
}
84+
}
85+
return 0;
86+
}
87+
7488
class DigiKeyCacheManager {
7589
public:
7690
static std::shared_ptr<DigiKeyCacheManager> &get()
@@ -103,9 +117,10 @@ class DigiKeyCacheManager {
103117
}
104118
}
105119

106-
json query(const std::string &MPN, const std::string &manufacturer)
120+
std::pair<json, unsigned int> query(const std::string &MPN, const std::string &manufacturer)
107121
{
108122
std::unique_lock<std::mutex> lk(mutex);
123+
unsigned int n_remaining = 0;
109124

110125
json j;
111126
std::string access_token;
@@ -133,6 +148,11 @@ class DigiKeyCacheManager {
133148
}
134149
cache_db.execute("COMMIT");
135150
}
151+
catch (const std::exception &e) {
152+
std::cerr << e.what() << std::endl;
153+
cache_db.execute("ROLLBACK");
154+
return {};
155+
}
136156
catch (...) {
137157
cache_db.execute("ROLLBACK");
138158
return {};
@@ -163,6 +183,8 @@ class DigiKeyCacheManager {
163183
{"ExcludeMarketPlaceProducts", true},
164184
};
165185
const json response = rest_client.post("Search/v3/Products/Keyword", query);
186+
n_remaining = remaining_from_headers(rest_client.get_response_headers());
187+
166188
{
167189
SQLite::Query q_insert(
168190
cache_db,
@@ -173,7 +195,7 @@ class DigiKeyCacheManager {
173195
q_insert.bind(3, response.dump());
174196
q_insert.step();
175197
}
176-
return response;
198+
return {response, n_remaining};
177199
}
178200
catch (const std::exception &e) {
179201
std::cout << e.what() << std::endl;
@@ -284,6 +306,11 @@ class StockInfoProviderDigiKeyWorker {
284306
return n_items_fetched;
285307
}
286308

309+
unsigned int get_n_remaining() const
310+
{
311+
return n_remaining;
312+
}
313+
287314
private:
288315
Pool pool;
289316
std::shared_ptr<class DigiKeyCacheManager> cache_mgr;
@@ -316,9 +343,10 @@ class StockInfoProviderDigiKeyWorker {
316343
void emit();
317344
std::thread fetch_thread;
318345

319-
unsigned int n_items_from_cache = 0;
320-
unsigned int n_items_to_fetch = 0;
321-
unsigned int n_items_fetched = 0;
346+
std::atomic<unsigned int> n_items_from_cache = 0;
347+
std::atomic<unsigned int> n_items_to_fetch = 0;
348+
std::atomic<unsigned int> n_items_fetched = 0;
349+
std::atomic<unsigned int> n_remaining = 0;
322350
};
323351

324352
void StockInfoProviderDigiKeyWorker::emit()
@@ -409,8 +437,9 @@ void StockInfoProviderDigiKeyWorker::fetch_worker()
409437
for (const auto &[uu, MPN, manufacturer] : my_parts) {
410438
n_items_fetched++;
411439

412-
const auto r = cache_mgr->query(MPN, manufacturer);
440+
const auto [r, remaining] = cache_mgr->query(MPN, manufacturer);
413441
add_record(uu, r, MPN, manufacturer);
442+
n_remaining = remaining;
414443

415444
if (my_parts_rev != parts_rev_fetch) {
416445
cancel = true;
@@ -546,6 +575,9 @@ StockInfoProviderDigiKey::StockInfoProviderDigiKey(const std::string &pool_base_
546575
if (worker->get_n_items_to_fetch()) {
547576
txt += ", fetching " + format_m_of_n(worker->get_n_items_fetched(), worker->get_n_items_to_fetch());
548577
}
578+
if (auto remaining = worker->get_n_remaining()) {
579+
txt += ", " + std::to_string(remaining) + " queries remaining";
580+
}
549581
status_label->set_text(txt);
550582
}
551583
});

0 commit comments

Comments
 (0)