Skip to content
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

Feature/2474/risk profile calculation for every language #2679

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/)

## [unreleased] (Added 🚀 | Changed | Removed 🗑 | Fixed 🐞 | Chore 👨‍💻 👩‍💻)

### Added 🚀
Hall-Ma marked this conversation as resolved.
Show resolved Hide resolved

- Risk profile is now calculated for every occurring programming languages in selected file(s)[#2679](https://github.com/MaibornWolff/codecharta/pull/2679)

## [1.91.0] - 2022-02-21

### Changed
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,42 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ArtificialIntelligenceController on files selection changed should calculate risk profile and suspicious metrics for all occurring programming languages in selected file 1`] = `
Object {
"highRisk": 145,
"lowRisk": 125,
"moderateRisk": 0,
"totalRloc": 270,
"veryHighRisk": 0,
}
`;

exports[`ArtificialIntelligenceController on files selection changed should calculate risk profile and suspicious metrics for all occurring programming languages in selected file 2`] = `
Object {
"highRisk": 54,
"lowRisk": 46,
"moderateRisk": 0,
"veryHighRisk": 0,
}
`;

exports[`ArtificialIntelligenceController on files selection changed should calculate risk profile but skip files with missing metrics 1`] = `
Object {
"highRisk": 0,
"lowRisk": 0,
"moderateRisk": 0,
"veryHighRisk": 0,
}
`;

exports[`ArtificialIntelligenceController on files selection changed should calculate risk profile for Java map 1`] = `
Object {
"highRisk": 37,
"lowRisk": 46,
"moderateRisk": 17,
"veryHighRisk": 0,
}
`;

exports[`ArtificialIntelligenceController on files selection changed should calculate risk profile for not excluded files only 1`] = `
Object {
"highRisk": 50,
Expand Down Expand Up @@ -43,11 +80,40 @@ Array [
]
`;

exports[`ArtificialIntelligenceController on files selection changed should clear and calculate risk profile for Java map 1`] = `
exports[`ArtificialIntelligenceController on files selection changed should set default values for risk profile 1`] = `
Object {
"highRisk": 37,
"lowRisk": 46,
"moderateRisk": 17,
"highRisk": 0,
"lowRisk": 0,
"moderateRisk": 0,
"totalRloc": 0,
"veryHighRisk": 0,
}
`;

exports[`ArtificialIntelligenceController on files selection changed should set default values for risk profile 2`] = `
Object {
"highRisk": 0,
"lowRisk": 0,
"moderateRisk": 0,
"veryHighRisk": 0,
}
`;

exports[`ArtificialIntelligenceController should calculate risk profile only for valid file extensions 1`] = `
Object {
"highRisk": 0,
"lowRisk": 125,
"moderateRisk": 45,
"totalRloc": 170,
"veryHighRisk": 0,
}
`;

exports[`ArtificialIntelligenceController should calculate risk profile only for valid file extensions 2`] = `
Object {
"highRisk": 0,
"lowRisk": 74,
"moderateRisk": 26,
"veryHighRisk": 0,
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -71,26 +71,24 @@
</md-button>

<md-menu-content class="ai-drop-down summary">
<md-menu-item class="title">
<p ng-show="$ctrl._viewModel.analyzedProgrammingLanguage.length">
Risk Profile for {{ $ctrl._viewModel.analyzedProgrammingLanguage }} code (most frequent programming language in project)
</p>
<md-menu-item class="title" ng-show="$ctrl._viewModel.analyzedProgrammingLanguage.length">
<p>Risk Profile</p>
<p>Calculations are always based on mcc (cyclomatic complexity)</p>
</md-menu-item>

<md-menu-item class="suspicious-metrics-summary">
<ul ng-show="$ctrl._viewModel.riskProfile !== undefined">
<ul ng-show="$ctrl._viewModel.analyzedProgrammingLanguage.length">
<li>{{ $ctrl._viewModel.riskProfile.lowRisk }}% of overall code is in files with low complexity</li>
<li>{{ $ctrl._viewModel.riskProfile.moderateRisk }}% of overall code is in files with moderate complexity</li>
<li>{{ $ctrl._viewModel.riskProfile.highRisk }}% of overall code is in files with high complexity</li>
<li>{{ $ctrl._viewModel.riskProfile.veryHighRisk }}% of overall code is in files with extreme complexity</li>
</ul>
<span ng-show="$ctrl._viewModel.riskProfile === undefined">
<span ng-show="$ctrl._viewModel.analyzedProgrammingLanguage === undefined">
Could not calculate Risk Profile. Metrics rloc and mcc not available.
</span>
</md-menu-item>

<md-menu-item class="risk-profiles" ng-show="$ctrl._viewModel.riskProfile !== undefined">
<md-menu-item class="risk-profiles" ng-show="$ctrl._viewModel.analyzedProgrammingLanguage.length">
<div
ng-if="$ctrl._viewModel.riskProfile.lowRisk > 0"
title="Low Risk ({{ $ctrl._viewModel.riskProfile.lowRisk }}% of code)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe("ArtificialIntelligenceController", () => {
describe("calculations", () => {
it("should not calculate suspicious metrics when experimental features are disabled", function () {
artificialIntelligenceController["getMostFrequentLanguage"] = jest.fn()
artificialIntelligenceController["clearRiskProfile"] = jest.fn()
artificialIntelligenceController["setDefaultRiskProfile"] = jest.fn()
artificialIntelligenceController["calculateRiskProfile"] = jest.fn()
artificialIntelligenceController["calculateSuspiciousMetrics"] = jest.fn()
artificialIntelligenceController["fileState"] = FILE_STATES_JAVA[0]
Expand All @@ -82,14 +82,14 @@ describe("ArtificialIntelligenceController", () => {
artificialIntelligenceController.onExperimentalFeaturesEnabledChanged(false)

expect(artificialIntelligenceController["getMostFrequentLanguage"]).not.toHaveBeenCalled()
expect(artificialIntelligenceController["clearRiskProfile"]).not.toHaveBeenCalled()
expect(artificialIntelligenceController["setDefaultRiskProfile"]).not.toHaveBeenCalled()
expect(artificialIntelligenceController["calculateRiskProfile"]).not.toHaveBeenCalled()
expect(artificialIntelligenceController["calculateSuspiciousMetrics"]).not.toHaveBeenCalled()
})

it("should calculate suspicious metrics when experimental features are enabled", function () {
artificialIntelligenceController["getMostFrequentLanguage"] = jest.fn().mockReturnValue("java")
artificialIntelligenceController["clearRiskProfile"] = jest.fn()
artificialIntelligenceController["setDefaultRiskProfile"] = jest.fn()
artificialIntelligenceController["calculateRiskProfile"] = jest.fn()
artificialIntelligenceController["calculateSuspiciousMetrics"] = jest.fn()
artificialIntelligenceController["fileState"] = FILE_STATES_JAVA[0]
Expand All @@ -99,7 +99,7 @@ describe("ArtificialIntelligenceController", () => {

expect(artificialIntelligenceController["getMostFrequentLanguage"]).toHaveBeenCalled()
expect(artificialIntelligenceController["_viewModel"].analyzedProgrammingLanguage).toBe("java")
expect(artificialIntelligenceController["clearRiskProfile"]).toHaveBeenCalled()
expect(artificialIntelligenceController["setDefaultRiskProfile"]).toHaveBeenCalled()
expect(artificialIntelligenceController["calculateRiskProfile"]).toHaveBeenCalled()
expect(artificialIntelligenceController["calculateSuspiciousMetrics"]).toHaveBeenCalled()
})
Expand Down Expand Up @@ -177,27 +177,43 @@ describe("ArtificialIntelligenceController", () => {
expect(artificialIntelligenceController["getMostFrequentLanguage"]).not.toHaveBeenCalled()
})

it("should set default values for risk profile", () => {
artificialIntelligenceController["_viewModel"].analyzedProgrammingLanguage = "java"
artificialIntelligenceController["calculateRiskProfile"] = jest.fn()
artificialIntelligenceController["calculateSuspiciousMetrics"] = jest.fn()
artificialIntelligenceController["getMostFrequentLanguage"] = jest.fn()

expect(artificialIntelligenceController["rlocRisk"]).toEqual({})
expect(artificialIntelligenceController["_viewModel"].riskProfile).toEqual({})
expect(artificialIntelligenceController["_viewModel"].analyzedProgrammingLanguage).toBe("java")

storeService.dispatch(setExperimentalFeaturesEnabled(true))
artificialIntelligenceController.onFilesSelectionChanged(FILE_STATES_JAVA)

expect(artificialIntelligenceController["_viewModel"].analyzedProgrammingLanguage).toBeUndefined()
expect(artificialIntelligenceController["rlocRisk"]).toMatchSnapshot()
expect(artificialIntelligenceController["_viewModel"].riskProfile).toMatchSnapshot()
})

it("should not calculate risk profile and suspicious metrics on empty main programming language", () => {
artificialIntelligenceController["clearRiskProfile"] = jest.fn()
artificialIntelligenceController["setDefaultRiskProfile"] = jest.fn()
artificialIntelligenceController["calculateRiskProfile"] = jest.fn()
artificialIntelligenceController["createCustomConfigSuggestions"] = jest.fn()
storeService.dispatch(setExperimentalFeaturesEnabled(true))
artificialIntelligenceController.onFilesSelectionChanged(FILE_STATES)

expect(artificialIntelligenceController["clearRiskProfile"]).toHaveBeenCalled()
expect(artificialIntelligenceController["setDefaultRiskProfile"]).toHaveBeenCalled()
expect(artificialIntelligenceController["calculateRiskProfile"]).not.toHaveBeenCalled()
expect(artificialIntelligenceController["createCustomConfigSuggestions"]).not.toHaveBeenCalled()
})

it("should clear and calculate risk profile for Java map", () => {
artificialIntelligenceController["clearRiskProfile"] = jest.fn()
it("should calculate risk profile for Java map", () => {
artificialIntelligenceController["calculateSuspiciousMetrics"] = jest.fn()

storeService.dispatch(setExperimentalFeaturesEnabled(true))
artificialIntelligenceController.onFilesSelectionChanged(FILE_STATES_JAVA)

expect(artificialIntelligenceController["_viewModel"].analyzedProgrammingLanguage).toBe("java")
expect(artificialIntelligenceController["clearRiskProfile"]).toHaveBeenCalled()
expect(artificialIntelligenceController["_viewModel"].riskProfile).toMatchSnapshot()
expect(artificialIntelligenceController["calculateSuspiciousMetrics"]).toHaveBeenCalled()
})
Expand All @@ -219,14 +235,14 @@ describe("ArtificialIntelligenceController", () => {
})

it("should calculate suspicious metrics sorted by isOutlier", () => {
artificialIntelligenceController["clearRiskProfile"] = jest.fn()
artificialIntelligenceController["setDefaultRiskProfile"] = jest.fn()
artificialIntelligenceController["calculateRiskProfile"] = jest.fn()

storeService.dispatch(setExperimentalFeaturesEnabled(true))
artificialIntelligenceController.onFilesSelectionChanged(FILE_STATES_JAVA)

expect(artificialIntelligenceController["_viewModel"].analyzedProgrammingLanguage).toBe("java")
expect(artificialIntelligenceController["clearRiskProfile"]).toHaveBeenCalled()
expect(artificialIntelligenceController["setDefaultRiskProfile"]).toHaveBeenCalled()
expect(artificialIntelligenceController["calculateRiskProfile"]).toHaveBeenCalled()

expect(artificialIntelligenceController["_viewModel"].suspiciousMetricSuggestionLinks).toMatchSnapshot()
Expand Down Expand Up @@ -267,30 +283,41 @@ describe("ArtificialIntelligenceController", () => {
artificialIntelligenceController.onFilesSelectionChanged(FILE_STATES_MISSING_METRICS)

expect(artificialIntelligenceController["_viewModel"].analyzedProgrammingLanguage).toBe("java")
expect(artificialIntelligenceController["_viewModel"].riskProfile).toBeUndefined()
expect(artificialIntelligenceController["_viewModel"].riskProfile).toMatchSnapshot()
expect(artificialIntelligenceController["calculateSuspiciousMetrics"]).toHaveBeenCalled()
})

it("should calculate risk profile and suspicious metrics for maps with other programming languages", () => {
artificialIntelligenceController["calculateRiskProfile"] = jest.fn()
it("should calculate risk profile and suspicious metrics for all occurring programming languages in selected file", () => {
artificialIntelligenceController["calculateSuspiciousMetrics"] = jest.fn()

const FILE_STATES_OTHER = klona(FILE_STATES_JAVA)
for (const codeMapNode of FILE_STATES_OTHER[0].file.map.children) {
codeMapNode.children.map(childCodeMapNode => {
childCodeMapNode.name = childCodeMapNode.name.replace(/\.java/, ".other")
})
for (const codeMapNode of FILE_STATES_OTHER[0].file.map.children[0].children) {
codeMapNode.name = codeMapNode.name.replace(/\.java/, ".other")
}

storeService.dispatch(setExperimentalFeaturesEnabled(true))
artificialIntelligenceController.onFilesSelectionChanged(FILE_STATES_OTHER)

expect(artificialIntelligenceController["_viewModel"].analyzedProgrammingLanguage).toBe("other")
expect(artificialIntelligenceController["calculateRiskProfile"]).toHaveBeenCalled()
expect(artificialIntelligenceController["rlocRisk"]).toMatchSnapshot()
expect(artificialIntelligenceController["_viewModel"].riskProfile).toMatchSnapshot()
expect(artificialIntelligenceController["calculateSuspiciousMetrics"]).toHaveBeenCalled()
})
})

it("should calculate risk profile only for valid file extensions", () => {
const FILE_STATES_OTHER = klona(FILE_STATES_JAVA)
for (const codeMapNode of FILE_STATES_OTHER[0].file.map.children[1].children) {
codeMapNode.name = codeMapNode.name.replace(/\.java/, ".html")
}

storeService.dispatch(setExperimentalFeaturesEnabled(true))
artificialIntelligenceController.onFilesSelectionChanged(FILE_STATES_OTHER)

expect(artificialIntelligenceController["rlocRisk"]).toMatchSnapshot()
expect(artificialIntelligenceController["_viewModel"].riskProfile).toMatchSnapshot()
})

describe("on blacklist changed", () => {
it("should do nothing on blacklist change if no file is selected", () => {
storeService.dispatch(setFiles(FILE_STATES_UNSELECTED))
Expand Down