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

Thiago/feature/us004 #50

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/metrics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: |
git config --global user.email "${{secrets.USER_EMAIL}}"
git config --global user.name "${{secrets.USER_NAME}}"
git clone --single-branch --branch main "https://x-access-token:${{secrets.API_TOKEN_DOC}}@github.com/fga-eps-mds/2023.2-MeasureSoftGram-DOC" doc
git clone --single-branch --branch main "https://x-access-token:${{secrets.API_TOKEN_DOC}}@github.com/fga-eps-mds/2024.1-MeasureSoftGram-DOC" doc
mkdir -p doc/analytics-raw-data
cp -R analytics-raw-data/*.json doc/analytics-raw-data
cd doc/
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ jobs:
run: tox

- name: Scanner do SonarCloud
uses: SonarSource/sonarcloud-github-action@v1.8
uses: SonarSource/sonarcloud-github-action@v2.3.0
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

- name: Envia cobertura para o Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4.0.1
with:
token: ${{ secrets.CODECOV_TOKEN }}
directory: ./
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ __pycache__/
venv/
ENV/
.tox
msgram-cli-venv
.python-version

# Distribution / packaging
.Python
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 2023-2 MeasureSoftGram-CLI

# 2024-1 MeasureSoftGram-CLI
Command line project to MeasureSoftGram
## Badges

[![Code Smells](https://sonarcloud.io/api/project_badges/measure?project=fga-eps-mds_2023-1-MeasureSoftGram-CLI&metric=code_smells)](https://sonarcloud.io/summary/new_code?id=fga-eps-mds_2023-1-MeasureSoftGram-CLI)
Expand Down
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
sonar.projectKey=fga-eps-mds_2023-2-MeasureSoftGram-CLI
sonar.projectKey=fga-eps-mds_2024.1-MeasureSoftGram-CLI
sonar.organization=fga-eps-mds-1

sonar.python.version=3
Expand Down
12 changes: 6 additions & 6 deletions src/cli/aggregate_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ def process_github_metrics(folder_path, github_files, metrics):
github_file_content = read_msgram(github_file_path)

if not github_file_content:
print_error(f"> [red] Error to read github metrics in: {github_file_path}\n")
print_error(
f"> [red] Error to read github metrics in: {github_file_path}\n"
)
continue

github_key = next(iter(github_file_content.keys() - metrics["sonar"]), "")
Expand Down Expand Up @@ -155,9 +157,9 @@ def process_github_metrics(folder_path, github_files, metrics):
def find_common_part(sonar_filename, github_result):
sonar_filename_root, _ = os.path.splitext(sonar_filename)

sonar_parts = sonar_filename_root.split('-')
sonar_parts = sonar_filename_root.split("-")
if len(sonar_parts) >= 7:
sonar_key = '-'.join(sonar_parts[:7])
sonar_key = "-".join(sonar_parts[:7])

for github_filename, github_metrics in github_result:
github_filename_root, _ = os.path.splitext(github_filename)
Expand Down Expand Up @@ -187,9 +189,7 @@ def aggregate_metrics(folder_path, config: json):
config_has_github = should_process_github_metrics(config)

if config_has_github:
github_result = process_github_metrics(
folder_path, github_files, metrics
)
github_result = process_github_metrics(folder_path, github_files, metrics)

if not github_result:
print_error("> [red]Error: Unexpected result from process_github_metrics")
Expand Down
1 change: 1 addition & 0 deletions src/cli/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from src.cli.commands.cmd_init import command_init
from src.cli.commands.cmd_extract import command_extract
from src.cli.commands.cmd_calculate import command_calculate
from src.cli.commands.cmd_norm_diff import command_norm_diff
23 changes: 15 additions & 8 deletions src/cli/commands/cmd_calculate.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,14 @@ def calculate_all(json_data, file_name, config):
"repository": [{"key": "repository", "value": repository}],
"version": [{"key": "version", "value": version}] if version else [],
"measures": data_measures["measures"] if data_measures else [],
"subcharacteristics": data_subcharacteristics["subcharacteristics"]
if data_subcharacteristics
else [],
"characteristics": data_characteristics["characteristics"]
if data_characteristics
else [],
"subcharacteristics": (
data_subcharacteristics["subcharacteristics"]
if data_subcharacteristics
else []
),
"characteristics": (
data_characteristics["characteristics"] if data_characteristics else []
),
"tsqmi": data_tsqmi["tsqmi"] if data_tsqmi else [],
}

Expand Down Expand Up @@ -186,12 +188,17 @@ def show_tree(data_calculated, pre_config):
for subchar_c in char_c["subcharacteristics"]:
subchar = get_obj_by_element(subcharacteristics, "key", subchar_c["key"])
if subchar:
sub_char_tree = Node(f"[blue]{subchar['key']} {subchar['value']}", parent=char_tree)
sub_char_tree = Node(
f"[blue]{subchar['key']} {subchar['value']}", parent=char_tree
)

for measure_c in subchar_c["measures"]:
measure = get_obj_by_element(measures, "key", measure_c["key"])
if measure:
Node(f"[yellow]{measure['key']} {measure['value']}", parent=sub_char_tree)
Node(
f"[yellow]{measure['key']} {measure['value']}",
parent=sub_char_tree,
)

for pre, fill, node in RenderTree(tsqmi_tree):
print(f"{pre}{node.name}")
Expand Down
93 changes: 93 additions & 0 deletions src/cli/commands/cmd_norm_diff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import logging
from core.transformations import norm_diff
from src.cli.jsonReader import open_json_file
from src.cli.utils import print_error, print_info, print_rule
from src.cli.exceptions import exceptions
import numpy as np

logger = logging.getLogger("msgram")


def read_planned_file(file_path, sort_key=None):
try:
json_data = open_json_file(file_path)
return sorted(json_data, key=lambda x: x[sort_key]) if sort_key else json_data
except exceptions.MeasureSoftGramCLIException as e:
print_error(f"[red]Error reading planned file in {file_path}: {e}\n")
print_rule()
exit(1)


def read_calculated_file(file_path):
try:
calculated_data = []
json = open_json_file(file_path)
for item in json:
characteristics = sorted(item["characteristics"], key=lambda x: x["key"])
repository = item["repository"][0]["value"]
version = item["version"][0]["value"]
calculated_data.append(
{
"repository": repository,
"version": version,
"characteristics": characteristics,
}
)

return calculated_data
except exceptions.MeasureSoftGramCLIException as e:
print_error(f"[red]Error reading calculated file in {file_path}: {e}\n")
print_rule()
exit(1)


def command_norm_diff(args):
try:
planned_path = args["planned_path"]
calculated_path = args["calculated_path"]
except KeyError as e:
logger.error(f"KeyError: args[{e}] - non-existent parameters")
print_error(f"KeyError: args[{e}] - non-existent parameters")
exit(1)

planned_data = read_planned_file(planned_path, sort_key="key")
calculated_data = read_calculated_file(calculated_path)

planned_vector, calculated_vector = extract_values(planned_data, calculated_data)
norm_diff_value = norm_diff(planned_vector, calculated_vector)

print_info("\n[#A9A9A9]Norm diff calculation performed successfully![/]")
print_info("[#A9A9A9]For more detailed informations use 'diff' command.[/]")
print(f"Norm Diff: {norm_diff_value}")
print_rule()


def extract_values(planned_data, calculated_data):
try:
planned = planned_data
calculated = []
for item in calculated_data:
for characteristic in item["characteristics"]:
calculated.append(
{"key": characteristic["key"], "value": characteristic["value"]}
)

planned_keys = {item["key"].strip() for item in planned}
calculated_keys = {item["key"].strip() for item in calculated}

if planned_keys != calculated_keys:
raise exceptions.MeasureSoftGramCLIException(
"Planned and calculated files have different characteristics"
)

planned_dict = {item["key"].strip(): item["value"] for item in planned}
calculated_dict = {item["key"].strip(): item["value"] for item in calculated}

planned_values = [planned_dict[key] for key in planned_keys]
calculated_values = [calculated_dict[key] for key in planned_keys]

return (np.array(planned_values), np.array(calculated_values))
except exceptions.MeasureSoftGramCLIException as e:
print_error(f"[red]Error extracting values: {e}\n")
print_rule()
exit(1)
25 changes: 25 additions & 0 deletions src/cli/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from src.cli.commands.cmd_extract import command_extract
from src.cli.commands.cmd_calculate import command_calculate
from src.cli.commands.cmd_list import command_list
from src.cli.commands.cmd_norm_diff import command_norm_diff

from src.config.settings import (
AVAILABLE_IMPORTS,
Expand Down Expand Up @@ -152,4 +153,28 @@ def create_parser():
)
parser_calculate.set_defaults(func=command_calculate) # function command calculate

# =====================================< COMMAND norm-diff >=====================================
parser_norm_diff = subparsers.add_parser(
"norm-diff",
help="Calculate the norm difference between the planned metrics and the developed.",
)

parser_norm_diff.add_argument(
"-p",
"--planned_path",
type=lambda p: Path(p).absolute(),
help="Path to the json with the planned metrics.",
)

parser_norm_diff.add_argument(
"-c",
"--calculated_path",
type=lambda p: Path(p).absolute(),
help="Path to the json with the calculated metrics.",
)

parser_norm_diff.set_defaults(
func=command_norm_diff
) # function command list config

return parser
52 changes: 52 additions & 0 deletions tests/unit/data/calculated.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[
{
"repository": [
{
"key": "repository",
"value": "Example Repository"
}
],
"version": [
{
"key": "version",
"value": "27-07-2024-21-40"
}
],
"measures": [
{
"key": "first_measure",
"value": 1.0
},
{
"key": "second_measure",
"value": 0.9996066627522133
}
],
"subcharacteristics": [
{
"key": "first_subcharacteristics",
"value": 0.8421061048464034
},
{
"key": "second_subcharacteristics",
"value": 0.6415437113263573
}
],
"characteristics": [
{
"key": "first_characteristics",
"value": 0.8421061048464034
},
{
"key": "second_characteristics",
"value": 0.6415437113263573
}
],
"tsqmi": [
{
"key": "tsqmi",
"value": 0.7485723162667646
}
]
}
]
14 changes: 14 additions & 0 deletions tests/unit/data/missmatch-planned.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"key": "first_characteristics",
"value": 0.95
},
{
"key": "second_characteristics ",
"value": 0.95
},
{
"key": "third_characteristics ",
"value": 0.95
}
]
10 changes: 10 additions & 0 deletions tests/unit/data/planned.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"key": "first_characteristics",
"value": 0.95
},
{
"key": "second_characteristics ",
"value": 0.95
}
]
Loading
Loading