Skip to content

Commit

Permalink
Add Duration property to HistoryInfo (PowerShell#5208)
Browse files Browse the repository at this point in the history
* Remove regions in HistoryInfo class
* Auto properties refactoring in HistoryInfo
* Adding Duration property to HistoryInfo
* Rename ExecutionTime => Duration
  • Loading branch information
powercode authored and iSazonov committed Aug 28, 2018
1 parent aec954e commit 1bfe96d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 132 deletions.
152 changes: 28 additions & 124 deletions src/System.Management.Automation/engine/hostifaces/History.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ namespace Microsoft.PowerShell.Commands
/// </summary>
public class HistoryInfo
{
#region constuctor

/// <summary>
/// Constructor
/// </summary>
Expand All @@ -34,11 +32,11 @@ internal HistoryInfo(long pipelineId, string cmdline, PipelineState status, Date
{
Dbg.Assert(cmdline != null, "caller should validate the parameter");
_pipelineId = pipelineId;
_cmdline = cmdline;
_status = status;
_startTime = startTime;
_endTime = endTime;
_cleared = false;
CommandLine = cmdline;
ExecutionStatus = status;
StartExecutionTime = startTime;
EndExecutionTime = endTime;
Cleared = false;
}

/// <summary>
Expand All @@ -47,193 +45,101 @@ internal HistoryInfo(long pipelineId, string cmdline, PipelineState status, Date
/// <param name="history"></param>
private HistoryInfo(HistoryInfo history)
{
_id = history._id;
Id = history.Id;
_pipelineId = history._pipelineId;
_cmdline = history._cmdline;
_status = history._status;
_startTime = history._startTime;
_endTime = history._endTime;
_cleared = history._cleared;
CommandLine = history.CommandLine;
ExecutionStatus = history.ExecutionStatus;
StartExecutionTime = history.StartExecutionTime;
EndExecutionTime = history.EndExecutionTime;
Cleared = history.Cleared;
}

#endregion constructor

#region public
/// <summary>
/// Id of this history entry.
/// </summary>
/// <value></value>
public long Id
{
get
{
return _id;
}
}
public long Id { get; private set; }

/// <summary>
/// CommandLine string
/// </summary>
/// <value></value>
public string CommandLine
{
get
{
return _cmdline;
}
}
public string CommandLine { get; private set; }

/// <summary>
/// Execution status of associated pipeline
/// </summary>
/// <value></value>
public PipelineState ExecutionStatus
{
get
{
return _status;
}
}
public PipelineState ExecutionStatus { get; private set; }

/// <summary>
/// Start time of execution of associated pipeline
/// </summary>
/// <value></value>
public DateTime StartExecutionTime
{
get
{
return _startTime;
}
}
public DateTime StartExecutionTime { get; }

/// <summary>
/// End time of execution of associated pipeline
/// </summary>
/// <value></value>
public DateTime EndExecutionTime
{
get
{
return _endTime;
}
}
public DateTime EndExecutionTime { get; private set; }

/// <summary>
/// The time it took to execute the associeated pipeline
/// </summary>
public TimeSpan Duration => EndExecutionTime - StartExecutionTime;

/// <summary>
/// Override for ToString() method
/// </summary>
/// <returns></returns>
public override string ToString()
{
if (string.IsNullOrEmpty(_cmdline))
if (string.IsNullOrEmpty(CommandLine))
{
return base.ToString();
}
else
{
return _cmdline;
return CommandLine;
}
}

#endregion public

#region internal

/// <summary>
/// Cleared status of an entry
/// </summary>

internal bool Cleared
{
get
{
return _cleared;
}
set
{
_cleared = value;
}
}
internal bool Cleared { get; set; } = false;

/// <summary>
/// Sets Id
/// </summary>
/// <param name="id"></param>
internal void SetId(long id)
{
_id = id;
}
internal void SetId(long id) => Id = id;

/// <summary>
/// Set status
/// </summary>
/// <param name="status"></param>
internal void SetStatus(PipelineState status)
{
_status = status;
}
internal void SetStatus(PipelineState status) => ExecutionStatus = status;

/// <summary>
/// Set endtime
/// </summary>
/// <param name="endTime"></param>
internal void SetEndTime(DateTime endTime)
{
_endTime = endTime;
}
internal void SetEndTime(DateTime endTime) => EndExecutionTime = endTime;

/// <summary>
/// Sets command
/// </summary>
/// <param name="command"></param>
internal void SetCommand(string command)
{
_cmdline = command;
}

#endregion internal

#region private
internal void SetCommand(string command) => CommandLine = command;

/// <summary>
/// Id of the pipeline corresponding to this history entry
/// </summary>
private long _pipelineId;

/// <summary>
/// Id of the history entry
/// </summary>
private long _id;

/// <summary>
/// CommandLine string
/// </summary>
private string _cmdline;

/// <summary>
/// ExecutionStatus of execution
/// </summary>
private PipelineState _status;

/// <summary>
/// Start time of execution
/// </summary>
private DateTime _startTime;

///
///End time of execution
///
private DateTime _endTime;

/// <summary>
/// Flag indicating an entry is present/cleared
/// </summary>

private bool _cleared = false;

#endregion private

#region ICloneable Members

/// <summary>
/// Returns a clone of this object
Expand All @@ -243,8 +149,6 @@ public HistoryInfo Clone()
{
return new HistoryInfo(this);
}

#endregion
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ Describe "History cmdlet test cases" -Tags "CI" {
$ps = [PowerShell]::Create("NewRunspace")
# we need to be sure that history is added, so use the proper
# Invoke variant
$null = $ps.addcommand("Get-Date").Invoke($null,$setting)
$null = $ps.addcommand("Get-Date").Invoke($null, $setting)
$ps.commands.clear()
$null = $ps.addscript("1+1").Invoke($null,$setting)
$null = $ps.addscript("1+1").Invoke($null, $setting)
$ps.commands.clear()
$null = $ps.addcommand("Get-Location").Invoke($null,$setting)
$null = $ps.addcommand("Get-Location").Invoke($null, $setting)
$ps.commands.clear()
}
AfterEach {
Expand All @@ -38,18 +38,18 @@ Describe "History cmdlet test cases" -Tags "CI" {
}
It "Add-History actually adds to history" {
# add this invocation to history
$ps.AddScript("Get-History|Add-History").Invoke($null,$setting)
$ps.AddScript("Get-History|Add-History").Invoke($null, $setting)
# that's 4 history lines * 2
$ps.Commands.Clear()
$result = $ps.AddCommand("Get-History").Invoke()
$result.Count | Should -Be 8
for($i = 0; $i -lt 4; $i++) {
$result[$i+4].CommandLine | Should -BeExactly $result[$i].CommandLine
for ($i = 0; $i -lt 4; $i++) {
$result[$i + 4].CommandLine | Should -BeExactly $result[$i].CommandLine
}
}
}

It "Tests Invoke-History on a cmdlet that generates output on all streams" {
It "Tests Invoke-History on a cmdlet that generates output on all streams" {
$streamSpammer = '
function StreamSpammer
{
Expand Down Expand Up @@ -93,7 +93,7 @@ Describe "History cmdlet test cases" -Tags "CI" {
$outputCount | Should -Be 12
}

It "Tests Invoke-History on a private command" {
It "Tests Invoke-History on a private command" {

$invocationSettings = New-Object System.Management.Automation.PSInvocationSettings
$invocationSettings.AddToHistory = $true
Expand All @@ -110,4 +110,19 @@ Describe "History cmdlet test cases" -Tags "CI" {

$errorResult | Should -BeExactly 'CommandNotFoundException'
}

It "HistoryInfo calculates Duration" {
$start = [datetime]::new(2001, 01, 01, 10, 01, 01)
$duration = [timespan] "1:2:21"
$end = $start + $duration
$history = [PSCustomObject] @{
CommandLine = "command"
ExecutionStatus = [Management.Automation.Runspaces.PipelineState]::Completed
StartExecutionTime = $start
EndExecutionTime = $end
}
$history | Add-History
$h = Get-History -count 1
$h.Duration | Should -Be $duration
}
}

0 comments on commit 1bfe96d

Please sign in to comment.