-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoshTimeTracker.psm1
268 lines (214 loc) · 7.81 KB
/
PoshTimeTracker.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
using namespace System.Collections.Generic
using namespace System.Linq
param(
[Parameter(Mandatory = $true)]
[string]$EntriesSaveFilePath # Where this module should save the entries to.
)
$Global:StartTimerModule = [StartTimerModule]::new($EntriesSaveFilePath)
# Stops the latest Timer then starts a new one. The new Entry is displayed.
function Start-Timer {
param (
[string]$Tag,
[string]$Description
)
$Entry = $Global:StartTimerModule.StartTimer($Tag, $Description)
# Prints the newly started timer.
$Entry | Format-Table -Property Id, Tag, Description, Start
}
# Stops the previous started timer. Prints the Entry if there was any.
function Stop-Timer {
$Entry = $Global:StartTimerModule.StopTimer();
# Prints the entry which was stopped.
$Entry | Format-Table -Property *, Duration
}
# Gets the list of all registred entries.
function Get-TimerEntry {
param (
[string]$Tag, # The entry tag to be shown. If not set, show any.
[switch]$Today,
[switch]$Week,
[nullable[DateTimeOffset]]$From, # Only Entries that started after FROM will be selected.
[nullable[DateTimeOffset]]$To # Only entries that started before TO will be selected.
)
$TodaysDate = [DateTime]::Now.Date
# Set From and To based on Today.
if ($Today) {
$From = $TodaysDate
$To = $TodaysDate.AddDays(1)
}
# Set From and To based on the week.
if ($Week) {
$From = $TodaysDate.AddDays(-$TodaysDate.DayOfWeek)
$To = $From.AddDays(7)
}
$Entries = $Global:StartTimerModule.ReadEntries()
# Filter the entries for the given time range.
$Entries = [Enumerable]::Where($Entries, [Func[TimerEntry, bool]] {
param ($TimerEntry)
return ($Tag -eq "" -or $Tag -eq $TimerEntry.Tag) -and
($null -eq $From -or $From -le $TimerEntry.Start) -and
($null -eq $To -or $To -ge $TimerEntry.Start)
}).ToArray()
# Sums up the duration from all filtered entries.
$TotalDuration = [timespan]::new(0)
foreach ($TimerEntry in $Entries) {
if ($null -ne $TimerEntry.Duration) {
$TotalDuration += $TimerEntry.Duration
}
else {
# If the entry is still running lets add for how long it is.
$TotalDuration += ([System.DateTimeOffset]::Now - $TimerEntry.Start)
}
}
Write-Host "Total Duration: $TotalDuration"
# Return the entries.
$Entries | Format-Table -Property *, Duration
}
# Removes a entry with the given id.
function Remove-TimerEntry {
param (
[int]$Id
)
$Entry = $Global:StartTimerModule.RemoveEntry($Id);
# Prints the removed entry if any.
$Entry | Format-Table -Property *, Duration
}
# TODO: Implement
function Update-TimerEntry {
param (
[int]$Id,
[string]$Tag,
[string]$Description,
[nullable[DateTimeOffset]]$Start,
[nullable[DateTimeOffset]]$End
)
}
Set-Alias -Name sat -Value Start-Timer
Set-Alias -Name stt -Value Stop-Timer
Set-Alias -Name gte -Value Get-TimerEntry
Set-Alias -Name rte -Value Remove-TimerEntry
Export-ModuleMember -Function Start-Timer, Stop-Timer, Get-TimerEntry, Remove-TimerEntry
Export-ModuleMember -Alias sat, stt, gte, rte
# Class abstraction for the publisher.
class TimeTrackerPublisher {
# Publishes the given Entry and return if the publish process was successful or not.
[bool]Publish([TimerEntry]$TimerEntry) {
return $false;
}
}
# Class that represents a entry for the timer tracker.
class TimerEntry {
[int]$Id
[string]$Tag
[string]$Description
[DateTimeOffset]$Start
# Possible null end if the entry is still in progress.
[nullable[DateTimeOffset]]$End
[bool]$Published
# Duration does not need to be serialized. So it is hidden.
hidden [nullable[timespan]]$Duration
TimerEntry([int]$Id, [string]$Tag, [string]$Description) {
$this.Id = $Id;
$this.Tag = $Tag;
$this.Description = $Description;
$this.Start = Get-Date;
$this.End = $null;
$this.Published = $false
$this.Duration = $null
}
# Construtor for when loading the entry from file.
TimerEntry([PSCustomObject]$Other) {
$this.Id = [int]::Parse($Other.Id)
$this.Tag = $Other.Tag
$this.Description = $Other.Description
$this.Start = [DateTimeOffset]::Parse($Other.Start)
# Process the end field for in progress entries.
if ($null -ne $Other.End -and $Other.End -ne "") {
$this.End = [DateTimeOffset]::Parse($Other.End)
$this.Duration = $this.End - $this.Start
}
else {
$this.End = $null
$this.Duration = $null
}
$this.Published = [bool]::Parse($Other.Published)
}
Stop() {
$this.End = Get-Date
$this.Duration = $this.End - $this.Start
}
}
# Module handler.
class StartTimerModule {
[string]$EntriesSaveFilePath
StartTimerModule([string]$EntriesSaveFilePath) {
$this.EntriesSaveFilePath = $EntriesSaveFilePath
}
# Read the entries from the persistence file.
[List[TimerEntry]]ReadEntries() {
if (Test-Path -Path $this.EntriesSaveFilePath) {
return Import-Csv -Path $this.EntriesSaveFilePath
}
return [List[TimerEntry]]::new()
}
# Writes the entries into the persistence file.
WriteEntries([List[TimerEntry]]$Entries) {
$Entries | Export-Csv -Path $this.EntriesSaveFilePath
}
# Starts a new timer and stops if theres already one in progress.
[TimerEntry]StartTimer([string]$Tag, [string]$Description) {
$this.StopTimer()
$Entries = $this.ReadEntries();
# Increment the id based on the last entry.
$Id = $Entries.Count -eq 0 ? 1 : $Entries[$Entries.Count - 1].Id + 1
$Entries.Add([TimerEntry]::new($Id, $Tag, $Description))
$this.WriteEntries($Entries);
return $Entries[$Entries.Count - 1]
}
# Stops the latest entry in progress. Return it if any is found.
[TimerEntry]StopTimer() {
$Entries = $this.ReadEntries()
if ($Entries.Count -eq 0) {
return $null
}
$LastEntry = $Entries[$Entries.Count - 1]
if ($null -ne $LastEntry.End) {
return $null
}
$LastEntry.Stop()
$this.WriteEntries($Entries)
return $LastEntry
}
# Removes the entry with the given id. Returns the entry if found.
[TimerEntry]RemoveEntry([int]$Id) {
$Entries = $this.ReadEntries()
# Looks for the entry on the entries list.
$Entry = [Enumerable]::FirstOrDefault($Entries, [Func[TimerEntry, bool]] {
param ($TimerEntry)
return $Id -eq $TimerEntry.Id
})
$Entries.Remove($Entry)
$this.WriteEntries($Entries)
return $Entry
}
# Publishes the entries that ain't yet published to the given publisher.
# A list with the entries that was tried to publish is returned.
[List[TimerEntry]]PublishEntries([TimeTrackerPublisher]$Publisher) {
$Entries = $this.ReadEntries()
if ($Entries.Count -eq 0) {
return $Entries
}
# Select only the entries that wasn't published and thats not currently in progress.
$FilteredEnties = [Enumerable]::Where($Entries, [Func[TimerEntry, bool]] {
param ($TimerEntry)
return $null -ne $TimerEntry.End -and -not $TimerEntry.Published
})
# Copies the Enumerable into a List.
$PublishedEntries = [List[TimerEntry]]::new($FilteredEnties)
foreach ($Entry in $PublishedEntries) {
$Entry.Published = $Publisher.Publish($Entry)
}
$this.WriteEntries($Entries)
return $PublishedEntries
}
}