|
| 1 | +/*! |
| 2 | + * Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import * as vscode from 'vscode' |
| 7 | +import * as path from 'path' |
| 8 | +import * as constants from '../shared/constants' |
| 9 | +import * as aslFormats from '../stepFunctions/constants/aslFormats' |
| 10 | +import * as telemetry from './telemetry/telemetry' |
| 11 | +import * as pathutil from '../shared/utilities/pathUtils' |
| 12 | +import * as fsutil from '../shared/filesystemUtilities' |
| 13 | +import * as sysutil from '../shared/systemUtilities' |
| 14 | +import * as collectionUtil from '../shared/utilities/collectionUtils' |
| 15 | +import globals from './extensionGlobals' |
| 16 | + |
| 17 | +/** AWS filetypes: vscode language ids */ |
| 18 | +export const awsFiletypeLangIds = { |
| 19 | + /** vscode language ids registered by AWS Toolkit or other AWS extensions for handling. */ |
| 20 | + awsOwned: [aslFormats.JSON_ASL, aslFormats.YAML_ASL, constants.ssmJson, constants.ssmYaml], |
| 21 | + /** generic vscode language ids that possibly are AWS filetypes */ |
| 22 | + ambiguous: ['ini', 'plaintext', aslFormats.JSON_TYPE, aslFormats.YAML_TYPE], |
| 23 | +} |
| 24 | + |
| 25 | +/** |
| 26 | + * Maps vscode language ids to AWS filetypes for telemetry. |
| 27 | + */ |
| 28 | +export function langidToAwsFiletype(langId: string): telemetry.AwsFiletype { |
| 29 | + switch (langId) { |
| 30 | + case constants.ssmJson: |
| 31 | + case constants.ssmYaml: |
| 32 | + return 'ssmDocument' |
| 33 | + case aslFormats.JSON_ASL: |
| 34 | + case aslFormats.YAML_ASL: |
| 35 | + return 'stepfunctionsAsl' |
| 36 | + default: |
| 37 | + return 'other' |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** Returns true if file `f` is somewhere in `~/.aws`. */ |
| 42 | +export function isAwsConfig(f: string): boolean { |
| 43 | + const awsDir = path.join(sysutil.SystemUtilities.getHomeDirectory(), '.aws') |
| 44 | + if (fsutil.isInDirectory(awsDir, f)) { |
| 45 | + return true |
| 46 | + } |
| 47 | + return false |
| 48 | +} |
| 49 | + |
| 50 | +export function isAwsFiletype(doc: vscode.TextDocument): boolean | undefined { |
| 51 | + if (awsFiletypeLangIds.awsOwned.includes(doc.languageId)) { |
| 52 | + return true |
| 53 | + } |
| 54 | + if (isAwsConfig(doc.fileName)) { |
| 55 | + return true |
| 56 | + } |
| 57 | + if (awsFiletypeLangIds.ambiguous.includes(doc.languageId)) { |
| 58 | + return undefined // Maybe |
| 59 | + } |
| 60 | + return false |
| 61 | +} |
| 62 | + |
| 63 | +export function activate(): void { |
| 64 | + globals.context.subscriptions.push( |
| 65 | + // TODO: onDidChangeTextDocument ? |
| 66 | + vscode.workspace.onDidOpenTextDocument(async (doc: vscode.TextDocument) => { |
| 67 | + const isAwsFileExt = isAwsFiletype(doc) |
| 68 | + const isSchemaHandled = globals.schemaService.isMapped(doc.uri) |
| 69 | + const isCfnTemplate = !!globals.templateRegistry.registeredItems.find( |
| 70 | + t => pathutil.normalize(t.path) === pathutil.normalize(doc.fileName) |
| 71 | + ) |
| 72 | + if (!isAwsFileExt && !isSchemaHandled && !isCfnTemplate) { |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + let fileExt: string | undefined = path.extname(doc.fileName) |
| 77 | + fileExt = fileExt ? fileExt : undefined // Telemetry client will fail on empty string. |
| 78 | + |
| 79 | + // TODO: ask templateRegistry if this is SAM or CFN. |
| 80 | + // TODO: ask schemaService for the precise filetype. |
| 81 | + let telemKind = isAwsConfig(doc.fileName) ? 'awsCredentials' : langidToAwsFiletype(doc.languageId) |
| 82 | + if (telemKind === 'other') { |
| 83 | + telemKind = isCfnTemplate ? 'cloudformationSam' : isSchemaHandled ? 'cloudformation' : 'other' |
| 84 | + } |
| 85 | + |
| 86 | + // HACK: for "~/.aws/foo" vscode sometimes _only_ emits "~/.aws/foo.git". |
| 87 | + if (telemKind === 'awsCredentials' && fileExt === '.git') { |
| 88 | + fileExt = undefined |
| 89 | + } |
| 90 | + |
| 91 | + if (await isSameMetricPending(telemKind, fileExt)) { |
| 92 | + return // Avoid redundant/duplicate metrics. |
| 93 | + } |
| 94 | + |
| 95 | + telemetry.recordFileEditAwsFile({ |
| 96 | + awsFiletype: telemKind, |
| 97 | + passive: true, |
| 98 | + result: 'Succeeded', |
| 99 | + filenameExt: fileExt, |
| 100 | + }) |
| 101 | + }, undefined) |
| 102 | + ) |
| 103 | +} |
| 104 | + |
| 105 | +async function isSameMetricPending(filetype: string, fileExt: string | undefined): Promise<boolean> { |
| 106 | + const pendingMetrics = await collectionUtil.first( |
| 107 | + globals.telemetry.findIter(m => { |
| 108 | + const m1 = m.Metadata?.find(o => o.Key === 'awsFiletype') |
| 109 | + const m2 = m.Metadata?.find(o => o.Key === 'filenameExt') |
| 110 | + return m.MetricName === 'file_editAwsFile' && m1?.Value === filetype && m2?.Value === fileExt |
| 111 | + }) |
| 112 | + ) |
| 113 | + return !!pendingMetrics |
| 114 | +} |
0 commit comments