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

Support produce message to pulsar #48

Merged
merged 2 commits into from
Jan 17, 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
21 changes: 21 additions & 0 deletions lib/api/pulsar/pulsar_partitioned_topic_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import 'dart:developer';
import 'package:http/http.dart' as http;
import 'package:paas_dashboard_flutter/api/http_util.dart';
import 'package:paas_dashboard_flutter/api/pulsar/pulsar_stat_api.dart';
import 'package:paas_dashboard_flutter/module/pulsar/const.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_consume.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_partitioned_topic_base.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_partitioned_topic_detail.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_produce.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_producer.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_subscription.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_topic.dart';
import 'package:paas_dashboard_flutter/ui/util/string_util.dart';
Expand Down Expand Up @@ -245,4 +247,23 @@ class PulsarPartitionedTopicApi {
return new PulsarPartitionedTopicBaseResp(
topicName, partitionNum, msgRateIn, msgRateOut, msgInCounter, msgOutCounter, storageSize);
}

static Future<String> sendMsgToPartitionTopic(
String host, int port, String tenant, String namespace, String topic, String key, String value) async {
ProducerMessage producerMessage = new ProducerMessage(key, value);
List<ProducerMessage> messageList = new List.empty(growable: true);
messageList.add(producerMessage);
PublishMessagesReq messagesReq = new PublishMessagesReq(PulsarConst.defaultProducerName, messageList);
var url = 'http://$host:${port.toString()}/topics/persistent/$tenant/$namespace/$topic/';
var response = await http.post(Uri.parse(url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: json.encode(messagesReq));
if (HttpUtil.abnormal(response.statusCode)) {
log('ErrorCode is ${response.statusCode}, body is ${response.body}');
return "send msg failed, " + response.body;
}
return "send msg success";
}
}
2 changes: 1 addition & 1 deletion lib/api/pulsar/pulsar_topic_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:http/http.dart' as http;
import 'package:paas_dashboard_flutter/api/http_util.dart';
import 'package:paas_dashboard_flutter/api/pulsar/pulsar_stat_api.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_consume.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_produce.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_producer.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_subscription.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_topic.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_topic_base.dart';
Expand Down
1 change: 1 addition & 0 deletions lib/module/pulsar/const.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ class PulsarConst {
static const String defaultHost = "localhost";
static const int defaultBrokerPort = 8080;
static const int defaultFunctionPort = 6650;
static const String defaultProducerName = "flutter-dashboard-producer";
}
31 changes: 21 additions & 10 deletions lib/module/pulsar/pulsar_produce.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
class ProducerResp {
class PublishMessagesReq {
final String producerName;
final double rateIn;
final double throughputIn;
final String clientVersion;
final double averageMsgSize;
final String address;
final List<ProducerMessage> messages;

ProducerResp(
this.producerName, this.rateIn, this.throughputIn, this.clientVersion, this.averageMsgSize, this.address);
PublishMessagesReq(this.producerName, this.messages);

ProducerResp deepCopy() {
return new ProducerResp(producerName, rateIn, throughputIn, clientVersion, averageMsgSize, address);
Map toJson() {
Map map = new Map();
map["producerName"] = this.producerName;
map["messages"] = this.messages;
return map;
}
}

class ProducerMessage {
final String payload;
final String key;

ProducerMessage(this.key, this.payload);
Map toJson() {
Map map = new Map();
map["payload"] = this.payload;
map["key"] = this.key;
return map;
}
}
15 changes: 15 additions & 0 deletions lib/module/pulsar/pulsar_producer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class ProducerResp {
final String producerName;
final double rateIn;
final double throughputIn;
final String clientVersion;
final double averageMsgSize;
final String address;

ProducerResp(
this.producerName, this.rateIn, this.throughputIn, this.clientVersion, this.averageMsgSize, this.address);

ProducerResp deepCopy() {
return new ProducerResp(producerName, rateIn, throughputIn, clientVersion, averageMsgSize, address);
}
}
11 changes: 10 additions & 1 deletion lib/ui/pulsar/screen/pulsar_partitioned_topic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_partitioned_topic_subscr
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_partitioned_topic_view_model.dart';
import 'package:provider/provider.dart';

import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_partitioned_topic_produce_view_model.dart';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import order looks weird

import 'package:paas_dashboard_flutter/ui/pulsar/widget/pulsar_partitioned_topic_produce.dart';

class PulsarPartitionedTopic extends StatefulWidget {
PulsarPartitionedTopic();

Expand All @@ -29,7 +32,7 @@ class _PulsarPartitionedTopicState extends State<PulsarPartitionedTopic> {
Widget build(BuildContext context) {
final vm = Provider.of<PulsarPartitionedTopicViewModel>(context);
return DefaultTabController(
length: 5,
length: 6,
child: Scaffold(
appBar: AppBar(
title: Text(
Expand All @@ -41,6 +44,7 @@ class _PulsarPartitionedTopicState extends State<PulsarPartitionedTopic> {
Tab(text: S.of(context).subscription),
Tab(text: S.of(context).consumer),
Tab(text: S.of(context).producer),
Tab(text: S.of(context).produce),
],
),
),
Expand Down Expand Up @@ -71,6 +75,11 @@ class _PulsarPartitionedTopicState extends State<PulsarPartitionedTopic> {
vm.pulsarInstancePo, vm.tenantResp, vm.namespaceResp, vm.topicResp),
child: PulsarPartitionedTopicProducerWidget(),
).build(context),
ChangeNotifierProvider(
create: (context) => PulsarPartitionedTopicProduceViewModel(
vm.pulsarInstancePo, vm.tenantResp, vm.namespaceResp, vm.topicResp),
child: PulsarPartitionedTopicProduceWidget(),
).build(context),
],
),
),
Expand Down
60 changes: 60 additions & 0 deletions lib/ui/pulsar/widget/pulsar_partitioned_topic_produce.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'package:flutter/material.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_produce_view_model.dart';
import 'package:provider/provider.dart';

class PulsarPartitionedTopicProduceWidget extends StatefulWidget {
PulsarPartitionedTopicProduceWidget();

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

class PulsarPartitionedTopicProduceWidgetState extends State<PulsarPartitionedTopicProduceWidget> {
@override
void initState() {
super.initState();
}

@override
Widget build(BuildContext context) {
final vm = Provider.of<PulsarPartitionedTopicProduceViewModel>(context);
if (vm.loading) {
WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
SpinnerUtil.create();
});
}
ExceptionUtil.processLoadException(vm, context);
ExceptionUtil.processOpException(vm, context);

var produceMsgButton = createInstanceButton(context);
var body = ListView(
children: [
Container(
height: 50,
child: ListView(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
children: [produceMsgButton],
),
),
],
);
return Scaffold(body: body);
}

ButtonStyleButton createInstanceButton(BuildContext context) {
final vm = Provider.of<PulsarPartitionedTopicProduceViewModel>(context, listen: false);
var list = [
FormFieldDef('message key'),
FormFieldDef('message value'),
];
return FormUtil.createButton2NoText("Send Message To Pulsar", list, context, (key, value) {
vm.sendMsg(key, value);
});
}
}
51 changes: 51 additions & 0 deletions lib/ui/util/form_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,57 @@ class FormUtil {
child: Text('Create ' + resourceName));
}

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

static ButtonStyleButton createButtonNoText(
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),
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(resourceName));
}

static ButtonStyleButton updateButton3(String resourceName, List<FormFieldDef> formFieldDefList, BuildContext context,
Function(String, String, String) callback) {
if (formFieldDefList.length != 3) {
Expand Down
51 changes: 51 additions & 0 deletions lib/vm/pulsar/pulsar_partitioned_topic_produce_view_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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';
import 'package:paas_dashboard_flutter/persistent/po/pulsar_instance_po.dart';
import 'package:paas_dashboard_flutter/vm/base_load_list_view_model.dart';

class PulsarPartitionedTopicProduceViewModel extends BaseLoadListViewModel {
final PulsarInstancePo pulsarInstancePo;
final TenantResp tenantResp;
final NamespaceResp namespaceResp;
final TopicResp topicResp;

PulsarPartitionedTopicProduceViewModel(this.pulsarInstancePo, this.tenantResp, this.namespaceResp, this.topicResp);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one space line is enough

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

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

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

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

String get tenant {
return this.tenantResp.tenant;
}

String get namespace {
return this.namespaceResp.namespace;
}

String get topic {
return this.topicResp.topicName;
}

String get message {
return this.topicResp.topicName;
}

Future<String> sendMsg(key, value) {
return PulsarPartitionedTopicApi.sendMsgToPartitionTopic(host, port, tenant, namespace, topic, key, value);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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_produce.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_producer.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_tenant.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_topic.dart';
import 'package:paas_dashboard_flutter/persistent/po/pulsar_instance_po.dart';
Expand All @@ -13,7 +13,6 @@ class PulsarPartitionedTopicProducerViewModel extends BaseLoadListViewModel<Prod
final TopicResp topicResp;

PulsarPartitionedTopicProducerViewModel(this.pulsarInstancePo, this.tenantResp, this.namespaceResp, this.topicResp);

PulsarPartitionedTopicProducerViewModel deepCopy() {
return new PulsarPartitionedTopicProducerViewModel(
pulsarInstancePo.deepCopy(), tenantResp.deepCopy(), namespaceResp.deepCopy(), topicResp.deepCopy());
Expand Down
2 changes: 1 addition & 1 deletion lib/vm/pulsar/pulsar_topic_producer_view_model.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:paas_dashboard_flutter/api/pulsar/pulsar_topic_api.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_namespace.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_produce.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_producer.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_tenant.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_topic.dart';
import 'package:paas_dashboard_flutter/persistent/po/pulsar_instance_po.dart';
Expand Down