|
| 1 | +# ---------------------------------------------------------------------------------- |
| 2 | +# |
| 3 | +# Copyright Microsoft Corporation |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.internal |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | +# ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +# This file is a temporary approach to prompt the user for a survey. |
| 16 | +# It doesn't cover every case well or not tested well: |
| 17 | +# 1. Allow two or more modules to show the survey link. |
| 18 | +# 2. When the major version is changed. |
| 19 | +# 3. Not sure about the way to handle survey id or if it's needed in future. |
| 20 | +# 4. The file format is also subject to change in future. |
| 21 | + |
| 22 | +param ( |
| 23 | + [Parameter(Mandatory)] |
| 24 | + [string] $moduleName, |
| 25 | + [Parameter(Mandatory)] |
| 26 | + [int] $majorVersion |
| 27 | +) |
| 28 | + |
| 29 | +if ([string]::IsNullOrWhiteSpace($moduleName)) { |
| 30 | + return |
| 31 | +} |
| 32 | + |
| 33 | +if ($majorVersion -lt 0) { |
| 34 | + return |
| 35 | +} |
| 36 | + |
| 37 | +if ($env:Azure_PS_Intercept_Survey -eq "false") { |
| 38 | + return |
| 39 | +} |
| 40 | + |
| 41 | +$mutexName = "AzModulesInterceptSurvey" |
| 42 | +$mutexTiimeout = 1000 |
| 43 | +$interceptDays = 30 |
| 44 | +$interceptLoadTimes = 3 |
| 45 | +$today = Get-Date |
| 46 | +$mutexTimeout = 500 |
| 47 | + |
| 48 | +function ConvertTo-String { |
| 49 | + param ( |
| 50 | + [Parameter(Mandatory)] |
| 51 | + [DateTime] $date |
| 52 | + ) |
| 53 | + |
| 54 | + return $date.ToString("yyyy-MM-dd") |
| 55 | +} |
| 56 | + |
| 57 | +function Init-InterceptFile { |
| 58 | + $interceptContent = @{ |
| 59 | + "lastInterceptCheckDate"=ConvertTo-String($today); |
| 60 | + "interceptTriggered"=$false; |
| 61 | + "modules"=@(@{ |
| 62 | + "name"=$moduleName; |
| 63 | + "majorVersion"=$majorVersion; |
| 64 | + "activeDays"=1; |
| 65 | + "lastActiveDate"=ConvertTo-String($today); |
| 66 | + }) |
| 67 | + } |
| 68 | + |
| 69 | + ConvertTo-Json -InputObject $interceptContent | Out-File -FilePath $interceptFilePath -Encoding utf8 |
| 70 | +} |
| 71 | + |
| 72 | +# Update the intercept object and return $true if we need to show the survey. |
| 73 | +function Update-InterceptObject { |
| 74 | + param ( |
| 75 | + $interceptObject |
| 76 | + ) |
| 77 | + |
| 78 | + $thisModule = $null |
| 79 | + |
| 80 | + foreach ($m in $interceptObject.modules) { |
| 81 | + if ($m.name -eq $moduleName) { |
| 82 | + $thisModule = $m |
| 83 | + break |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + if ($thisModule -eq $null) { |
| 88 | + # There is no information about this module. The file could be created by another module or in some other way. |
| 89 | + # We need to add this module to the list. |
| 90 | + |
| 91 | + $thisModule = @{ |
| 92 | + "name"=$moduleName; |
| 93 | + "majorVersion"=$majorVersion; |
| 94 | + "activeDays"=1; |
| 95 | + "lastActiveDate"=ConvertTo-String($today); |
| 96 | + } |
| 97 | + |
| 98 | + $interceptObject.modules += $thisModule |
| 99 | + |
| 100 | + return $false |
| 101 | + } |
| 102 | + |
| 103 | + $recordedMajorVersion = $thisModule.majorVersion |
| 104 | + $thisModule.majorVersion = $majorVersion |
| 105 | + |
| 106 | + if ($recordedMajorVersion -ne $majorVersion) { |
| 107 | + $thisModule.activeDays = 1 |
| 108 | + $thisModule.lastActiveDate = ConvertTo-String($today) |
| 109 | + $interceptObject.interceptTriggered = $false |
| 110 | + |
| 111 | + return $false |
| 112 | + } |
| 113 | + |
| 114 | + $recordedLastActiveDate = Get-Date $thisModule.lastActiveDate |
| 115 | + $recordedActiveDays = $thisModule.activeDays |
| 116 | + |
| 117 | + $elapsedDays = ($today - $recordedLastActiveDate).Days |
| 118 | + |
| 119 | + if ($elapsedDays -gt $interceptDays) { |
| 120 | + $thisModule.activeDays = 1 |
| 121 | + $thisModule.lastActiveDate = ConvertTo-String($today) |
| 122 | + |
| 123 | + return $false |
| 124 | + } |
| 125 | + |
| 126 | + $newActiveDays = $recordedActiveDays |
| 127 | + |
| 128 | + if ($elapsedDays -ne 0) { |
| 129 | + $newActiveDays++ |
| 130 | + } |
| 131 | + |
| 132 | + if ($newActiveDays -ge $interceptLoadTimes) { |
| 133 | + $thisModule.activeDays = 0 |
| 134 | + $thisModule.lastActiveDate = ConvertTo-String($today) |
| 135 | + $interceptObject.interceptTriggered = $true |
| 136 | + return $true |
| 137 | + } |
| 138 | + |
| 139 | + $thisModule.activeDays = $newActiveDays |
| 140 | + $thisModule.lastActiveDate = ConvertTo-String($today) |
| 141 | +} |
| 142 | + |
| 143 | +$mutex = New-Object System.Threading.Mutex($false, $mutexName) |
| 144 | + |
| 145 | +$hasMutex = $mutex.WaitOne($mutexTimeout) |
| 146 | + |
| 147 | +if (-not $hasMutex) { |
| 148 | + return |
| 149 | +} |
| 150 | + |
| 151 | +$shouldIntercept = $false |
| 152 | + |
| 153 | +try |
| 154 | +{ |
| 155 | + $interceptFilePath = Join-Path -Path (Join-Path -Path $env:USERPROFILE -ChildPath ".Azure") -ChildPath "InterceptSurvey.json" |
| 156 | + |
| 157 | + if (-not (Test-Path $interceptFilePath)) { |
| 158 | + New-Item -ItemType File -Force -Path $interceptFilePath |
| 159 | + Init-InterceptFile |
| 160 | + } else { |
| 161 | + $interceptObject = $null |
| 162 | + try { |
| 163 | + $fileContent = Get-Content $interceptFilePath | Out-String |
| 164 | + $interceptObject = ConvertFrom-Json $fileContent |
| 165 | + } catch { |
| 166 | + Init-InterceptFile |
| 167 | + } |
| 168 | + |
| 169 | + if (-not ($interceptObject -eq $null)) { |
| 170 | + $shouldIntercept = Update-InterceptObject($interceptObject) |
| 171 | + |
| 172 | + ConvertTo-Json -InputObject $interceptObject | Out-File $interceptFilePath -Encoding utf8 |
| 173 | + } |
| 174 | + } |
| 175 | +} catch { |
| 176 | +} |
| 177 | + |
| 178 | +$mutex.ReleaseMutex() |
| 179 | + |
| 180 | +if ($shouldIntercept) { |
| 181 | + $userId = (Get-AzContext).Account.Id |
| 182 | + $surveyId = "000000" |
| 183 | + |
| 184 | + if ($userId -ne $null) |
| 185 | + { |
| 186 | + $surveyId = Get-Random -Maximum 1000000 -SetSeed $userId.GetHashCode() |
| 187 | + try { |
| 188 | + $azPredictorSettingFilePath = Join-Path -Path (Join-Path -Path $env:USERPROFILE -ChildPath ".Azure") -ChildPath "AzPredictorSettings.json" |
| 189 | + $setting = @{ |
| 190 | + "surveyId"=$surveyId; |
| 191 | + } |
| 192 | + |
| 193 | + if (Test-Path $azPredictorSettingFilePath) { |
| 194 | + try { |
| 195 | + $setting = Get-Content $azPredictorSettingFilePath | Out-String | ConvertFrom-Json |
| 196 | + $setting | Add-Member -NotePropertyName "surveyId" -NotePropertyValue $surveyId -Force |
| 197 | + } catch { |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + ConvertTo-Json -InputObject $setting | Out-File -FilePath $azPredictorSettingFilePath -Encoding utf8 |
| 202 | + } catch { |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + $escape = $([char]27) |
| 207 | + Write-Host "`n$escape[7mHow was your experience using Az predictor? $escape[27m`n" -NoNewline; Write-Host "$escape[7mhttp://aka.ms/azpredictorisurvey?SessionId=$surveyId$escape[27m" -NoNewline |
| 208 | + Write-Host "`n" |
| 209 | +} |
0 commit comments