refresh fixes
This commit is contained in:
parent
32cc139ef1
commit
c596d8c12f
1 changed files with 33 additions and 25 deletions
46
src/main.zig
46
src/main.zig
|
|
@ -261,7 +261,22 @@ fn upperDupe(allocator: std.mem.Allocator, s: []const u8) ![]u8 {
|
|||
return d;
|
||||
}
|
||||
|
||||
/// Format as percentage string for XML (e.g., 0.1234 -> "12.34000")
|
||||
fn printRateLimitWait(svc: *zfin.DataService, stdout: *std.Io.Writer) !void {
|
||||
if (svc.estimateWaitSeconds()) |wait| {
|
||||
if (wait > 0) {
|
||||
try stdout.print("\n (rate limit -- waiting {d}s)\n ", .{wait});
|
||||
try stdout.flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Mutual funds typically have 5-letter tickers ending in X.
|
||||
/// These don't have quarterly earnings on Finnhub.
|
||||
fn isMutualFund(symbol: []const u8) bool {
|
||||
return symbol.len == 5 and symbol[4] == 'X';
|
||||
}
|
||||
|
||||
/// Format as percentage (e.g., 0.1234 -> "12.34000"), or "null" if absent.
|
||||
fn fmtPct(arena: std.mem.Allocator, value: ?f64) []const u8 {
|
||||
if (value) |v| return std.fmt.allocPrint(arena, "{d:.5}", .{v * 100.0}) catch "null";
|
||||
return "null";
|
||||
|
|
@ -345,16 +360,11 @@ fn refresh(allocator: std.mem.Allocator) !void {
|
|||
};
|
||||
defer allocator.free(data);
|
||||
|
||||
const portfolio = zfin.cache.deserializePortfolio(allocator, data) catch {
|
||||
var portfolio = zfin.cache.deserializePortfolio(allocator, data) catch {
|
||||
log.err("failed to parse portfolio", .{});
|
||||
return error.ParseFailed;
|
||||
};
|
||||
defer {
|
||||
for (portfolio.lots) |*lot| {
|
||||
if (lot.note) |n| allocator.free(n);
|
||||
}
|
||||
allocator.free(portfolio.lots);
|
||||
}
|
||||
defer portfolio.deinit();
|
||||
|
||||
var symbols = std.StringHashMap(void).init(allocator);
|
||||
defer symbols.deinit();
|
||||
|
|
@ -382,21 +392,13 @@ fn refresh(allocator: std.mem.Allocator) !void {
|
|||
var it = symbols.iterator();
|
||||
while (it.next()) |entry| {
|
||||
const sym = entry.key_ptr.*;
|
||||
|
||||
// Check if we need to wait for rate limits
|
||||
if (svc.estimateWaitSeconds()) |wait| {
|
||||
if (wait > 0) {
|
||||
try stdout.print(" (rate limit -- waiting {d}s)\n", .{wait});
|
||||
try stdout.flush();
|
||||
}
|
||||
}
|
||||
|
||||
try stdout.print("{s}: ", .{sym});
|
||||
try stdout.flush();
|
||||
|
||||
var sym_ok = true;
|
||||
|
||||
// Candles
|
||||
try printRateLimitWait(&svc, stdout);
|
||||
if (svc.getCandles(sym)) |result| {
|
||||
allocator.free(result.data);
|
||||
try stdout.print("candles ok", .{});
|
||||
|
|
@ -406,15 +408,18 @@ fn refresh(allocator: std.mem.Allocator) !void {
|
|||
}
|
||||
|
||||
// Dividends
|
||||
try printRateLimitWait(&svc, stdout);
|
||||
if (svc.getDividends(sym)) |result| {
|
||||
allocator.free(result.data);
|
||||
zfin.Dividend.freeSlice(allocator, result.data);
|
||||
try stdout.print(", dividends ok", .{});
|
||||
} else |err| {
|
||||
try stdout.print(", dividends FAILED ({s})", .{@errorName(err)});
|
||||
sym_ok = false;
|
||||
}
|
||||
|
||||
// Earnings
|
||||
// Earnings (skip for mutual funds — they don't report quarterly earnings)
|
||||
if (!isMutualFund(sym)) {
|
||||
try printRateLimitWait(&svc, stdout);
|
||||
if (svc.getEarnings(sym)) |result| {
|
||||
allocator.free(result.data);
|
||||
try stdout.print(", earnings ok", .{});
|
||||
|
|
@ -422,6 +427,9 @@ fn refresh(allocator: std.mem.Allocator) !void {
|
|||
try stdout.print(", earnings FAILED ({s})", .{@errorName(err)});
|
||||
sym_ok = false;
|
||||
}
|
||||
} else {
|
||||
try stdout.print(", earnings skipped (fund)", .{});
|
||||
}
|
||||
|
||||
try stdout.print("\n", .{});
|
||||
try stdout.flush();
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue