tbf/Statistics/StatisticsDlg.cs

260 lines
10 KiB
C#
Raw Normal View History

2018-12-21 07:14:42 +00:00
///
/// 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 ISessionFactory[] sessionFactories = new ISessionFactory[LocalSettings.BenchesCount];
public StatisticsDlg()
{
InitializeComponent();
2018-12-21 07:14:42 +00:00
///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;
2018-12-21 07:14:42 +00:00
radioButton1.Checked = true;
timePeriodGroupBox.Enabled = false;
Localize();
}
void Localize()
{
Text = Strings.Statistics;
2018-12-21 07:14:42 +00:00
//timePeriodCheckBox.Text = Strings.Time_period;
fromLabel1.Text = Strings.From;
toLabel1.Text = Strings.To;
2018-12-21 07:14:42 +00:00
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)
{
2018-12-21 07:14:42 +00:00
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;
switch (ls.SelectedRadioButtonNumber)
{
case 1: radioButton1.Checked = true; break;
case 2: radioButton2.Checked = true; break;
case 3: radioButton3.Checked = true; break;
case 4: radioButton4.Checked = true; break;
}
for (int i = 0; i < (int)MidId.Count; i++)
{
(midsTabControl.TabPages[i].Controls[0] as MidOrWaterMetersCtrl).Data2UI();
}
for (int i = 0; i < LocalSettings.BenchesCount; i++)
{
if (!string.IsNullOrEmpty(Program.LocalSettings.GetConnectionString(i)))
{
sessionFactories[i] = null;
try
{
sessionFactories[i] = Fluently.Configure()
.Database(MySQLConfiguration.Standard.ConnectionString(Program.LocalSettings.GetConnectionString(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}", Program.LocalSettings.GetBenchName(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;
}
2018-12-21 07:14:42 +00:00
//private void timePeriodCheckBox_CheckedChanged(object sender, EventArgs e)
//{
// ManageCheckGroupBox(timePeriodCheckBox, timePeriodGroupBox);
//}
public IList<TestRslt> UpdateRequest(int benchId, MidId midId)
{
if ((benchId < 0) || (benchId >= LocalSettings.BenchesCount) || (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}", Program.LocalSettings.GetBenchName(benchId)));
}
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();
2018-12-21 07:14:42 +00:00
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>()
2018-12-21 07:14:42 +00:00
.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}", Program.LocalSettings.GetBenchName(benchId), exc.Message));
return null;
}
}
2018-12-21 07:14:42 +00:00
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;
}
private void StatisticsDlg_FormClosing(object sender, FormClosingEventArgs e)
{
LocalSettings ls = Program.LocalSettings;
ls.ManiWndMaximized = (WindowState == FormWindowState.Maximized);
if (WindowState == FormWindowState.Normal)
{
ls.MainWndLeft = Location.X;
ls.MainWndTop = Location.Y;
ls.MainWndWidth = Size.Width;
ls.MainWndHeight = Size.Height;
}
else
{
/// Maximized or minimized (when restarted, minimized window is restored as normal)
ls.MainWndLeft = RestoreBounds.Left;
ls.MainWndTop = RestoreBounds.Top;
ls.MainWndWidth = RestoreBounds.Width;
ls.MainWndHeight = RestoreBounds.Height;
}
if (radioButton1.Checked) ls.SelectedRadioButtonNumber = 1;
else if (radioButton2.Checked) ls.SelectedRadioButtonNumber = 2;
else if (radioButton3.Checked) ls.SelectedRadioButtonNumber = 3;
else if (radioButton4.Checked) ls.SelectedRadioButtonNumber = 4;
else ls.SelectedRadioButtonNumber = 0;
for (int i = 0; i < (int)MidId.Count; i++)
{
(midsTabControl.TabPages[i].Controls[0] as MidOrWaterMetersCtrl).UI2Data();
}
ls.Save();
}
}
}