Skip to content

Commit

Permalink
[Task]14569737: add throttle cdn config (#1906)
Browse files Browse the repository at this point in the history
* add throttle cdn config

* update throttle cdn config

* remove aisku throttle cdn scripts

* remove throtle json from aisku

* update throttle config cdn file name

* update scripts

* change throttle config to config

* disable default config

Co-authored-by: Nev <54870357+MSNev@users.noreply.github.com>
  • Loading branch information
Karlie-777 and MSNev authored Oct 9, 2022
1 parent 9dc437b commit d929ac8
Show file tree
Hide file tree
Showing 9 changed files with 502 additions and 1 deletion.
2 changes: 1 addition & 1 deletion common/publish/AzureStorageHelper/AzureStorageHelper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ Function Get-VersionDetails (
Function Get-FileVersion (
[string] $name
) {
$regMatch = '^(.*\/)*([^\/\d]*\.)(\d+(\.\d+)*(-[\w\d\-\+]+\.?[\w\d\-\+]*)?)(\.(?:gbl\.js|gbl\.min\.js|cjs\.js|cjs\.min\.js|js|min\.js|integrity\.json|zip)(?:\.map)?)$'
$regMatch = '^(.*\/)*([^\/\d]*\.)(\d+(\.\d+)*(-[\w\d\-\+]+\.?[\w\d\-\+]*)?)(\.(?:gbl\.js|gbl\.min\.js|cjs\.js|cjs\.min\.js|js|min\.js|integrity\.json|cfg\.json|zip)(?:\.map)?)$'
$match = ($name | select-string $regMatch -AllMatches).matches
$contentType = $jsContentType

Expand Down
31 changes: 31 additions & 0 deletions gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ module.exports = function (grunt) {
"./src/InternalConstants.ts"
];

const configVer = getConfigVersion(false);
const configMajorVer = getConfigVersion(true);

function _encodeStr(str) {
return str.replace(/\\/g, '\\\\').
replace(/"/g, '\\"').
Expand Down Expand Up @@ -45,6 +48,23 @@ module.exports = function (grunt) {
};
}

function getConfigVersion(isMajorVer) {
let version = "";
try {
let config = grunt.file.readJSON("./tools/config/package.json");
let configVer= config["version"];
version = "." + configVer;
if (isMajorVer) {
version = "." + configVer.split(".")[0];
}

} catch (e) {
console.log("stack: '" + e.stack + "', message: '" + e.message + "', name: '" + e.name + "'");
}
return version;
}


function _createRegEx(str) {
// Converts a string into a global regex, escaping any special characters
return new RegExp(str.replace(/([.+?^=!:${}()|\[\]\/\\])/g, '\\$1'), 'g');
Expand Down Expand Up @@ -599,6 +619,14 @@ module.exports = function (grunt) {
},
'string-replace': {
'generate-snippet': generateNewSnippet()
},
copy: {
config: {
files: [
{ src: "./tools/config/config.json", dest: `./tools/config/browser/ai.config${configVer}.cfg.json` },
{ src: "./tools/config/config.json", dest: `./tools/config/browser/ai.config${configMajorVer}.cfg.json`}
]
}
}
}));

Expand All @@ -612,8 +640,11 @@ module.exports = function (grunt) {
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-string-replace');
grunt.loadNpmTasks('grunt-contrib-copy');

grunt.loadTasks('./tools/grunt-tasks');
grunt.registerTask("default", ["ts:rollupuglify", "ts:rollupes3", "ts:rollupes3test", "qunit:rollupes3", "ts:shims", "ts:shimstest", "qunit:shims", "ts:default", "uglify:ai", "uglify:snippet"]);


grunt.registerTask("core", tsBuildActions("core"));
grunt.registerTask("core-min", minTasks("core"));
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"grunt": "^1.5.3",
"grunt-cli": "^1.4.3",
"grunt-contrib-connect": "^3.0.0",
"grunt-contrib-copy": "^1.0.0",
"grunt-contrib-qunit": "^5.0.1",
"grunt-contrib-uglify": "^5.0.1",
"grunt-string-replace": "^1.3.1",
Expand Down
49 changes: 49 additions & 0 deletions tools/config/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name":"Application Insights Config",
"description":" Application Insights JavaScript SDK - Web Config",
"copyright": "(c) Microsoft and contributors. All rights reserved.",
"author": "Microsoft Application Insights Team",
"throttleConfig": {
"default": {
"msgKey": 0,
"limit": {
"sendPercentage": 0,
"maxSendNumber": 0
},
"interval": {
"monthInterval": 100,
"dayInterval": 100,
"maxTimesPerMonth": 0
}
},
"config": {
"ikeyDeprecation": {
"msgKey": 1,
"limit": {
"sendPercentage": 1,
"maxSendNumber": 10
},
"interval": {
"monthInterval": 3,
"dayInterval": 28,
"maxTimesPerMonth": 1
}
},
"cdnDeprecation": {
"ikeyDeprecation": {
"msgKey": 2,
"limit": {
"sendPercentage": 1,
"maxSendNumber": 10
},
"interval": {
"monthInterval": 3,
"dayInterval": 28,
"maxTimesPerMonth": 1
}
}
}
}
}
}

16 changes: 16 additions & 0 deletions tools/config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "applicationinsights-web-config",
"description":" Application Insights JavaScript SDK - Web Config",
"version": "0.0.0",
"copyright": "(c) Microsoft and contributors. All rights reserved.",
"author": "Microsoft Application Insights Team",
"repository": {
"type": "git",
"url": "git+https://github.com/microsoft/ApplicationInsights-JS.git"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/microsoft/ApplicationInsights-JS/issues"
},
"homepage": "https://github.com/microsoft/ApplicationInsights-JS#readme"
}
114 changes: 114 additions & 0 deletions tools/config/scripts/listCdnVersions.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
param (
[string] $container = $null, # Identify the container that you want to check blank == all
[string] $storeContainer = "cdn", # Identifies the destination storage account container
[string] $cdnStorePath = "cdnstoragename", # Identifies the target Azure Storage account (by name)
[string] $subscriptionId = $null, # Identifies the target Azure Subscription Id (if not encoded in the cdnStorePath)
[string] $resourceGroup = $null, # Identifies the target Azure Subscription Resource Group (if not encoded in the cdnStorePath)
[string] $sasToken = $null, # The SAS Token to use rather than using or attempting to login
[string] $logPath = $null, # The location where logs should be written
[switch] $showFiles = $false, # Show the individual files with details as well
[switch] $activeOnly = $false, # Only show the active (deployed) versions
[switch] $testOnly = $false, # Uploads to a "tst" test container on the storage account
[switch] $cdn = $false # (No longer used -- kept for now for backward compatibility)
)

Import-Module -Force -Name "../../../common/publish/Logging"
Import-Module -Force -Name "../../../common/publish/AzureStorageHelper"

[hashtable]$global:connectDetails = @{}
$global:connectDetails.storeContainer = $storeContainer
$global:connectDetails.cdnStorePath = $cdnStorePath
$global:connectDetails.resourceGroup = $resourceGroup
$global:connectDetails.storeName = $null # The endpoint needs to the base name of the endpoint, not the full URL (eg. “my-cdn” rather than “my-cdn.azureedge.net”)
$global:connectDetails.subscriptionId = $subscriptionId
$global:connectDetails.sasToken = $sasToken
$global:connectDetails.storageContext = $null
$global:connectDetails.testOnly = $testOnly

Function Write-LogParams
{
$logDir = Get-LogPath

Write-Log "Container : $container"
Write-Log "Storage Container : $storeContainer"
Write-Log "Store Path : $($global:connectDetails.cdnStorePath)"
Write-Log "Log Path : $logDir"
Write-Log "Show Files : $showFiles"
Write-Log "Test Mode : $testOnly"

if ([string]::IsNullOrWhiteSpace($global:connectDetails.sasToken) -eq $true) {
Write-Log "Mode : User-Credentials"
} else {
Write-Log "Mode : Sas-Token"
}
}

Function Validate-Params
{
# Validate parameters
if ([string]::IsNullOrWhiteSpace($container) -ne $true -and "beta","next","public", "dev", "nightly" -NotContains $container) {
Write-LogFailure "[$($container)] is not a valid value, must be beta, next or public"
}
}

Function Get-AllVersionFiles(
[system.collections.generic.dictionary[string, system.collections.generic.list[hashtable]]] $files,
[string] $storagePath
) {
Get-VersionFiles $files "$storagePath" "ai.config." $null
}

$Error.Clear()

#-----------------------------------------------------------------------------
# Start of Script
#-----------------------------------------------------------------------------
Set-LogPath $logPath, "listCdnVersionsLog"
Write-LogParams
Validate-Params

# Don't try and list anything if any errors have been logged
if (Get-HasErrors -eq $true) {
exit 2
}

# You will need to at least have the AzureRM module installed
InstallRequiredModules
$global:connectDetails = ParseCdnStorePath $global:connectDetails

if ([string]::IsNullOrWhiteSpace($global:connectDetails.sasToken) -eq $true) {
Write-Log "**********************************************************************"
Write-Log "Validating user access"
Write-Log "**********************************************************************"
$global:connectDetails = ValidateAccess $global:connectDetails
}

Write-Log "======================================================================"
# List the files for each container
$files = New-Object 'system.collections.generic.dictionary[string, system.collections.generic.list[hashtable]]'


# Get the public files (scripts/b)
if ([string]::IsNullOrWhiteSpace($container) -eq $true) {
Get-AllVersionFiles $files "scripts/b"
Get-AllVersionFiles $files "beta"
Get-AllVersionFiles $files "next"
Get-AllVersionFiles $files "dev"
Get-AllVersionFiles $files "nightly"
}

if ([string]::IsNullOrWhiteSpace($container) -ne $true) {
if ($container -eq "public") {
Get-AllVersionFiles $files "scripts/b"
} elseif ($container -eq "beta" -or $container -eq "next" -or $container -eq "dev" -or $container -eq "nightly") {
Get-AllVersionFiles $files "$container"
} else {
$global:connectDetails.testOnly = $true
$global:connectDetails.storeContainer = "tst"
Get-AllVersionFiles $files "$container"
}
}

ListVersions $files $activeOnly $showFiles

Write-Log "======================================================================"
132 changes: 132 additions & 0 deletions tools/config/scripts/publishReleaseToCdn.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
param (
[string] $releaseFrom = $null, # The root path for where to find the files to be released
[string] $storeContainer = "cdn", # Identifies the destination storage account container
[string] $cdnStorePath = "cdnstoragename", # Identifies the target Azure Storage account (by name)
[string] $subscriptionId = $null, # Identifies the target Azure Subscription Id (if not encoded in the cdnStorePath)
[string] $resourceGroup = $null, # Identifies the target Azure Subscription Resource Group (if not encoded in the cdnStorePath)
[string] $sasToken = $null, # The SAS Token to use rather than using or attempting to login
[string] $logPath = $null, # The location where logs should be written
[switch] $overwrite = $false, # Overwrite any existing files
[switch] $testOnly = $false, # Uploads to a "tst" test container on the storage account
[switch] $cdn = $false # (No longer used -- kept for now for backward compatibility)
)

Import-Module -Force -Name "../../../common/publish/Logging"
Import-Module -Force -Name "../../../common/publish/AzureStorageHelper"

[hashtable]$global:connectDetails = @{}
$global:connectDetails.storeContainer = $storeContainer
$global:connectDetails.cdnStorePath = $cdnStorePath
$global:connectDetails.resourceGroup = $resourceGroup
$global:connectDetails.storeName = $null # The endpoint needs to the base name of the endpoint, not the full URL (eg. “my-cdn” rather than “my-cdn.azureedge.net”)
$global:connectDetails.subscriptionId = $subscriptionId
$global:connectDetails.sasToken = $sasToken
$global:connectDetails.storageContext = $null
$global:connectDetails.testOnly = $testOnly

$global:cacheValue = $null

Function Write-LogParams
{
$logDir = Get-LogPath

Write-Log "Storage Container : $storeContainer"
Write-Log "Store Path : $($global:connectDetails.cdnStorePath)"
Write-Log "Overwrite : $overwrite"
Write-Log "Test Mode : $testOnly"
Write-Log "SourcePath : $jsSdkDir"
Write-Log "Log Path : $logDir"

if ([string]::IsNullOrWhiteSpace($global:connectDetails.sasToken) -eq $true) {
Write-Log "Mode : User-Credentials"
} else {
Write-Log "Mode : Sas-Token"
}
}

Function GetReleaseFiles (
[hashtable] $verDetails
)
{
$version = $verDetails.full
Write-Log "Version : $($verDetails.full)"
Write-Log " Number : $($verDetails.ver)"
Write-Log " Type : $($verDetails.type)"
Write-Log " BldNum : $($verDetails.bldNum)"

# check if the minified dir exists
$jsSdkSrcDir = Join-Path $jssdkDir -ChildPath "browser\";

if (-Not (Test-Path $jsSdkSrcDir)) {
Write-LogWarning "'$jsSdkSrcDir' directory doesn't exist. Compile JSSDK first.";
exit
}

$files = New-Object 'system.collections.generic.dictionary[string,string]'

Write-Log "Adding files";
AddReleaseFile $files $jsSdkSrcDir "ai.config.$version.cfg.json"
return $files
}

#-----------------------------------------------------------------------------
# Start of Script
#-----------------------------------------------------------------------------
Set-LogPath $logPath "publishReleaseCdnLog"

$jsSdkDir = $releaseFrom
if ([string]::IsNullOrWhiteSpace($jsSdkDir) -eq $true) {
$jsSdkDir = Split-Path (Split-Path $MyInvocation.MyCommand.Path) -Parent
}

$cacheControl1Year = "public, max-age=31536000, immutable, no-transform";
$contentType = "text/javascript; charset=utf-8";

Write-LogParams

# You will need to at least have the AzureRM module installed
InstallRequiredModules
$global:connectDetails = ParseCdnStorePath $global:connectDetails

if ([string]::IsNullOrWhiteSpace($global:connectDetails.sasToken) -eq $true) {
Write-Log "**********************************************************************"
Write-Log "Validating user access"
Write-Log "**********************************************************************"
$global:connectDetails = ValidateAccess $global:connectDetails
}

Write-Log "======================================================================"

$version = GetPackageVersion $jsSdkDir

$releaseFiles = GetReleaseFiles $version # Get the versioned files only
if ($null -eq $releaseFiles -or $releaseFiles.Count -eq 0) {
Write-LogFailure "Unable to find any release files"
}

Write-Log "Release Files : $($releaseFiles.Count)"
Write-Log "----------------------------------------------------------------------"

# Publish the full versioned files to all release folders
if ($version.type -eq "release") {
# Normal publishing deployment
PublishFiles $releaseFiles "beta" $cacheControl1Year $contentType $overwrite
PublishFiles $releaseFiles "next" $cacheControl1Year $contentType $overwrite
PublishFiles $releaseFiles "scripts/b" $cacheControl1Year $contentType $overwrite
}
elseif ($version.type -eq "rc") {
PublishFiles $releaseFiles "beta" $cacheControl1Year $contentType $overwrite
PublishFiles $releaseFiles "next" $cacheControl1Year $contentType $overwrite
}
elseif ($version.type -eq "dev" -or $version.type -eq "beta" -or $version.type -eq "nightly") {
# Publish to release type folder folder
PublishFiles $releaseFiles "$($version.type)" $cacheControl1Year $contentType $overwrite
}
else {
# Upload to the test container rather than the supplied one
$global:connectDetails.testOnly = $true
$global:connectDetails.storeContainer = "tst"
PublishFiles $releaseFiles "$($version.type)" $cacheControl1Year $contentType $overwrite
}

Write-Log "======================================================================"
Loading

0 comments on commit d929ac8

Please sign in to comment.