2018-12-21 07:14:42 +00:00
///
/// Copyright (c) 2018 Sensus Slovensko a.s.
///
using System ;
2018-12-10 20:17:00 +00:00
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 ;
2018-12-19 11:23:41 +00:00
using System.Threading ;
using System.Globalization ;
2018-12-10 20:17:00 +00:00
namespace Statistics
{
public partial class StatisticsDlg : Form
{
2019-02-28 09:54:40 +00:00
static ISessionFactory [ ] sessionFactories = new ISessionFactory [ Program . LocalSettings . ConnectionStrings . Length ] ;
2018-12-10 20:17:00 +00:00
public StatisticsDlg ( )
{
InitializeComponent ( ) ;
2018-12-21 07:14:42 +00:00
///ManageCheckGroupBox(timePeriodCheckBox, timePeriodGroupBox);
2018-12-10 20:17:00 +00:00
2019-02-28 09:54:40 +00:00
for ( int midId = 1 ; midId < Program . LocalSettings . MidNames . Length ; midId + + )
2018-12-10 20:17:00 +00:00
{
2019-02-28 09:54:40 +00:00
TabPage tp = new TabPage ( ) ;
tp . Text = Program . LocalSettings . MidNames [ midId ] ;
MidOrWaterMetersCtrl ctrl = new MidOrWaterMetersCtrl ( ) ;
ctrl . MidId = midId ;
2022-08-09 16:13:10 +00:00
ctrl . ParentDlg = this ;
2019-02-28 09:54:40 +00:00
ctrl . Dock = DockStyle . Fill ;
tp . Controls . Add ( ctrl ) ;
midsTabControl . TabPages . Add ( tp ) ;
2018-12-10 20:17:00 +00:00
}
2018-12-21 07:14:42 +00:00
radioButton1 . Checked = true ;
timePeriodGroupBox . Enabled = false ;
2018-12-10 20:17:00 +00:00
Localize ( ) ;
}
void Localize ( )
{
2019-02-28 09:54:40 +00:00
Text = string . Format ( "MID {0}" , Strings . Statistics ) ;
2018-12-21 07:14:42 +00:00
//timePeriodCheckBox.Text = Strings.Time_period;
2018-12-10 20:17:00 +00:00
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 ;
2018-12-10 20:17:00 +00:00
}
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 ;
2018-12-31 22:13:47 +00:00
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 ;
}
2019-02-28 09:54:40 +00:00
for ( int i = 1 ; i < Program . LocalSettings . MidNames . Length ; i + + )
2018-12-10 20:17:00 +00:00
{
2019-02-28 09:54:40 +00:00
( midsTabControl . TabPages [ i - 1 ] . Controls [ 0 ] as MidOrWaterMetersCtrl ) . Data2UI ( ) ;
2018-12-31 22:13:47 +00:00
}
2019-02-28 09:54:40 +00:00
for ( int i = 0 ; i < Program . LocalSettings . ConnectionStrings . Length ; i + + )
2018-12-31 22:13:47 +00:00
{
2020-10-30 07:23:59 +00:00
string connStr = Program . LocalSettings . GetConnectionString ( i ) ;
if ( ! string . IsNullOrEmpty ( connStr ) )
2018-12-10 20:17:00 +00:00
{
try
{
sessionFactories [ i ] = Fluently . Configure ( )
2020-10-30 07:23:59 +00:00
. Database ( MySQLConfiguration . Standard . ConnectionString ( connStr ) )
2018-12-10 20:17:00 +00:00
. Mappings ( m = > m . FluentMappings . AddFromAssemblyOf < Results . Entities . Batch > ( ) )
. ExposeConfiguration ( BuildSchema )
. BuildSessionFactory ( ) ;
}
catch ( Exception )
{
2020-10-30 07:23:59 +00:00
sessionFactories [ i ] = null ;
2018-12-31 22:13:47 +00:00
MessageBox . Show ( string . Format ( "Cannot open one of database of {0}" , Program . LocalSettings . GetBenchName ( i ) ) , "Error" , MessageBoxButtons . OK , MessageBoxIcon . Exclamation ) ;
2018-12-10 20:17:00 +00:00
}
}
}
}
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);
//}
2018-12-10 20:17:00 +00:00
2019-02-28 09:54:40 +00:00
/// <summary>
/// Called by a child form.
/// Reads and returns results from DB required by the child form to update it's content.
/// </summary>
/// <param name="benchId">0-based bench Id.</param>
/// <param name="midId">1-based MID Id.</param>
/// <returns>Required test results</returns>
public IList < TestRslt > UpdateRequest ( int benchId , int midId )
2018-12-10 20:17:00 +00:00
{
2019-02-28 09:54:40 +00:00
if ( ( benchId < 0 ) | | ( benchId > = Program . LocalSettings . BenchNames . Length ) | | ( sessionFactories [ benchId ] = = null ) )
2018-12-10 20:17:00 +00:00
{
2019-02-28 09:54:40 +00:00
MessageBox . Show ( string . Format ( "Cannot load statistics of {0}" , Program . LocalSettings . GetBenchName ( benchId ) ) ) ;
2018-12-19 11:23:41 +00:00
return null ;
2018-12-10 20:17:00 +00:00
}
2018-12-19 11:23:41 +00:00
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 ) ;
2018-12-10 20:17:00 +00:00
try
{
ISession session = sessionFactories [ benchId ] . OpenSession ( ) ;
var listOfComponentsWithFlowmeter = session . QueryOver < Components > ( )
2019-02-28 09:54:40 +00:00
. Where ( x = > ( x . Flowmeter = = Program . LocalSettings . MidNames [ midId ] ) )
2018-12-10 20:17:00 +00:00
. 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 ) ) )
2018-12-10 20:17:00 +00:00
. And ( x = > ( x . Components = = cmpnts ) )
. List ( ) ;
foreach ( var tr in listOfRslts ) testRslts . Add ( tr ) ;
}
2018-12-19 11:23:41 +00:00
Forms . ModelessForm . CloseForm ( ) ;
2018-12-10 20:17:00 +00:00
session . Close ( ) ;
2018-12-19 11:23:41 +00:00
return testRslts ;
2018-12-10 20:17:00 +00:00
}
catch ( Exception exc )
{
2018-12-19 11:23:41 +00:00
Forms . ModelessForm . CloseForm ( ) ;
2018-12-31 22:13:47 +00:00
MessageBox . Show ( string . Format ( "Error loading statistics of {0}\r\n{1}" , Program . LocalSettings . GetBenchName ( benchId ) , exc . Message ) ) ;
2018-12-19 11:23:41 +00:00
return null ;
2018-12-10 20:17:00 +00:00
}
}
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 ;
}
2018-12-31 22:13:47 +00:00
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 ;
2019-02-28 09:54:40 +00:00
for ( int i = 1 ; i < Math . Min ( Program . LocalSettings . MidNames . Length , midsTabControl . TabPages . Count + 1 ) ; i + + )
2018-12-31 22:13:47 +00:00
{
2019-02-28 09:54:40 +00:00
if ( midsTabControl . TabPages [ i - 1 ] . Controls . Count > 0 & & midsTabControl . TabPages [ i - 1 ] . Controls [ 0 ] is MidOrWaterMetersCtrl )
{
( midsTabControl . TabPages [ i - 1 ] . Controls [ 0 ] as MidOrWaterMetersCtrl ) . UI2Data ( ) ;
}
2018-12-31 22:13:47 +00:00
}
ls . Save ( ) ;
}
2018-12-10 20:17:00 +00:00
}
}