Permalink
Browse files

Basic GUI spruce up, validation checks and reporting results. Import …

…checks connection to ElasticSearch before proceeding
  • Loading branch information...
AdaTheDev committed Feb 11, 2013
1 parent 3b2b99b commit 5250b22d43c8031aae4136a6bb359268b01f4e40
Showing with 153 additions and 17 deletions.
  1. +2 −0 ElasticTweets.Library/Importer.cs
  2. +50 −13 ElasticTweets/Form1.Designer.cs
  3. +95 −4 ElasticTweets/Form1.cs
  4. +6 −0 ElasticTweets/Form1.resx
@@ -63,6 +63,8 @@ public string SourceDirectory
public ImportResult Import()
{
var client = _clientProvider.GetClient(_elasticConnectionSettings);
+ if (!client.IsValid)
+ throw new ElasticSearchException("Could not connect to ElasticSearch. Please check the connection settings");
var results = new ImportResult();

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,4 +1,10 @@
using System;
+using System.Diagnostics;
+using System.Globalization;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
using System.Windows.Forms;
using ElasticTweets.Library;
using ElasticTweets.Library.IO;
@@ -11,17 +17,102 @@ public partial class frmImport : Form
public frmImport()
{
InitializeComponent();
- }
+ }
private void btnImport_Click(object sender, EventArgs e)
+ {
+ ToggleControls(false);
+
+ Task.Factory.StartNew(DoImport).ContinueWith(_ => ToggleControls(true));
+ }
+
+ private void DoImport()
{
+ string validationResult = ValidateInputs();
+ if (!String.IsNullOrWhiteSpace(validationResult))
+ {
+ MessageBox.Show(validationResult, @"Something's not quite right...");
+ return;
+ }
+
var fileSystem = new FileSystem();
var connectionSettings = new ElasticConnectionSettings(txtHost.Text, int.Parse(txtPort.Text), txtIndexName.Text);
- var importer = new Importer(fileSystem, new TweetDataFileParser(fileSystem), new ClientProvider(),
- connectionSettings, txtSourceDirectory.Text);
+ try
+ {
+ var stopwatch = Stopwatch.StartNew();
+ var importer = new Importer(fileSystem, new TweetDataFileParser(fileSystem), new ClientProvider(),
+ connectionSettings, txtSourceDirectory.Text);
+
+ var results = importer.Import();
+ stopwatch.Stop();
+
+ var summary = new StringBuilder();
+ summary.AppendFormat("Finished processing {0} files in {1}s.\n",
+ results.Files.Count().ToString(CultureInfo.InvariantCulture), stopwatch.Elapsed.TotalSeconds.ToString("F3"));
+ summary.AppendFormat("{0} tweets have been imported.\n", results.Files.Sum(f => f.NumberOfTweets));
+
+ if (results.Files.Any(f => !f.Success))
+ {
+ summary.AppendFormat("{0} files failed to import. Errors (max of 3 will be shown):\n",
+ results.Files.Count(f => !f.Success));
+
+ foreach (var failure in results.Files.Where(f => !f.Success).Take(3))
+ {
+ string fileName = failure.FileName.Substring(failure.FileName.LastIndexOf('\\') + 1);
+ summary.AppendFormat("{0} : {1}", fileName, failure.ErrorMessage);
+ }
+ }
+ MessageBox.Show(summary.ToString(), @"Finished");
+ }
+ catch (Exception exception)
+ {
+ MessageBox.Show(exception.Message, @"Something went pop...");
+ }
+ }
+
+ private void ToggleControls(bool enabled)
+ {
+ if (txtSourceDirectory.InvokeRequired)
+ {
+ BeginInvoke(new MethodInvoker(() => ToggleControls(enabled)));
+ }
+ else
+ {
+ txtSourceDirectory.Enabled = enabled;
+ btnChangeDirectory.Enabled = enabled;
+ txtHost.Enabled = enabled;
+ txtPort.Enabled = enabled;
+ txtIndexName.Enabled = enabled;
+ btnImport.Enabled = enabled;
+ }
+ }
+
+ private string ValidateInputs()
+ {
+ if (String.IsNullOrWhiteSpace(txtSourceDirectory.Text))
+ return "Please select the directory where the JS data files are";
+
+ if (!Directory.Exists(txtSourceDirectory.Text))
+ return "The chosen data directory doesn't exist";
+
+ if (String.IsNullOrWhiteSpace(txtHost.Text))
+ return "Please specify the ElasticSearch host name";
- var results = importer.Import();
+ int port;
+ if (String.IsNullOrWhiteSpace(txtPort.Text) || !int.TryParse(txtPort.Text, out port))
+ return "Please enter a valid ElasticSearch port number";
+
+ if (String.IsNullOrWhiteSpace(txtIndexName.Text))
+ return "Please enter the name of the index to import into";
+
+ return String.Empty;
+ }
+
+ private void btnChangeDirectory_Click(object sender, EventArgs e)
+ {
+ folderDialog.ShowDialog();
+ txtSourceDirectory.Text = folderDialog.SelectedPath;
}
}
}
View
@@ -117,4 +117,10 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
+ <metadata name="folderDialog.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
+ <value>17, 17</value>
+ </metadata>
+ <metadata name="$this.TrayHeight" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
+ <value>50</value>
+ </metadata>
</root>

0 comments on commit 5250b22

Please sign in to comment.