1- using Microsoft . Data . SqlClient ;
1+ using DBADashGUI . Theme ;
2+ using Microsoft . Data . SqlClient ;
23using System ;
34using System . Collections . Generic ;
45using System . Data ;
56using System . Drawing ;
67using System . Linq ;
78using System . Windows . Forms ;
8- using DBADashGUI . Theme ;
99
1010namespace DBADashGUI . Performance
1111{
@@ -24,27 +24,16 @@ public ObjectExecutionSummary()
2424 } ;
2525 }
2626
27- private string Instance => _context . InstanceName ;
28- private int InstanceID => _context . InstanceID ;
29- private int DatabaseID => _context . DatabaseID ;
30- private long ObjectID => ( _context . Type is SQLTreeItem . TreeType . Database or SQLTreeItem . TreeType . AzureDatabase ) ? - 1 : _context . ObjectID ;
27+ private string Instance => CurrentContext . InstanceName ;
28+ private int InstanceID => CurrentContext . InstanceID ;
29+ private int DatabaseID => CurrentContext . DatabaseID ;
30+ private long ObjectID => ( CurrentContext . Type is SQLTreeItem . TreeType . Database or SQLTreeItem . TreeType . AzureDatabase ) ? - 1 : CurrentContext . ObjectID ;
3131
32- private DBADashContext _context ;
32+ private DBADashContext CurrentContext ;
3333
3434 public string Types
3535 {
36- get
37- {
38- string types = "" ;
39- foreach ( ToolStripMenuItem mnu in tsType . DropDownItems )
40- {
41- if ( mnu . Checked )
42- {
43- types += ( types . Length > 0 ? "," : "" ) + mnu . Tag ;
44- }
45- }
46- return types ;
47- }
36+ get => tsType . DropDownItems . Cast < ToolStripMenuItem > ( ) . Where ( mnu => mnu . Checked ) . Aggregate ( "" , ( current , mnu ) => current + ( ( current . Length > 0 ? "," : "" ) + mnu . Tag ) ) ;
4837 set
4938 {
5039 var types = value . Split ( ',' ) ;
@@ -55,11 +44,10 @@ public string Types
5544 }
5645 }
5746
58- private int CompareOffset => int . Parse ( SelectedCompareOffsetItem . Tag . ToString ( ) ?? "-1" ) ;
47+ private int CompareOffset => int . Parse ( SelectedCompareOffsetItem . Tag ? . ToString ( ) ?? "-1" ) ;
5948
6049 private DateTime _compareTo = DateTime . MinValue ;
6150 private DateTime _compareFrom = DateTime . MinValue ;
62- private DataTable dt ;
6351
6452 private ToolStripMenuItem SelectedCompareOffsetItem => tsCompare . DropDownItems . OfType < ToolStripMenuItem > ( ) . FirstOrDefault ( mnu => mnu . Checked , tsNoCompare ) ;
6553
@@ -122,15 +110,13 @@ private DateTime CompareFrom
122110
123111 private void TsColumn_Click ( object sender , EventArgs e )
124112 {
125- if ( sender . GetType ( ) == typeof ( ToolStripMenuItem ) )
113+ if ( sender . GetType ( ) != typeof ( ToolStripMenuItem ) ) return ;
114+ var mnu = ( ToolStripMenuItem ) sender ;
115+ foreach ( var name in new [ ] { mnu . Text , "Compare " + mnu . Text } )
126116 {
127- var mnu = ( ToolStripMenuItem ) sender ;
128- foreach ( string name in new [ ] { mnu . Text , "Compare " + mnu . Text } )
117+ if ( dgv . Columns . Contains ( name ) )
129118 {
130- if ( dgv . Columns . Contains ( name ) )
131- {
132- dgv . Columns [ name ] . Visible = mnu . Checked ;
133- }
119+ dgv . Columns [ name ] ! . Visible = mnu . Checked ;
134120 }
135121 }
136122 }
@@ -148,37 +134,35 @@ public List<DataGridViewColumn> Columns
148134 col . Visible = true ;
149135 _cols . Add ( col ) ;
150136 displayIdx += 1 ;
151- if ( CompareFrom > DateTime . MinValue )
137+ if ( CompareFrom <= DateTime . MinValue ) continue ;
138+ var compareCol = new DataGridViewTextBoxColumn
152139 {
153- var compareCol = new DataGridViewTextBoxColumn
154- {
155- Name = "Compare " + col . Name ,
156- DataPropertyName = "compare_" + col . DataPropertyName ,
157- DisplayIndex = displayIdx ,
158- DefaultCellStyle = col . DefaultCellStyle . Clone ( )
159- } ;
160- compareCol . DefaultCellStyle . BackColor = Color . BlanchedAlmond ;
161- compareCol . DefaultCellStyle . ForeColor = DashColors . TrimbleBlue ;
162- _cols . Add ( compareCol ) ;
140+ Name = "Compare " + col . Name ,
141+ DataPropertyName = "compare_" + col . DataPropertyName ,
142+ DisplayIndex = displayIdx ,
143+ DefaultCellStyle = col . DefaultCellStyle . Clone ( )
144+ } ;
145+ compareCol . DefaultCellStyle . BackColor = Color . BlanchedAlmond ;
146+ compareCol . DefaultCellStyle . ForeColor = DashColors . TrimbleBlue ;
147+ _cols . Add ( compareCol ) ;
148+ displayIdx += 1 ;
149+ if ( col . DataPropertyName is "avg_duration_sec" or "avg_cpu_sec" )
150+ {
151+ var diffCol = new DataGridViewTextBoxColumn ( ) { Name = "Diff " + col . Name . Replace ( "sec" , "%" ) , DataPropertyName = "diff_" + col . DataPropertyName . Replace ( "_sec" , "_pct" ) , DisplayIndex = displayIdx } ;
152+ diffCol . DefaultCellStyle . Format = "P2" ;
153+ diffCol . DefaultCellStyle . BackColor = Color . AliceBlue ;
154+ _cols . Add ( diffCol ) ;
163155 displayIdx += 1 ;
164- if ( col . DataPropertyName is "avg_duration_sec" or "avg_cpu_sec" )
165- {
166- var diffCol = new DataGridViewTextBoxColumn ( ) { Name = "Diff " + col . Name . Replace ( "sec" , "%" ) , DataPropertyName = "diff_" + col . DataPropertyName . Replace ( "_sec" , "_pct" ) , DisplayIndex = displayIdx } ;
167- diffCol . DefaultCellStyle . Format = "P2" ;
168- diffCol . DefaultCellStyle . BackColor = Color . AliceBlue ;
169- _cols . Add ( diffCol ) ;
170- displayIdx += 1 ;
171- }
172156 }
173157 }
174158
175159 return _cols ;
176160 }
177161 }
178162
179- public void SetContext ( DBADashContext _context )
163+ public void SetContext ( DBADashContext context )
180164 {
181- this . _context = _context ;
165+ CurrentContext = context ;
182166 RefreshData ( ) ;
183167 }
184168
@@ -231,45 +215,43 @@ public void RefreshData()
231215
232216 private DataTable GetObjectExecutionStatsSummary ( )
233217 {
234- using ( var cn = new SqlConnection ( Common . ConnectionString ) )
235- using ( var cmd = new SqlCommand ( "dbo.ObjectExecutionStatsSummary_Get" , cn ) { CommandType = CommandType . StoredProcedure , CommandTimeout = Config . DefaultCommandTimeout } )
236- using ( var da = new SqlDataAdapter ( cmd ) )
237- {
238- cn . Open ( ) ;
218+ using var cn = new SqlConnection ( Common . ConnectionString ) ;
219+ using var cmd = new SqlCommand ( "dbo.ObjectExecutionStatsSummary_Get" , cn ) { CommandType = CommandType . StoredProcedure , CommandTimeout = Config . DefaultCommandTimeout } ;
220+ using var da = new SqlDataAdapter ( cmd ) ;
221+ cn . Open ( ) ;
239222
240- if ( cmd . Parameters . AddIfGreaterThanZero ( "InstanceID" , InstanceID ) == null && cmd . Parameters . AddStringIfNotNullOrEmpty ( "InstanceGroupName" , Instance ) == null )
241- {
242- throw new Exception ( "Instance not provided to Object Execution Summary" ) ;
243- }
223+ if ( cmd . Parameters . AddIfGreaterThanZero ( "InstanceID" , InstanceID ) == null && cmd . Parameters . AddStringIfNotNullOrEmpty ( "InstanceGroupName" , Instance ) == null )
224+ {
225+ throw new Exception ( "Instance not provided to Object Execution Summary" ) ;
226+ }
244227
245- cmd . Parameters . AddIfGreaterThanZero ( "ObjectID" , ObjectID ) ;
246- cmd . Parameters . AddIfGreaterThanZero ( "DatabaseID" , DatabaseID ) ;
228+ cmd . Parameters . AddIfGreaterThanZero ( "ObjectID" , ObjectID ) ;
229+ cmd . Parameters . AddIfGreaterThanZero ( "DatabaseID" , DatabaseID ) ;
247230
248- if ( HasCompare )
249- {
250- cmd . Parameters . AddWithValue ( "CompareFrom" , CompareFrom ) ;
251- cmd . Parameters . AddWithValue ( "CompareTo" , CompareTo ) ;
252- }
231+ if ( HasCompare )
232+ {
233+ cmd . Parameters . AddWithValue ( "CompareFrom" , CompareFrom ) ;
234+ cmd . Parameters . AddWithValue ( "CompareTo" , CompareTo ) ;
235+ }
253236
254- cmd . Parameters . AddStringIfNotNullOrEmpty ( "Types" , Types ) ;
237+ cmd . Parameters . AddStringIfNotNullOrEmpty ( "Types" , Types ) ;
255238
256- if ( DateRange . HasDayOfWeekFilter )
257- {
258- cmd . Parameters . AddWithValue ( "DaysOfWeek" , DateRange . DayOfWeek . AsDataTable ( ) ) ;
259- }
260- if ( DateRange . HasTimeOfDayFilter )
261- {
262- cmd . Parameters . AddWithValue ( "Hours" , DateRange . TimeOfDay . AsDataTable ( ) ) ;
263- }
264-
265- cmd . Parameters . AddWithValue ( "FromDate" , DateRange . FromUTC ) ;
266- cmd . Parameters . AddWithValue ( "ToDate" , DateRange . ToUTC ) ;
267- cmd . Parameters . AddWithValue ( "UTCOffset" , DateHelper . UtcOffset ) ;
268- cmd . Parameters . AddWithValue ( "InstanceIDs" , _context . InstanceIDs . AsDataTable ( ) ) ;
269- dt = new DataTable ( ) ;
270- da . Fill ( dt ) ;
271- return dt ;
239+ if ( DateRange . HasDayOfWeekFilter )
240+ {
241+ cmd . Parameters . AddWithValue ( "DaysOfWeek" , DateRange . DayOfWeek . AsDataTable ( ) ) ;
242+ }
243+ if ( DateRange . HasTimeOfDayFilter )
244+ {
245+ cmd . Parameters . AddWithValue ( "Hours" , DateRange . TimeOfDay . AsDataTable ( ) ) ;
272246 }
247+
248+ cmd . Parameters . AddWithValue ( "FromDate" , DateRange . FromUTC ) ;
249+ cmd . Parameters . AddWithValue ( "ToDate" , DateRange . ToUTC ) ;
250+ cmd . Parameters . AddWithValue ( "UTCOffset" , DateHelper . UtcOffset ) ;
251+ cmd . Parameters . AddWithValue ( "InstanceIDs" , CurrentContext . InstanceIDs . AsDataTable ( ) ) ;
252+ var dt = new DataTable ( ) ;
253+ da . Fill ( dt ) ;
254+ return dt ;
273255 }
274256
275257 private void TsRefresh_Click ( object sender , EventArgs e )
@@ -312,13 +294,11 @@ private void TsCustomCompare_Click(object sender, EventArgs e)
312294 ToDate = CompareTo > DateTime . MinValue && CompareTo < DateTime . MaxValue ? CompareTo . ToAppTimeZone ( ) : DateRange . ToUTC . ToAppTimeZone ( )
313295 } ;
314296 frm . ShowDialog ( ) ;
315- if ( frm . DialogResult == DialogResult . OK )
316- {
317- _compareFrom = frm . FromDate . AppTimeZoneToUtc ( ) ;
318- _compareTo = frm . ToDate . AppTimeZoneToUtc ( ) ;
319- CheckCompareOffset ( tsCustomCompare ) ;
320- RefreshData ( ) ;
321- }
297+ if ( frm . DialogResult != DialogResult . OK ) return ;
298+ _compareFrom = frm . FromDate . AppTimeZoneToUtc ( ) ;
299+ _compareTo = frm . ToDate . AppTimeZoneToUtc ( ) ;
300+ CheckCompareOffset ( tsCustomCompare ) ;
301+ RefreshData ( ) ;
322302 }
323303
324304 private void TsType_Click ( object sender , EventArgs e )
@@ -340,12 +320,12 @@ private void Dgv_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
340320 {
341321 if ( dgv . Columns . Contains ( "Diff Avg Duration (%)" ) )
342322 {
343- for ( int idx = e . RowIndex ; idx < e . RowIndex + e . RowCount ; idx += 1 )
323+ for ( var idx = e . RowIndex ; idx < e . RowIndex + e . RowCount ; idx += 1 )
344324 {
345325 var r = dgv . Rows [ idx ] ;
346326 var row = ( DataRowView ) r . DataBoundItem ;
347- double diffAvgDurationPct = row [ "diff_avg_duration_pct" ] == DBNull . Value ? 0d : Convert . ToDouble ( row [ "diff_avg_duration_pct" ] ) ;
348- double diffAvgCPUPct = row [ "diff_avg_cpu_pct" ] == DBNull . Value ? 0d : Convert . ToDouble ( row [ "diff_avg_cpu_pct" ] ) ;
327+ var diffAvgDurationPct = row [ "diff_avg_duration_pct" ] == DBNull . Value ? 0d : Convert . ToDouble ( row [ "diff_avg_duration_pct" ] ) ;
328+ var diffAvgCPUPct = row [ "diff_avg_cpu_pct" ] == DBNull . Value ? 0d : Convert . ToDouble ( row [ "diff_avg_cpu_pct" ] ) ;
349329 r . Cells [ "Diff Avg Duration (%)" ] . SetColor ( DiffColorFromDouble ( diffAvgDurationPct ) ) ;
350330 r . Cells [ "Diff Avg CPU (%)" ] . SetColor ( DiffColorFromDouble ( diffAvgCPUPct ) ) ;
351331 }
@@ -400,13 +380,11 @@ private void RefreshChart(long objectID, string title)
400380
401381 private void Dgv_CellContentClick ( object sender , DataGridViewCellEventArgs e )
402382 {
403- if ( e . RowIndex >= 0 )
383+ if ( e . RowIndex < 0 ) return ;
384+ if ( dgv . Columns [ e . ColumnIndex ] . Name == "Name" )
404385 {
405- if ( dgv . Columns [ e . ColumnIndex ] . Name == "Name" )
406- {
407- var row = ( DataRowView ) dgv . Rows [ e . RowIndex ] . DataBoundItem ;
408- RefreshChart ( ( long ) row [ "ObjectID" ] , ( string ) row [ "ObjectName" ] ) ;
409- }
386+ var row = ( DataRowView ) dgv . Rows [ e . RowIndex ] . DataBoundItem ;
387+ RefreshChart ( ( long ) row [ "ObjectID" ] , ( string ) row [ "ObjectName" ] ) ;
410388 }
411389 }
412390
@@ -439,19 +417,17 @@ private void TmrSearch_Tick(object sender, EventArgs e)
439417 catch ( Exception ex )
440418 {
441419 MessageBox . Show ( ex . Message , "Filter Error" , MessageBoxButtons . OK , MessageBoxIcon . Warning ) ;
442- dgv . DataSource = new DataView ( dt , null , "total_duration_sec DESC" , DataViewRowState . CurrentRows ) ;
420+ dgv . ClearFilter ( ) ;
443421 }
444422 }
445423 tmrSearch . Enabled = false ;
446424 }
447425
448426 private void TsCols_Click ( object sender , EventArgs e )
449427 {
450- if ( dgv . PromptColumnSelection ( ) == DialogResult . OK )
451- {
452- dgv . AutoResizeColumns ( ) ;
453- dgv . AutoResizeRows ( ) ;
454- }
428+ if ( dgv . PromptColumnSelection ( ) != DialogResult . OK ) return ;
429+ dgv . AutoResizeColumns ( ) ;
430+ dgv . AutoResizeRows ( ) ;
455431 }
456432 }
457433}
0 commit comments