Skip to content
66 changes: 4 additions & 62 deletions private/projectDatabase/project_database.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ function Get-ProjectFromDatabase{
$prj = getProjectDatabaseCache -KeyLock $keyLock

if($null -ne $prj){
"🟩 Get Project Database from MEMORY $Owner/$ProjectNumber" | Write-MyDebug -Section "ProjectDatabase"
"🟩 Get Project from MEMORY $Owner/$ProjectNumber" | Write-MyDebug -Section "ProjectDatabase"
return $prj
} else {
"🟧 Get Project Database from FILE $Owner/$ProjectNumber" | Write-MyDebug -Section "ProjectDatabase"
"🟧 Get Project from DATABASE $Owner/$ProjectNumber" | Write-MyDebug -Section "ProjectDatabase"
}

# No cache or cache mismatch, read from database
Expand All @@ -73,7 +73,7 @@ function Get-ProjectFromDatabase{
$prj.items = $prj.items | Copy-MyHashTable
$prj.Staged = $prj.Staged | Copy-MyHashTable

# Savige to cache for future calls
# Save to cache for future calls
setProjectDatabaseCache -KeyLock $keyLock -SafeId $prj.safeId -Database $prj

return $prj
Expand Down Expand Up @@ -172,8 +172,7 @@ function Save-ProjectDatabase{
# Add safe mark
$Database.safeId = [guid]::NewGuid().ToString()

# Trace
"🟥 Save Project Database from FILE $Owner/$ProjectNumber safeId [$($Database.safeId)]" | Write-MyDebug -Section "ProjectDatabase"
"🟥 Set Project from DATABASE $Owner/$ProjectNumber safeid [$($Database.safeId)]" | Write-MyDebug -Section "ProjectDatabase"

# Save database
Save-Database -Key $dbkey -Database $Database
Expand All @@ -191,61 +190,4 @@ function Get-ProjectDatabaseKey{
$keylock = "$key-lock"

return $key, $keylock
}

$script:ProjectDatabaseCache = @{}

function getProjectDatabaseCache{
param(
[Parameter(Mandatory,Position = 0)][string]$KeyLock
)

$lock = Get-Database -Key $KeyLock

if([string]::IsNullOrWhiteSpace($lock)){
"No cache lock found for $KeyLock. Cache will be ignored." | Write-MyDebug -Section "ProjectDatabase"
return $null
}

$cache = $script:ProjectDatabaseCache[$KeyLock]

if($lock -cne $cache.safeId) {
"Cache lock mismatch for $KeyLock. Cache safeId [$($cache.SafeId)], lock [$lock]. Cache will be ignored." | Write-MyDebug -Section "ProjectDatabase"
resetProjectDatabaseCache -KeyLock $KeyLock
return $null
}

"Getting fields cache for $KeyLock with lock [$lock] and cache safeId [$($cache.SafeId)]" | Write-MyDebug -Section "ProjectDatabase"
return $cache.Database
}

function setProjectDatabaseCache{
param(
[Parameter(Mandatory,Position = 0)][string]$KeyLock,
[Parameter(Mandatory,Position = 1)][string]$SafeId,
[Parameter(Mandatory,Position = 2)][object]$Database
)

"Setting project cache for $KeyLock with safeId [$SafeId]" | Write-MyDebug -Section "ProjectDatabase"

# Save safeId to project-lock
Save-Database -Database $SafeId -Key $KeyLock

# Set lock in database to prevent concurrent updates
$script:ProjectDatabaseCache[$KeyLock] = @{
Database = $Database
SafeId = $SafeId
}
}

function resetProjectDatabaseCache{
param(
[Parameter(Mandatory,Position = 0)][string]$KeyLock
)

"Resetting project cache for $KeyLock" | Write-MyDebug -Section "ProjectDatabase"

Reset-Database -Key $KeyLock

$script:ProjectDatabaseCache.Remove($KeyLock)
}
56 changes: 56 additions & 0 deletions private/projectDatabase/project_database_cache.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
$script:ProjectDatabaseCache = @{}

function getProjectDatabaseCache{
param(
[Parameter(Mandatory,Position = 0)][string]$KeyLock
)

$lock = Get-Database -Key $KeyLock

if([string]::IsNullOrWhiteSpace($lock)){
"No cache lock found for $KeyLock. Cache will be ignored." | Write-MyDebug -Section "ProjectDatabaseCache"
return $null
}

$cache = $script:ProjectDatabaseCache[$KeyLock]

if($lock -cne $cache.safeId) {
"Cache lock mismatch for $KeyLock. Cache safeId [$($cache.SafeId)], lock [$lock]. Cache will be ignored." | Write-MyDebug -Section "ProjectDatabaseCache"
resetProjectDatabaseCache -KeyLock $KeyLock
return $null
}

"Getting fields cache for $KeyLock with lock [$lock] and cache safeId [$($cache.SafeId)]" | Write-MyDebug -Section "ProjectDatabaseCache"
return $cache.Database
}

function setProjectDatabaseCache{
param(
[Parameter(Mandatory,Position = 0)][string]$KeyLock,
[Parameter(Mandatory,Position = 1)][string]$SafeId,
[Parameter(Mandatory,Position = 2)][object]$Database
)

"Setting project cache for $KeyLock with safeId [$SafeId]" | Write-MyDebug -Section "ProjectDatabaseCache"

# Save safeId to project-lock
Save-Database -Database $SafeId -Key $KeyLock

# Set lock in database to prevent concurrent updates
$script:ProjectDatabaseCache[$KeyLock] = @{
Database = $Database
SafeId = $SafeId
}
}

function resetProjectDatabaseCache{
param(
[Parameter(Mandatory,Position = 0)][string]$KeyLock
)

"Resetting project cache for $KeyLock" | Write-MyDebug -Section "ProjectDatabase"

Reset-Database -Key $KeyLock

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$script:ProjectDatabaseCache.Remove($KeyLock)
}
3 changes: 2 additions & 1 deletion public/fields/getValidNames.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ function Get-ValidNames{
return $null
}

$field = Get-ProjectFields -Owner $owner -ProjectNumber $projectNumber -Name $FieldName
$field = Get-ProjectFields -Owner $owner -ProjectNumber $projectNumber -Name $FieldName -Exact

if($field.dataType -eq "SINGLE_SELECT"){
$ret = $field.MoreInfo
$ret +=""
} else {
throw "Get-ValidNames only supports fields with SINGLE_SELECT dataType. Field $FieldName has $($field.dataType) dataType."
}
Expand Down
7 changes: 6 additions & 1 deletion public/fields/project_fields_list.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function Get-ProjectFields{
[Parameter()] [string]$Owner,
[Parameter()] [string]$ProjectNumber,
[Parameter(Position = 0)][string]$Name,
[Parameter()][switch]$Exact,
[Parameter()][switch]$Force
)

Expand All @@ -61,7 +62,11 @@ function Get-ProjectFields{
# if name
if($Name){
# Filter fields by name
$fieldList = $fieldList | Where-Object { $_.name -like "*$Name*" }
if($Exact){
$fieldList = $fieldList | Where-Object { $_.name -eq $Name }
} else {
$fieldList = $fieldList | Where-Object { $_.name -like "*$Name*" }
}
}

# return if #db is null
Expand Down
2 changes: 1 addition & 1 deletion public/items/edit_project_item.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function Edit-ProjectItem {

# Default
if($DefaultValues){
Write-MyWarning "No Default values are currently setup. Please use other parameters to set values or update the DefaultValues parameter with default values"
"DefaultValues EMPTY" | Write-MyDebug -section "Edit-ProjectItem"

# Do not edit but leave this parameters flow the edit process
}
Expand Down
4 changes: 1 addition & 3 deletions public/items/use_order.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ function Use-Order {
#return item
if($PassThru) {
$i = Get-ProjectItem -ItemId $itemId
return [PsCustomObject]$i
Write-Output ([PsCustomObject]$i)
}


}
} Export-ModuleMember -Function Use-Order -Alias "uo"
Loading