This repository has been archived by the owner on May 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef1e405
commit f6c03bd
Showing
28 changed files
with
472 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Threading.Tasks; | ||
using Refit; | ||
|
||
namespace Newbe.BookmarkManager.Services | ||
{ | ||
public interface IPinyinApi | ||
{ | ||
[Post("/pinyin")] | ||
Task<PinyinOutput> GetPinyinAsync(PinyinInput input); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Newbe.BookmarkManager.Services | ||
{ | ||
public record PinyinOutput | ||
{ | ||
public bool IsOk { get; set; } | ||
public Dictionary<string, string> Pinyin { get; set; } | ||
} | ||
|
||
public record PinyinInput | ||
{ | ||
public string[] Text { get; set; } | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Newbe.BookmarkManager/Services/Configuration/AliasJobOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Newbe.BookmarkManager.Services.Configuration | ||
{ | ||
public record AliasJobOptions | ||
{ | ||
public bool EnablePinyinAlias { get; set; } | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/Newbe.BookmarkManager/Services/Configuration/BaseUriOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Newbe.BookmarkManager.Services.Configuration | ||
{ | ||
public record BaseUriOptions | ||
{ | ||
public string PinyinApi { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Newbe.BookmarkManager.Services | ||
{ | ||
public interface ISyncAliasJob | ||
{ | ||
ValueTask StartAsync(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/Newbe.BookmarkManager/Services/Job/Impl/SyncAliasJob.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reactive.Linq; | ||
using System.Reactive.Subjects; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Newbe.BookmarkManager.Services.Configuration; | ||
|
||
namespace Newbe.BookmarkManager.Services | ||
{ | ||
public class SyncAliasJob : ISyncAliasJob | ||
{ | ||
private readonly ILogger<SyncAliasJob> _logger; | ||
private readonly IOptions<AliasJobOptions> _options; | ||
private readonly IBkDataHolder _bkDataHolder; | ||
private readonly IClock _clock; | ||
private readonly IEnumerable<ITextAliasProvider> _fillers; | ||
private readonly Subject<long> _jobSubject = new(); | ||
|
||
// ReSharper disable once NotAccessedField.Local | ||
private IDisposable _loadHandler; | ||
|
||
public SyncAliasJob( | ||
ILogger<SyncAliasJob> logger, | ||
IOptions<AliasJobOptions> options, | ||
IBkDataHolder bkDataHolder, | ||
IClock clock, | ||
IEnumerable<ITextAliasProvider> fillers) | ||
{ | ||
_logger = logger; | ||
_options = options; | ||
_bkDataHolder = bkDataHolder; | ||
_clock = clock; | ||
_fillers = fillers; | ||
} | ||
|
||
public ValueTask StartAsync() | ||
{ | ||
if (_options.Value.EnablePinyinAlias) | ||
{ | ||
_loadHandler = _jobSubject | ||
.Merge(Observable.Interval(TimeSpan.FromSeconds(60))) | ||
.Select(_ => Observable.FromAsync(async () => | ||
{ | ||
try | ||
{ | ||
foreach (var textAliasFiller in _fillers) | ||
{ | ||
var bkTags = _bkDataHolder.Collection.Tags.Values | ||
.Where(tag => textAliasFiller.NeedFill(tag)) | ||
.ToArray(); | ||
if (bkTags.Length > 0) | ||
{ | ||
await _bkDataHolder.PushDataChangeActionAsync(async () => | ||
{ | ||
var updateResult = await textAliasFiller.FillAsync(bkTags); | ||
if (updateResult.IsOk) | ||
{ | ||
_logger.LogInformation("New alias add for tag"); | ||
} | ||
return updateResult.IsOk; | ||
}); | ||
} | ||
} | ||
foreach (var textAliasFiller in _fillers) | ||
{ | ||
var bks = _bkDataHolder.Collection.Bks.Values | ||
.Where(x => textAliasFiller.NeedFill(x)) | ||
.ToArray(); | ||
if (bks.Length > 0) | ||
{ | ||
_logger.LogInformation("Found {Count} bks need to add alias in {TextAliasType}", | ||
bks.Length, | ||
textAliasFiller.TextAliasType); | ||
const int pageSize = 100; | ||
var pageCount = (int) Math.Ceiling(1.0 * bks.Length / pageSize); | ||
await _bkDataHolder.PushDataChangeActionAsync(async () => | ||
{ | ||
var updated = false; | ||
for (var i = 0; i < pageCount; i++) | ||
{ | ||
var items = bks.Skip(i * pageSize).Take(pageSize).ToArray(); | ||
var updateResult = await textAliasFiller.FillAsync(items); | ||
updated = updated || updateResult.IsOk; | ||
} | ||
return updated; | ||
}); | ||
} | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(e, "Failed to fill alias"); | ||
} | ||
})) | ||
.Concat() | ||
.Subscribe(_ => { }); | ||
_jobSubject.OnNext(_clock.UtcNow); | ||
} | ||
|
||
return ValueTask.CompletedTask; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Newbe.BookmarkManager.Services | ||
{ | ||
public record TextAlias | ||
{ | ||
public TextAliasType TextAliasType { get; set; } | ||
public string Alias { get; set; } | ||
public long LastUpdatedTime { get; set; } | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...arkManager/Services/Models/BkAliasType.cs → ...kManager/Services/Models/TextAliasType.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
namespace Newbe.BookmarkManager.Services | ||
{ | ||
public enum BkAliasType | ||
public enum TextAliasType | ||
{ | ||
Pinyin | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
src/Newbe.BookmarkManager/Services/Services/ITitleAliasProvider.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.