Skip to content

Commit

Permalink
Merge pull request #59 from cloud-barista/feature/update-web-ui
Browse files Browse the repository at this point in the history
Feature/update web UI
  • Loading branch information
heedaeshin authored Sep 20, 2024
2 parents 465dc04 + 4f9da7c commit a1ac173
Show file tree
Hide file tree
Showing 55 changed files with 1,971 additions and 288 deletions.
1 change: 0 additions & 1 deletion internal/auth/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ func GetNRDMS(params *models.ProviderConfig) (*nrdbc.NRDBController, error) {
if err != nil {
return nil, err
}

NRDBC, err = nrdbc.New(ncpmgdb.New(ncpnrdb, params.DatabaseName))
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion models/basicTask.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Schedule struct {
}

type GenarateTask struct {
Task
Task `json:"inline"`
TargetPoint GenTaskTarget `json:"targetPoint"`
}

Expand Down
2 changes: 1 addition & 1 deletion models/genDataParams.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type GenDataParams struct {

type GenFileParams struct {
Directory string `json:"Directory,omitempty" swaggerignore:"true"`
DummyPath string `json:"dummyPath,omitempty" swaggerignore:"true"`
DummyPath string `json:"path,omitempty" swaggerignore:"true"`
FileFormatParams
FileSizeParams
}
Expand Down
14 changes: 12 additions & 2 deletions pkg/rdbms/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (d *MysqlDBMS) Exec(query string) error {
if err != nil {
log.Error().Err(err).Str("query", query).Msg("Failed to execute SQL query")
}
log.Info().Str("query", query).Msg("SQL query executed successfully")
log.Debug().Str("query", query).Msg("SQL query executed successfully")
return err
}

Expand Down Expand Up @@ -106,13 +106,16 @@ func (d *MysqlDBMS) DeleteDB(dbName string) error {
func (d *MysqlDBMS) ListDB(dst *[]string) error {
rows, err := d.db.Query("SHOW DATABASES")
if err != nil {
log.Error().Err(err).Msgf("SQL query executed failed %v", rows)
return err
}

defer rows.Close()

for rows.Next() {
var dbName string
if err := rows.Scan(&dbName); err != nil {
log.Error().Err(err).Msgf("SQL query executed failed %v", rows)
return err
}

Expand All @@ -127,6 +130,7 @@ func (d *MysqlDBMS) ListDB(dst *[]string) error {
func (d *MysqlDBMS) ListTable(dbName string, dst *[]string) error {
_, err := d.db.Exec(fmt.Sprintf("USE %s;", dbName))
if err != nil {
log.Error().Err(err).Msgf("SQL query executed failed")
return err
}

Expand All @@ -150,6 +154,7 @@ func (d *MysqlDBMS) ListTable(dbName string, dst *[]string) error {
func (d *MysqlDBMS) ShowCreateDBSql(dbName string, dbCreateSql *string) error {
err := d.db.QueryRow(fmt.Sprintf("SHOW CREATE DATABASE %s;", dbName)).Scan(&dbName, dbCreateSql)
if err != nil {
log.Error().Err(err).Msgf("SQL query executed failed")
return err
}

Expand All @@ -173,9 +178,11 @@ func (d *MysqlDBMS) ShowCreateDBSql(dbName string, dbCreateSql *string) error {
// Get table create sql
func (d *MysqlDBMS) ShowCreateTableSql(dbName, tableName string, tableCreateSql *string) error {
if err := d.Exec(fmt.Sprintf("USE %s;", dbName)); err != nil {
log.Error().Err(err).Msgf("SQL query executed failed")
return err
}
if err := d.db.QueryRow(fmt.Sprintf("SHOW CREATE TABLE %s;", tableName)).Scan(&tableName, tableCreateSql); err != nil {
log.Error().Err(err).Msgf("SQL query executed failed")
return err
}
*tableCreateSql = removeSequenceOption(*tableCreateSql)
Expand All @@ -188,6 +195,7 @@ func (d *MysqlDBMS) ShowCreateTableSql(dbName, tableName string, tableCreateSql
func (d *MysqlDBMS) GetInsert(dbName, tableName string, insertSql *[]string) error {
colRows, err := d.db.Query("SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?", dbName, tableName)
if err != nil {
log.Error().Err(err).Msgf("SQL query executed failed")
return err
}
defer colRows.Close()
Expand All @@ -196,6 +204,7 @@ func (d *MysqlDBMS) GetInsert(dbName, tableName string, insertSql *[]string) err
for colRows.Next() {
var columnName string
if err := colRows.Scan(&columnName); err != nil {
log.Error().Err(err).Msgf("SQL query executed failed")
return err
}
columns = append(columns, columnName)
Expand All @@ -210,6 +219,7 @@ func (d *MysqlDBMS) GetInsert(dbName, tableName string, insertSql *[]string) err
selectQuery := "SELECT " + strings.Join(escapedColumns, ", ") + " FROM " + tableName
selRows, err := d.db.Query(selectQuery)
if err != nil {
log.Error().Err(err).Msgf("SQL query executed failed")
return err
}
defer selRows.Close()
Expand All @@ -225,7 +235,7 @@ func (d *MysqlDBMS) GetInsert(dbName, tableName string, insertSql *[]string) err

err := selRows.Scan(valuePtrs...)
if err != nil {

log.Error().Err(err).Msgf("SQL query executed failed")
return err
}

Expand Down
2 changes: 1 addition & 1 deletion service/rdbc/rdbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func New(rdb RDBMS, opts ...Option) (*RDBController, error) {
func (rdb *RDBController) ListDB(dst *[]string) error {
err := rdb.client.ListDB(dst)
if err != nil {
log.Info().Msgf("RDB", *dst)
log.Error().Err(err).Msgf("RDB %v", *dst)
return err
}
return nil
Expand Down
199 changes: 175 additions & 24 deletions web/js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,56 @@ window.addEventListener('DOMContentLoaded', event => {
if (document.getElementById('backForm')) {
backUpFormSubmit();
}
if (document.getElementById('restoreForm')) {
RestoreFormSubmit();
}

});

function convertCheckboxParams(obj) {
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
if (obj[key] === "on") {
obj[key] = true;
} else if (obj[key] === "off") {
obj[key] = false;
} else if (typeof obj[key] === "object" && !Array.isArray(obj[key])) {
convertCheckboxParams(obj[key]);
}
}
}
return obj;
}

function formDataToObject(formData) {
const data = {};
formData.forEach((value, key) => {
const match = key.match(/(\w+)\[(\w+)\]/);
if (match) {
const objName = match[1];
const paramName = match[2];
if (!data[objName]) {
data[objName] = {};
}
data[objName][paramName] = value;
} else {
data[key] = value;
}
});
return data;
}

function getInputValue(id) {
const element = document.getElementById(id);
if (!element) {
// console.warn(`Element with id '${id}' not found.`);
return null;
}

const value = element.value.trim();
return value !== "" ? value : null;
}


function generateFormSubmit() {
const form = document.getElementById('genForm');
Expand All @@ -45,41 +92,52 @@ function generateFormSubmit() {
resultCollpase();

const payload = new FormData(form);
let jsonData = JSON.stringify(Object.fromEntries(payload));
console.log(jsonData);
let jsonData = Object.fromEntries(payload);
jsonData=convertCheckboxParams(jsonData)
jsonData.targetPoint = {
...jsonData
};
console.log(jsonData)
jsonData.targetPoint.provider = document.getElementById('provider').value;
const target = document.getElementById('genTarget').value;

const target= document.getElementById('genTarget').value;
if ( (jsonData.targetPoint.provider =="ncp") && (jsonData.targetPoint.endpoint =="") ) {
jsonData.targetPoint.endpoint ="https://kr.object.ncloudstorage.com"
}
const url = "/generate/" + target;

console.log(url);

let req;
if (target == "gcp" || target == "firestore") {
req = { method: 'POST', body: payload };
} else {
req = { method: 'POST', body: jsonData };
}

req = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(jsonData)
};

fetch(url, req)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(json => {
const resultText = document.getElementById('resultText');
resultText.value = json.Result;
console.log(json);
console.log("generate done.");
console.log("Generate done.");
})
.catch(reason => {
console.log(reason);
alert(reason);
console.error("Error during generate:", reason);
alert(reason.message || reason);
})
.finally(() => {
loadingButtonOff();
});

console.log("generate progressing...");

console.log("Generate progressing...");
});
}

Expand All @@ -92,19 +150,39 @@ function migrationFormSubmit() {
resultCollpase();

const payload = new FormData(form);
let jsonData= formDataToObject(payload)
console.log(jsonData)
const dest = document.getElementById('migDest').value;
const source = document.getElementById('migSource').value;
jsonData.targetPoint.provider = getInputValue('targetPoint[provider]');
jsonData.sourcePoint.provider = getInputValue('sourcePoint[provider]');


let url = "/migration/" + source;
if (source != dest) {
url = url + "/" + dest;
}

if ( (jsonData.targetPoint.provider =="ncp") && (jsonData.targetPoint.endpoint =="") ) {
jsonData.targetPoint.endpoint ="https://kr.object.ncloudstorage.com"
}
if ( (jsonData.sourcePoint.provider =="ncp") && (jsonData.sourcePoint.endpoint =="") ) {
jsonData.sourcePoint.endpoint ="https://kr.object.ncloudstorage.com"
}

console.log(url);

fetch(url, {
let req;

req = {
method: 'POST',
body: payload
})
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(jsonData)
};

fetch(url, req)
.then(response => {
return response.json();
})
Expand Down Expand Up @@ -135,16 +213,32 @@ function backUpFormSubmit() {
resultCollpase();

const payload = new FormData(form);
const dest = document.getElementById('backDest').value;
const source = document.getElementById('backSource').value;
let url = "/backup/" + source;

var service = document.getElementById('srcService').value;
let url = "/backup/" + service;
console.log(url);

fetch(url, {

let jsonData= formDataToObject(payload)
console.log(jsonData)


if ( (jsonData.targetPoint.provider =="ncp") && (jsonData.targetPoint.endpoint =="") ) {
jsonData.targetPoint.endpoint ="https://kr.object.ncloudstorage.com"
}
if ( (jsonData.sourcePoint.provider =="ncp") && (jsonData.sourcePoint.endpoint =="") ) {
jsonData.sourcePoint.endpoint ="https://kr.object.ncloudstorage.com"
}

let req= {
method: 'POST',
body: payload
})
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(jsonData)
};

fetch(url, req )
.then(response => {
return response.json();
})
Expand All @@ -166,6 +260,63 @@ function backUpFormSubmit() {
});
}

function RestoreFormSubmit() {
const form = document.getElementById('restoreForm');

form.addEventListener('submit', (e) => {
e.preventDefault();
loadingButtonOn();
resultCollpase();

const payload = new FormData(form);

var service = document.getElementById('srcService').value;
let url = "/restore/" + service;
console.log(url);


let jsonData= formDataToObject(payload)
console.log(jsonData)


if ( (jsonData.targetPoint.provider =="ncp") && (jsonData.targetPoint.endpoint =="") ) {
jsonData.targetPoint.endpoint ="https://kr.object.ncloudstorage.com"
}
if ( (jsonData.sourcePoint.provider =="ncp") && (jsonData.sourcePoint.endpoint =="") ) {
jsonData.sourcePoint.endpoint ="https://kr.object.ncloudstorage.com"
}

let req= {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(jsonData)
};

fetch(url, req )
.then(response => {
return response.json();
})
.then(json => {
const resultText = document.getElementById('resultText');
resultText.value = json.Result;
console.log(json);
console.log("restore done.");
})
.catch(reason => {
console.log(reason);
alert(reason);
})
.finally(() => {
loadingButtonOff();
});

console.log("restore progressing...");
});
}


function loadingButtonOn() {
let btn = document.getElementById('submitBtn');
btn.disabled = true;
Expand Down
Loading

0 comments on commit a1ac173

Please sign in to comment.