IndexPriceSyncService.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using Application.Abstractions.Data;
  2. using Application.Helpers;
  3. using Domain.Entities.Stocks;
  4. using Domain.Entities.Stocks.ValueObject;
  5. using Microsoft.EntityFrameworkCore;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Logging;
  8. using Microsoft.Extensions.Options;
  9. using SharedKernel;
  10. namespace Infrastructure.StockData;
  11. /// <summary>
  12. /// 지수(시장) 일별시세 수집 — KRX OpenAPI (KOSPI/KOSDAQ/KRX + 파생상품지수 시리즈, AUTH_KEY 헤더).
  13. /// 기본 18:30 KST 실행(장 마감 후 확정). 공용 KrxBackfill 로 최근 BackfillYears(기본 3)년치를 endDate(직전 영업일)부터
  14. /// 과거로 훑으며 미적재일만 채운다. quota 보호를 위해 1회 실행당 BackfillMaxPerRun(기본 60)일까지만 fetch →
  15. /// 여러 날에 걸쳐 3년치를 메우고 이후엔 최신만 유지한다. 각 날짜는 4개 엔드포인트를 모두 수집해 IndexDailyPrice upsert
  16. /// (UQ = Series+IndexName+TradeDate).
  17. /// 파생상품지수(idx/drvprod_dd_trd)는 BAS_DD/IDX_NM/CLSPRC_IDX/… OHLC shape 이 지수와 동일하여 재사용(거래량/거래대금/시총 미제공 → 0/null).
  18. /// 채권지수(idx/bon_dd_trd)는 응답 shape 이 달라 별도 BondIndexPriceSyncService 로 분리한다.
  19. /// ApiKey 미설정 시 로그만 남기고 skip (다른 KRX 배치와 동일 정책).
  20. /// </summary>
  21. internal sealed class IndexPriceSyncService(
  22. IServiceScopeFactory scopeFactory,
  23. IHttpClientFactory httpClientFactory,
  24. IOptions<AppSettings> settings,
  25. ILogger<IndexPriceSyncService> logger
  26. ) : DailyScheduledService(logger)
  27. {
  28. // (계열, 엔드포인트 경로)
  29. private static readonly (MarketIndexSeries Series, string Path)[] Endpoints =
  30. [
  31. (MarketIndexSeries.KOSPI, "/svc/apis/idx/kospi_dd_trd"),
  32. (MarketIndexSeries.KOSDAQ, "/svc/apis/idx/kosdaq_dd_trd"),
  33. (MarketIndexSeries.KRX, "/svc/apis/idx/krx_dd_trd"),
  34. (MarketIndexSeries.Derivative, "/svc/apis/idx/drvprod_dd_trd")
  35. ];
  36. protected override string JobName => "IndexPriceSync";
  37. protected override TimeOnly TargetTime => ParseTime(settings.Value.KRXCoKr.IndexSyncTime, new TimeOnly(18, 30));
  38. protected override int MaxRetryCount => 2;
  39. protected override TimeSpan RetryDelay => TimeSpan.FromHours(2);
  40. protected override async Task<bool> RunOnceAsync(DateOnly todayKst, CancellationToken ct)
  41. {
  42. var cfg = settings.Value.KRXCoKr;
  43. if (string.IsNullOrWhiteSpace(cfg.ApiKey))
  44. {
  45. Logger.LogWarning("[{Job}] KRXCoKr:ApiKey 미설정 — 수집 skip", JobName);
  46. return true;
  47. }
  48. using var scope = scopeFactory.CreateScope();
  49. var db = scope.ServiceProvider.GetRequiredService<IAppDbContext>();
  50. var client = httpClientFactory.CreateClient(KrxCoKrHttp.ClientName);
  51. var endDate = await MarketCalendar.GetPreviousBusinessDayAsync(db, todayKst.AddDays(1), ct);
  52. var years = cfg.BackfillYears > 0 ? cfg.BackfillYears : 3;
  53. var startDate = todayKst.AddYears(-years);
  54. // 백필 창 전체의 휴장일을 한 번에 로드 (KrxBackfill 은 주말은 자동 제외, 휴장일만 필요)
  55. var holidays = (await db.MarketHoliday.AsNoTracking()
  56. .Where(c => c.Date >= startDate && c.Date <= endDate)
  57. .Select(c => c.Date)
  58. .ToListAsync(ct)).ToHashSet();
  59. var maxPerRun = cfg.BackfillMaxPerRun > 0 ? cfg.BackfillMaxPerRun : 60;
  60. var fetched = await KrxBackfill.RunAsync(
  61. existsForDate: (day, token) => db.IndexDailyPrice.AsNoTracking().AnyAsync(c => c.TradeDate == day, token),
  62. fetchAndUpsertForDate: (day, token) => FetchAndUpsertAsync(db, client, cfg.BaseUrl, cfg.ApiKey, day, token),
  63. startDate: startDate,
  64. endDate: endDate,
  65. holidays: holidays,
  66. maxPerRun: maxPerRun,
  67. delayMs: 300,
  68. ct: ct);
  69. Logger.LogInformation("[{Job}] 완료 — 창=[{Start}~{End}], 이번 실행 fetch={Fetched}일 (maxPerRun={Max})",
  70. JobName, startDate, endDate, fetched, maxPerRun);
  71. // fetch 가 0 이어도(이미 최신까지 적재됨) 정상 완료 — 재시도 불필요
  72. return true;
  73. }
  74. /// <summary>한 날짜에 대해 4개 지수 엔드포인트를 모두 수집하고 IndexDailyPrice upsert.</summary>
  75. private async Task FetchAndUpsertAsync(IAppDbContext db, HttpClient client, string baseUrl, string apiKey, DateOnly day, CancellationToken ct)
  76. {
  77. var rows = new List<KrxIndexParser.IndexRow>();
  78. foreach (var (series, path) in Endpoints)
  79. {
  80. var url = $"{baseUrl.TrimEnd('/')}{path}?basDd={day:yyyyMMdd}";
  81. var json = await KrxCoKrHttp.GetStringWithRetryAsync(client, url, apiKey, Logger, ct);
  82. var parsed = KrxIndexParser.ParseIndexPrices(json, series);
  83. Logger.LogInformation("[{Job}] {Series} basDd={Day} rows={Rows}", JobName, series, day, parsed.Count);
  84. rows.AddRange(parsed);
  85. }
  86. if (rows.Count == 0)
  87. {
  88. Logger.LogInformation("[{Job}] basDd={Day} 지수 미반영 (0건)", JobName, day);
  89. return;
  90. }
  91. var existing = await db.IndexDailyPrice.Where(c => c.TradeDate == day).ToListAsync(ct);
  92. var existingByKey = existing.ToDictionary(c => (c.Series, c.IndexName));
  93. var inserted = 0;
  94. var updated = 0;
  95. foreach (var row in rows)
  96. {
  97. if (existingByKey.TryGetValue((row.Series, row.IndexName), out var price))
  98. {
  99. price.Update(row.Close, row.ChangeVal, row.FlucRateBp, row.Open, row.High, row.Low, row.Volume, row.Value, row.MarketCap);
  100. updated++;
  101. }
  102. else
  103. {
  104. var created = IndexDailyPrice.Create(row.Series, row.IndexName, row.TradeDate, row.Close, row.ChangeVal, row.FlucRateBp, row.Open, row.High, row.Low, row.Volume, row.Value, row.MarketCap);
  105. await db.IndexDailyPrice.AddAsync(created, ct);
  106. existingByKey[(row.Series, row.IndexName)] = created;
  107. inserted++;
  108. }
  109. }
  110. await db.SaveChangesAsync(ct);
  111. Logger.LogInformation("[{Job}] basDd={Day} 적재 — rows={Rows}, inserted={Inserted}, updated={Updated}",
  112. JobName, day, rows.Count, inserted, updated);
  113. }
  114. }