<commit_before>const { Lambda } = require('aws-sdk');

function LambdaManager(region) {
	this.LambdaFactory = new Lambda({ region });
}

LambdaManager.prototype.PublishNewVersion = function(functionName, bucket, deploymentKey) {
	return this.LambdaFactory.updateFunctionCode({
		FunctionName: functionName,
		Publish: true,
		S3Bucket: bucket,
		S3Key: deploymentKey
	}).promise();
};

LambdaManager.prototype.getAliasMap = async function(functionName) {
	let nextToken;
	let aliases = [];
	do {
		const functionsBatch = await this.LambdaFactory.listAliases({ FunctionName: functionName, Marker: nextToken }).promise();
		aliases = aliases.concat(functionsBatch.Aliases);
		nextToken = functionsBatch.NextMarker;
	} while (nextToken);

	const versionAliasMap = {};
	aliases.forEach(alias => {
		if (!versionAliasMap[alias.FunctionVersion]) {
			versionAliasMap[alias.FunctionVersion] = [];
		}
		versionAliasMap[alias.FunctionVersion].push(alias.Name);
	});
	return versionAliasMap;
};

LambdaManager.prototype.cleanupProduction = async function(functionName, forceRemovalOfAliases = false, dryRun = true) {
	const keepFunctionsYoungerThanDays = 60;
	let nextToken;
	let versions = [];

	// get lambda versions
	do {
		const functionsBatch = await this.LambdaFactory.listVersionsByFunction({ FunctionName: functionName, Marker: nextToken }).promise();
		versions = versions.concat(functionsBatch.Versions);
		nextToken = functionsBatch.NextMarker;
	} while (nextToken);

	// log statistics
	const totalCodeSize = versions.map(f => f.CodeSize).reduce((prev, curr) => prev + curr, 0);
	console.log(`Total code size of all versions: ${Math.floor(totalCodeSize / 2 ** 20)} MB`);

	// filter out newer versions to only cleanup old versions
	const cutoff = new Date();
	cutoff.setDate(new Date().getDate() - keepFunctionsYoungerThanDays);
	const productionAliasData = await this.LambdaFactory.getAlias({ FunctionName: functionName, Name: 'production' }).promise();
	const versionsToDelete = versions.slice(0, -10).filter(f => f.LastModified < cutoff.toISOString() && f.Version !== '$LATEST' && f.Version !== productionAliasData.FunctionVersion);

	if (!versionsToDelete.length) {
		return;
	}<commit_msg>fix force removal of aliases.<commit_after>const { Lambda } = require('aws-sdk');

function LambdaManager(region) {
	this.LambdaFactory = new Lambda({ region });
}

LambdaManager.prototype.PublishNewVersion = function(functionName, bucket, deploymentKey) {
	return this.LambdaFactory.updateFunctionCode({
		FunctionName: functionName,
		Publish: true,
		S3Bucket: bucket,
		S3Key: deploymentKey
	}).promise();
};

LambdaManager.prototype.getAliasMap = async function(functionName) {
	let nextToken;
	let aliases = [];
	do {
		const functionsBatch = await this.LambdaFactory.listAliases({ FunctionName: functionName, Marker: nextToken }).promise();
		aliases = aliases.concat(functionsBatch.Aliases);
		nextToken = functionsBatch.NextMarker;
	} while (nextToken);

	const versionAliasMap = {};
	aliases.forEach(alias => {
		if (!versionAliasMap[alias.FunctionVersion]) {
			versionAliasMap[alias.FunctionVersion] = [];
		}
		versionAliasMap[alias.FunctionVersion].push(alias.Name);
	});
	return versionAliasMap;
};

LambdaManager.prototype.cleanupProduction = async function(functionName, forceRemovalOfAliases = false, dryRun = true) {
	const keepFunctionsYoungerThanDays = 60;
	let nextToken;
	let versions = [];

	// get lambda versions
	do {
		const functionsBatch = await this.LambdaFactory.listVersionsByFunction({ FunctionName: functionName, Marker: nextToken }).promise();
		versions = versions.concat(functionsBatch.Versions);
		nextToken = functionsBatch.NextMarker;
	} while (nextToken);

	// log statistics
	const totalCodeSize = versions.map(f => f.CodeSize).reduce((prev, curr) => prev + curr, 0);
	console.log(`Total code size of all versions: ${Math.floor(totalCodeSize / 2 ** 20)} MB`);

	// filter out newer versions to only cleanup old versions
	const cutoff = new Date();
	cutoff.setDate(new Date().getDate() - keepFunctionsYoungerThanDays);
	const productionAliasData = await this.LambdaFactory.getAlias({ FunctionName: functionName, Name: 'production' }).promise();
	const filteredVersions = versions.filter(f => f.Version !== '$LATEST' && f.Version !== productionAliasData.FunctionVersion);
	const versionsToDelete = forceRemovalOfAliases ? filteredVersions : filteredVersions.slice(0, -10).filter(f => f.LastModified < cutoff.toISOString());

	if (!versionsToDelete.length) {
		return;
	}
{'fraction_non_alphanumeric': 0.12685739631847417, 'fraction_numerical': 0.004879130627633622, 'mean_word_length': 5.651917404129794, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '311717', 'n_tokens_mistral': 1373, 'n_tokens_neox': 1307, 'n_words': 303}