Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data-validator function #1318

Draft
wants to merge 10 commits into
base: develop
Choose a base branch
from
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,7 @@ target_query.sql

# Commit msg
commits.txt
test.py
test.py

# Backend
packages/backend/node_modules
7 changes: 7 additions & 0 deletions packages/backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Data-valition backend

1. <strong>Remember</strong> to change the ip address and port number in [app.ts](../backend/src/app.ts) & [index.ts](../backend/src/index.ts) in the production environment. <br><br>
2. <strong>Install data-validation:</strong> Run command 'pip install .' under the [data-validator](../../) folder. <br><br>
3. <strong>Install dependencies: </strong> Run command 'npm install' under the [backend](../backend/) folder.<br><br>
4. <strong>Run the server: </strong> Run command 'npx ts-node src/index.ts'

20 changes: 20 additions & 0 deletions packages/backend/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// src/app.ts
import express from 'express';
import CreateRouter from './routers/CreateRoute';
import cors from 'cors';
import ValidationRouter from './routers/ValidationRoute';

const app = express();

app.use(express.json());
app.use(cors({
// TODO:
origin: "http://localhost:8000",
methods: 'GET,POST,PUT,DELETE,OPTIONS',
allowedHeaders: 'Content-Type,Authorization'
}))

app.use('/api', CreateRouter);
app.use('/api/validation', ValidationRouter);

export default app;
47 changes: 47 additions & 0 deletions packages/backend/src/controller/CreateConnectionController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// src/controllers/ScriptController.ts
import { Request, Response } from 'express';
import CreateConnectionService from '../service/CreateConnectionService';


class CreateConnectionController {
public static async createConnection(req: Request, res: Response): Promise<void> {
try {
const { connName, dbType, host, port, user, password, database } = req.body;
let schema = null;

if (dbType == 'Snowflake') {
const { requestSchema } = req.body;
schema = requestSchema;
}

const result = await CreateConnectionService.createConnection(connName, dbType, host, port, user, password, database, schema);

if (result.error != null) {
const resultFinal = {
'validationStatus': "ERROR",
'information': result
}

res.status(200).json(resultFinal);
} else {
const resultFinal = {
'validationStatus': "OK",
'information': result
}
res.status(200).json(resultFinal);
}

} catch (error: any) {
res.status(500).json({ error: 'Error running validation', details: error.message });
}
};

public static async testConnection(req: Request, res: Response): Promise<void> {
console.log("Connect successfully");
res.status(200).json({ message: 'Connection test successful' });

}
}


export default CreateConnectionController;
68 changes: 68 additions & 0 deletions packages/backend/src/controller/ValidationController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// src/controllers/ScriptController.ts
import { Request, Response } from 'express';
import ValidationService from '../service/ValidationService';


class ValidationController {
public static async validationColumn(req: Request, res: Response): Promise<void> {

console.log("Validating column...")

try {
const { host, user, password, database, source_conn, target_conn, resultType, schema, content } = req.body;
const result = await ValidationService.validationColumn(host, user, password, database, source_conn, target_conn, resultType, schema, content);
const jsonResult = JSON.parse(result);

if (jsonResult.error != null) {
const resultFinal = {
'validationStatus': "ERROR",
'information': jsonResult
}

res.status(200).json(resultFinal);
} else {
const resultFinal = {
'validationStatus': "OK",
'information': jsonResult
}
res.status(200).json(resultFinal);
}

} catch (error: any) {
res.status(500).json({ error: 'Error running validation', details: error.message });
}
};

public static async validationRow(req: Request, res: Response): Promise<void> {

console.log("Validating row...")

try {
const { host, user, password, database, source_conn, target_conn, resultType, schema, content } = req.body;
const result = await ValidationService.validationRow(host, user, password, database, source_conn, target_conn, resultType, schema, content);
const jsonResult = JSON.parse(result);
// console.log("===> ", jsonResult)

if (jsonResult.error != null) {
const resultFinal = {
'validationStatus': "ERROR",
'information': jsonResult
}

res.status(200).json(resultFinal);
} else {
const resultFinal = {
'validationStatus': "OK",
'information': jsonResult
}
res.status(200).json(resultFinal);
}
} catch (error: any) {
res.status(500).json({ error: 'Error running validation', details: error.message });
}

}
}


export default ValidationController;
9 changes: 9 additions & 0 deletions packages/backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// src/index.ts
import app from './app';

// TODO:
const port = process.env.PORT || 9000;

app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
10 changes: 10 additions & 0 deletions packages/backend/src/routers/CreateRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

import { Router } from 'express';
import CreateConnectionController from '../controller/CreateConnectionController';

const CreateRouter = Router();

CreateRouter.post('/create-connection', CreateConnectionController.createConnection);
CreateRouter.get('/test-connection', CreateConnectionController.testConnection);

export default CreateRouter;
10 changes: 10 additions & 0 deletions packages/backend/src/routers/ValidationRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

import { Router } from 'express';
import ValidationController from '../controller/ValidationController';

const ValidationRouter = Router();

ValidationRouter.post('/column', ValidationController.validationColumn);
ValidationRouter.post('/row', ValidationController.validationRow);

export default ValidationRouter;
61 changes: 61 additions & 0 deletions packages/backend/src/service/CreateConnectionService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { spawn } from 'child_process';
import path from 'path';

class CreateConnectionService {
public static async createConnection(connName: string, dbType: string, host: string, port: string, user: string, password: string, database: string, schema: string): Promise<any> {
return new Promise((resolve, reject) => {

try {
console.log("===Go into CreateConnectionService===")

const scriptPath = path.join(__dirname, '../../src/validation_script/connection.py');

let command: string[];

if (dbType == 'Snowflake') {
const snowDB = `${database}/${schema}`;

console.log("snowflake的db===>", snowDB);

command = [scriptPath, connName, dbType, host, port, user, password, snowDB];
} else {
command = [scriptPath, connName, dbType, host, port, user, password, database];
}

const process = spawn('python', command);

let output = '';
let errorOutput = '';

process.stdout.on('data', (data) => {
output += data.toString();
});

process.stderr.on('data', (data) => {
errorOutput += data.toString();
});

process.on('close', (code) => {
if (code === 0) {
try {
const result = JSON.parse(output); // assuming the Python script returns JSON
resolve(result);
} catch (parseError) {
console.log("运行时出错=====>", parseError);
reject(new Error('Failed to parse Python script output as JSON'));
}
} else {
reject(new Error(`Python script error: ${errorOutput}`));
}
});
} catch (error: any) {
console.log("创建连接时出错=====> ", error);
reject(new Error(error));
}


});
}
}

export default CreateConnectionService;
Loading