Skip to content

Commit

Permalink
Support to update partition num
Browse files Browse the repository at this point in the history
  • Loading branch information
shoothzj committed Nov 16, 2021
1 parent 7bbc69f commit e6e6d55
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 1 deletion.
17 changes: 17 additions & 0 deletions lib/api/pulsar/pulsar_partitioned_topic_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ class PulsarPartitionedTopicAPi {
return response.body;
}

static Future<String> modifyPartitionTopic(String host, int port,
String tenant, String namespace, String topic, int partitionNum) async {
var url =
'http://$host:${port.toString()}/admin/v2/persistent/$tenant/$namespace/$topic/partitions';
var response = await http.post(Uri.parse(url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: partitionNum.toString());
if (HttpUtil.abnormal(response.statusCode)) {
log('ErrorCode is ${response.statusCode}, body is ${response.body}');
throw Exception(
'ErrorCode is ${response.statusCode}, body is ${response.body}');
}
return response.body;
}

static Future<List<TopicResp>> getTopics(
String host, int port, String tenant, String namespace) async {
var url =
Expand Down
27 changes: 27 additions & 0 deletions lib/ui/pulsar/widget/pulsar_partitioned_topic_basic.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:paas_dashboard_flutter/generated/l10n.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_partitioned_topic_basic_view_model.dart';
import 'package:provider/provider.dart';
Expand All @@ -21,6 +22,7 @@ class PulsarPartitionedTopicBasicWidgetState
super.initState();
final vm = Provider.of<PulsarPartitionedTopicBasicViewModel>(context,
listen: false);
vm.fetchPartitions();
}

@override
Expand All @@ -33,6 +35,7 @@ class PulsarPartitionedTopicBasicWidgetState
}
ExceptionUtil.processLoadException(vm, context);
ExceptionUtil.processOpException(vm, context);
var formButton = modifyPartitionTopicButton(context);
var refreshButton =
TextButton(onPressed: () {}, child: Text(S.of(context).refresh));
var body = ListView(
Expand All @@ -45,8 +48,32 @@ class PulsarPartitionedTopicBasicWidgetState
children: [refreshButton],
),
),
Container(
height: 50,
child: ListView(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
children: [
Text(
'partition num is ${vm.partitionNum}',
style: new TextStyle(fontSize: 20),
),
formButton
],
),
),
],
);
return body;
}

ButtonStyleButton modifyPartitionTopicButton(BuildContext context) {
var list = [FormFieldDef('New Partition Number')];
return FormUtil.updateButton1("Topic Partitions", list, context,
(partition) async {
final vm = Provider.of<PulsarPartitionedTopicBasicViewModel>(context,
listen: false);
vm.modifyTopicPartition(vm.topic, int.parse(partition));
});
}
}
88 changes: 87 additions & 1 deletion lib/ui/util/form_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class FormUtil {
throw AssertionError('args not match');
}
return createButton(resourceName, formFieldDefList, context,
(list) => callback(list[0], list[1], list[2]));
(list) => callback(list[0], list[1], list[2]));
}

static ButtonStyleButton createButton2(
Expand Down Expand Up @@ -95,4 +95,90 @@ class FormUtil {
},
child: Text('Create ' + resourceName));
}

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

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

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

static ButtonStyleButton updateButton(
String resourceName,
List<FormFieldDef> formFieldDefList,
BuildContext context,
Function(List<String>) callback) {
return TextButton(
onPressed: () {
var editControllerList =
formFieldDefList.map((e) => TextEditingController()).toList();
List<TextFormField> formFieldsList = List.generate(
formFieldDefList.length,
(index) => TextFormField(
decoration: InputDecoration(
labelText: formFieldDefList[index].fieldName),
controller: editControllerList[index],
));
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
scrollable: true,
title: Text(resourceName + ' Form'),
content: Form(
child: Column(
children: formFieldsList,
)),
actions: [
ElevatedButton(
child: Text(CREATE),
onPressed: () {
var list = editControllerList
.map((e) => e.value.text)
.toList();
callback(list);
Navigator.of(context).pop();
},
),
ElevatedButton(
child: Text(CANCEL),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
});
},
child: Text('Update ' + resourceName));
}
}
26 changes: 26 additions & 0 deletions lib/vm/pulsar/pulsar_partitioned_topic_basic_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:paas_dashboard_flutter/api/pulsar/pulsar_partitioned_topic_api.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_namespace.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_tenant.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_topic.dart';
Expand All @@ -9,6 +10,7 @@ class PulsarPartitionedTopicBasicViewModel extends BaseLoadViewModel {
final TenantResp tenantResp;
final NamespaceResp namespaceResp;
final TopicResp topicResp;
String partitionNum = "";

PulsarPartitionedTopicBasicViewModel(this.pulsarInstancePo, this.tenantResp,
this.namespaceResp, this.topicResp);
Expand Down Expand Up @@ -45,4 +47,28 @@ class PulsarPartitionedTopicBasicViewModel extends BaseLoadViewModel {
String get topic {
return this.topicResp.topicName;
}

Future<void> fetchPartitions() async {
try {
final results = await PulsarPartitionedTopicAPi.getDetails(
host, port, tenant, namespace, topic);
partitionNum = results.length.toString();
loadSuccess();
} on Exception catch (e) {
loadException = e;
loading = false;
}
notifyListeners();
}

Future<void> modifyTopicPartition(String topic, int partition) async {
try {
await PulsarPartitionedTopicAPi.modifyPartitionTopic(
host, port, tenant, namespace, topic, partition);
await fetchPartitions();
} on Exception catch (e) {
opException = e;
notifyListeners();
}
}
}

0 comments on commit e6e6d55

Please sign in to comment.