Skip to content

Commit

Permalink
refactor: Diff endpoints & stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
vik378 committed Jul 31, 2024
1 parent 49c2453 commit e72ae50
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 29 deletions.
38 changes: 17 additions & 21 deletions capella_model_explorer/backend/explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
LOGGER = logging.getLogger(__name__)


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

Expand Down Expand Up @@ -86,7 +86,7 @@ def __post_init__(self):
self.templates_index = self.templates_loader.index_path(
self.templates_path
)
self.data = {}
self.diff = {}

@self.app.middleware("http")
async def update_last_interaction_time(request: Request, call_next):
Expand Down Expand Up @@ -147,7 +147,7 @@ def render_instance_page(self, template_text, base, object=None):
# render the template with the object
template = self.env.from_string(template_text)
rendered = template.render(
object=object, model=self.model, data=self.data
object=object, model=self.model, data=self.diff
)
return HTMLResponse(content=rendered, status_code=200)
except TemplateSyntaxError as e:
Expand Down Expand Up @@ -288,32 +288,28 @@ async def catch_all(request: Request, rest_of_path: str):
async def version():
return {"version": self.app.version}

@self.app.post("/api/data")
async def post_data(payload: DataPayload):
@self.app.post("/api/compare")
async def post_data(commit_range: CommitRange):
try:
self.data = {"head": payload.head, "prev": payload.prev}
return self.data
except Exception:
return {}
self.diff = model_diff.get_data(
self.model, commit_range.head, commit_range.prev
)
return {"success": True}
except Exception as e:
return {"success": False, "error": str(e)}

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

@self.app.get("/api/data")
@self.app.get("/api/diff")
async def retrieve_data():
if "metadata" not in self.data:
try:
self.data = model_diff.get_data(
self.model,
self.data["head"],
self.data["prev"],
)
return {"data": self.data}
except Exception:
return {"error": "Couldn't retrieve model comparison data"}
return self.data
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):
Expand Down
5 changes: 3 additions & 2 deletions capella_model_explorer/backend/model_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import copy
import datetime
import logging
import pathlib
import subprocess

import capellambse
Expand Down Expand Up @@ -52,7 +53,7 @@ def populate_commits(model: capellambse.MelodyModel):

def get_data(model: capellambse.MelodyModel, head: str, prev: str):
path, model_data = init_model(model)

path = pathlib.Path(path).resolve()
old_model = capellambse.MelodyModel(path=f"git+{path}", revision=prev)

metadata: types.Metadata = {
Expand Down Expand Up @@ -100,7 +101,7 @@ def _get_revision_info(
def get_commit_hashes(path: str):
commit_hashes = (
subprocess.check_output(
["git", "log", "-n", "7", "--format=%H"],
["git", "log", "-n", "20", "--format=%H"],
cwd=path,
encoding="utf-8",
)
Expand Down
17 changes: 12 additions & 5 deletions frontend/src/components/ModelDiff.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@ export const ModelDiff = () => {
setCompleteLoading(false);
setIsLoading(true);
try {
const url = new URL(API_BASE_URL + '/data');
const postDataResponse = await postData(url, {
const url = new URL(API_BASE_URL + '/compare');
const response = await postData(url, {
head: commitDetails[0].hash,
prev: prevSelection
});
const response = await fetch(API_BASE_URL + '/data');
//const response = await fetch(API_BASE_URL + '/data');
const data = await response.json();
if (data.error) {
throw new Error(data.error);
}
} catch (error) {
console.error('Error:', error);
} finally {
Expand Down Expand Up @@ -103,7 +106,7 @@ export const ModelDiff = () => {
setCompleteLoading(false);
}}></div>
<div
className="absolute left-1/2 top-1/2 z-20 w-1/3 -translate-x-1/2
className="absolute left-1/2 top-1/2 z-20 w-1/2 -translate-x-1/2
-translate-y-1/2 transform rounded bg-gray-100 p-4 shadow-lg
dark:bg-custom-dark-2 dark:text-gray-100"
onClick={(e) => e.stopPropagation()}>
Expand Down Expand Up @@ -151,9 +154,13 @@ export const ModelDiff = () => {
))}
</select>
{selectedDetails && (
<div className="text-left font-semibold">
<div className="text-left">
<p>Hash: {selectedDetails.hash}</p>
<p>Author: {selectedDetails.author}</p>
<div>
<p className='text-bold'>Description</p>
<p className='whitespace-pre-wrap'>{selectedDetails.description}</p>
</div>
<p>Description: {selectedDetails.description}</p>
<p>Date: {selectedDetails.date.substring(0, 10)}</p>
</div>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/TemplateDetails.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const TemplateDetails = ({ endpoint, onSingleInstance }) => {
useEffect(() => {
const fetchModelDiff = async () => {
try {
const response = await fetch(API_BASE_URL + '/data');
const response = await fetch(API_BASE_URL + '/diff');
const data = await response.json();
setModelDiff(data);
} catch (err) {
Expand Down

0 comments on commit e72ae50

Please sign in to comment.