Skip to content

Commit

Permalink
Merge pull request #26 from DSD-DBS/feat-display-git-diff
Browse files Browse the repository at this point in the history
New Model Comparison Features
freshavocado7 authored Sep 18, 2024
2 parents c9e4157 + 2b157df commit bc0601a
Showing 22 changed files with 1,543 additions and 135 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/build-test-publish.yml
Original file line number Diff line number Diff line change
@@ -18,12 +18,11 @@ jobs:
matrix:
os: [ubuntu-latest]
python_version:
- "3.10"
- "3.11"
- "3.12"
include:
- os: windows-latest
python_version: "3.10"
python_version: "3.11"
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{matrix.python_version}}
@@ -56,7 +55,7 @@ jobs:
- name: Setup Python
uses: actions/setup-python@v2
with:
python-version: "3.10"
python-version: "3.11"
- name: Install dependencies
run: |-
python -m pip install -U pip
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: "3.10"
python-version: "3.11"
- name: Upgrade pip
run: |-
python -m pip install -U pip
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@ repos:
hooks:
- id: mypy
additional_dependencies:
- capellambse==0.5.70
- capellambse==0.5.72
- types-pyyaml==6.0.11
- repo: https://github.com/pylint-dev/pylint
rev: v3.2.7
7 changes: 6 additions & 1 deletion capella_model_explorer/backend/__main__.py
Original file line number Diff line number Diff line change
@@ -25,7 +25,12 @@
default=PATH_TO_TEMPLATES,
)
def run(model: capellambse.MelodyModel, templates: Path):
backend = explorer.CapellaModelExplorerBackend(Path(templates), model)

backend = explorer.CapellaModelExplorerBackend(
Path(templates),
model,
)

uvicorn.run(backend.app, host=HOST, port=int(PORT))


75 changes: 73 additions & 2 deletions capella_model_explorer/backend/explorer.py
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@
import markupsafe
import prometheus_client
import yaml
from fastapi import APIRouter, FastAPI, Request
from fastapi import APIRouter, FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
@@ -27,7 +27,9 @@
TemplateSyntaxError,
is_undefined,
)
from pydantic import BaseModel

from capella_model_explorer.backend import model_diff
from capella_model_explorer.backend import templates as tl

from . import __version__
@@ -39,6 +41,15 @@
LOGGER = logging.getLogger(__name__)


class CommitRange(BaseModel):
head: str
prev: str


class ObjectDiffID(BaseModel):
uuid: str


@dataclasses.dataclass
class CapellaModelExplorerBackend:
app: FastAPI = dataclasses.field(init=False)
@@ -80,6 +91,8 @@ def __post_init__(self):
self.templates_index = self.templates_loader.index_path(
self.templates_path
)
self.diff = {}
self.object_diff = {}

@self.app.middleware("http")
async def update_last_interaction_time(request: Request, call_next):
@@ -139,7 +152,12 @@ def render_instance_page(self, template_text, base, object=None):
try:
# render the template with the object
template = self.env.from_string(template_text)
rendered = template.render(object=object, model=self.model)
rendered = template.render(
object=object,
model=self.model,
diff_data=self.diff,
object_diff=self.object_diff,
)
return HTMLResponse(content=rendered, status_code=200)
except TemplateSyntaxError as e:
error_message = markupsafe.Markup(
@@ -276,6 +294,38 @@ async def catch_all(request: Request, rest_of_path: str):
async def version():
return {"version": self.app.version}

@self.app.post("/api/compare")
async def post_compare(commit_range: CommitRange):
try:
self.diff = model_diff.get_diff_data(
self.model, commit_range.head, commit_range.prev
)
self.diff["lookup"] = create_diff_lookup(self.diff["objects"])
return {"success": True}
except Exception as e:
return {"success": False, "error": str(e)}

@self.app.post("/api/object-diff")
async def post_object_diff(object_id: ObjectDiffID):
if object_id.uuid not in self.diff["lookup"]:
raise HTTPException(status_code=404, detail="Object not found")

self.object_diff = self.diff["lookup"][object_id.uuid]
return {"success": True}

@self.app.get("/api/commits")
async def get_commits():
result = model_diff.populate_commits(self.model)
return result

@self.app.get("/api/diff")
async def get_diff():
if self.diff:
return self.diff
return {
"error": "No data available. Please compare two commits first."
}


def index_template(template, templates, templates_grouped, filename=None):
idx = filename if filename else template["idx"]
@@ -306,3 +356,24 @@ def index_templates(
template, templates, templates_grouped, filename=idx
)
return templates_grouped, templates


def create_diff_lookup(data, lookup=None):
if lookup is None:
lookup = {}
try:
if isinstance(data, dict):
for _, obj in data.items():
if "uuid" in obj:
lookup[obj["uuid"]] = {
"uuid": obj["uuid"],
"display_name": obj["display_name"],
"change": obj["change"],
"attributes": obj["attributes"],
}
if "children" in obj:
if obj["children"]:
create_diff_lookup(obj["children"], lookup)
except Exception:
pass
return lookup
Loading

0 comments on commit bc0601a

Please sign in to comment.