Skip to content

Commit

Permalink
Add command to Scan Hashtags Now (#1611)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencohn authored Oct 14, 2024
1 parent 658efd1 commit bff0c3a
Show file tree
Hide file tree
Showing 20 changed files with 204 additions and 34 deletions.
5 changes: 5 additions & 0 deletions OneMore/AddInCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,11 @@ public async Task SaveSnippetCmd(IRibbonControl control)
=> await factory.Run<SaveSnippetCommand>();


[Command("ribScanHashtagsButton_Label", Keys.Control | Keys.Alt | Keys.F9, "ribSearchMenu")]
public async Task ScanHashtagsCmd(IRibbonControl control)
=> await factory.Run<ScanHashtagsCommand>();


[Command("ribScheduleHashtagScanButton_Label", Keys.None, "ribSearchMenu")]
public async Task ScheduleHashtagScanCmd(IRibbonControl control)
=> await factory.Run<HashtagScanCommand>();
Expand Down
12 changes: 2 additions & 10 deletions OneMore/Commands/Tagging/HashtagDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace River.OneMoreAddIn.Commands
using River.OneMoreAddIn.UI;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Linq;
Expand Down Expand Up @@ -421,15 +420,8 @@ private async void ScanNow(object sender, EventArgs e)
ShowScanTimes();

using var scanner = new HashtagScanner();

var clock = new Stopwatch();
clock.Start();

var (dirtyPages, totalPages) = await scanner.Scan();

clock.Stop();
var time = clock.ElapsedMilliseconds;
logger.WriteLine($"scanned {totalPages} pages, updating {dirtyPages}, in {time}ms");
await scanner.Scan();
scanner.Report();

await PopulateTags(sender, e);
}
Expand Down
14 changes: 14 additions & 0 deletions OneMore/Commands/Tagging/HashtagScanCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ namespace River.OneMoreAddIn.Commands
using Resx = Properties.Resources;


#region Wrappers
internal class ScanHashtagsCommand : ToCaseCommand
{
public ScanHashtagsCommand() : base() { }
public override async Task Execute(params object[] args)
{
using var scanner = new HashtagScanner();
await scanner.Scan();
scanner.Report();
}
}
#endregion


internal class HashtagScanCommand : Command
{

Expand Down
55 changes: 48 additions & 7 deletions OneMore/Commands/Tagging/HashtagScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace River.OneMoreAddIn.Commands
using River.OneMoreAddIn.Styles;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
Expand All @@ -19,6 +20,18 @@ namespace River.OneMoreAddIn.Commands
/// </summary>
internal class HashtagScanner : Loggable, IDisposable
{
public class Statistics
{
public int Notebooks;
public int KnownNotebooks;
public int FilteredNotebooks;
public int Sections;
public int TotalPages;
public int DirtyPages;
public int Tags;
public long Time;
}

public const int DefaultThrottle = 40;

private readonly string lastTime;
Expand All @@ -44,17 +57,23 @@ public HashtagScanner()
GetStyleTemplate(),
settings.Get<bool>("unfiltered"));

Stats = new Statistics();

lastTime = provider.ReadScanTime();
//logger.Verbose($"HashtagScanner lastTime {lastTime}");
}


public Statistics Stats { get; private set; }


/// <summary>
/// A list of notebook IDs to target, used for rescans and rebuilds
/// </summary>
public void SetNotebookFilters(string[] filters)
{
notebookFilters = filters;
Stats.FilteredNotebooks = filters.Length;
}


Expand Down Expand Up @@ -119,10 +138,10 @@ public void Dispose()
/// Scan all notebooks for all hashtags
/// </summary>
/// <returns></returns>
public async Task<(int, int)> Scan()
public async Task Scan()
{
int dirtyPages = 0;
int totalPages = 0;
var clock = new Stopwatch();
clock.Start();

await using var one = new OneNote();

Expand All @@ -131,7 +150,7 @@ public void Dispose()
if (root is null)
{
logger.WriteLine("error HashtagScanner one.GetNotebooks()");
return (0, 0);
return;
}

var ns = one.GetNamespace(root);
Expand All @@ -141,6 +160,9 @@ public void Dispose()
{
var knownNotebooks = provider.ReadKnownNotebooks();

Stats.Notebooks += notebooks.Count();
Stats.KnownNotebooks += knownNotebooks.Count;

foreach (var notebook in notebooks)
{
// gets sections for this notebook
Expand Down Expand Up @@ -180,8 +202,8 @@ public void Dispose()
one, sections, notebookID, $"/{name}",
known?.LastModified == string.Empty);

dirtyPages += dp;
totalPages += tp;
Stats.DirtyPages += dp;
Stats.TotalPages += tp;
}

// record the notebook regardless of whether we find tags; must be done
Expand All @@ -197,7 +219,8 @@ public void Dispose()

provider.WriteScanTime();

return (dirtyPages, totalPages);
clock.Stop();
Stats.Time = clock.ElapsedMilliseconds;
}


Expand Down Expand Up @@ -262,6 +285,8 @@ public void Dispose()
}
}
}

Stats.Sections += sectionRefs.Count();
}

var groups = parent.Elements(ns + "SectionGroup")
Expand Down Expand Up @@ -359,6 +384,8 @@ private async Task<bool> ScanPage(
// few copied records. should scale without issue into the many tens-of-tags
provider.WriteTags(pageID, candidates);
dirtyPage = true;

Stats.Tags += updated.Count + discovered.Count;
}

// if first time hashtags were discovered on this page then set omPageID
Expand Down Expand Up @@ -386,5 +413,19 @@ private async Task<bool> ScanPage(

return false;
}


public void Report(string title = null)
{
if (!string.IsNullOrWhiteSpace(title))
{
logger.Write($"{title} ");
}

logger.WriteLine($"scanned {Stats.TotalPages} pages, " +
$"{Stats.KnownNotebooks}/{Stats.Notebooks} notebooks, " +
$"{Stats.Sections} sections, updating {Stats.DirtyPages} pages, " +
$"saving {Stats.Tags} tags, in {Stats.Time}ms");
}
}
}
26 changes: 10 additions & 16 deletions OneMore/Commands/Tagging/HashtagService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace River.OneMoreAddIn.Commands
{
using River.OneMoreAddIn.Settings;
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand Down Expand Up @@ -208,30 +207,23 @@ private async Task<bool> WaitForReady()

protected async Task Scan()
{
var clock = new Stopwatch();
clock.Start();

using var scanner = new HashtagScanner();

if (notebookFilters is not null && notebookFilters.Length > 0)
{
scanner.SetNotebookFilters(notebookFilters);
}

var (dirtyPages, totalPages) = await scanner.Scan();

clock.Stop();
var time = clock.ElapsedMilliseconds;
await scanner.Scan();

var s = scanner.Stats;
scanCount++;
scanTime += time;
scanTime += scanner.Stats.Time;

var avg = scanTime / scanCount;

OnHashtagScanned?.Invoke(this,
new HashtagScannedEventArgs(totalPages, dirtyPages, time, scanCount, avg));

logger.Debug($"hashtag service scanned {scanCount} times...");
new HashtagScannedEventArgs(s.TotalPages, s.DirtyPages, s.Time, scanCount, avg));

if (hour != DateTime.Now.Hour)
{
Expand All @@ -240,11 +232,13 @@ protected async Task Scan()
scanCount = 0;
scanTime = 0;
}

if (dirtyPages > 0 || time > 1000)
else if (s.DirtyPages > 0 || s.Time > 1000)
{
scanner.Report("hashtag SERVICE");
}
else if (logger.IsDebug)
{
logger.WriteLine(
$"hashtag service scanned {totalPages} pages, updating {dirtyPages}, in {time}ms");
scanner.Report("hashtag service");
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion OneMore/Commands/Tagging/LegacyTaggingConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ public async Task<bool> UpgradeLegacyTags()
if (!scheduler.ScheduleExists && scheduler.State == ScanningState.Ready)
{
using var scanner = new HashtagScanner();
(_, _) = await scanner.Scan();
await scanner.Scan();
scanner.Report("legacy converter");
}
}

Expand Down
11 changes: 11 additions & 0 deletions OneMore/Helpers/ILogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ namespace River.OneMoreAddIn
/// </summary>
public interface ILogger : IDisposable
{
/// <summary>
///
/// </summary>
bool IsDebug { get; }


/// <summary>
///
/// </summary>
bool IsVerbose { get; }


/// <summary>
/// Gets the system file path of the log file
Expand Down
6 changes: 6 additions & 0 deletions OneMore/Helpers/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ public static ILogger Current
}


public bool IsDebug => debug;


public bool IsVerbose => verbose;


public string LogPath { get; private set; }


Expand Down
18 changes: 18 additions & 0 deletions OneMore/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions OneMore/Properties/Resources.ar-SA.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3950,6 +3950,14 @@ ISO-code then comma then language name</comment>
<value>احفظ مقتطفًا مخصصًا جديدًا من المحتوى المحدد</value>
<comment>ribbon snippets</comment>
</data>
<data name="ribScanHashtagsButton_Label" xml:space="preserve">
<value>مسح علامات التصنيف</value>
<comment>ribbon search</comment>
</data>
<data name="ribScanHashtagsButton_Screentip" xml:space="preserve">
<value>مسح علامات التصنيف على الفور</value>
<comment>ribbon search</comment>
</data>
<data name="ribScheduleHashtagScanButton_Label" xml:space="preserve">
<value>جدولة مسح الهاشتاج</value>
<comment>palette command</comment>
Expand Down
8 changes: 8 additions & 0 deletions OneMore/Properties/Resources.de-DE.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3943,6 +3943,14 @@ Nach der letzten Gruppe</value>
<value>Speichert ein neues benutzerdefiniertes Snippet aus dem ausgewählten Inhalt</value>
<comment>ribbon snippets</comment>
</data>
<data name="ribScanHashtagsButton_Label" xml:space="preserve">
<value>Hashtags scannen</value>
<comment>ribbon search</comment>
</data>
<data name="ribScanHashtagsButton_Screentip" xml:space="preserve">
<value>Hashtags sofort scannen</value>
<comment>ribbon search</comment>
</data>
<data name="ribScheduleHashtagScanButton_Label" xml:space="preserve">
<value>Planen Sie den Hashtag-Scan</value>
<comment>palette command</comment>
Expand Down
8 changes: 8 additions & 0 deletions OneMore/Properties/Resources.es-ES.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3950,6 +3950,14 @@ Después del último grupo</value>
<value>Guardar un nuevo fragmento personalizado del contenido seleccionado</value>
<comment>ribbon snippets</comment>
</data>
<data name="ribScanHashtagsButton_Label" xml:space="preserve">
<value>Escanear hashtags</value>
<comment>ribbon search</comment>
</data>
<data name="ribScanHashtagsButton_Screentip" xml:space="preserve">
<value>Escanea hashtags inmediatamente</value>
<comment>ribbon search</comment>
</data>
<data name="ribScheduleHashtagScanButton_Label" xml:space="preserve">
<value>Programar escaneo de hashtags</value>
<comment>palette command</comment>
Expand Down
8 changes: 8 additions & 0 deletions OneMore/Properties/Resources.fr-FR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -3949,6 +3949,14 @@ Après le dernier groupe</value>
<value>Enregistrer un nouvel extrait personnalisé à partir du contenu sélectionné</value>
<comment>ribbon snippets</comment>
</data>
<data name="ribScanHashtagsButton_Label" xml:space="preserve">
<value>Scanner les hashtags</value>
<comment>ribbon search</comment>
</data>
<data name="ribScanHashtagsButton_Screentip" xml:space="preserve">
<value>Scannez immédiatement les hashtags</value>
<comment>ribbon search</comment>
</data>
<data name="ribScheduleHashtagScanButton_Label" xml:space="preserve">
<value>Planifier une analyse de hashtag</value>
<comment>palette command</comment>
Expand Down
Loading

0 comments on commit bff0c3a

Please sign in to comment.