Skip to content
Snippets Groups Projects
Commit 738dc04c authored by Björn Modée's avatar Björn Modée
Browse files

Add metrics script and readme

parent 0e87d25c
No related branches found
No related tags found
No related merge requests found
Pipeline #44669 failed
# Code Metrics
This documents describes how to install and run the rust-code-analysis on a unix machine, some alteration needed for a Windows system.
## Installing
You will need to do the following things to install the client:
1. Install Rust on your system by visiting https://www.rust-lang.org/tools/install or typing following in the terminal
```bash
curl https://sh.rustup.rs -sSf | sh
```
2. Clone the rust-code-analysis repository from https://github.com/mozilla/rust-code-analysis
```bash
git clone https://github.com/mozilla/rust-code-analysis.git
```
3. Enter the rust-code-analysis folder
```bash
cargo build --workspace
cargo install rust-code-analysis-cli
```
## Using
For each function space, rust-code-analysis computes the list of metrics described above. At the end of this process, rust-code-analysis-cli dumps the result formatted in a certain way on the screen.
1. Create a folder for the the outputs, for this guide it will be called "metrics-client" and "metrics-server"
# Frontend
2. Run the analysis tool for the frontend
```bash
./rust-code-analysis-cli -m --pr -p /path/to/teknikattan-scoring-system/client/src -o /path/to/metrics-client -O json
```
# Backend
3. Run the analysis tool for the backend
```bash
./rust-code-analysis-cli -m --pr -p /path/to/teknikattan-scoring-system/server/app -o /path/to/metrics-server -O json
```
# Metrics
Now the tool has analyzed the projekt and has outputted json files which the script will extrac the metrics from.
Don't forget to change the path in the script.
```bash
python3 metrics-script.py
```
## rust-code-analysis documentation
To read more about the tool, see https://mozilla.github.io/rust-code-analysis/index.html
# Citation
```
@article{ARDITO2020100635,
title = {rust-code-analysis: A Rust library to analyze and extract maintainability information from source codes},
journal = {SoftwareX},
volume = {12},
pages = {100635},
year = {2020},
issn = {2352-7110},
doi = {https://doi.org/10.1016/j.softx.2020.100635},
url = {https://www.sciencedirect.com/science/article/pii/S2352711020303484},
author = {Luca Ardito and Luca Barbato and Marco Castelluccio and Riccardo Coppola and Calixte Denizet and Sylvestre Ledru and Michele Valsesia},
keywords = {Algorithm, Software metrics, Software maintainability, Software quality},
abstract = {The literature proposes many software metrics for evaluating the source code non-functional properties, such as its complexity and maintainability. The literature also proposes several tools to compute those properties on source codes developed with many different software languages. However, the Rust language emergence has not been paired by the community’s effort in developing parsers and tools able to compute metrics for the Rust source code. Also, metrics tools often fall short in providing immediate means of comparing maintainability metrics between different algorithms or coding languages. We hence introduce rust-code-analysis, a Rust library that allows the extraction of a set of eleven maintainability metrics for ten different languages, including Rust. rust-code-analysis, through the Abstract Syntax Tree (AST) of a source file, allows the inspection of the code structure, analyzing source code metrics at different levels of granularity, and finding code syntax errors before compiling time. The tool also offers a command-line interface that allows exporting the results in different formats. The possibility of analyzing source codes written in different programming languages enables simple and systematic comparisons between the metrics produced from different empirical and large-scale analysis sources.}
}
```
# Change these path to the output folder in your system:
client_path = "/Path/to/metrics-client"
server_path = "/Path/to/metrics-server"
import json
import os
mi_list = []
cyc_list = []
lloc_list = []
def get_metrics_from_file(filename):
with open(filename) as json_file:
data = json.load(json_file)
mi_list.append(data["metrics"]["mi"]["mi_visual_studio"])
cyc_list.append(data["metrics"]["cyclomatic"]["sum"])
lloc_list.append(data["metrics"]["loc"]["lloc"])
def main(filepath):
for root, dirs, files in os.walk(filepath):
for filename in files:
get_metrics_from_file(root + "/" + filename)
print("Maintanability Index:", sum(mi_list) / len(mi_list))
print()
print("Cyclomatic complexity:", sum(cyc_list) / len(cyc_list))
print()
print("Lines of code (lloc):", sum(lloc_list))
mi_list.clear()
cyc_list.clear()
lloc_list.clear()
print(" ")
print("========== Metrics for client ==========")
main(client_path)
print(" ")
print("========== Metrics for server ==========")
main(server_path)
print("===================================")
print(" ")
...@@ -66,7 +66,10 @@ class User(db.Model): ...@@ -66,7 +66,10 @@ class User(db.Model):
locked = db.Column(db.DateTime(timezone=True), nullable=True, default=None) locked = db.Column(db.DateTime(timezone=True), nullable=True, default=None)
role_id = db.Column(db.Integer, db.ForeignKey("role.id"), nullable=False) role_id = db.Column(db.Integer, db.ForeignKey("role.id"), nullable=False)
role_name = db.Column(db.Integer, db.ForeignKey("role.name"), nullable=False)
city_id = db.Column(db.Integer, db.ForeignKey("city.id"), nullable=False) city_id = db.Column(db.Integer, db.ForeignKey("city.id"), nullable=False)
city_name = db.Column(db.Integer, db.ForeignKey("city.name"), nullable=False)
media = db.relationship("Media", backref="upload_by") media = db.relationship("Media", backref="upload_by")
...@@ -74,7 +77,9 @@ class User(db.Model): ...@@ -74,7 +77,9 @@ class User(db.Model):
self._password = bcrypt.generate_password_hash(plaintext_password) self._password = bcrypt.generate_password_hash(plaintext_password)
self.email = email self.email = email
self.role_id = role_id self.role_id = role_id
self.role_name = role_name
self.city_id = city_id self.city_id = city_id
self.city_name = city_name
self.authenticated = False self.authenticated = False
self.name = name self.name = name
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment