tbf/Statistics/StatisticsDlg.cs
2018-12-21 08:14:42 +01:00

235 lines
9.6 KiB
C#

///
/// Copyright (c) 2018 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernate;
using FluentNHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using Results;
using Results.Entities;
using Statistics.Resources;
using Statistics.UserControls;
using System.Threading;
using System.Globalization;
namespace Statistics
{
public partial class StatisticsDlg : Form
{
static string[] testBenchNames = new string[] { "WR10", "WR11", "WR13", "WR14", "WR15", "PT7" };
///
#if true
static string[] connectionStrings = new string[] {
"SERVER=10.42.128.197; DATABASE=st-wr10-r; UID=vysledky; PASSWORD=GAqx76QUB6NbnpZP; CHARSET=utf8;", /// WR10
"SERVER=10.42.128.98; DATABASE=st-wr11-r; UID=vysledky; PASSWORD=GAqx76QUB6NbnpZP; CHARSET=utf8;", /// WR11
"SERVER=10.42.128.152; DATABASE=st-wr13-r; UID=vysledky; PASSWORD=GAqx76QUB6NbnpZP; CHARSET=utf8;", /// WR13
"SERVER=10.42.128.143; DATABASE=st-wr14-r; UID=vysledky; PASSWORD=GAqx76QUB6NbnpZP; CHARSET=utf8;", /// WR14
"SERVER=10.42.128.63; DATABASE=st-wr15-r; UID=vysledky; PASSWORD=GAqx76QUB6NbnpZP; CHARSET=utf8;", /// WR15
"" /// PT7
};
#else
static string[] connectionStrings = new string[] {
"SERVER=localhost; DATABASE=st-wr10-r; UID=root; PASSWORD=; CHARSET=utf8;", /// WR10
"SERVER=localhost; DATABASE=st-wr11-r; UID=root; PASSWORD=; CHARSET=utf8;", /// WR11
"SERVER=localhost; DATABASE=st-wr13-r; UID=root; PASSWORD=; CHARSET=utf8;", /// WR13
"SERVER=localhost; DATABASE=st-wr14-r; UID=root; PASSWORD=; CHARSET=utf8;", /// WR14
"SERVER=localhost; DATABASE=st-wr15-r; UID=root; PASSWORD=; CHARSET=utf8;", /// WR15
"" /// PT7
};
#endif
///
static ISessionFactory[] sessionFactories = new ISessionFactory[6];
public StatisticsDlg()
{
InitializeComponent();
///ManageCheckGroupBox(timePeriodCheckBox, timePeriodGroupBox);
for (MidId midId = 0; midId < MidId.Count; midId++)
{
int i = (int)midId;
midsTabControl.TabPages[i].Text = midId.ToString();
(midsTabControl.TabPages[i].Controls[0] as MidOrWaterMetersCtrl).MidId = midId;
(midsTabControl.TabPages[i].Controls[0] as MidOrWaterMetersCtrl).Parent = this;
}
midsTabControl.TabPages[0].Text = Strings.Water_meters;
radioButton1.Checked = true;
timePeriodGroupBox.Enabled = false;
Localize();
}
void Localize()
{
Text = Strings.Statistics;
//timePeriodCheckBox.Text = Strings.Time_period;
fromLabel1.Text = Strings.From;
toLabel1.Text = Strings.To;
radioButton1.Text = Strings.Last_week;
radioButton2.Text = Strings.Last_month;
radioButton3.Text = Strings.Last_3_months;
radioButton4.Text = Strings.From_to;
}
private void StatisticsDlg_Load(object sender, EventArgs e)
{
Statistics.LocalSettings ls = Program.LocalSettings;
Width = (ls.MainWndWidth > 0) ? ls.MainWndWidth : 1000;
Height = (ls.MainWndHeight > 0) ? ls.MainWndHeight : 750;
Left = (ls.MainWndLeft != 0) ? ls.MainWndLeft : 50;
Top = (ls.MainWndTop != 0) ? ls.MainWndTop : 50;
for (int i = 0; i < testBenchNames.Length; i++)
{
if ((i < connectionStrings.Length) && !string.IsNullOrEmpty(connectionStrings[i]))
{
sessionFactories[i] = null;
try
{
sessionFactories[i] = Fluently.Configure()
.Database(MySQLConfiguration.Standard.ConnectionString(connectionStrings[i]))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Results.Entities.Batch>())
.ExposeConfiguration(BuildSchema)
.BuildSessionFactory();
}
catch (Exception)
{
MessageBox.Show(string.Format("Cannot open one of database of {0}", testBenchNames[i]), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
void BuildSchema(NHibernate.Cfg.Configuration config)
{
/// This NHibernate tool takes a configuration with mapping info and exports a database schema
new NHibernate.Tool.hbm2ddl.SchemaExport(config).SetOutputFile("db_schema");
}
private void ManageCheckGroupBox(CheckBox chk, GroupBox grp)
{
/// Make sure the CheckBox isn't in the GroupBox. This will only happen the first time.
if (chk.Parent == grp)
{
grp.Parent.Controls.Add(chk); /// Reparent the CheckBox so it's not in the GroupBox.
chk.Location = new Point(chk.Left + grp.Left, chk.Top + grp.Top); /// Adjust the CheckBox's location.
chk.BringToFront(); /// Move the CheckBox to the top of the stacking order.
}
/// Enable or disable the GroupBox.
grp.Enabled = chk.Checked;
}
//private void timePeriodCheckBox_CheckedChanged(object sender, EventArgs e)
//{
// ManageCheckGroupBox(timePeriodCheckBox, timePeriodGroupBox);
//}
public IList<TestRslt> UpdateRequest(int benchId, MidId midId)
{
if (benchId >= sessionFactories.Length || sessionFactories[benchId] == null || (midId == MidId.WaterMeters))
{
if (midId == MidId.WaterMeters)
{
MessageBox.Show("Loading statistics of water meters is not implemented yet");
}
else
{
MessageBox.Show(string.Format("Cannot load statistics of {0}", (benchId < testBenchNames.Length) ? testBenchNames[benchId] : "XY"));
}
return null;
}
Thread thread = new Thread(() => Application.Run(new Forms.ModelessForm()
{
Title = string.Empty,
Message = Strings.searching_,
BackgroundColor = Color.LightGreen,
FontFamily = "Arial",
FontSize = 14,
FontStyle = FontStyle.Regular,
}));
thread.CurrentCulture = CultureInfo.CurrentCulture;
thread.CurrentUICulture = CultureInfo.CurrentUICulture;
thread.Start();
DateTime from;
DateTime to;
GetDateRange(out from, out to);
try
{
ISession session = sessionFactories[benchId].OpenSession();
var listOfComponentsWithFlowmeter = session.QueryOver<Components>()
.Where(x => (x.Flowmeter == midId.ToString()))
.List();
IList<TestRslt> testRslts = new List<TestRslt>();
foreach (var cmpnts in listOfComponentsWithFlowmeter)
{
var listOfRslts = session.QueryOver<TestRslt>()
.Where(x => ((x.EndTime >= from) && (x.EndTime <= to)))
.And(x => (x.Components == cmpnts))
.List();
foreach (var tr in listOfRslts) testRslts.Add(tr);
}
Forms.ModelessForm.CloseForm();
session.Close();
return testRslts;
}
catch (Exception exc)
{
Forms.ModelessForm.CloseForm();
MessageBox.Show(string.Format("Error loading statistics of {0}\r\n{1}", (benchId < testBenchNames.Length) ? testBenchNames[benchId] : "XY", exc.Message));
return null;
}
}
void GetDateRange(out DateTime from, out DateTime to)
{
if (radioButton1.Checked)
{
/// Last week
from = DateTime.Now.Date.AddDays(-7);
to = DateTime.Now;
}
else if (radioButton2.Checked)
{
/// Last month
from = DateTime.Now.Date.AddDays(-31);
to = DateTime.Now;
}
else if (radioButton3.Checked)
{
/// Last 3 months
from = DateTime.Now.Date.AddDays(-91);
to = DateTime.Now;
}
else
{
/// From / to
from = dateTimeFromPicker.Value.Date;
to = dateTimeToPicker.Value.Date.AddDays(1).AddTicks(-1);
}
}
private void radioButton4_CheckedChanged(object sender, EventArgs e)
{
timePeriodGroupBox.Enabled = radioButton4.Checked;
}
}
}