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

Use const instead of hardcode #26

Merged
merged 1 commit into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/flutter_check_style.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ jobs:
with:
channel: 'beta'
- run: flutter pub get
- run: flutter format --set-exit-if-changed .
- run: flutter format --set-exit-if-changed -l 120 .
4 changes: 4 additions & 0 deletions lib/module/bk/const.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class BkConst {
static const String defaultHost = "localhost";
static const int defaultPort = 8080;
}
3 changes: 3 additions & 0 deletions lib/module/mongo/const.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class MongoConst {
static const String defaultAddr = "mongodb://localhost:27017";
}
5 changes: 5 additions & 0 deletions lib/module/pulsar/const.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class PulsarConst {
static const String defaultHost = "localhost";
static const int defaultBrokerPort = 8080;
static const int defaultFunctionPort = 6650;
}
4 changes: 4 additions & 0 deletions lib/module/zk/const.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class ZkConst {
static const String defaultHost = "localhost";
static const int defaultPort = 8080;
}
7 changes: 7 additions & 0 deletions lib/persistent/persistent_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:paas_dashboard_flutter/persistent/po/k8s_instance_po.dart';
import 'package:paas_dashboard_flutter/persistent/po/mongo_instance_po.dart';
import 'package:paas_dashboard_flutter/persistent/po/mysql_instance_po.dart';
import 'package:paas_dashboard_flutter/persistent/po/pulsar_instance_po.dart';
import 'package:paas_dashboard_flutter/persistent/po/zk_instance_po.dart';

abstract class PersistentApi {
Future<void> savePulsar(String name, String host, int port,
Expand All @@ -19,6 +20,12 @@ abstract class PersistentApi {

Future<List<BkInstancePo>> bookkeeperInstances();

Future<void> saveZooKeeper(String name, String host, int port);

Future<void> deleteZooKeeper(int id);

Future<List<ZkInstancePo>> zooKeeperInstances();

Future<void> saveKubernetesSsh(String name, List<SshStep> sshSteps);

Future<void> deleteKubernetes(int id);
Expand Down
89 changes: 51 additions & 38 deletions lib/persistent/persistent_db.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import 'dart:developer';
import 'dart:io';

import 'package:paas_dashboard_flutter/module/bk/const.dart';
import 'package:paas_dashboard_flutter/module/mongo/const.dart';
import 'package:paas_dashboard_flutter/module/mysql/const.dart';
import 'package:paas_dashboard_flutter/module/pulsar/const.dart';
import 'package:paas_dashboard_flutter/module/ssh/ssh_step.dart';
import 'package:paas_dashboard_flutter/module/zk/const.dart';
import 'package:paas_dashboard_flutter/persistent/persistent_api.dart';
import 'package:paas_dashboard_flutter/persistent/po/bk_instance_po.dart';
import 'package:paas_dashboard_flutter/persistent/po/k8s_instance_po.dart';
import 'package:paas_dashboard_flutter/persistent/po/mongo_instance_po.dart';
import 'package:paas_dashboard_flutter/persistent/po/mysql_instance_po.dart';
import 'package:paas_dashboard_flutter/persistent/po/pulsar_instance_po.dart';
import 'package:paas_dashboard_flutter/persistent/po/zk_instance_po.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
Expand Down Expand Up @@ -55,19 +61,19 @@ class PersistentDb implements PersistentApi {
'CREATE TABLE pulsar_instances(id INTEGER PRIMARY KEY, name TEXT, host TEXT, port INTEGER, function_host TEXT, function_port INTEGER)',
);
await db.execute(
'INSERT INTO pulsar_instances(name, host, port, function_host, function_port) VALUES ("example", "localhost", 8080, "localhost", 8080)',
'INSERT INTO pulsar_instances(name, host, port, function_host, function_port) VALUES ("example", "${PulsarConst.defaultHost}", ${PulsarConst.defaultBrokerPort}, "${PulsarConst.defaultHost}", ${PulsarConst.defaultFunctionPort})',
);
await db.execute(
'CREATE TABLE bookkeeper_instances(id INTEGER PRIMARY KEY, name TEXT, host TEXT, port INTEGER)',
);
await db.execute(
'INSERT INTO bookkeeper_instances(name, host, port) VALUES ("example", "localhost", 8080)',
'INSERT INTO bookkeeper_instances(name, host, port) VALUES ("example", "${BkConst.defaultHost}", ${BkConst.defaultPort})',
);
await db.execute(
'CREATE TABLE zookeeper_instances(id INTEGER PRIMARY KEY, name TEXT, host TEXT, port INTEGER)',
);
await db.execute(
'INSERT INTO zookeeper_instances(name, host, port) VALUES ("example", "localhost", 8080)',
'INSERT INTO zookeeper_instances(name, host, port) VALUES ("example", "${ZkConst.defaultHost}", ${ZkConst.defaultPort})',
);
// type: api、host
await db.execute(
Expand All @@ -80,24 +86,22 @@ class PersistentDb implements PersistentApi {
'CREATE TABLE mongo_instances(id INTEGER PRIMARY KEY, name TEXT, addr TEXT, username TEXT, password TEXT)',
);
await db.execute(
'INSERT INTO mongo_instances(name, addr, username, password) VALUES ("example", "mongodb://localhost:27017", "", "")',
'INSERT INTO mongo_instances(name, addr, username, password) VALUES ("example", "${MongoConst.defaultAddr}", "", "")',
);
await db.execute(
'CREATE TABLE mysql_instances(id INTEGER PRIMARY KEY, name TEXT, host TEXT, port INTEGER, username TEXT, password TEXT)',
);
await db.execute(
'INSERT INTO mysql_instances(name, host, port, username, password) VALUES ("example", "localhost", 3306, "hzj", "Mysql@123")',
'INSERT INTO mysql_instances(name, host, port, username, password) VALUES ("example", "${MysqlConst.defaultHost}", ${MysqlConst.defaultPort}, "${MysqlConst.defaultUsername}", "${MysqlConst.defaultPassword}")',
);
}

@override
Future<void> savePulsar(String name, String host, int port,
String functionHost, int functionPort) async {
Future<void> savePulsar(String name, String host, int port, String functionHost, int functionPort) async {
var aux = await getInstance();
var list = [name, host, port, functionHost, functionPort];
aux.database.execute(
'INSERT INTO pulsar_instances(name, host, port, function_host, function_port) VALUES (?, ?, ?, ?, ?)',
list);
'INSERT INTO pulsar_instances(name, host, port, function_host, function_port) VALUES (?, ?, ?, ?, ?)', list);
}

@override
Expand All @@ -109,42 +113,60 @@ class PersistentDb implements PersistentApi {
@override
Future<List<PulsarInstancePo>> pulsarInstances() async {
var aux = await getInstance();
final List<Map<String, dynamic>> maps =
await aux.database.query('pulsar_instances');
final List<Map<String, dynamic>> maps = await aux.database.query('pulsar_instances');
return List.generate(maps.length, (i) {
var aux = maps[i];
return PulsarInstancePo(aux['id'], aux['name'], aux['host'], aux['port'],
aux['function_host'], aux['function_port']);
return PulsarInstancePo(
aux['id'], aux['name'], aux['host'], aux['port'], aux['function_host'], aux['function_port']);
});
}

@override
Future<void> saveBookkeeper(String name, String host, int port) async {
var aux = await getInstance();
var list = [name, host, port];
aux.database.execute(
'INSERT INTO bookkeeper_instances(name, host, port) VALUES (?, ?, ?)',
list);
aux.database.execute('INSERT INTO bookkeeper_instances(name, host, port) VALUES (?, ?, ?)', list);
}

@override
Future<void> deleteBookkeeper(int id) async {
var aux = await getInstance();
aux.database
.delete('bookkeeper_instances', where: 'id = ?', whereArgs: [id]);
aux.database.delete('bookkeeper_instances', where: 'id = ?', whereArgs: [id]);
}

@override
Future<List<BkInstancePo>> bookkeeperInstances() async {
var aux = await getInstance();
final List<Map<String, dynamic>> maps =
await aux.database.query('bookkeeper_instances');
final List<Map<String, dynamic>> maps = await aux.database.query('bookkeeper_instances');
return List.generate(maps.length, (i) {
var aux = maps[i];
return BkInstancePo(aux['id'], aux['name'], aux['host'], aux['port']);
});
}

@override
Future<void> deleteZooKeeper(int id) async {
var aux = await getInstance();
aux.database.delete('zookeeper_instances', where: 'id = ?', whereArgs: [id]);
}

@override
Future<void> saveZooKeeper(String name, String host, int port) async {
var aux = await getInstance();
var list = [name, host, port];
aux.database.execute('INSERT INTO zookeeper_instances(name, host, port) VALUES (?, ?, ?)', list);
}

@override
Future<List<ZkInstancePo>> zooKeeperInstances() async {
var aux = await getInstance();
final List<Map<String, dynamic>> maps = await aux.database.query('zookeeper_instances');
return List.generate(maps.length, (i) {
var aux = maps[i];
return ZkInstancePo(aux['id'], aux['name'], aux['host'], aux['port']);
});
}

@override
Future<void> saveKubernetesSsh(String name, List<SshStep> sshSteps) {
// TODO: implement saveKubernetesSsh
Expand Down Expand Up @@ -172,23 +194,18 @@ class PersistentDb implements PersistentApi {
@override
Future<List<MongoInstancePo>> mongoInstances() async {
var aux = await getInstance();
final List<Map<String, dynamic>> maps =
await aux.database.query('mongo_instances');
final List<Map<String, dynamic>> maps = await aux.database.query('mongo_instances');
return List.generate(maps.length, (i) {
var aux = maps[i];
return MongoInstancePo(aux['id'], aux['name'], aux['addr'],
aux['username'], aux['password']);
return MongoInstancePo(aux['id'], aux['name'], aux['addr'], aux['username'], aux['password']);
});
}

@override
Future<void> saveMongo(
String name, String addr, String username, String password) async {
Future<void> saveMongo(String name, String addr, String username, String password) async {
var aux = await getInstance();
var list = [name, addr, username, password];
aux.database.execute(
'INSERT INTO mongo_instances(name, addr, username, password) VALUES (?, ?, ?, ?)',
list);
aux.database.execute('INSERT INTO mongo_instances(name, addr, username, password) VALUES (?, ?, ?, ?)', list);
}

@override
Expand All @@ -200,22 +217,18 @@ class PersistentDb implements PersistentApi {
@override
Future<List<MysqlInstancePo>> mysqlInstances() async {
var aux = await getInstance();
final List<Map<String, dynamic>> maps =
await aux.database.query('mysql_instances');
final List<Map<String, dynamic>> maps = await aux.database.query('mysql_instances');
return List.generate(maps.length, (i) {
var aux = maps[i];
return MysqlInstancePo(aux['id'], aux['name'], aux['host'], aux['port'],
aux['username'], aux['password']);
return MysqlInstancePo(aux['id'], aux['name'], aux['host'], aux['port'], aux['username'], aux['password']);
});
}

@override
Future<void> saveMysql(String name, String host, int port, String username,
String password) async {
Future<void> saveMysql(String name, String host, int port, String username, String password) async {
var aux = await getInstance();
var list = [name, host, port, username, password];
aux.database.execute(
'INSERT INTO mysql_instances(name, host, port, username, password) VALUES (?, ?, ?, ?, ?)',
list);
aux.database
.execute('INSERT INTO mysql_instances(name, host, port, username, password) VALUES (?, ?, ?, ?, ?)', list);
}
}
44 changes: 32 additions & 12 deletions lib/persistent/persistent_memory.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import 'package:paas_dashboard_flutter/module/bk/const.dart';
import 'package:paas_dashboard_flutter/module/mongo/const.dart';
import 'package:paas_dashboard_flutter/module/mysql/const.dart';
import 'package:paas_dashboard_flutter/module/pulsar/const.dart';
import 'package:paas_dashboard_flutter/module/ssh/ssh_step.dart';
import 'package:paas_dashboard_flutter/module/zk/const.dart';
import 'package:paas_dashboard_flutter/persistent/persistent_api.dart';
import 'package:paas_dashboard_flutter/persistent/po/bk_instance_po.dart';
import 'package:paas_dashboard_flutter/persistent/po/k8s_instance_po.dart';
import 'package:paas_dashboard_flutter/persistent/po/mongo_instance_po.dart';
import 'package:paas_dashboard_flutter/persistent/po/mysql_instance_po.dart';
import 'package:paas_dashboard_flutter/persistent/po/pulsar_instance_po.dart';
import 'package:paas_dashboard_flutter/persistent/po/zk_instance_po.dart';

class PersistentMemory implements PersistentApi {
@override
Future<void> savePulsar(String name, String host, int port,
String functionHost, int functionPort) {
Future<void> savePulsar(String name, String host, int port, String functionHost, int functionPort) {
// TODO: implement savePulsar
throw UnimplementedError();
}
Expand All @@ -23,7 +28,8 @@ class PersistentMemory implements PersistentApi {
@override
Future<List<PulsarInstancePo>> pulsarInstances() async {
return [
new PulsarInstancePo(0, "example", "localhost", 8080, "localhost", 8080)
new PulsarInstancePo(0, "example", PulsarConst.defaultHost, PulsarConst.defaultBrokerPort,
PulsarConst.defaultHost, PulsarConst.defaultFunctionPort)
];
}

Expand All @@ -41,7 +47,24 @@ class PersistentMemory implements PersistentApi {

@override
Future<List<BkInstancePo>> bookkeeperInstances() async {
return [new BkInstancePo(0, "example", "localhost", 8080)];
return [new BkInstancePo(0, "example", BkConst.defaultHost, BkConst.defaultPort)];
}

@override
Future<void> deleteZooKeeper(int id) {
// TODO: implement deleteZooKeeper
throw UnimplementedError();
}

@override
Future<void> saveZooKeeper(String name, String host, int port) {
// TODO: implement saveZooKeeper
throw UnimplementedError();
}

@override
Future<List<ZkInstancePo>> zooKeeperInstances() async {
return [new ZkInstancePo(0, "example", ZkConst.defaultHost, ZkConst.defaultPort)];
}

@override
Expand All @@ -68,14 +91,11 @@ class PersistentMemory implements PersistentApi {

@override
Future<List<MongoInstancePo>> mongoInstances() async {
return [
new MongoInstancePo(0, "example", "mongodb://localhost:27017", "", "")
];
return [new MongoInstancePo(0, "example", MongoConst.defaultAddr, "", "")];
}

@override
Future<void> saveMongo(
String name, String addr, String username, String password) {
Future<void> saveMongo(String name, String addr, String username, String password) {
throw UnimplementedError();
}

Expand All @@ -88,13 +108,13 @@ class PersistentMemory implements PersistentApi {
@override
Future<List<MysqlInstancePo>> mysqlInstances() async {
return [
new MysqlInstancePo(0, "example", "localhost", 3306, "hzj", "Mysql@123")
new MysqlInstancePo(0, "example", MysqlConst.defaultHost, MysqlConst.defaultPort, MysqlConst.defaultUsername,
MysqlConst.defaultPassword)
];
}

@override
Future<void> saveMysql(
String name, String host, int port, String username, String password) {
Future<void> saveMysql(String name, String host, int port, String username, String password) {
// TODO: implement saveMysql
throw UnimplementedError();
}
Expand Down
21 changes: 21 additions & 0 deletions lib/persistent/po/zk_instance_po.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:paas_dashboard_flutter/persistent/po/http_endpoint.dart';

class ZkInstancePo extends HttpEndpoint {
final int id;

ZkInstancePo(this.id, String name, String host, int port) : super(name, host, port);

Map<String, dynamic> toMap() {
return {
'id': id,
'name': name,
'host': host,
'port': port,
};
}

@override
String toString() {
return 'ZooKeeperInstance{id: $id, name: $name, host: $host, port: $port}';
}
}
5 changes: 3 additions & 2 deletions test/api/mongo/mongo_databases_api_query_test.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:paas_dashboard_flutter/api/mongo/mongo_databases_api.dart';
import 'package:paas_dashboard_flutter/module/mongo/const.dart';

void main() {
test("test_query_databases", () async {
var list = await MongoDatabaseApi.getDatabaseList(
"mongodb://localhost:27017", "", "");
var list =
await MongoDatabaseApi.getDatabaseList(MongoConst.defaultAddr, "", "");
print(list);
});
}
3 changes: 2 additions & 1 deletion test/api/mongo/mongo_tables_api_query_test.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:paas_dashboard_flutter/api/mongo/mongo_tables_api.dart';
import 'package:paas_dashboard_flutter/module/mongo/const.dart';

void main() {
test("test_query_tables", () async {
var list = await MongoTablesApi.getTableList(
"mongodb://localhost:27017", "", "", "admin");
MongoConst.defaultAddr, "", "", "admin");
print(list);
});
}
Loading