WinForm gets more info
69fb12ada28ce38001004f3a3c1f6da1bb97940a
5 files changed
src/OpenKamer.LogicControllers/EntityController.cssrc/OpenKamer.LogicControllers/FeedController.cssrc/OpenKamer.WinForm/Form1.Designer.cssrc/OpenKamer.WinForm/Form1.cssrc/OpenKamer.WinForm/Form1.resx
diff --git a/src/OpenKamer.LogicControllers/EntityController.cs b/src/OpenKamer.LogicControllers/EntityController.cs
index 62d50f5..fa73d37 100644
--- a/src/OpenKamer.LogicControllers/EntityController.cs
+++ b/src/OpenKamer.LogicControllers/EntityController.cs
@@ -8,6 +8,12 @@ using System.Xml;
using System.Xml.Serialization;
using System.Diagnostics;
+// nuget MongoDB.MvcCore
+
+using MongoDB.Bson;
+using MongoDB.Driver;
+using MongoDB.MvcCore;
+
using nl.tweedekamer.opendata.DataModel;
@@ -15,16 +21,31 @@ namespace OpenKamer.LogicControllers;
public class EntityController
{
+ private readonly IMongoDatabase mongodb;
- private readonly string DB;
- public EntityController(string DB)
+ private readonly string FilesDirectory;
+ public string SkipToken { get; set; }
+ public int FileCount { get; set; }
+ public int EntityCount { get; set; }
+ public int Error { get; set; }
+
+ public EntityController(string MongoConnectionString, string MongoDbName, string FilesDirectory, string skiptoken)
{
- this.DB = DB;
- }
+ this.FilesDirectory = FilesDirectory;
+
+ this.SkipToken = skiptoken;
+
+ this.FileCount = 0;
+
+ this.EntityCount = 0;
- public delegate Task AsyncEventHandler(object sender, EventArgs e);
+ this.Error = 0;
+
+ var mongoClient = new MongoClient(MongoConnectionString);
+
+ this.mongodb = mongoClient.GetDatabase(MongoDbName);
+ }
- public event AsyncEventHandler? OnEntity;
public event EventHandler? OnLog;
@@ -33,20 +54,20 @@ public class EntityController
OnLog?.Invoke(message, new EventArgs());
}
- async public Task DecodeEntriesAsync(string? skiptoken)
+ async public Task DecodeEntriesAsync(CancellationToken cancellationToken)
{
Log("DecodeEntriesAsync");
XmlSerializer serializer = new(typeof(Feed));
- if (string.IsNullOrWhiteSpace(skiptoken))
- skiptoken = "empty";
+ if (string.IsNullOrWhiteSpace(SkipToken))
+ SkipToken = "0";
do
{
- Log("Skiptoken: " + skiptoken);
+ this.FileCount++;
- string FileName = DB + @"\" + skiptoken + ".xml";
+ var FileName = FilesDirectory + @"\" + SkipToken + ".xml";
using StreamReader reader = new(FileName);
@@ -54,7 +75,7 @@ public class EntityController
if (feed == null)
{
- Log("no valid feed exit");
+ Log("No valid feed exit");
return;
}
@@ -79,16 +100,43 @@ public class EntityController
return;
}
if (intI > 0)
- skiptoken = nextNode.Href[(intI + 10)..];
+ SkipToken = nextNode.Href[(intI + 10)..];
- await ProcessEntitiesAsync2(feed);
+ await ProcessEntitiesAsync(feed, cancellationToken);
- } while (!string.IsNullOrWhiteSpace(skiptoken));
+ } while (!string.IsNullOrWhiteSpace(SkipToken));
Log("Abnormal empty skiptoken exit");
}
- async private Task<T?> GetEntityAsync<T>(XmlNode xmlNode)
+ async private Task ReplaceEntityByIdAsync(object entity, CancellationToken cancellationToken)
+ {
+ var collection = this.mongodb.GetCollection(entity.GetType().Name);
+
+ if (collection == null)
+ return;
+
+ try
+ {
+ var replaceOneResult = await collection.ReplaceOneByIdAsync(entity, cancellationToken);
+
+ this.EntityCount++;
+ }
+ catch (BsonSerializationException)
+ {
+ this.Error++;
+ }
+ catch (MongoWriteException)
+ {
+ this.Error++;
+ }
+ catch (Exception)
+ {
+ this.Error++;
+ }
+ }
+
+ async private Task<T?> GetEntityAsync<T>(XmlNode xmlNode, CancellationToken cancellationToken)
{
XmlSerializer serializer = new(typeof(T));
@@ -98,8 +146,8 @@ public class EntityController
try
{
var result = (T?)serializer.Deserialize(new XmlNodeReader(xmlNode));
- if(result != null && OnEntity != null)
- await OnEntity.Invoke(result, new EventArgs());
+ if(result != null)
+ await ReplaceEntityByIdAsync(result, cancellationToken);
return result;
}
catch (Exception eeee)
@@ -112,9 +160,8 @@ public class EntityController
#pragma warning disable IDE0059
- async private Task ProcessEntitiesAsync2(Feed feed)
+ async private Task ProcessEntitiesAsync(Feed feed, CancellationToken cancellationToken)
{
-
var entries = feed.Entries;
if (entries == null)
@@ -128,117 +175,117 @@ public class EntityController
if (entry.Content.FirstChild == null)
continue;
+ await Task.Delay(1, cancellationToken);
+
switch (entry.Category.Term)
{
default:
Log($"Unknown Category {entry.Category.Term}");
break;
case "zaal":
- var zaal = await GetEntityAsync<zaalType>(entry.Content.FirstChild);
+ var zaal = await GetEntityAsync<zaalType>(entry.Content.FirstChild, cancellationToken);
break;
case "reservering":
- var reservering = await GetEntityAsync<reserveringType>(entry.Content.FirstChild);
+ var reservering = await GetEntityAsync<reserveringType>(entry.Content.FirstChild, cancellationToken);
break;
case "vergadering":
- var vergadering = await GetEntityAsync<vergaderingType>(entry.Content.FirstChild);
+ var vergadering = await GetEntityAsync<vergaderingType>(entry.Content.FirstChild, cancellationToken);
break;
case "verslag":
- var verslag = await GetEntityAsync<verslagType>(entry.Content.FirstChild);
+ var verslag = await GetEntityAsync<verslagType>(entry.Content.FirstChild, cancellationToken);
break;
case "activiteit":
- var activiteit = await GetEntityAsync<activiteitType>(entry.Content.FirstChild);
+ var activiteit = await GetEntityAsync<activiteitType>(entry.Content.FirstChild, cancellationToken);
break;
case "documentVersie":
- var documentversie = await GetEntityAsync<documentVersieType>(entry.Content.FirstChild);
+ var documentversie = await GetEntityAsync<documentVersieType>(entry.Content.FirstChild, cancellationToken);
break;
case "document":
- var document = await GetEntityAsync<documentType>(entry.Content.FirstChild); // document.Soort == "Motie"
+ var document = await GetEntityAsync<documentType>(entry.Content.FirstChild, cancellationToken); // document.Soort == "Motie"
break;
case "zaak":
- var zaak = await GetEntityAsync<zaakType>(entry.Content.FirstChild);
+ var zaak = await GetEntityAsync<zaakType>(entry.Content.FirstChild, cancellationToken);
break;
case "agendapunt":
- var agendapunt = await GetEntityAsync<agendapuntType>(entry.Content.FirstChild);
+ var agendapunt = await GetEntityAsync<agendapuntType>(entry.Content.FirstChild, cancellationToken);
break;
case "besluit":
- var besluit = await GetEntityAsync<besluitType>(entry.Content.FirstChild);
+ var besluit = await GetEntityAsync<besluitType>(entry.Content.FirstChild, cancellationToken);
break;
case "kamerstukdossier":
- var kamerstukdossier = await GetEntityAsync<kamerstukdossierType>(entry.Content.FirstChild);
+ var kamerstukdossier = await GetEntityAsync<kamerstukdossierType>(entry.Content.FirstChild, cancellationToken);
break;
case "activiteitActor":
- var activiteitActor = await GetEntityAsync<activiteitActorType>(entry.Content.FirstChild);
+ var activiteitActor = await GetEntityAsync<activiteitActorType>(entry.Content.FirstChild, cancellationToken);
break;
case "zaakActor":
- var zaakActor = await GetEntityAsync<zaakActorType>(entry.Content.FirstChild);
+ var zaakActor = await GetEntityAsync<zaakActorType>(entry.Content.FirstChild, cancellationToken);
break;
case "documentActor":
- var documentActor = await GetEntityAsync<documentActorType>(entry.Content.FirstChild);
+ var documentActor = await GetEntityAsync<documentActorType>(entry.Content.FirstChild, cancellationToken);
break;
case "stemming":
- var stemming = await GetEntityAsync<stemmingType>(entry.Content.FirstChild);
+ var stemming = await GetEntityAsync<stemmingType>(entry.Content.FirstChild, cancellationToken);
break;
case "persoon":
- var persoon = await GetEntityAsync<persoonType>(entry.Content.FirstChild);
+ var persoon = await GetEntityAsync<persoonType>(entry.Content.FirstChild, cancellationToken);
break;
case "fractie":
- var fractie = await GetEntityAsync<fractieType>(entry.Content.FirstChild);
+ var fractie = await GetEntityAsync<fractieType>(entry.Content.FirstChild, cancellationToken);
break;
case "persoonNevenfunctie":
- var persoonNevenfunctie = await GetEntityAsync<persoonNevenfunctieType>(entry.Content.FirstChild);
+ var persoonNevenfunctie = await GetEntityAsync<persoonNevenfunctieType>(entry.Content.FirstChild, cancellationToken);
break;
case "commissie":
- var commissie = await GetEntityAsync<commissieType>(entry.Content.FirstChild);
+ var commissie = await GetEntityAsync<commissieType>(entry.Content.FirstChild, cancellationToken);
break;
case "persoonGeschenk":
- var persoonGeschenk = await GetEntityAsync<persoonGeschenkType>(entry.Content.FirstChild);
+ var persoonGeschenk = await GetEntityAsync<persoonGeschenkType>(entry.Content.FirstChild, cancellationToken);
break;
case "persoonReis":
- var persoonReis = await GetEntityAsync<persoonReisType>(entry.Content.FirstChild);
+ var persoonReis = await GetEntityAsync<persoonReisType>(entry.Content.FirstChild, cancellationToken);
break;
case "persoonLoopbaan":
- var persoonLoopbaan = await GetEntityAsync<persoonLoopbaanType>(entry.Content.FirstChild);
+ var persoonLoopbaan = await GetEntityAsync<persoonLoopbaanType>(entry.Content.FirstChild, cancellationToken);
break;
case "persoonOnderwijs":
- var persoonOnderwijs = await GetEntityAsync<persoonOnderwijsType>(entry.Content.FirstChild);
+ var persoonOnderwijs = await GetEntityAsync<persoonOnderwijsType>(entry.Content.FirstChild, cancellationToken);
break;
case "commissieZetel":
- var commissieZetel = await GetEntityAsync<commissieZetelType>(entry.Content.FirstChild);
+ var commissieZetel = await GetEntityAsync<commissieZetelType>(entry.Content.FirstChild, cancellationToken);
break;
case "commissieZetelVastPersoon":
- var commissieZetelVastPersoon = await GetEntityAsync<commissieZetelVastPersoonType>(entry.Content.FirstChild);
+ var commissieZetelVastPersoon = await GetEntityAsync<commissieZetelVastPersoonType>(entry.Content.FirstChild, cancellationToken);
break;
case "commissieZetelVervangerVacature":
- var commissieZetelVervangerVacature = await GetEntityAsync<commissieZetelVervangerVacatureType>(entry.Content.FirstChild);
+ var commissieZetelVervangerVacature = await GetEntityAsync<commissieZetelVervangerVacatureType>(entry.Content.FirstChild, cancellationToken);
break;
case "commissieZetelVervangerPersoon":
- var commissieZetelVervangerPersoon = await GetEntityAsync<commissieZetelVervangerPersoonType>(entry.Content.FirstChild);
+ var commissieZetelVervangerPersoon = await GetEntityAsync<commissieZetelVervangerPersoonType>(entry.Content.FirstChild, cancellationToken);
break;
case "commissieZetelVastVacature":
- var commissieZetelVastVacature = await GetEntityAsync<commissieZetelVastVacatureType>(entry.Content.FirstChild);
+ var commissieZetelVastVacature = await GetEntityAsync<commissieZetelVastVacatureType>(entry.Content.FirstChild, cancellationToken);
break;
case "fractieZetel":
- var fractieZetel = await GetEntityAsync<fractieZetelType>(entry.Content.FirstChild);
+ var fractieZetel = await GetEntityAsync<fractieZetelType>(entry.Content.FirstChild, cancellationToken);
break;
case "fractieZetelPersoon":
- var fractieZetelPersoon = await GetEntityAsync<fractieZetelPersoonType>(entry.Content.FirstChild);
+ var fractieZetelPersoon = await GetEntityAsync<fractieZetelPersoonType>(entry.Content.FirstChild, cancellationToken);
break;
case "fractieZetelVacature":
- var fractieZetelVacature = await GetEntityAsync<fractieZetelVacatureType>(entry.Content.FirstChild);
+ var fractieZetelVacature = await GetEntityAsync<fractieZetelVacatureType>(entry.Content.FirstChild, cancellationToken);
break;
case "persoonNevenfunctieInkomsten":
- var persoonNevenfunctieInkomsten = await GetEntityAsync<persoonNevenfunctieInkomstenType>(entry.Content.FirstChild);
+ var persoonNevenfunctieInkomsten = await GetEntityAsync<persoonNevenfunctieInkomstenType>(entry.Content.FirstChild, cancellationToken);
break;
case "commissieContactinformatie":
- var commissieContactinformatie = await GetEntityAsync<commissieContactinformatieType>(entry.Content.FirstChild);
+ var commissieContactinformatie = await GetEntityAsync<commissieContactinformatieType>(entry.Content.FirstChild, cancellationToken);
break;
case "persoonContactinformatie":
- var persoonContactinformatie = await GetEntityAsync<persoonContactinformatieType>(entry.Content.FirstChild);
+ var persoonContactinformatie = await GetEntityAsync<persoonContactinformatieType>(entry.Content.FirstChild, cancellationToken);
break;
}
}
-
- //Log($"{entries.Length}");
}
#pragma warning restore IDE0059
diff --git a/src/OpenKamer.LogicControllers/FeedController.cs b/src/OpenKamer.LogicControllers/FeedController.cs
index dba267b..3ed069a 100644
--- a/src/OpenKamer.LogicControllers/FeedController.cs
+++ b/src/OpenKamer.LogicControllers/FeedController.cs
@@ -5,84 +5,81 @@
// https://opendata.tweedekamer.nl/documentatie/syncfeed-api
//
-using System.Xml;
+using System.Xml.Linq;
namespace OpenKamer.LogicControllers;
-
public class FeedController
{
private static readonly string FEED = "https://gegevensmagazijn.tweedekamer.nl/SyncFeed/2.0/";
private string DB = @"\\192.168.28.195\f$\gegevensmagazijn";
- public FeedController(string DB)
+ public FeedController(string DB, string skipToken)
{
this.DB = DB;
+ this.SkipToken = skipToken;
+ this.Status = string.Empty;
}
public event EventHandler? OnLog;
- public string LastSkipToken { get; set; }
+ public string Status { get; set; }
+
+ public string SkipToken { get; set; }
+
+ public int FileCount { get; set; }
private void Log(string message)
{
OnLog?.Invoke(message, new EventArgs());
}
-
- async public Task SyncFeedAsync(string? skiptoken)
+ async public Task SyncFeedAsync(CancellationToken cancellationToken)
{
Log("SyncFeedAsync");
HttpClient httpclient = new();
- XmlDocument xmlDoc = new();
+ XDocument xDoc;
+
+ XNamespace ns = "http://www.w3.org/2005/Atom";
+
+ string? urlFeed = FEED;
- string urlFeed = FEED;
+ this.FileCount = 0;
- if (string.IsNullOrWhiteSpace(skiptoken) == false)
- urlFeed += "?skiptoken=" + skiptoken;
+ if (string.IsNullOrWhiteSpace(SkipToken) == false)
+ urlFeed += "?skiptoken=" + this.SkipToken;
- if (string.IsNullOrWhiteSpace(skiptoken))
- skiptoken = "empty";
+ if (string.IsNullOrWhiteSpace(this.SkipToken))
+ this.SkipToken = "0";
do
{
- LastSkipToken = skiptoken;
+ this.FileCount++;
- Log("Skiptoken: " + skiptoken);
-
- string FileName = DB + @"\" + skiptoken + ".xml";
+ var FileName = DB + @"\" + this.SkipToken + ".xml";
if (File.Exists(FileName))
{
- Log("Cached");
-
- xmlDoc.Load(FileName);
+ Status = "cached";
}
else
{
- var xml = await httpclient.GetStringAsync(urlFeed);
-
- await File.WriteAllTextAsync(FileName, xml);
+ var xml = await httpclient.GetStringAsync(urlFeed, cancellationToken);
- Log("Saved");
+ await File.WriteAllTextAsync(FileName, xml, cancellationToken);
- xmlDoc.LoadXml(xml);
+ Status = "saved";
}
- XmlNamespaceManager nsmgr = new(xmlDoc.NameTable);
- if (xmlDoc.DocumentElement != null)
- nsmgr.AddNamespace("ns", xmlDoc.DocumentElement.NamespaceURI);
+ using var textReader = File.OpenText(FileName);
+ xDoc = await XDocument.LoadAsync(textReader, LoadOptions.None, cancellationToken);
- if (nsmgr == null)
- {
- Log("XmlNamespaceManager missing");
- return;
- }
+ var links = xDoc.Descendants(ns + "link");
- var resumeNode = xmlDoc.SelectSingleNode("/ns:feed/ns:link[@rel='resume']", nsmgr);
+ var resumeNode = links.FirstOrDefault(x => x.Attribute("rel")?.Value == "resume");
if(resumeNode != null)
{
@@ -90,7 +87,7 @@ public class FeedController
return;
}
- var nextNode = xmlDoc.SelectSingleNode("/ns:feed/ns:link[@rel='next']", nsmgr);
+ var nextNode = links.FirstOrDefault(x => x.Attribute("rel")?.Value == "next");
if (nextNode == null)
{
@@ -98,21 +95,7 @@ public class FeedController
return;
}
- if (nextNode.Attributes == null)
- {
- Log("Abnormal Attributes exit");
- return;
- }
-
- var attr = nextNode.Attributes["href"];
-
- if (attr == null)
- {
- Log("Abnormal href exit");
- return;
- }
-
- urlFeed = attr.Value;
+ urlFeed = nextNode.Attribute("href")?.Value;
if (urlFeed == null)
{
@@ -127,9 +110,9 @@ public class FeedController
return;
}
- skiptoken = urlFeed[(intI + 10)..];
+ this.SkipToken = urlFeed[(intI + 10)..];
- } while (!string.IsNullOrWhiteSpace(skiptoken));
+ } while (!string.IsNullOrWhiteSpace(this.SkipToken));
Log("Abnormal empty skiptoken exit");
}
diff --git a/src/OpenKamer.WinForm/Form1.Designer.cs b/src/OpenKamer.WinForm/Form1.Designer.cs
index 2199a04..ebcf2ed 100644
--- a/src/OpenKamer.WinForm/Form1.Designer.cs
+++ b/src/OpenKamer.WinForm/Form1.Designer.cs
@@ -30,8 +30,9 @@
{
this.components = new System.ComponentModel.Container();
this.button1 = new System.Windows.Forms.Button();
- this.textBox1 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
+ this.ButtonStop = new System.Windows.Forms.Button();
+ this.textBox1 = new System.Windows.Forms.TextBox();
this.txtSkipToken = new System.Windows.Forms.TextBox();
this.txtMongoConnectionString = new System.Windows.Forms.TextBox();
this.txtDbFileSystem = new System.Windows.Forms.TextBox();
@@ -47,6 +48,9 @@
this.label1 = new System.Windows.Forms.Label();
this.txtDbName = new System.Windows.Forms.TextBox();
this.timer1 = new System.Windows.Forms.Timer(this.components);
+ this.timer2 = new System.Windows.Forms.Timer(this.components);
+ this.labelStatus = new System.Windows.Forms.Label();
+ this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
@@ -60,6 +64,27 @@
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.Button1_Click);
//
+ // button2
+ //
+ this.button2.Location = new System.Drawing.Point(243, 50);
+ this.button2.Name = "button2";
+ this.button2.Size = new System.Drawing.Size(94, 23);
+ this.button2.TabIndex = 2;
+ this.button2.Text = "Get Entries";
+ this.button2.UseVisualStyleBackColor = true;
+ this.button2.Click += new System.EventHandler(this.Button2_Click);
+ //
+ // ButtonStop
+ //
+ this.ButtonStop.Enabled = false;
+ this.ButtonStop.Location = new System.Drawing.Point(255, 187);
+ this.ButtonStop.Name = "ButtonStop";
+ this.ButtonStop.Size = new System.Drawing.Size(94, 23);
+ this.ButtonStop.TabIndex = 16;
+ this.ButtonStop.Text = "Stop";
+ this.ButtonStop.UseVisualStyleBackColor = true;
+ this.ButtonStop.Click += new System.EventHandler(this.ButtonStop_Click);
+ //
// textBox1
//
this.textBox1.AcceptsReturn = true;
@@ -75,16 +100,6 @@
this.textBox1.Size = new System.Drawing.Size(306, 243);
this.textBox1.TabIndex = 1;
//
- // button2
- //
- this.button2.Location = new System.Drawing.Point(243, 50);
- this.button2.Name = "button2";
- this.button2.Size = new System.Drawing.Size(94, 23);
- this.button2.TabIndex = 2;
- this.button2.Text = "Get Entries";
- this.button2.UseVisualStyleBackColor = true;
- this.button2.Click += new System.EventHandler(this.Button2_Click);
- //
// txtSkipToken
//
this.txtSkipToken.Location = new System.Drawing.Point(67, 50);
@@ -129,7 +144,7 @@
this.groupBox1.Controls.Add(this.txtSkipToken);
this.groupBox1.Location = new System.Drawing.Point(12, 12);
this.groupBox1.Name = "groupBox1";
- this.groupBox1.Size = new System.Drawing.Size(342, 243);
+ this.groupBox1.Size = new System.Drawing.Size(342, 169);
this.groupBox1.TabIndex = 6;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "settings";
@@ -226,13 +241,37 @@
// timer1
//
this.timer1.Interval = 1000;
- this.timer1.Tick += new System.EventHandler(this.Timer1_Tick);
+ this.timer1.Tick += new System.EventHandler(this.EntityController_Tick);
+ //
+ // timer2
+ //
+ this.timer2.Interval = 1000;
+ this.timer2.Tick += new System.EventHandler(this.FeedController_Tick);
+ //
+ // labelStatus
+ //
+ this.labelStatus.AutoSize = true;
+ this.labelStatus.Location = new System.Drawing.Point(22, 195);
+ this.labelStatus.Name = "labelStatus";
+ this.labelStatus.Size = new System.Drawing.Size(16, 15);
+ this.labelStatus.TabIndex = 17;
+ this.labelStatus.Text = "...";
+ //
+ // progressBar1
+ //
+ this.progressBar1.Location = new System.Drawing.Point(10, 242);
+ this.progressBar1.Name = "progressBar1";
+ this.progressBar1.Size = new System.Drawing.Size(339, 12);
+ this.progressBar1.TabIndex = 18;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(678, 267);
+ this.Controls.Add(this.progressBar1);
+ this.Controls.Add(this.labelStatus);
+ this.Controls.Add(this.ButtonStop);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
@@ -248,8 +287,9 @@
#endregion
private Button button1;
- private TextBox textBox1;
private Button button2;
+ private Button ButtonStop;
+ private TextBox textBox1;
private TextBox txtSkipToken;
private TextBox txtMongoConnectionString;
private TextBox txtDbFileSystem;
@@ -265,5 +305,8 @@
private Label lblCount;
private Label label5;
private System.Windows.Forms.Timer timer1;
+ private System.Windows.Forms.Timer timer2;
+ private Label labelStatus;
+ private ProgressBar progressBar1;
}
}
\ No newline at end of file
diff --git a/src/OpenKamer.WinForm/Form1.cs b/src/OpenKamer.WinForm/Form1.cs
index a4c2e04..efcdb01 100644
--- a/src/OpenKamer.WinForm/Form1.cs
+++ b/src/OpenKamer.WinForm/Form1.cs
@@ -1,142 +1,182 @@
//
// (c) 2022, Alphons van der Heijden
//
-
-// nuget MongoDB.MvcCore
-
-using MongoDB.Bson;
-using MongoDB.Driver;
-using MongoDB.MvcCore;
-
using System.Diagnostics;
using OpenKamer.LogicControllers;
-namespace OpenKamer.WinForm
+namespace OpenKamer.WinForm;
+
+public partial class Form1 : Form
{
- public partial class Form1 : Form
+ private Stopwatch? sw;
+
+ private CancellationTokenSource? cancelSource;
+
+ private EntityController? entityController1;
+ private FeedController? feedController1;
+
+ public Form1()
{
- private IMongoDatabase? db;
+ InitializeComponent();
- private Stopwatch? sw;
- private long lngError;
- private long lngCount;
+ //this.Text += $" (MongoDB.MvcCore.BsonJsonSerializer: {typeof(BsonJsonSerializer).Assembly.GetName().Version})";
+ }
- public Form1()
+ private void OnLog(object? sender, EventArgs e)
+ {
+ Log(sender + Environment.NewLine);
+ }
+
+ private void Log(string line)
+ {
+ this.textBox1.Invoke((MethodInvoker)delegate
{
- InitializeComponent();
+ this.textBox1.AppendText(line);
+ });
+ }
- this.Text += $" (MongoDB.MvcCore.BsonJsonSerializer: {typeof(BsonJsonSerializer).Assembly.GetName().Version})";
- }
+ private void Form1_Load(object sender, EventArgs e)
+ {
+ var lastXml = Directory.GetFileSystemEntries(this.txtDbFileSystem.Text, "*.xml")
+ .OrderByDescending(x => x)
+ .ToList()
+ .FirstOrDefault();
+
+ if (lastXml == null)
+ return;
+
+ var last = long.Parse(Path.GetFileNameWithoutExtension(lastXml));
+ this.txtSkipToken.Text = last.ToString();
+ }
+
+ private void ButtonStop_Click(object sender, EventArgs e)
+ {
+ this.cancelSource?.Cancel();
+
+ this.ButtonStop.Enabled = false;
+ this.groupBox1.Enabled = true;
+ }
+
+
+ async private void Button1_Click(object sender, EventArgs e)
+ {
+ this.cancelSource = new();
+
+ this.groupBox1.Enabled = false;
- private void OnLog(object? sender, EventArgs e)
+ this.ButtonStop.Enabled = true;
+
+ this.lblErrors.Text = "0";
+
+ this.progressBar1.Value = 0;
+
+ // txtSkipToken whitespace tot start from 0
+ this.feedController1 = new FeedController(this.txtDbFileSystem.Text, this.txtSkipToken.Text);
+
+ this.feedController1.OnLog += OnLog;
+
+ this.sw = Stopwatch.StartNew();
+ this.timer2.Start();
+
+ try
{
- Log(string.Empty+sender);
+ await this.feedController1.SyncFeedAsync(cancelSource.Token);
}
-
- private void Log(string line)
+ catch(OperationCanceledException)
{
- this.textBox1.Invoke((MethodInvoker)delegate
- {
- this.textBox1.AppendText(line + Environment.NewLine);
- });
+
}
- async private void Button1_Click(object sender, EventArgs e)
- {
- this.groupBox1.Enabled = false;
+ this.timer2.Stop();
- FeedController feedController = new(this.txtDbFileSystem.Text);
+ this.txtSkipToken.Text = this.feedController1.SkipToken;
- feedController.OnLog += OnLog;
+ this.ButtonStop.Enabled = false;
- await feedController.SyncFeedAsync(this.txtSkipToken.Text); // whitespace tot build DB
+ this.groupBox1.Enabled = true;
- this.txtSkipToken.Text = feedController.LastSkipToken;
+ this.labelStatus.Text = "...";
- this.groupBox1.Enabled = true;
- }
+ this.progressBar1.Value = 0;
+ }
- async private void Button2_Click(object sender, EventArgs e)
- {
- this.groupBox1.Enabled = false;
+ async private void Button2_Click(object sender, EventArgs e)
+ {
+ this.labelStatus.Text = "...";
- MongoClient mongo = new(this.txtMongoConnectionString.Text);
- this.db = mongo.GetDatabase(this.txtDbName.Text);
+ this.progressBar1.Value = 0;
- this.lngCount = 0;
- this.lngError = 0;
+ this.cancelSource = new();
- this.sw = Stopwatch.StartNew();
- this.timer1.Start();
+ this.groupBox1.Enabled = false;
- EntityController entityController = new(this.txtDbFileSystem.Text);
- entityController.OnLog += OnLog;
- entityController.OnEntity += EntityController_OnEntity;
- await entityController.DecodeEntriesAsync(this.txtSkipToken.Text); // whitespace tot start from beginning
+ this.ButtonStop.Enabled = true;
- this.timer1.Stop();
+ this.sw = Stopwatch.StartNew();
+ this.timer1.Start();
- this.groupBox1.Enabled = true;
- }
+ // txtSkipToken whitespace tot start from beginning
+ this.entityController1 = new EntityController(this.txtMongoConnectionString.Text, this.txtDbName.Text, this.txtDbFileSystem.Text, this.txtSkipToken.Text);
+ this.entityController1.OnLog += OnLog;
- /// <summary>
- /// Replacing (or adding) entity to mongo db
- /// </summary>
- /// <param name="entity"></param>
- /// <param name="e"></param>
- /// <returns></returns>
- async private Task EntityController_OnEntity(object entity, EventArgs e)
+ try
{
- if (db == null)
- return;
-
- var collection = this.db.GetCollection(entity.GetType().Name);
-
- if (collection == null)
- return;
-
- try
- {
- var replaceOneResult = await collection.ReplaceOneByIdAsync(entity);
- this.lngCount++;
- }
- catch (BsonSerializationException)
- {
- this.lngError++;
- }
- catch (MongoWriteException)
- {
- this.lngError++;
- }
- catch (Exception)
- {
- this.lngError++;
- }
+ await this.entityController1.DecodeEntriesAsync(this.cancelSource.Token);
}
-
- private void Timer1_Tick(object sender, EventArgs e)
+ catch (OperationCanceledException)
{
- this.lblCount.Text = this.lngCount.ToString();
- this.lblErrors.Text = this.lngError.ToString();
- if (this.sw == null)
- return;
+ }
- var speed = (1000 * this.lngCount) / this.sw.ElapsedMilliseconds;
+ this.txtSkipToken.Text = this.entityController1.SkipToken;
- this.lblSpeed.Text = $"{speed} e/sec";
+ this.timer1.Stop();
- }
+ this.ButtonStop.Enabled = false;
- private void Form1_Load(object sender, EventArgs e)
- {
- var last = Directory.GetFileSystemEntries(this.txtDbFileSystem.Text, "*.xml")
- .Select(x => long.Parse(Path.GetFileNameWithoutExtension(x)))
- .ToList()
- .OrderByDescending(x => x)
- .FirstOrDefault();
- this.txtSkipToken.Text = last.ToString();
- }
+ this.groupBox1.Enabled = true;
+
+ this.progressBar1.Value = 0;
}
+
+
+
+ private void EntityController_Tick(object sender, EventArgs e)
+ {
+ if (this.sw == null || this.entityController1 == null)
+ return;
+
+ this.lblCount.Text = this.entityController1.EntityCount.ToString();
+ this.lblErrors.Text = this.entityController1.Error.ToString();
+
+ this.progressBar1.Value = (this.entityController1.FileCount / 200);
+
+ this.labelStatus.Text = $"Files: {this.entityController1.FileCount}";
+
+ var speed = (1000 * this.entityController1.EntityCount) / this.sw.ElapsedMilliseconds;
+
+ this.lblSpeed.Text = $"{speed} e/sec";
+
+ this.txtSkipToken.Text = this.entityController1.SkipToken;
+ }
+
+ private void FeedController_Tick(object sender, EventArgs e)
+ {
+ if (this.sw == null || this.feedController1 == null)
+ return;
+
+ this.lblCount.Text = this.feedController1.FileCount.ToString();
+
+ this.progressBar1.Value = (this.feedController1.FileCount / 200);
+
+ var speed = (1000 * this.feedController1.FileCount) / this.sw.ElapsedMilliseconds;
+
+ this.lblSpeed.Text = $"{speed} e/sec";
+
+ this.txtSkipToken.Text = this.feedController1.SkipToken;
+
+ this.labelStatus.Text = this.feedController1.Status;
+ }
+
}
\ No newline at end of file
diff --git a/src/OpenKamer.WinForm/Form1.resx b/src/OpenKamer.WinForm/Form1.resx
index d731088..a94009a 100644
--- a/src/OpenKamer.WinForm/Form1.resx
+++ b/src/OpenKamer.WinForm/Form1.resx
@@ -60,4 +60,10 @@
<metadata name="timer1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
+ <metadata name="timer2.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+ <value>108, 17</value>
+ </metadata>
+ <metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
+ <value>55</value>
+ </metadata>
</root>
\ No newline at end of file