Skip to content

Commit

Permalink
Add quotes cache to API
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Aug 5, 2020
1 parent 3c5dcf9 commit 48affa2
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 2 deletions.
74 changes: 74 additions & 0 deletions Tzkt.Api/Services/Cache/Quotes/QuotesCache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Dapper;

namespace Tzkt.Api.Services.Cache
{
public class QuotesCache : DbConnection
{
readonly List<double>[] Quotes;
readonly ILogger Logger;

public QuotesCache(IConfiguration config, ILogger<QuotesCache> logger) : base(config)
{
Quotes = new List<double>[3];
Logger = logger;

Logger.LogDebug("Initializing quotes cache...");

var sql = @"
SELECT ""Btc"", ""Eur"", ""Usd""
FROM ""Quotes""
ORDER BY ""Level""";

using var db = GetConnection();
var rows = db.Query(sql);

Quotes[0] = rows.Select(x => (double)x.Btc).ToList();
Quotes[1] = rows.Select(x => (double)x.Eur).ToList();
Quotes[2] = rows.Select(x => (double)x.Usd).ToList();

Logger.LogDebug($"Quotes cache initialized with {Quotes[0].Count} items");
}

public double Get(int symbol)
{
return Quotes[symbol][^1];
}

public double Get(int symbol, int level)
{
return level >= Quotes[symbol].Count ? Quotes[symbol][^1] : Quotes[symbol][level];
}

public async Task UpdateAsync()
{
var sql = $@"
SELECT ""Btc"", ""Eur"", ""Usd""
FROM ""Quotes""
WHERE ""Level"" >= {Quotes[0].Count}
ORDER BY ""Level""";

using var db = GetConnection();
var rows = await db.QueryAsync(sql);

Quotes[0].AddRange(rows.Select(x => (double)x.Btc));
Quotes[1].AddRange(rows.Select(x => (double)x.Eur));
Quotes[2].AddRange(rows.Select(x => (double)x.Usd));
}
}

public static class QuotesCacheExt
{
public static void AddQuotesCache(this IServiceCollection services)
{
services.AddSingleton<QuotesCache>();
}
}
}
7 changes: 7 additions & 0 deletions Tzkt.Api/Services/Cache/State/RawModels/RawState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,12 @@ public class RawState
public int MigrationOpsCount { get; set; }
public int RevelationPenaltyOpsCount { get; set; }
#endregion

#region quotes
public int QuoteLevel { get; set; }
public double QuoteBtc { get; set; }
public double QuoteEur { get; set; }
public double QuoteUsd { get; set; }
#endregion
}
}
2 changes: 1 addition & 1 deletion Tzkt.Api/Services/Cache/Time/TimeCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class TimeCache : DbConnection
readonly List<DateTime> Times;
readonly ILogger Logger;

public TimeCache(IConfiguration config, ILogger<StateCache> logger) : base(config)
public TimeCache(IConfiguration config, ILogger<TimeCache> logger) : base(config)
{
Logger = logger;

Expand Down
6 changes: 5 additions & 1 deletion Tzkt.Api/Services/Sync/SyncWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class SyncWorker : BackgroundService
readonly string ConnectionString;
readonly AccountsCache Accounts;
readonly ProtocolsCache Protocols;
readonly QuotesCache Quotes;
readonly StateCache State;
readonly TimeCache Times;
readonly ILogger Logger;
Expand All @@ -29,12 +30,13 @@ public class SyncWorker : BackgroundService
RawState LastState;
DateTime NextSyncTime;

public SyncWorker(AccountsCache accounts, ProtocolsCache protocols, StateCache state, TimeCache times, IConfiguration config, ILogger<SyncWorker> logger)
public SyncWorker(AccountsCache accounts, ProtocolsCache protocols, QuotesCache quotes, StateCache state, TimeCache times, IConfiguration config, ILogger<SyncWorker> logger)
{
Config = config.GetSyncConfig();
ConnectionString = config.GetConnectionString("DefaultConnection");
Accounts = accounts;
Protocols = protocols;
Quotes = quotes;
State = state;
Times = times;
Logger = logger;
Expand Down Expand Up @@ -75,6 +77,8 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)

await Protocols.UpdateAsync(currentState);

await Quotes.UpdateAsync();

await Times.UpdateAsync();

State.Update(currentState);
Expand Down
1 change: 1 addition & 0 deletions Tzkt.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void ConfigureServices(IServiceCollection services)

services.AddAccountsCache();
services.AddProtocolsCache();
services.AddQuotesCache();
services.AddStateCache();
services.AddTimeCache();

Expand Down

0 comments on commit 48affa2

Please sign in to comment.