Skip to content

Commit

Permalink
Automatically update cached metadata on post
Browse files Browse the repository at this point in the history
  • Loading branch information
Groxan committed Jun 30, 2021
1 parent c4380a9 commit cb68333
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 38 deletions.
2 changes: 0 additions & 2 deletions Tzkt.Api/Controllers/AccountsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NSwag.Annotations;

using Tzkt.Api.Models;
using Tzkt.Api.Repositories;
using Tzkt.Api.Services.Cache;
using Tzkt.Api.Services.Metadata;

namespace Tzkt.Api.Controllers
{
Expand Down
44 changes: 40 additions & 4 deletions Tzkt.Api/Controllers/MetadataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using NSwag.Annotations;

using Tzkt.Api.Services;
using Tzkt.Api.Services.Auth;
using Tzkt.Api.Services.Cache;
using Tzkt.Api.Repositories;
using Tzkt.Api.Models;

namespace Tzkt.Api.Controllers
{
Expand All @@ -17,11 +21,17 @@ namespace Tzkt.Api.Controllers
[Route("v1/metadata")]
public class MetadataController : ControllerBase
{
readonly AccountsCache Accounts;
readonly SoftwareCache Software;
readonly SearchService Search;
readonly MetadataRepository Metadata;
readonly IAuthService Auth;

public MetadataController(MetadataRepository metadata, IAuthService auth)
public MetadataController(AccountsCache accounts, SoftwareCache software, SearchService search, MetadataRepository metadata, IAuthService auth)
{
Accounts = accounts;
Software = software;
Search = search;
Metadata = metadata;
Auth = auth;
}
Expand All @@ -35,7 +45,7 @@ public async Task<ActionResult<IEnumerable<ObjectMetadata>>> GetAccountMetadata(
{
if (!Auth.TryAuthenticate(headers, out var error))
return Unauthorized(error);

return Ok(await Metadata.GetAccountMetadata(offset, limit));
}

Expand Down Expand Up @@ -64,7 +74,31 @@ public async Task<ActionResult<IEnumerable<ObjectMetadata>>> UpdateAccountMetada
if (metadata.Any(x => !Regex.IsMatch(x.Key, "^(tz1|tz2|tz3|KT1)[0-9A-Za-z]{33}$")))
return BadRequest("Invalid account address");

return Ok(await Metadata.UpdateAccountMetadata(metadata));
var res = await Metadata.UpdateAccountMetadata(metadata);
#region update cached metadata
foreach (var item in res)
{
var acc = Accounts.Get(item.Key);
var prevAlias = acc.Alias;

acc.Metadata = item.Metadata == null ? null :
JsonSerializer.Deserialize<AccountMetadata>(item.Metadata.Json);

if (prevAlias != acc.Alias)
{
lock (Search.Aliases)
{
if (prevAlias == null)
Search.Aliases.Add(acc.Info);
else if (acc.Alias == null)
Search.Aliases.RemoveAll(x => x.Address == acc.Address);
else
Search.Aliases.First(x => x.Address == acc.Address).Name = acc.Alias;
}
}
}
#endregion
return Ok(res);
}
catch (JsonException ex)
{
Expand Down Expand Up @@ -204,7 +238,9 @@ public async Task<ActionResult<IEnumerable<ObjectMetadata>>> UpdateSoftwareMetad
if (metadata.Any(x => !Regex.IsMatch(x.Key, "^[0-9a-f]{8}$")))
return BadRequest("Invalid software short hash");

return Ok(await Metadata.UpdateSoftwareMetadata(metadata));
var res = await Metadata.UpdateSoftwareMetadata(metadata);
if (res.Count > 0) Software.Initialize();
return Ok(res);
}
catch (JsonException ex)
{
Expand Down
1 change: 0 additions & 1 deletion Tzkt.Api/Models/Accounts/Contract.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using Tzkt.Api.Services.Metadata;

namespace Tzkt.Api.Models
{
Expand Down
4 changes: 0 additions & 4 deletions Tzkt.Api/Models/Accounts/Delegate.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Tzkt.Api.Services.Metadata;

namespace Tzkt.Api.Models
{
Expand Down
4 changes: 0 additions & 4 deletions Tzkt.Api/Models/Accounts/User.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Tzkt.Api.Services.Metadata;

namespace Tzkt.Api.Models
{
Expand Down
8 changes: 1 addition & 7 deletions Tzkt.Api/Models/Voting/ProposalAlias.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Tzkt.Api.Services.Metadata;

namespace Tzkt.Api.Models
namespace Tzkt.Api.Models
{
public class ProposalAlias
{
Expand Down
1 change: 0 additions & 1 deletion Tzkt.Api/Repositories/AccountRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

using Tzkt.Api.Models;
using Tzkt.Api.Services.Cache;
using Tzkt.Api.Services.Metadata;
using Tzkt.Api.Utils;

namespace Tzkt.Api.Repositories
Expand Down
1 change: 0 additions & 1 deletion Tzkt.Api/Repositories/BlockRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

using Tzkt.Api.Models;
using Tzkt.Api.Services.Cache;
using Tzkt.Api.Services.Metadata;

namespace Tzkt.Api.Repositories
{
Expand Down
31 changes: 18 additions & 13 deletions Tzkt.Api/Services/Cache/Software/SoftwareCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
using Microsoft.Extensions.Logging;
using Dapper;
using Tzkt.Api.Models;
using Tzkt.Api.Services.Cache;

namespace Tzkt.Api.Services.Metadata
namespace Tzkt.Api.Services.Cache
{
public class SoftwareCache : DbConnection
{
public SoftwareAlias this[int id]
{
get
{
lock (Aliases)
lock (this)
{
if (!Aliases.TryGetValue(id, out var alias))
{
Expand All @@ -42,7 +41,7 @@ public SoftwareAlias this[int id]
}
}

readonly Dictionary<int, SoftwareAlias> Aliases;
Dictionary<int, SoftwareAlias> Aliases;
readonly TimeCache Time;
readonly ILogger Logger;

Expand All @@ -52,21 +51,27 @@ public SoftwareCache(TimeCache time, IConfiguration config, ILogger<SoftwareCach
Time = time;

Logger.LogDebug("Initializing software cache...");
Initialize();
Logger.LogDebug("Loaded {0} software", Aliases.Count);
}

using var db = GetConnection();
var rows = db.Query(@"
public void Initialize()
{
lock (this)
{
using var db = GetConnection();
var rows = db.Query(@"
SELECT ""Id"", ""FirstLevel"",
""Metadata""->>'version' as ""Version"",
""Metadata""->>'commitDate' as ""CommitDate""
FROM ""Software""");

Aliases = rows.ToDictionary(row => (int)row.Id, row => new SoftwareAlias
{
Version = row.Version,
Date = DateTimeOffset.TryParse(row.CommitDate, out DateTimeOffset dt) ? dt.DateTime : Time[row.FirstLevel]
});

Logger.LogDebug("Loaded {0} software", Aliases.Count);
Aliases = rows.ToDictionary(row => (int)row.Id, row => new SoftwareAlias
{
Version = row.Version,
Date = DateTimeOffset.TryParse(row.CommitDate, out DateTimeOffset dt) ? dt.DateTime : Time[row.FirstLevel]
});
}
}
}

Expand Down
1 change: 0 additions & 1 deletion Tzkt.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Tzkt.Api.Services;
using Tzkt.Api.Services.Auth;
using Tzkt.Api.Services.Cache;
using Tzkt.Api.Services.Metadata;
using Tzkt.Api.Services.Sync;
using Tzkt.Api.Swagger;
using Tzkt.Api.Websocket;
Expand Down

0 comments on commit cb68333

Please sign in to comment.