Skip to content

Commit

Permalink
Add function field to pulsar instance (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
shoothzj authored Nov 30, 2021
1 parent 2c6f7ab commit 2b24090
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 42 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ Flutter Paas Dashboard
## 其他语言文档
- [English Doc](README_en.md)

# 开发环境准备
```bash
flutter config --enable-macos-desktop
```

# 运行
## web
如需在Web下运行,需要关闭`chrome`的安全开关,步骤如下,参考链接: https://stackoverflow.com/questions/65630743/how-to-solve-flutter-web-api-cors-error-only-with-dart-code
```bash
# 跳转到flutter安装目录
rm -rf bin/cache/flutter_tools.stamp
```
Step1 跳转到flutter安装目录
Step2 rm -rf bin/cache/flutter_tools.stamp
Step3 vi packages/flutter_tools/lib/src/web/chrome.dart
Step4 add `'--disable-web-security',` behind `'--disable-extensions',`
```
编辑`packages/flutter_tools/lib/src/web/chrome.dart`<br/>
`'--disable-extensions',`后面添加`'--disable-web-security',`
5 changes: 3 additions & 2 deletions lib/persistent/persistent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ class Persistent {
return api!;
}

static Future<void> savePulsar(String name, String host, int port) async {
return (await getApi()).savePulsar(name, host, port);
static Future<void> savePulsar(String name, String host, int port,
String functionHost, int functionPort) async {
return (await getApi()).savePulsar(name, host, port, functionHost, functionPort);
}

static Future<void> deletePulsar(int id) async {
Expand Down
2 changes: 1 addition & 1 deletion lib/persistent/persistent_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:paas_dashboard_flutter/persistent/po/bk_instance_po.dart';
import 'package:paas_dashboard_flutter/persistent/po/pulsar_instance_po.dart';

abstract class PersistentApi {
Future<void> savePulsar(String name, String host, int port);
Future<void> savePulsar(String name, String host, int port, String functionHost, int functionPort);

Future<void> deletePulsar(int id);

Expand Down
8 changes: 4 additions & 4 deletions lib/persistent/persistent_db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class PersistentDb implements PersistentApi {
static initTable(Database db) async {
log('init tables start');
await db.execute(
'CREATE TABLE pulsar_instances(id INTEGER PRIMARY KEY, name TEXT, host TEXT, port INTEGER)',
'CREATE TABLE pulsar_instances(id INTEGER PRIMARY KEY, name TEXT, broker_host TEXT, broker_port INTEGER, function_host TEXT, function_port INTEGER)',
);
await db.execute(
'INSERT INTO pulsar_instances(name, host, port) VALUES ("example", "localhost", 8080)',
Expand All @@ -62,11 +62,11 @@ class PersistentDb implements PersistentApi {
}

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

Expand All @@ -83,7 +83,7 @@ class PersistentDb implements PersistentApi {
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']);
return PulsarInstancePo(aux['id'], aux['name'], aux['host'], aux['port'], aux['function_host'], aux['function_port']);
});
}

Expand Down
4 changes: 2 additions & 2 deletions lib/persistent/persistent_memory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:paas_dashboard_flutter/persistent/po/pulsar_instance_po.dart';

class PersistentMemory implements PersistentApi {
@override
Future<void> savePulsar(String name, String host, int port) {
Future<void> savePulsar(String name, String host, int port, String functionHost, int functionPort) {
// TODO: implement savePulsar
throw UnimplementedError();
}
Expand All @@ -17,7 +17,7 @@ class PersistentMemory implements PersistentApi {

@override
Future<List<PulsarInstancePo>> pulsarInstances() async {
return [new PulsarInstancePo(0, "example", "localhost", 8080)];
return [new PulsarInstancePo(0, "example", "localhost", 8080, "localhost", 6650)];
}

@override
Expand Down
17 changes: 11 additions & 6 deletions lib/persistent/po/pulsar_instance_po.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import 'package:paas_dashboard_flutter/persistent/po/http_endpoint.dart';

class PulsarInstancePo extends HttpEndpoint {
class PulsarInstancePo {
final int id;
final String name;
final String host;
final int port;
final String functionHost;
final int functionPort;

PulsarInstancePo(this.id, String name, String host, int port)
: super(name, host, port);
PulsarInstancePo(this.id, this.name, this.host, this.port, this.functionHost, this.functionPort);

PulsarInstancePo deepCopy() {
return new PulsarInstancePo(id, name, host, port);
return new PulsarInstancePo(id, name, host, port, functionHost, functionPort);
}

Map<String, dynamic> toMap() {
Expand All @@ -16,11 +19,13 @@ class PulsarInstancePo extends HttpEndpoint {
'name': name,
'host': host,
'port': port,
'function_host': functionHost,
'function_port': functionPort,
};
}

@override
String toString() {
return 'PulsarInstance{id: $id, name: $name, host: $host, port: port}';
return 'PulsarInstance{id: $id, name: $name, host: $host, port: $port, functionHost: $functionHost, functionPort: $functionPort}';
}
}
18 changes: 12 additions & 6 deletions lib/ui/pulsar/pulsar_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ class _PulsarPageState extends State<PulsarPage> {
columns: [
DataColumn(label: Text('Id')),
DataColumn(label: Text('Name')),
DataColumn(label: Text('Address')),
DataColumn(label: Text('Host')),
DataColumn(label: Text('Port')),
DataColumn(label: Text('FunctionHost')),
DataColumn(label: Text('FunctionPort')),
DataColumn(label: Text('Delete instance')),
],
rows: vm.instances
Expand All @@ -46,6 +48,8 @@ class _PulsarPageState extends State<PulsarPage> {
DataCell(Text(itemRow.name)),
DataCell(Text(itemRow.host)),
DataCell(Text(itemRow.port.toString())),
DataCell(Text(itemRow.functionHost)),
DataCell(Text(itemRow.functionPort.toString())),
DataCellUtil.newDellDataCell(() {
vm.deletePulsar(itemRow.id);
}),
Expand Down Expand Up @@ -86,12 +90,14 @@ class _PulsarPageState extends State<PulsarPage> {
final vm = Provider.of<PulsarInstanceListViewModel>(context, listen: false);
var list = [
FormFieldDef('Instance Name'),
FormFieldDef('Instance Host'),
FormFieldDef('Instance Port')
FormFieldDef('Host'),
FormFieldDef('Port'),
FormFieldDef('Function Host'),
FormFieldDef('Function Port'),
];
return FormUtil.createButton3("Pulsar Instance", list, context,
(name, host, port) {
vm.createPulsar(name, host, int.parse(port));
return FormUtil.createButton5("Pulsar Instance", list, context,
(name, host, port, functionHost, functionPort) {
vm.createPulsar(name, host, int.parse(port), functionHost, int.parse(functionPort));
});
}
}
104 changes: 104 additions & 0 deletions lib/ui/pulsar/widget/pulsar_partitioned_topic_list.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import 'package:flutter/material.dart';
import 'package:paas_dashboard_flutter/generated/l10n.dart';
import 'package:paas_dashboard_flutter/route/page_route_const.dart';
import 'package:paas_dashboard_flutter/ui/component/searchable_title.dart';
import 'package:paas_dashboard_flutter/ui/util/data_cell_util.dart';
import 'package:paas_dashboard_flutter/ui/util/exception_util.dart';
import 'package:paas_dashboard_flutter/ui/util/form_util.dart';
import 'package:paas_dashboard_flutter/ui/util/spinner_util.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_namespace_view_model.dart';
import 'package:provider/provider.dart';

class PulsarPartitionedTopicListWidget extends StatefulWidget {
PulsarPartitionedTopicListWidget();

@override
State<StatefulWidget> createState() {
return new PulsarPartitionedTopicListWidgetState();
}
}

class PulsarPartitionedTopicListWidgetState extends State<PulsarPartitionedTopicListWidget> {
final searchTextController = TextEditingController();

@override
void initState() {
super.initState();
final vm = Provider.of<PulsarNamespaceViewModel>(context, listen: false);
vm.fetchTopics();
searchTextController.addListener(() {
vm.filter(searchTextController.text);
});
}

@override
void dispose() {
searchTextController.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
final vm = Provider.of<PulsarNamespaceViewModel>(context);
if (vm.loading) {
WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
SpinnerUtil.create();
});
}
ExceptionUtil.processLoadExceptionPageable(vm, context);
ExceptionUtil.processOpExceptionPageable(vm, context);
vm.setDataConverter((item) => DataRow(
onSelectChanged: (bool? selected) {
Navigator.pushNamed(context, PageRouteConst.PulsarTopic,
arguments: item.deepCopy());
},
cells: [
DataCell(
Text(item.topic),
),
DataCellUtil.newDellDataCell(() {
vm.deleteTopic(item.topic);
}),
]));
var topicsTable = SingleChildScrollView(
child: PaginatedDataTable(
showCheckboxColumn: false,
columns: [
DataColumn(label: Text(S.of(context).topicName)),
DataColumn(label: Text(S.of(context).deleteTopic)),
],
source: vm),
);
var formButton = createPartitionTopicButton(context);
var refreshButton = TextButton(
onPressed: () {
vm.fetchTopics();
},
child: Text(S.of(context).refresh));
var body = ListView(
children: <Widget>[
Container(
height: 50,
child: ListView(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
children: [formButton, refreshButton],
),
),
SearchableTitle(S.of(context).topics, S.of(context).searchByTopic,
searchTextController),
topicsTable
],
);
return body;
}

ButtonStyleButton createPartitionTopicButton(BuildContext context) {
var list = [FormFieldDef('Topic Name'), FormFieldDef('Partition Number')];
return FormUtil.createButton2("Partitioned Topic", list, context,
(topic, partition) async {
final vm = Provider.of<PulsarNamespaceViewModel>(context, listen: false);
vm.createTopic(topic, int.parse(partition));
});
}
}
24 changes: 24 additions & 0 deletions lib/ui/util/form_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,30 @@ class FormUtil {
static const String CANCEL = 'cancel';
static const String CREATE = 'create';

static ButtonStyleButton createButton5(
String resourceName,
List<FormFieldDef> formFieldDefList,
BuildContext context,
Function(String, String, String, String, String) callback) {
if (formFieldDefList.length != 5) {
throw AssertionError('args not match');
}
return createButton(resourceName, formFieldDefList, context,
(list) => callback(list[0], list[1], list[2], list[3], list[4]));
}

static ButtonStyleButton createButton4(
String resourceName,
List<FormFieldDef> formFieldDefList,
BuildContext context,
Function(String, String, String, String) callback) {
if (formFieldDefList.length != 4) {
throw AssertionError('args not match');
}
return createButton(resourceName, formFieldDefList, context,
(list) => callback(list[0], list[1], list[2], list[3]));
}

static ButtonStyleButton createButton3(
String resourceName,
List<FormFieldDef> formFieldDefList,
Expand Down
4 changes: 2 additions & 2 deletions lib/vm/pulsar/pulsar_instance_list_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class PulsarInstanceListViewModel extends ChangeNotifier {
notifyListeners();
}

Future<void> createPulsar(String name, String host, int port) async {
Persistent.savePulsar(name, host, port);
Future<void> createPulsar(String name, String host, int port, String functionHost, int functionPort) async {
Persistent.savePulsar(name, host, port, functionHost, functionPort);
fetchPulsarInstances();
}

Expand Down
8 changes: 8 additions & 0 deletions lib/vm/pulsar/pulsar_instance_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ class PulsarInstanceViewModel
return this.pulsarInstancePo.port;
}

String get functionHost {
return this.pulsarInstancePo.functionHost;
}

int get functionPort {
return this.pulsarInstancePo.functionPort;
}

Future<void> fetchTenants() async {
try {
final results = await PulsarTenantAPi.getTenants(host, port);
Expand Down
Loading

0 comments on commit 2b24090

Please sign in to comment.