-
Notifications
You must be signed in to change notification settings - Fork 15
CLOUDP-290417: [Product Metrics/Observability] Implement Collector class #352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6db3071
CLOUDP-290417: [Product Metrics/Observability] Implement Collector class
yelizhenden-mdb e6c1b06
Fix collector class singleton pattern
yelizhenden-mdb daaaa87
Prettier fix
yelizhenden-mdb fa6362c
Fix: Include exception reason
yelizhenden-mdb 0fca929
little fix
yelizhenden-mdb 4b48f99
fix: collect violations and adoptions in helper functions
yelizhenden-mdb 695af05
fix: add jsdoc
yelizhenden-mdb 3ab740a
fix: all collect helpers in collectionUtils file
yelizhenden-mdb d83044c
little fix: schemaPath
yelizhenden-mdb f1a52c0
prettier fix
yelizhenden-mdb 8c4b9a6
fix: import exception_extension
yelizhenden-mdb c00e9d7
fix: update .gitignore
yelizhenden-mdb 56e520f
fix: update .gitignore
yelizhenden-mdb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { beforeEach, describe, expect, it } from '@jest/globals'; | ||
| import collector, { EntryType } from '../../metrics/collector'; | ||
| import * as fs from 'node:fs'; | ||
|
|
||
| jest.mock('node:fs'); | ||
|
|
||
| describe('Collector Class', () => { | ||
| const expectedOutput = { | ||
| violations: [ | ||
| { componentId: 'example.component', ruleName: 'rule-1' }, | ||
| { componentId: 'example.component', ruleName: 'rule-2' }, | ||
| ], | ||
| adoptions: [{ componentId: 'example.component', ruleName: 'rule-3' }], | ||
| exceptions: [{ componentId: 'example.component', ruleName: 'rule-4', exceptionReason: 'exception-reason' }], | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| collector.entries = { | ||
| [EntryType.VIOLATION]: [], | ||
| [EntryType.ADOPTION]: [], | ||
| [EntryType.EXCEPTION]: [], | ||
| }; | ||
|
|
||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('should collect violations, adoptions, and exceptions correctly', () => { | ||
| collector.add(EntryType.VIOLATION, ['example', 'component'], 'rule-1'); | ||
| collector.add(EntryType.VIOLATION, ['example', 'component'], 'rule-2'); | ||
| collector.add(EntryType.ADOPTION, ['example', 'component'], 'rule-3'); | ||
| collector.add(EntryType.EXCEPTION, ['example', 'component'], 'rule-4', 'exception-reason'); | ||
|
|
||
| expect(collector.entries).toEqual(expectedOutput); | ||
|
|
||
| collector.flushToFile(); | ||
| const writtenData = JSON.stringify(expectedOutput, null, 2); | ||
| expect(fs.writeFileSync).toHaveBeenCalledWith('ipa-collector-results-combined.log', writtenData); | ||
| }); | ||
|
|
||
| it('should not add invalid entries', () => { | ||
| collector.add(null, 'rule-1', EntryType.VIOLATION); | ||
| collector.add(['example', 'component'], null, EntryType.ADOPTION); | ||
| collector.add(['example', 'component'], 'rule-4', null); | ||
|
|
||
| expect(collector.entries).toEqual({ | ||
| violations: [], | ||
| adoptions: [], | ||
| exceptions: [], | ||
| }); | ||
|
|
||
| expect(fs.writeFileSync).not.toHaveBeenCalled(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import * as fs from 'node:fs'; | ||
|
|
||
| export const EntryType = Object.freeze({ | ||
| EXCEPTION: 'exceptions', | ||
| VIOLATION: 'violations', | ||
| ADOPTION: 'adoptions', | ||
| }); | ||
|
|
||
| class Collector { | ||
| static instance = null; | ||
|
|
||
| static getInstance() { | ||
| if (!this.instance) { | ||
| this.instance = new Collector(); | ||
| } | ||
| return this.instance; | ||
| } | ||
|
|
||
| constructor() { | ||
| if (Collector.instance) { | ||
| throw new Error('Use Collector.getInstance()'); | ||
| } | ||
|
|
||
| this.entries = { | ||
| [EntryType.VIOLATION]: [], | ||
| [EntryType.ADOPTION]: [], | ||
| [EntryType.EXCEPTION]: [], | ||
| }; | ||
|
|
||
| this.fileName = 'ipa-collector-results-combined.log'; | ||
|
|
||
| process.on('exit', () => this.flushToFile()); | ||
| process.on('SIGINT', () => { | ||
| this.flushToFile(); | ||
| process.exit(); | ||
| }); | ||
| } | ||
|
|
||
| add(type, componentId, ruleName, exceptionReason = null) { | ||
| if (componentId && ruleName && type) { | ||
| if (!Object.values(EntryType).includes(type)) { | ||
| throw new Error(`Invalid entry type: ${type}`); | ||
| } | ||
|
|
||
| componentId = componentId.join('.'); | ||
| const entry = { componentId, ruleName }; | ||
|
|
||
| if (type === EntryType.EXCEPTION && exceptionReason) { | ||
| entry.exceptionReason = exceptionReason; | ||
| } | ||
|
|
||
| this.entries[type].push(entry); | ||
| } | ||
| } | ||
|
|
||
| flushToFile() { | ||
| try { | ||
| const data = JSON.stringify(this.entries, null, 2); | ||
| fs.writeFileSync(this.fileName, data); | ||
| } catch (error) { | ||
| console.error('Error writing exceptions to file:', error); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const collector = Collector.getInstance(); | ||
| export default collector; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
tools/spectral/ipa/rulesets/functions/utils/collectionUtils.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import collector, { EntryType } from '../../../metrics/collector.js'; | ||
|
|
||
| /** | ||
| * Collects a violation entry and returns formatted error data. | ||
| * | ||
| * @param {string} path - The JSON path for the object where the rule violation occurred. | ||
| * @param {string} ruleName - The name of the rule that was violated. | ||
| * @param {string|Array<Object>} errorData - The error information. Can be either a string message or an array of error objects. | ||
| * @returns {Array<Object>} An array of error objects. Each object has a 'message' property. | ||
| * @throws {Error} Throws an error if errorData is neither a string nor an array. | ||
| * | ||
| */ | ||
| export function collectAndReturnViolation(path, ruleName, errorData) { | ||
yelizhenden-mdb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| collector.add(EntryType.VIOLATION, path, ruleName); | ||
|
|
||
| if (typeof errorData === 'string') { | ||
| return [{ message: errorData }]; | ||
| } else if (Array.isArray(errorData)) { | ||
| return errorData; | ||
| } else { | ||
| throw new Error('Invalid error data type. Expected string or array.'); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Collects an adoption entry. | ||
| * | ||
| * @param {string} path - The JSON path for the object where the rule adoption occurred. | ||
| * @param {string} ruleName - The name of the rule that was adopted. | ||
| */ | ||
| export function collectAdoption(path, ruleName) { | ||
| collector.add(EntryType.ADOPTION, path, ruleName); | ||
| } | ||
|
|
||
| /** | ||
| * Collects an exception entry. | ||
| * | ||
| * @param object the object to evaluate | ||
| * @param {string} path - The JSON path for the object where the rule exception occurred. | ||
| * @param {string} ruleName - The name of the rule that the exception is defined for. | ||
| */ | ||
| export function collectException(object, ruleName, path) { | ||
| const EXCEPTION_EXTENSION = 'x-xgen-IPA-exception'; | ||
yelizhenden-mdb marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let exceptionReason = object[EXCEPTION_EXTENSION][ruleName]; | ||
| if (exceptionReason) { | ||
| collector.add(EntryType.EXCEPTION, path, ruleName, exceptionReason); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.