Skip to content

Commit

Permalink
refactor: Apply pull request comments of #1866
Browse files Browse the repository at this point in the history
  • Loading branch information
MoritzWeber0 committed Oct 4, 2024
1 parent b2b0f7a commit 9524c73
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,39 @@
"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.orm import Session

# revision identifiers, used by Alembic.
revision = "3818a5009130"
down_revision = "7cf3357ddd7b"
branch_labels = None
depends_on = None

t_tool_versions = sa.Table(
"versions",
sa.MetaData(),
sa.Column("id", sa.Integer()),
sa.Column("name", sa.String()),
)

t_t4c_instances_old = sa.Table(
"t4c_instances",
sa.MetaData(),
sa.Column("id", sa.Integer()),
sa.Column("usage_api", sa.String()),
sa.Column("license", sa.String()),
)


t_t4c_instances_new = sa.Table(
"t4c_instances",
sa.MetaData(),
sa.Column("id", sa.Integer()),
sa.Column("license_server_id", sa.Integer()),
)


def upgrade():
op.create_table(
t_license_servers = op.create_table(
"t4c_license_servers",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("name", sa.String(), nullable=False),
Expand All @@ -47,44 +69,38 @@ def upgrade():
["id"],
)

# Fetch existing t4c_instances and group by usage_api and license
bind = op.get_bind()
session = Session(bind=bind)
t4c_instances = session.execute(
sa.text("SELECT id, usage_api, license FROM t4c_instances")
).fetchall()
t4c_instances = (
bind.execute(sa.select(t_t4c_instances_old)).mappings().all()
)

grouped_instances = {}
grouped_instances: dict[tuple[str, str], list[int]] = {}
for instance in t4c_instances:
key = (instance.usage_api, instance.license)
if key not in grouped_instances:
grouped_instances[key] = []
grouped_instances[key].append(instance.id)

# Create new license_server for each group and associate with instances
for (usage_api, license_key), instance_ids in grouped_instances.items():
license_server_id = session.execute(
sa.text(
"INSERT INTO t4c_license_servers (name, usage_api, license_key) VALUES (:name, :usage_api, :license_key) RETURNING id"
),
{
"name": usage_api,
"usage_api": usage_api,
"license_key": license_key,
},
for idx, ((usage_api, license_key), instance_ids) in enumerate(
grouped_instances.items()
):
license_server_id = bind.execute(
t_license_servers.insert()
.values(
name=f"License server {idx+1}",
usage_api=usage_api,
license_key=license_key,
)
.returning(t_license_servers.c.id)
).scalar()

session.execute(
sa.text(
"UPDATE t4c_instances SET license_server_id = :license_server_id WHERE id = ANY(:instance_ids)"
),
{
"license_server_id": license_server_id,
"instance_ids": instance_ids,
},
)

session.commit()
for instance_id in instance_ids:
bind.execute(
sa.update(t_t4c_instances_new)
.where(t_t4c_instances_new.c.id == instance_id)
.values(license_server_id=license_server_id)
)

op.alter_column("t4c_instances", "license_server_id", nullable=False)
op.drop_column("t4c_instances", "license")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,8 @@ class T4CInstance(T4CInstanceComplete):
id: int
version: tools_models.ToolVersion
is_archived: bool


class SimpleT4CInstace(core_pydantic.BaseModel):
id: int
name: str
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from capellacollab.core import database
from capellacollab.core import exceptions as core_exceptions
from capellacollab.core import models as core_models
from capellacollab.core import pydantic as core_pydantic
from capellacollab.settings.modelsources.t4c.instance import (
models as instance_models,
Expand Down Expand Up @@ -58,7 +59,8 @@ class T4CLicenseServer(T4CLicenseServerBase):
id: int
license_server_version: str | None = None
usage: interface.T4CLicenseServerUsage | None = None
instances: list[instance_models.T4CInstance] = []
warnings: list[core_models.Message] = []
instances: list[instance_models.SimpleT4CInstace] = []

@pydantic.model_validator(mode="after")
def add_from_api(self) -> t.Any:
Expand All @@ -67,7 +69,11 @@ def add_from_api(self) -> t.Any:
)
try:
self.usage = interface.get_t4c_license_server_usage(self.usage_api)
except core_exceptions.BaseError:
pass
except core_exceptions.BaseError as exc:
self.warnings.append(
core_models.Message(
err_code=exc.err_code, title=exc.title, reason=exc.reason
)
)

return self
30 changes: 22 additions & 8 deletions frontend/src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,23 +449,37 @@ export const routes: Routes = [
component: EditT4CInstanceComponent,
},
{
path: 'instance/:instance',
path: 'instance',
data: {
breadcrumb: (data: Data) => data.t4cInstance?.name,
breadcrumb: 'Instances',
},
component: EditT4CInstanceComponent,
children: [
{
path: ':instance',
data: {
breadcrumb: (data: Data) => data.t4cInstance?.name,
},
component: EditT4CInstanceComponent,
},
],
},
{
path: 'create-license-server',
data: { breadcrumb: 'New License Server' },
component: EditT4cLicenseServerComponent,
},
{
path: 'license-server/:licenseServer',
data: {
breadcrumb: (data: Data) => data.licenseServer?.name,
},
component: EditT4cLicenseServerComponent,
path: 'license-server',
data: { breadcrumb: 'License Servers' },
children: [
{
path: ':licenseServer',
data: {
breadcrumb: (data: Data) => data.licenseServer?.name,
},
component: EditT4cLicenseServerComponent,
},
],
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
-->

<div class="flex flex-wrap gap-5">
<div class="collab-card h-fit">
<div class="collab-card h-fit w-full sm:w-[432px]">
<div class="mb-2">
<h2 class="text-xl font-medium">
{{ existing ? "Edit License Server" : "Create License Server" }}
Expand All @@ -14,9 +14,9 @@ <h2 class="text-xl font-medium">
<form
[formGroup]="form"
(ngSubmit)="submit()"
class="flex flex-col flex-wrap gap-2"
class="flex flex-col flex-wrap"
>
<fieldset class="flex flex-wrap items-start gap-2">
<fieldset>
<mat-form-field appearance="fill">
<mat-label>Name</mat-label>
<input matInput formControlName="name" />
Expand All @@ -25,7 +25,7 @@ <h2 class="text-xl font-medium">
}
</mat-form-field>
</fieldset>
<fieldset class="flex flex-wrap items-start gap-2">
<fieldset>
<mat-form-field appearance="fill">
<mat-label>License Server API</mat-label>
<input matInput formControlName="usage_api" />
Expand All @@ -36,7 +36,7 @@ <h2 class="text-xl font-medium">
}
</mat-form-field>
</fieldset>
<fieldset class="flex flex-wrap items-start gap-2">
<fieldset>
<mat-form-field appearance="fill">
<mat-label>License Key</mat-label>
<input matInput formControlName="license_key" />
Expand All @@ -56,12 +56,7 @@ <h2 class="text-xl font-medium">
Create
</button>
} @else if (editing) {
<button
mat-flat-button
type="button"
color="primary"
(click)="cancelEditing()"
>
<button mat-stroked-button type="button" (click)="cancelEditing()">
Cancel
</button>
<div
Expand Down Expand Up @@ -97,52 +92,50 @@ <h2 class="text-xl font-medium">
</div>

@if (existing) {
<div class="collab-card h-fit">
<div class="collab-card w-full sm:w-fit">
<h2 class="mb-2 text-xl font-medium">License Server Status</h2>

@if (
t4cLicenseServerWrapperService.licenseServer$ | async;
as licenseServer
) {
@if (licenseServer.license_server_version) {
<div>
<mat-icon class="mat-icon-position top">tag</mat-icon>
<b>License Server Version</b>:
{{ licenseServer.license_server_version }}
<div>
<div class="flex items-center gap-2">
<mat-icon>tag</mat-icon>
<span>
<b>License Server Version:</b>
{{ licenseServer.license_server_version || "Unknown" }}
</span>
</div>
} @else {
<div>
<mat-icon class="mat-icon-position top">error</mat-icon>
<b> License Server is Unreachable</b>
</div>
}

@if (licenseServer.usage) {
<div>
<mat-icon class="mat-icon-position top">123</mat-icon>
<b> Available licenses</b>: {{ licenseServer.usage.free }} /
{{ licenseServer.usage.total }}
</div>
} @else {
<div>
<mat-icon class="mat-icon-position top">123</mat-icon>
<b>Available licenses</b>: Unknown
<div class="flex items-center gap-2">
<mat-icon>123</mat-icon>
<div>
<b>Available licenses</b>:
@if (licenseServer.usage) {
{{ licenseServer.usage.free }} /
{{ licenseServer.usage.total }}
} @else {
Unknown
}
</div>
</div>
}

@if (licenseServer.instances.length > 0) {
<div>
<mat-icon class="mat-icon-position top">link</mat-icon>
<b>
Used by {{ licenseServer.instances.length }} T4C
<div class="flex items-start gap-2">
<mat-icon>link</mat-icon>
<div>
<b>Used by</b> {{ licenseServer.instances.length }} T4C

{{
licenseServer.instances.length > 1 ? "instances" : "instance"
}}</b
>
<ul class="list-inside list-disc">
@for (instance of licenseServer.instances; track instance.name) {
<li>
licenseServer.instances.length === 1 ? "Instance" : "Instances"
}}
@if (licenseServer.instances.length > 0) {
@for (
instance of licenseServer.instances;
track instance.name
) {
<a
class="flex w-fit items-center gap-2 text-primary"
[routerLink]="[
'/settings',
'modelsources',
Expand All @@ -151,13 +144,14 @@ <h2 class="mb-2 text-xl font-medium">License Server Status</h2>
instance.id,
]"
>
{{ instance.name }}
<span>{{ instance.name }}</span>
<mat-icon [inline]="true">open_in_browser</mat-icon>
</a>
</li>
}
}
</ul>
</div>
</div>
}
</div>
}
</div>
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,10 @@ export class EditT4cLicenseServerComponent implements OnInit, OnDestroy {
}
const dialogRef = this.dialog.open(ConfirmationDialogComponent, {
data: {
title: 'Delete TeamForCapella License Server',
text: 'TODO Write this text',
title: 'Disconnect TeamForCapella License Server',
text:
'Do you want to disconnect the TeamForCapella license server? ' +
'This will not delete the license server itself.',
},
});

Expand Down
Loading

0 comments on commit 9524c73

Please sign in to comment.