Skip to content

Commit

Permalink
Merge pull request #95 from kuafuai/feat/optimize
Browse files Browse the repository at this point in the history
feat(app): support update app info
  • Loading branch information
booboosui authored Sep 1, 2023
2 parents 0c5964c + a3058c2 commit c0d815a
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 43 deletions.
13 changes: 10 additions & 3 deletions backend/app/controllers/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,25 @@ def add():
_ = getI18n("controllers")
name = request.json.get('app_name')
tenant_id = request.json.get('app_tenant_id')
app_id = request.json.get('app_id')
default_source_branch = request.json.get('app_default_source_branch')
default_target_branch = request.json.get('app_default_target_branch')
description = request.json.get('app_description')
services = request.json.get('service')
creater = session['username']

try:
app = Application.create(tenant_id, creater, name, description, default_source_branch, default_target_branch)
appID = app.app_id
if app_id:
app = Application.update_application(app_id, name=name, description=description, default_source_branch=default_source_branch, default_target_branch=default_target_branch)
ApplicationService.delete_service_by_app_id(app_id)
appID = app_id
else:
app = Application.create(tenant_id, creater, name, description, default_source_branch, default_target_branch)
appID = app.app_id

for service in services:
newService = ApplicationService.create_service(appID, service["service_name"], service["service_git_path"], service["service_workflow"], service["service_role"], service["service_language"], service["service_framework"], service["service_database"], service["service_api_type"], service["service_api_location"], service["service_container_name"], service["service_container_group"], service["service_region"], service["service_public_ip"], service["service_security_group"], service["service_cd_subnet"], service["service_struct_cache"])
if "service_name" in service:
newService = ApplicationService.create_service(appID, service["service_name"], service["service_git_path"], service["service_workflow"], service["service_role"], service["service_language"], service["service_framework"], service["service_database"], service["service_api_type"], service["service_api_location"], service["service_container_name"], service["service_container_group"], service["service_region"], service["service_public_ip"], service["service_security_group"], service["service_cd_subnet"], service["service_struct_cache"])

ApplicationServiceLib.create_libs(newService.service_id, service["service_libs_name"])

Expand Down
11 changes: 10 additions & 1 deletion backend/app/models/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,13 @@ def get_application_by_id(appID):
'default_target_branch': app.default_target_branch,
'service': ApplicationService.get_services_by_app_id(app.app_id)
}
return app_dict
return app_dict

def update_application(app_id, **kwargs):
app = Application.query.get(app_id)
if app:
for key, value in kwargs.items():
setattr(app, key, value)
db.session.commit()
return app
return None
41 changes: 25 additions & 16 deletions backend/app/models/application_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class ApplicationService(db.Model):
service_id = db.Column(db.Integer, primary_key=True)
app_id = db.Column(db.Integer, db.ForeignKey('application.app_id'), nullable=False)
name = db.Column(db.String(255), nullable=False)
status = db.Column(db.String(50))
git_path = db.Column(db.String(255))
git_workflow = db.Column(db.String(255))
role = db.Column(db.Text)
Expand All @@ -23,12 +24,16 @@ class ApplicationService(db.Model):
created_at = db.Column(db.TIMESTAMP, server_default=db.text('CURRENT_TIMESTAMP'))
updated_at = db.Column(db.TIMESTAMP, server_default=db.text('CURRENT_TIMESTAMP'))

STATUS_DELETE = "DELETED"
STATUS_OK = "OK"

def create_service(app_id, name, git_path, git_workflow, role, language, framework, database, api_type, api_location,
cd_container_name, cd_container_group, cd_region, cd_public_ip, cd_security_group, cd_subnet, struct_cache):
service = ApplicationService(
app_id=app_id,
name=name,
git_path=git_path,
status=ApplicationService.STATUS_OK,
git_workflow=git_workflow,
role=role,
language=language,
Expand Down Expand Up @@ -59,10 +64,6 @@ def get_service_by_id(cls, service_id):
@staticmethod
def get_service_by_name(appID, service_name):
services = ApplicationService.query.filter_by(name=service_name, app_id=appID).all()
print("########")
print(appID)
print(service_name)
print(services)

service_dict = {}
for service in services:
Expand All @@ -71,6 +72,7 @@ def get_service_by_name(appID, service_name):
'app_id': service.app_id,
'name': service.name,
'git_path': service.git_path,
'status': service.status,
'git_workflow': service.git_workflow,
'role': service.role,
'language': service.language,
Expand All @@ -90,17 +92,14 @@ def get_service_by_name(appID, service_name):

return service_dict

def update_service(self, name, git_path, git_workflow, role, language, framework, database, api_type, api_location):
self.name = name
self.git_path = git_path
self.git_workflow = git_workflow
self.role = role
self.language = language
self.framework = framework
self.database = database
self.api_type = api_type
self.api_location = api_location
db.session.commit()
def update_service(self, service_id, **kwargs):
service = self.query.get(service_id)
if service:
for key, value in kwargs.items():
setattr(service, key, value)
db.session.commit()
return service
return None

@classmethod
def delete_service(cls, service_id):
Expand All @@ -110,10 +109,19 @@ def delete_service(cls, service_id):
db.session.commit()
return True
return False

@classmethod
def delete_service_by_app_id(cls, app_id):
services = cls.query.filter_by(app_id=app_id).all()
for service in services:
re = cls.update_service(cls, service.service_id, status=cls.STATUS_DELETE)
if not re:
return False
return True

@classmethod
def get_services_by_app_id(cls, app_id):
services = cls.query.filter_by(app_id=app_id).all()
services = cls.query.filter_by(app_id=app_id, status=cls.STATUS_OK).all()
services_list = []

for service in services:
Expand All @@ -122,6 +130,7 @@ def get_services_by_app_id(cls, app_id):
'app_id': service.app_id,
'name': service.name,
'git_path': service.git_path,
'status': service.status,
'git_workflow': service.git_workflow,
'role': service.role,
'language': service.language,
Expand Down
Binary file modified db/database.db
Binary file not shown.
Binary file modified docs/files/WeChat-zhushou.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 40 additions & 23 deletions frontend/static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ $(document).ready(function () {
});

$("#add-application").click(function () {
cleanUp()
$('#app-edit').modal('show');
});

$("#app-edit-save").click(function () {
var requestData = {
'app_id': $("#app_id").val(),
'app_name': $("#app_name").val(),
'app_description': $("#app_description").val(),
'app_default_source_branch': $("#app_default_source_branch").val(),
Expand Down Expand Up @@ -51,7 +53,7 @@ $(document).ready(function () {
});

$("#app-edit-cancel").click(function () {
$('#app-edit').modal('hide');
location.reload();
});

$("#add-service").click(function(){
Expand Down Expand Up @@ -141,8 +143,22 @@ $(document).ready(function () {
});
});

function removeSubservice(idx){
$("#subservice_"+idx).remove()
}

function cleanUp() {
$('.subservice').remove();
$("#app_id").val('')
$("#app_default_source_branch").val('')
$("#app_default_target_branch").val('')
$("#app_description").val('')
$("#app_name").val('')
}

function showApp(appID) {
$('#app-edit').modal('show');
cleanUp()

var requestData = { 'app_id': appID }

Expand All @@ -158,90 +174,91 @@ function showApp(appID) {
subserviceLen = subservice.length
serviceID = subserviceLen+1

data.service.forEach(function (service, element_index, element_array) {
data.service.forEach(function (service, ele_idx, element_array) {
idx = ele_idx+1
serviceID = service.service_id
libsStr = ""
service.libs.forEach(function (lib, element_index, element_array) {
libsStr += lib.sys_lib_name+","
});
libsStr = libsStr.replace(/,$/, '');
str = `<div class="ui segment subservice" style="display: none;" id="subservice_`+serviceID+`">
<h2 class="ui floated header">`+globalFrontendText["app_sub_service"]+` `+serviceID+`</h2>
str = `<div class="ui segment subservice" style="display: none;" id="subservice_`+idx+`">
<h2 class="ui floated header">`+globalFrontendText["app_sub_service"]+` `+idx+` <i class="red times circle outline icon" onClick="removeSubservice(`+idx+`)"></i></h2>
<div class="field">
<label>`+globalFrontendText["git_path"]+`</label>
<div class="ui action input">
<input type="text" id="service_git_path_`+serviceID+`" value="`+service.git_path+`">
<button class="ui button" onClick="analyzeService(`+serviceID+`)" value="1"><span id="ai_analyze_service_icon"><i class="orange reddit square icon"></i></span>`+globalFrontendText["ai_code_analysis"]+`</button>
<input type="text" id="service_git_path_`+idx+`" value="`+service.git_path+`">
<button class="ui button" onClick="analyzeService(`+idx+`)" value="1"><span id="ai_analyze_service_icon"><i class="orange reddit square icon"></i></span>`+globalFrontendText["ai_code_analysis"]+`</button>
</div>
</div>
<div class="field">
<label>`+globalFrontendText["service_name"]+`</label>
<input type="text" id="service_name_`+serviceID+`" value="`+service.name+`">
<input type="text" id="service_name_`+idx+`" value="`+service.name+`">
</div>
<div class="field">
<label>`+globalFrontendText["service_role"]+`</label>
<textarea id="service_role_`+serviceID+`" rows="4">`+service.role+`</textarea>
<textarea id="service_role_`+idx+`" rows="4">`+service.role+`</textarea>
</div>
<div class="field">
<label>`+globalFrontendText["service_language"]+`</label>
<input type="text" id="service_language_`+serviceID+`" value="`+service.language+`">
<input type="text" id="service_language_`+idx+`" value="`+service.language+`">
</div>
<div class="field">
<label>`+globalFrontendText["service_framework"]+`</label>
<input type="text" id="service_framework_`+serviceID+`" value="`+service.framework+`">
<input type="text" id="service_framework_`+idx+`" value="`+service.framework+`">
</div>
<div class="field">
<label>`+globalFrontendText["service_libs"]+`</label>
<input type="text" id="service_libs_name_`+serviceID+`" value="`+libsStr+`">
<input type="text" id="service_libs_name_`+idx+`" value="`+libsStr+`">
</div>
<div class="field">
<label>`+globalFrontendText["service_api_type"]+`</label>
<input type="text" id="service_api_type_`+serviceID+`" value="`+service.api_type+`">
<input type="text" id="service_api_type_`+idx+`" value="`+service.api_type+`">
</div>
<div class="field">
<label>`+globalFrontendText["service_api_path"]+`</label>
<input type="text" id="service_api_location_`+serviceID+`" value="`+service.api_location+`">
<input type="text" id="service_api_location_`+idx+`" value="`+service.api_location+`">
</div>
<div class="field">
<label>`+globalFrontendText["service_database"]+`</label>
<input type="text" id="service_database_`+serviceID+`" value="`+service.database+`">
<input type="text" id="service_database_`+idx+`" value="`+service.database+`">
</div>
<div class="field">
<label>`+globalFrontendText["service_code_struct"]+`</label>
<textarea id="service_struct_cache_`+serviceID+`" rows="4">`+service.struct_cache+`</textarea>
<textarea id="service_struct_cache_`+idx+`" rows="4">`+service.struct_cache+`</textarea>
</div>
<div class="field">
<label>CI - GitHub workflow</label>
<input type="text" id="service_workflow_`+serviceID+`" value="`+service.git_workflow+`">
<input type="text" id="service_workflow_`+idx+`" value="`+service.git_workflow+`">
</div>
<div class="field">
<label>CD - GROUP</label>
<input type="text" id="service_container_group_`+serviceID+`" value="`+service.cd_container_group+`">
<input type="text" id="service_container_group_`+idx+`" value="`+service.cd_container_group+`">
</div>
<div class="field">
<label>CD - NAME</label>
<input type="text" id="service_container_name_`+serviceID+`" value="`+service.cd_container_name+`">
<input type="text" id="service_container_name_`+idx+`" value="`+service.cd_container_name+`">
</div>
<div class="field">
<label>CD - REGION</label>
<input type="text" id="service_region_`+serviceID+`" value="`+service.cd_region+`">
<input type="text" id="service_region_`+idx+`" value="`+service.cd_region+`">
</div>
<div class="field">
<label>CD - PUBLIC IP</label>
<input type="text" id="service_public_ip_`+serviceID+`" value="`+service.cd_public_ip+`">
<input type="text" id="service_public_ip_`+idx+`" value="`+service.cd_public_ip+`">
</div>
<div class="field">
<label>CD - SECURITY GROUP</label>
<input type="text" id="service_security_group_`+serviceID+`" value="`+service.cd_security_group+`">
<input type="text" id="service_security_group_`+idx+`" value="`+service.cd_security_group+`">
</div>
<div class="field">
<label>CD - SUBNET/SWITCH</label>
<input type="text" id="service_cd_subnet_`+serviceID+`" value="`+service.cd_subnet+`">
<input type="text" id="service_cd_subnet_`+idx+`" value="`+service.cd_subnet+`">
</div>
</div>
</div>`
$("#add-service").after(str)
$("#subservice_"+serviceID).slideDown("500")
$("#subservice_"+idx).slideDown("500")
setTimeout(function () {
$('#app-edit').modal('refresh');
}, 550);
Expand Down

0 comments on commit c0d815a

Please sign in to comment.