moties default cached (or refresh)

alphons <alphons@heijden.com> 8 Dec 2022, 16:26
868b3b78dfb562f88a480a3bab8ad5545cfaddf1
7 files changed
  • src/OpenKamer.LogicControllers/FeedController.cs
  • src/OpenKamer.LogicControllers/Web/Moties.cs
  • src/OpenKamer.LogicControllers/Web/MotiesCached.cs
  • src/OpenKamer.WebCore/wwwroot/Moties.htm
  • src/OpenKamer.WebCore/wwwroot/Moties.js
  • src/OpenKamer.WinForm/Form1.Designer.cs
  • src/OpenKamer.WinForm/Form1.cs
diff --git a/src/OpenKamer.LogicControllers/FeedController.cs b/src/OpenKamer.LogicControllers/FeedController.cs
index 4d78f3e..dba267b 100644
--- a/src/OpenKamer.LogicControllers/FeedController.cs
+++ b/src/OpenKamer.LogicControllers/FeedController.cs
@@ -13,129 +13,124 @@ 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)
- {
- this.DB = DB;
- }
-
- public event EventHandler? OnLog;
+ private string DB = @"\\192.168.28.195\f$\gegevensmagazijn";
+ public FeedController(string DB)
+ {
+ this.DB = DB;
+ }
- private void Log(string message)
- {
- OnLog?.Invoke(message, new EventArgs());
- }
-
+ public event EventHandler? OnLog;
- async public Task SyncFeedAsync(string? skiptoken)
- {
- Log("SyncFeedAsync");
+ public string LastSkipToken { get; set; }
- XmlNamespaceManager? nsmgr = null;
+ private void Log(string message)
+ {
+ OnLog?.Invoke(message, new EventArgs());
+ }
- HttpClient httpclient = new();
- XmlDocument xmlDoc = new();
+ async public Task SyncFeedAsync(string? skiptoken)
+ {
+ Log("SyncFeedAsync");
- string urlFeed = FEED;
+ HttpClient httpclient = new();
- if (string.IsNullOrWhiteSpace(skiptoken) == false)
- urlFeed += "?skiptoken=" + skiptoken;
+ XmlDocument xmlDoc = new();
- if (string.IsNullOrWhiteSpace(skiptoken))
- skiptoken = "empty";
+ string urlFeed = FEED;
- do
- {
- Log("Skiptoken: " + skiptoken);
+ if (string.IsNullOrWhiteSpace(skiptoken) == false)
+ urlFeed += "?skiptoken=" + skiptoken;
- string FileName = DB + @"\" + skiptoken + ".xml";
+ if (string.IsNullOrWhiteSpace(skiptoken))
+ skiptoken = "empty";
- var refresh = true;
+ do
+ {
+ LastSkipToken = skiptoken;
- if (File.Exists(FileName))
- {
- xmlDoc.Load(FileName);
+ Log("Skiptoken: " + skiptoken);
- nsmgr = new(xmlDoc.NameTable);
- if (xmlDoc.DocumentElement != null)
- nsmgr.AddNamespace("ns", xmlDoc.DocumentElement.NamespaceURI);
+ string FileName = DB + @"\" + skiptoken + ".xml";
- var resumeNode = xmlDoc.SelectSingleNode("/ns:feed/ns:link[@rel='resume']", nsmgr);
+ if (File.Exists(FileName))
+ {
+ Log("Cached");
- refresh = resumeNode != null;
+ xmlDoc.Load(FileName);
+ }
+ else
+ {
+ var xml = await httpclient.GetStringAsync(urlFeed);
- if (refresh)
- Log("Refresh");
- else
- Log("Cached");
- }
+ await File.WriteAllTextAsync(FileName, xml);
- if (refresh)
- {
- var xml = await httpclient.GetStringAsync(urlFeed);
+ Log("Saved");
- await File.WriteAllTextAsync(FileName, xml);
+ xmlDoc.LoadXml(xml);
+ }
- Log("Saved");
+ XmlNamespaceManager nsmgr = new(xmlDoc.NameTable);
+ if (xmlDoc.DocumentElement != null)
+ nsmgr.AddNamespace("ns", xmlDoc.DocumentElement.NamespaceURI);
- xmlDoc.LoadXml(xml);
+ if (nsmgr == null)
+ {
+ Log("XmlNamespaceManager missing");
+ return;
+ }
- nsmgr = new(xmlDoc.NameTable);
- if (xmlDoc.DocumentElement != null)
- nsmgr.AddNamespace("ns", xmlDoc.DocumentElement.NamespaceURI);
- }
+ var resumeNode = xmlDoc.SelectSingleNode("/ns:feed/ns:link[@rel='resume']", nsmgr);
- if (nsmgr == null)
- {
- Log("XmlNamespaceManager missing");
- return;
- }
+ if(resumeNode != null)
+ {
+ Log("Resume exit");
+ return;
+ }
- var nextNode = xmlDoc.SelectSingleNode("/ns:feed/ns:link[@rel='next']", nsmgr);
+ var nextNode = xmlDoc.SelectSingleNode("/ns:feed/ns:link[@rel='next']", nsmgr);
- if (nextNode == null)
- {
- Log("Normal exit");
- return;
- }
+ if (nextNode == null)
+ {
+ Log("Normal exit");
+ return;
+ }
- if (nextNode.Attributes == null)
- {
- Log("Abnormal Attributes exit");
- return;
- }
+ if (nextNode.Attributes == null)
+ {
+ Log("Abnormal Attributes exit");
+ return;
+ }
- var attr = nextNode.Attributes["href"];
+ var attr = nextNode.Attributes["href"];
- if (attr == null)
- {
- Log("Abnormal href exit");
- return;
- }
+ if (attr == null)
+ {
+ Log("Abnormal href exit");
+ return;
+ }
- urlFeed = attr.Value;
+ urlFeed = attr.Value;
- if (urlFeed == null)
- {
- Log("Abnormal feed exit");
- return;
- }
+ if (urlFeed == null)
+ {
+ Log("Abnormal feed exit");
+ return;
+ }
- var intI = urlFeed.IndexOf("skiptoken=");
- if (intI < 0)
- {
- Log("Abnormal skiptoken exit");
- return;
- }
+ var intI = urlFeed.IndexOf("skiptoken=");
+ if (intI < 0)
+ {
+ Log("Abnormal skiptoken exit");
+ return;
+ }
- skiptoken = urlFeed[(intI + 10)..];
+ skiptoken = urlFeed[(intI + 10)..];
- } while (!string.IsNullOrWhiteSpace(skiptoken));
+ } while (!string.IsNullOrWhiteSpace(skiptoken));
- Log("Abnormal empty skiptoken exit");
- }
+ Log("Abnormal empty skiptoken exit");
+ }
}
diff --git a/src/OpenKamer.LogicControllers/Web/Moties.cs b/src/OpenKamer.LogicControllers/Web/Moties.cs
index a2fc485..a9a3ec6 100644
--- a/src/OpenKamer.LogicControllers/Web/Moties.cs
+++ b/src/OpenKamer.LogicControllers/Web/Moties.cs
@@ -9,7 +9,7 @@ public partial class WebController : ControllerBase
{
[HttpPost]
[Route("~/api/Moties")]
- public async Task<IActionResult> Moties()
+ public async Task<IActionResult> Moties(bool MakeCache = false)
{
// documentType
// onderwerp: { $regex: '.*Motie van het lid Bromet.*', $options: 'i' },
@@ -150,7 +150,7 @@ public partial class WebController : ControllerBase
{
$project:
{
- _id : 1,
+ _id : 0,
onderwerp:1,
documentNummer:1,
@@ -167,13 +167,19 @@ public partial class WebController : ControllerBase
}
},
{
- $sort: { date:-1, _id:1 }
+ $sort: { date:-1, documentNummer:1 }
}
{ $skip: 0 },
-{ $limit: 100 }
+{ $limit: 20 }
]
";
+ if(MakeCache)
+ {
+ var i = a.LastIndexOf(']');
+ a = string.Concat(a.AsSpan(0, i), ",{ $out: { db:'public', coll:'moties'} } ]");
+ }
+
var List = await db.GetCollection("documentType").Aggregate(a).ToListAsync();
return Ok(new
diff --git a/src/OpenKamer.LogicControllers/Web/MotiesCached.cs b/src/OpenKamer.LogicControllers/Web/MotiesCached.cs
index 19d97c1..148fbb7 100644
--- a/src/OpenKamer.LogicControllers/Web/MotiesCached.cs
+++ b/src/OpenKamer.LogicControllers/Web/MotiesCached.cs
@@ -11,12 +11,16 @@ public partial class WebController : ControllerBase
[Route("~/api/MotiesCached")]
public async Task<IActionResult> MotiesCached()
{
- // documentType
var pub = mongo.GetDatabase("public");
var a = @"[ { $skip: 0 }, { $limit: 100 } ]";
- var List = await pub.GetCollection("list").Aggregate(a).ToListAsync();
+ //var count = await pub.GetCollection("moties").EstimatedDocumentCountAsync();
+
+ //if (count == 0)
+ // await Moties(true);
+
+ var List = await pub.GetCollection("moties").Aggregate(a).ToListAsync();
return Ok(new
{
diff --git a/src/OpenKamer.WebCore/wwwroot/Moties.htm b/src/OpenKamer.WebCore/wwwroot/Moties.htm
index 3907f92..42351d0 100644
--- a/src/OpenKamer.WebCore/wwwroot/Moties.htm
+++ b/src/OpenKamer.WebCore/wwwroot/Moties.htm
@@ -17,7 +17,7 @@
<body>
<img src="images/netproxyspinner.gif" id="netproxyspinner" />
- <input type="checkbox" id="UseCache" />
+ <div><input type="checkbox" id="RefreshCache" />Refresh Cache</div>
<div id="Version"></div>
diff --git a/src/OpenKamer.WebCore/wwwroot/Moties.js b/src/OpenKamer.WebCore/wwwroot/Moties.js
index 4e27b94..04fb539 100644
--- a/src/OpenKamer.WebCore/wwwroot/Moties.js
+++ b/src/OpenKamer.WebCore/wwwroot/Moties.js
@@ -18,19 +18,19 @@ function PageEvents()
window[e.target.id].call(e, e);
});
- $id("UseCache").on("click", function (e)
+ $id("RefreshCache").on("click", function (e)
{
- if ($id("UseCache").checked)
- MotiesCached();
+ if ($id("RefreshCache").checked)
+ Moties();
else
- Moties2();
+ MotiesCached();
});
}
function Init()
{
HelloWorld();
- Moties();
+ MotiesCached();
}
function NewApi(url, datain, outputelement, templateelement, append)
@@ -52,7 +52,7 @@ function HelloWorld()
function Moties()
{
- NewApi("./api/Moties", {}, Output, TemplateMoties, false);
+ NewApi("./api/Moties", { MakeCache : true }, Output, TemplateMoties, false);
}
function MotiesCached()
diff --git a/src/OpenKamer.WinForm/Form1.Designer.cs b/src/OpenKamer.WinForm/Form1.Designer.cs
index 7925fde..2199a04 100644
--- a/src/OpenKamer.WinForm/Form1.Designer.cs
+++ b/src/OpenKamer.WinForm/Form1.Designer.cs
@@ -36,16 +36,16 @@
this.txtMongoConnectionString = new System.Windows.Forms.TextBox();
this.txtDbFileSystem = new System.Windows.Forms.TextBox();
this.groupBox1 = new System.Windows.Forms.GroupBox();
+ this.lblSpeed = new System.Windows.Forms.Label();
+ this.lblErrors = new System.Windows.Forms.Label();
+ this.label8 = new System.Windows.Forms.Label();
+ this.lblCount = new System.Windows.Forms.Label();
+ this.label5 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.txtDbName = new System.Windows.Forms.TextBox();
- this.label5 = new System.Windows.Forms.Label();
- this.lblCount = new System.Windows.Forms.Label();
- this.lblErrors = new System.Windows.Forms.Label();
- this.label8 = new System.Windows.Forms.Label();
- this.lblSpeed = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.groupBox1.SuspendLayout();
this.SuspendLayout();
@@ -91,7 +91,6 @@
this.txtSkipToken.Name = "txtSkipToken";
this.txtSkipToken.Size = new System.Drawing.Size(70, 23);
this.txtSkipToken.TabIndex = 3;
- this.txtSkipToken.Text = "15113529";
//
// txtMongoConnectionString
//
@@ -135,6 +134,51 @@
this.groupBox1.TabStop = false;
this.groupBox1.Text = "settings";
//
+ // lblSpeed
+ //
+ this.lblSpeed.AutoSize = true;
+ this.lblSpeed.Location = new System.Drawing.Point(273, 22);
+ this.lblSpeed.Name = "lblSpeed";
+ this.lblSpeed.Size = new System.Drawing.Size(16, 15);
+ this.lblSpeed.TabIndex = 15;
+ this.lblSpeed.Text = "...";
+ //
+ // lblErrors
+ //
+ this.lblErrors.AutoSize = true;
+ this.lblErrors.Location = new System.Drawing.Point(173, 22);
+ this.lblErrors.Name = "lblErrors";
+ this.lblErrors.Size = new System.Drawing.Size(16, 15);
+ this.lblErrors.TabIndex = 14;
+ this.lblErrors.Text = "...";
+ //
+ // label8
+ //
+ this.label8.AutoSize = true;
+ this.label8.Location = new System.Drawing.Point(126, 22);
+ this.label8.Name = "label8";
+ this.label8.Size = new System.Drawing.Size(40, 15);
+ this.label8.TabIndex = 13;
+ this.label8.Text = "Errors:";
+ //
+ // lblCount
+ //
+ this.lblCount.AutoSize = true;
+ this.lblCount.Location = new System.Drawing.Point(69, 22);
+ this.lblCount.Name = "lblCount";
+ this.lblCount.Size = new System.Drawing.Size(16, 15);
+ this.lblCount.TabIndex = 12;
+ this.lblCount.Text = "...";
+ //
+ // label5
+ //
+ this.label5.AutoSize = true;
+ this.label5.Location = new System.Drawing.Point(22, 22);
+ this.label5.Name = "label5";
+ this.label5.Size = new System.Drawing.Size(43, 15);
+ this.label5.TabIndex = 11;
+ this.label5.Text = "Count:";
+ //
// label4
//
this.label4.AutoSize = true;
@@ -179,51 +223,6 @@
this.txtDbName.TabIndex = 6;
this.txtDbName.Text = "tweedekamer";
//
- // label5
- //
- this.label5.AutoSize = true;
- this.label5.Location = new System.Drawing.Point(22, 22);
- this.label5.Name = "label5";
- this.label5.Size = new System.Drawing.Size(43, 15);
- this.label5.TabIndex = 11;
- this.label5.Text = "Count:";
- //
- // lblCount
- //
- this.lblCount.AutoSize = true;
- this.lblCount.Location = new System.Drawing.Point(69, 22);
- this.lblCount.Name = "lblCount";
- this.lblCount.Size = new System.Drawing.Size(16, 15);
- this.lblCount.TabIndex = 12;
- this.lblCount.Text = "...";
- //
- // lblErrors
- //
- this.lblErrors.AutoSize = true;
- this.lblErrors.Location = new System.Drawing.Point(173, 22);
- this.lblErrors.Name = "lblErrors";
- this.lblErrors.Size = new System.Drawing.Size(16, 15);
- this.lblErrors.TabIndex = 14;
- this.lblErrors.Text = "...";
- //
- // label8
- //
- this.label8.AutoSize = true;
- this.label8.Location = new System.Drawing.Point(126, 22);
- this.label8.Name = "label8";
- this.label8.Size = new System.Drawing.Size(40, 15);
- this.label8.TabIndex = 13;
- this.label8.Text = "Errors:";
- //
- // lblSpeed
- //
- this.lblSpeed.AutoSize = true;
- this.lblSpeed.Location = new System.Drawing.Point(273, 22);
- this.lblSpeed.Name = "lblSpeed";
- this.lblSpeed.Size = new System.Drawing.Size(16, 15);
- this.lblSpeed.TabIndex = 15;
- this.lblSpeed.Text = "...";
- //
// timer1
//
this.timer1.Interval = 1000;
@@ -238,6 +237,7 @@
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "3e kamer";
+ this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
diff --git a/src/OpenKamer.WinForm/Form1.cs b/src/OpenKamer.WinForm/Form1.cs
index e4965e6..a4c2e04 100644
--- a/src/OpenKamer.WinForm/Form1.cs
+++ b/src/OpenKamer.WinForm/Form1.cs
@@ -52,6 +52,8 @@ namespace OpenKamer.WinForm
await feedController.SyncFeedAsync(this.txtSkipToken.Text); // whitespace tot build DB
+ this.txtSkipToken.Text = feedController.LastSkipToken;
+
this.groupBox1.Enabled = true;
}
@@ -126,5 +128,15 @@ namespace OpenKamer.WinForm
this.lblSpeed.Text = $"{speed} e/sec";
}
+
+ 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();
+ }
}
}
\ No newline at end of file