Skip to content

Commit de95474

Browse files
authored
Handle missing files and fix byte metrics (#262)
1 parent de2ce54 commit de95474

File tree

16 files changed

+118
-45
lines changed

16 files changed

+118
-45
lines changed

eslint.config.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,14 @@ export default tseslint.config([
160160
message:
161161
'Usage of raw ResourceAttribute strings is restricted in src code. Use ResourceAttribute enum instead',
162162
},
163+
{
164+
selector: "ImportSpecifier[imported.name='readFileSync']",
165+
message: 'Use methods in File.ts',
166+
},
167+
{
168+
selector: "ImportSpecifier[imported.name='readFile']",
169+
message: 'Use methods in File.ts',
170+
},
163171
],
164172
},
165173
},
@@ -212,7 +220,6 @@ export default tseslint.config([
212220
'@typescript-eslint/no-unsafe-call': 'off',
213221
'@typescript-eslint/no-non-null-assertion': 'off',
214222
'@typescript-eslint/no-unsafe-argument': 'off',
215-
'@typescript-eslint/no-unused-vars': 'off',
216223
'@typescript-eslint/no-explicit-any': 'off',
217224
'@typescript-eslint/no-unused-expressions': 'off',
218225
'@typescript-eslint/ban-ts-comment': 'off',

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
"test:go": "GOPROXY=direct go test -C cfn-init ./...",
3636
"bundle": "rm -rf out && webpack --env mode=development && npm run build:go:dev",
3737
"bundle:alpha": "rm -rf out && webpack --env mode=production --env env=alpha",
38-
"download-wheels": "tsx tools/download-wheels.ts",
3938
"bundle:beta": "rm -rf out && webpack --env mode=production --env env=beta",
4039
"bundle:prod": "rm -rf out && webpack --env mode=production --env env=prod",
40+
"download-wheels": "tsx tools/download-wheels.ts",
4141
"benchmark": "NODE_ENV=test AWS_ENV=alpha node --max-old-space-size=16384 --expose-gc -r ts-node/register tools/benchmark.ts",
4242
"generate-metrics": "NODE_ENV=development AWS_ENV=alpha node --max-old-space-size=16384 -r ts-node/register tools/telemetry-generator.ts",
4343
"debug-tree": "node -r ts-node/register tools/debug_tree.ts",

src/ai/llm/LLMConfig.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { readFileSync, existsSync } from 'fs';
1+
import { existsSync } from 'fs';
22
import { homedir } from 'os';
33
import { join } from 'path';
44
import { DeepReadonly } from 'ts-essentials';
55
import { LoggerFactory } from '../../telemetry/LoggerFactory';
6+
import { readFileIfExists } from '../../utils/File';
67
import { parseWithPrettyError } from '../../utils/ZodErrorWrapper';
78
import { LLMConfigType, parseLLMConfig } from './LLMTypes';
89

@@ -21,7 +22,7 @@ export class LLMConfig {
2122
}
2223

2324
try {
24-
const config: unknown = JSON.parse(readFileSync(LLMConfig.ConfigFile, 'utf8'));
25+
const config: unknown = JSON.parse(readFileIfExists(LLMConfig.ConfigFile, 'utf8'));
2526
const llmConfig = parseWithPrettyError(parseLLMConfig, config);
2627
logger.info({ provider: llmConfig.provider, model: llmConfig.model }, 'LLM configuration');
2728
return llmConfig;

src/artifactexporter/ResourceExporters.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { existsSync, mkdtempSync, copyFileSync, rmSync, createWriteStream, statSync, readFileSync } from 'fs';
1+
import { existsSync, mkdtempSync, copyFileSync, rmSync, createWriteStream, statSync } from 'fs';
22
import { tmpdir } from 'os';
33
import path, { join, basename } from 'path';
44
import { pathToFileURL } from 'url';
55
import archiver from 'archiver';
66
import { dump } from 'js-yaml';
77
import { detectDocumentType } from '../document/DocumentUtils';
88
import { S3Service } from '../services/S3Service';
9+
import { readFileIfExists } from '../utils/File';
910
import { ArtifactExporter } from './ArtifactExporter';
1011

1112
export function isS3Url(url: string): boolean {
@@ -254,7 +255,7 @@ class CloudFormationStackResource extends Resource {
254255
}
255256

256257
const templateUri = pathToFileURL(templateAbsPath).href;
257-
const content = readFileSync(templateAbsPath, 'utf8');
258+
const content = readFileIfExists(templateAbsPath, 'utf8');
258259
const templateType = detectDocumentType(templateUri, content).type;
259260

260261
const template = new ArtifactExporter(this.s3Service, templateType, templateUri, content);

src/datastore/LMDB.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,38 +128,34 @@ export class LMDBStoreFactory implements DataStoreFactory {
128128
}
129129

130130
private registerLMDBGauges(): void {
131-
let totalMb = 0;
131+
let totalBytes = 0;
132132
const globalStat = stats(this.env);
133133
this.telemetry.registerGaugeProvider('version', () => VersionNumber);
134-
this.telemetry.registerGaugeProvider('global.size', () => globalStat.totalSizeMB, { unit: 'MB' });
135-
this.telemetry.registerGaugeProvider('global.max.size', () => globalStat.maxSizeMB, {
136-
unit: 'MB',
134+
this.telemetry.registerGaugeProvider('global.size', () => globalStat.totalSize, { unit: 'By' });
135+
this.telemetry.registerGaugeProvider('global.max.size', () => globalStat.maxSize, {
136+
unit: 'By',
137137
});
138138
this.telemetry.registerGaugeProvider('global.entries', () => globalStat.entries);
139-
totalMb += globalStat.totalSizeMB;
139+
totalBytes += globalStat.totalSize;
140140

141141
for (const [name, store] of this.stores.entries()) {
142142
const stat = store.stats();
143-
totalMb += stat.totalSizeMB;
143+
totalBytes += stat.totalSize;
144144

145-
this.telemetry.registerGaugeProvider(`store.${name}.size`, () => stat.totalSizeMB, {
146-
unit: 'MB',
145+
this.telemetry.registerGaugeProvider(`store.${name}.size`, () => stat.totalSize, {
146+
unit: 'By',
147147
});
148148
this.telemetry.registerGaugeProvider(`store.${name}.entries`, () => stat.entries, {
149-
unit: 'MB',
149+
unit: 'By',
150150
});
151151
}
152152

153-
this.telemetry.registerGaugeProvider('global.usage', () => totalMb / TotalMaxDbSize, {
153+
this.telemetry.registerGaugeProvider('global.usage', () => (100 * totalBytes) / TotalMaxDbSize, {
154154
unit: '%',
155155
});
156156
}
157157
}
158158

159-
function bytesToMB(bytes: number) {
160-
return Number((bytes / (1024 * 1024)).toFixed(4));
161-
}
162-
163159
const VersionNumber = 2;
164160
const Version = `v${VersionNumber}`;
165161
const Encoding: 'msgpack' | 'json' | 'string' | 'binary' | 'ordered-binary' = 'msgpack';
@@ -173,8 +169,8 @@ function stats(store: RootDatabase | Database): StoreStatsType {
173169
const overflowPages = stats['overflowPages'];
174170

175171
return {
176-
totalSizeMB: bytesToMB((branchPages + leafPages + overflowPages) * pageSize),
177-
maxSizeMB: bytesToMB(stats['mapSize']),
172+
totalSize: (branchPages + leafPages + overflowPages) * pageSize,
173+
maxSize: stats['mapSize'],
178174
entries: stats['entryCount'],
179175
maxReaders: stats['maxReaders'],
180176
numReaders: stats['numReaders'],
@@ -185,8 +181,8 @@ function stats(store: RootDatabase | Database): StoreStatsType {
185181
}
186182

187183
type StoreStatsType = {
188-
totalSizeMB: number;
189-
maxSizeMB: number;
184+
totalSize: number;
185+
maxSize: number;
190186
entries: number;
191187
maxReaders: number; // The configured maximum number of concurrent reader slots
192188
numReaders: number; // The number of reader slots currently in use

src/featureFlag/FeatureFlagProvider.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { readFileSync, writeFileSync } from 'fs';
1+
import { writeFileSync } from 'fs';
22
import { join } from 'path';
33
import { LoggerFactory } from '../telemetry/LoggerFactory';
44
import { Measure } from '../telemetry/TelemetryDecorator';
55
import { Closeable } from '../utils/Closeable';
66
import { AwsEnv } from '../utils/Environment';
7+
import { readFileIfExists } from '../utils/File';
78
import { downloadJson } from '../utils/RemoteDownload';
89
import { FeatureFlag, TargetedFeatureFlag } from './FeatureFlagI';
910
import { FeatureFlagSupplier, FeatureFlagConfigKey, TargetedFeatureFlagConfigKey } from './FeatureFlagSupplier';
@@ -20,7 +21,7 @@ export class FeatureFlagProvider implements Closeable {
2021
private readonly getLatestFeatureFlags: (env: string) => Promise<unknown>,
2122
private readonly localFile = join(__dirname, 'assets', 'featureFlag', `${AwsEnv.toLowerCase()}.json`),
2223
) {
23-
this.config = JSON.parse(readFileSync(localFile, 'utf8'));
24+
this.config = JSON.parse(readFileIfExists(localFile, 'utf8'));
2425

2526
this.supplier = new FeatureFlagSupplier(() => {
2627
return this.config;

src/services/RelationshipSchemaService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { readFileSync } from 'fs';
21
import { join } from 'path';
32
import { LoggerFactory } from '../telemetry/LoggerFactory';
3+
import { readFileIfExists } from '../utils/File';
44

55
const logger = LoggerFactory.getLogger('RelationshipSchemaService');
66

@@ -32,7 +32,7 @@ export class RelationshipSchemaService {
3232

3333
private loadAllSchemas(): void {
3434
try {
35-
const schemaContent = readFileSync(this.schemaFilePath, 'utf8');
35+
const schemaContent = readFileIfExists(this.schemaFilePath, 'utf8');
3636
const allSchemas = JSON.parse(schemaContent) as RelationshipSchemaData;
3737

3838
for (const [resourceTypeKey, relationships] of Object.entries(allSchemas)) {

src/services/S3Service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readFileSync } from 'fs';
1+
import { readFileSync } from 'fs'; // eslint-disable-line no-restricted-syntax -- Needs to be fixed
22
import { fileURLToPath } from 'url';
33
import { S3Client, PutObjectCommand, ListBucketsCommand, HeadObjectCommand } from '@aws-sdk/client-s3';
44
import { Measure } from '../telemetry/TelemetryDecorator';

src/services/guard/GuardService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { readFile } from 'fs/promises';
21
import { performance } from 'perf_hooks';
32
import { Diagnostic, DiagnosticSeverity, Range } from 'vscode-languageserver';
43
import { SyntaxTreeManager } from '../../context/syntaxtree/SyntaxTreeManager';
@@ -13,6 +12,7 @@ import { Count, Telemetry } from '../../telemetry/TelemetryDecorator';
1312
import { Closeable } from '../../utils/Closeable';
1413
import { Delayer } from '../../utils/Delayer';
1514
import { extractErrorMessage } from '../../utils/Errors';
15+
import { readFileIfExistsAsync } from '../../utils/File';
1616
import { byteSize } from '../../utils/String';
1717
import { DiagnosticCoordinator } from '../DiagnosticCoordinator';
1818
import { getRulesForPack, getAvailableRulePacks, GuardRuleData } from './GeneratedGuardRules';
@@ -640,7 +640,7 @@ export class GuardService implements SettingsConfigurable, Closeable {
640640
*/
641641
private async loadRulesFromFile(filePath: string): Promise<GuardRule[]> {
642642
try {
643-
const fileContent = await readFile(filePath, 'utf8');
643+
const fileContent = await readFileIfExistsAsync(filePath, 'utf8');
644644
return this.parseRulesFromContent(fileContent, filePath);
645645
} catch (error) {
646646
throw new Error(`Failed to read rules file '${filePath}': ${extractErrorMessage(error)}`);

src/telemetry/LoggerFactory.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable @typescript-eslint/no-unsafe-function-type */
2-
import { readdir, stat, unlink, readFile, writeFile } from 'fs/promises';
2+
// eslint-disable-next-line no-restricted-syntax -- circular dependency
3+
import { readdir, stat, unlink, writeFile, readFile } from 'fs/promises';
34
import { join } from 'path';
45
import { DateTime } from 'luxon';
56
import pino, { LevelWithSilent, Logger } from 'pino';

0 commit comments

Comments
 (0)