Skip to content

Commit

Permalink
Add pulsar instance basic page
Browse files Browse the repository at this point in the history
  • Loading branch information
fu-turer committed Aug 2, 2021
1 parent dc94c08 commit 164c9e5
Show file tree
Hide file tree
Showing 13 changed files with 272 additions and 8 deletions.
50 changes: 50 additions & 0 deletions lib/api/pulsar/pulsar_cluster_api.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'dart:convert';
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_tenant_api.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_cluster.dart';

class PulsarClusterAPi {
static Future<List<ClusterResp>> cluster(String host, int port) async {
var url = 'http://$host:${port.toString()}/admin/v2/brokers/version';
final versionResponse = await http.get(Uri.parse(url));
if (HttpUtil.abnormal(versionResponse.statusCode)) {
log('ErrorCode is ${versionResponse.statusCode}, body is ${versionResponse.body}');
throw Exception(
'ErrorCode is ${versionResponse.statusCode}, body is ${versionResponse.body}');
}
String version = versionResponse.body;

String tenantInfo = "";
await PulsarTenantAPi.getTenantInfo(host, port, "public")
.then((value) => tenantInfo = value);
String cluster =
((json.decode(tenantInfo) as Map)["allowedClusters"] as List)[0];
url = 'http://$host:${port.toString()}/admin/v2/brokers/$cluster';
final brokersResponse = await http.get(Uri.parse(url));
if (HttpUtil.abnormal(brokersResponse.statusCode)) {
log('ErrorCode is ${brokersResponse.statusCode}, body is ${brokersResponse.body}');
throw Exception(
'ErrorCode is ${brokersResponse.statusCode}, body is ${brokersResponse.body}');
}
List brokers = json.decode(brokersResponse.body) as List;

url = 'http://$host:${port.toString()}/admin/v2/brokers/leaderBroker';
final leaderBrokerResponse = await http.get(Uri.parse(url));
if (HttpUtil.abnormal(leaderBrokerResponse.statusCode)) {
log('ErrorCode is ${leaderBrokerResponse.statusCode}, body is ${leaderBrokerResponse.body}');
throw Exception(
'ErrorCode is ${leaderBrokerResponse.statusCode}, body is ${leaderBrokerResponse.body}');
}
String leader = json.decode(leaderBrokerResponse.body)["serviceUrl"];

List<ClusterResp> respList = new List.empty(growable: true);
brokers.forEach((element) {
respList.add(new ClusterResp(
element, leader.contains(element).toString(), version));
});
return respList;
}
}
20 changes: 19 additions & 1 deletion lib/api/pulsar/pulsar_tenant_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ import 'package:paas_dashboard_flutter/module/pulsar/pulsar_tenant.dart';

class PulsarTenantAPi {
static Future<void> createTenant(String host, int port, String tenant) async {
String tenantInfo = "";
await getTenantInfo(host, port, tenant).then((value) => tenantInfo = value);
var url = 'http://$host:${port.toString()}/admin/v2/tenants/$tenant';
final response = await http.put(Uri.parse(url));
final response = await http.put(Uri.parse(url),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: tenantInfo);
if (HttpUtil.abnormal(response.statusCode)) {
log('ErrorCode is ${response.statusCode}, body is ${response.body}');
throw Exception(
Expand Down Expand Up @@ -37,4 +43,16 @@ class PulsarTenantAPi {
List jsonResponse = json.decode(response.body) as List;
return jsonResponse.map((name) => new TenantResp.fromJson(name)).toList();
}

static Future<String> getTenantInfo(
String host, int port, String tenant) async {
var url = 'http://$host:${port.toString()}/admin/v2/tenants/public';
final response = await http.get(Uri.parse(url));
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;
}
}
5 changes: 4 additions & 1 deletion lib/generated/intl/messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class MessageLookup extends MessageLookupByLibrary {
static Map<String, Function> _notInlinedMessages(_) => <String, Function>{
"aboutAuthor": MessageLookupByLibrary.simpleMessage("About author"),
"appName": MessageLookupByLibrary.simpleMessage("Paas Dashboard"),
"brokersName": MessageLookupByLibrary.simpleMessage("Broker Instance"),
"cancel": MessageLookupByLibrary.simpleMessage("Cancel"),
"clearBacklog": MessageLookupByLibrary.simpleMessage("Clear Backlog"),
"confirm": MessageLookupByLibrary.simpleMessage("Confirm"),
Expand All @@ -34,6 +35,7 @@ class MessageLookup extends MessageLookupByLibrary {
"deleteTenant": MessageLookupByLibrary.simpleMessage("Delete Tenant"),
"deleteTopic": MessageLookupByLibrary.simpleMessage("Delete Topic"),
"email": MessageLookupByLibrary.simpleMessage("email"),
"isLeader": MessageLookupByLibrary.simpleMessage("Is Leader"),
"languageSettings":
MessageLookupByLibrary.simpleMessage("Language Settings"),
"name": MessageLookupByLibrary.simpleMessage("name"),
Expand All @@ -53,6 +55,7 @@ class MessageLookup extends MessageLookupByLibrary {
"tenantName": MessageLookupByLibrary.simpleMessage("Tenant Name"),
"tenants": MessageLookupByLibrary.simpleMessage("Tenants"),
"topicName": MessageLookupByLibrary.simpleMessage("Topic Name"),
"topics": MessageLookupByLibrary.simpleMessage("Topics")
"topics": MessageLookupByLibrary.simpleMessage("Topics"),
"versionName": MessageLookupByLibrary.simpleMessage("Version")
};
}
5 changes: 4 additions & 1 deletion lib/generated/intl/messages_zh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class MessageLookup extends MessageLookupByLibrary {
static Map<String, Function> _notInlinedMessages(_) => <String, Function>{
"aboutAuthor": MessageLookupByLibrary.simpleMessage("关于作者"),
"appName": MessageLookupByLibrary.simpleMessage("Paas 仪表盘"),
"brokersName": MessageLookupByLibrary.simpleMessage("Pulsar 实例"),
"cancel": MessageLookupByLibrary.simpleMessage("取消"),
"clearBacklog": MessageLookupByLibrary.simpleMessage("清理积压"),
"confirm": MessageLookupByLibrary.simpleMessage("确认"),
Expand All @@ -32,6 +33,7 @@ class MessageLookup extends MessageLookupByLibrary {
"deleteTenant": MessageLookupByLibrary.simpleMessage("删除租户"),
"deleteTopic": MessageLookupByLibrary.simpleMessage("删除 Topic"),
"email": MessageLookupByLibrary.simpleMessage("邮箱"),
"isLeader": MessageLookupByLibrary.simpleMessage("是否是主节点"),
"languageSettings": MessageLookupByLibrary.simpleMessage("语言设置"),
"name": MessageLookupByLibrary.simpleMessage("名称"),
"namespaceName": MessageLookupByLibrary.simpleMessage("命名空间名称"),
Expand All @@ -46,6 +48,7 @@ class MessageLookup extends MessageLookupByLibrary {
"tenantName": MessageLookupByLibrary.simpleMessage("租户名称"),
"tenants": MessageLookupByLibrary.simpleMessage("租户列表"),
"topicName": MessageLookupByLibrary.simpleMessage("Topic 名称"),
"topics": MessageLookupByLibrary.simpleMessage("Topic 列表")
"topics": MessageLookupByLibrary.simpleMessage("Topic 列表"),
"versionName": MessageLookupByLibrary.simpleMessage("版本")
};
}
30 changes: 30 additions & 0 deletions lib/generated/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"cancel": "Cancel",
"confirm": "Confirm",
"confirmDeleteQuestion": "ConfirmDelete?",
"brokersName": "Broker Instance",
"isLeader": "Is Leader",
"versionName": "Version",
"tenants": "Tenants",
"searchByTenant": "Search by Tenant Name",
"tenantName": "Tenant Name",
Expand Down
3 changes: 3 additions & 0 deletions lib/l10n/intl_zh.arb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"cancel": "取消",
"confirm": "确认",
"confirmDeleteQuestion": "确认删除吗?",
"brokersName": "Pulsar 实例",
"isLeader": "是否是主节点",
"versionName": "版本",
"tenants": "租户列表",
"searchByTenant": "按租户名称搜索",
"tenantName": "租户名称",
Expand Down
11 changes: 11 additions & 0 deletions lib/module/pulsar/pulsar_cluster.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ClusterResp {
final String instance;
final String isLeader;
final String version;

ClusterResp(this.instance, this.isLeader, this.version);

ClusterResp deepCopy() {
return new ClusterResp(instance, isLeader, version);
}
}
16 changes: 16 additions & 0 deletions lib/module/pulsar/pulsar_tenant.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,19 @@ class TenantResp {
return TenantResp(name);
}
}


class TenantInfoResp {
final String adminRoles;
final String allowedClusters;

TenantInfoResp(this.adminRoles, this.allowedClusters);

TenantInfoResp deepCopy() {
return new TenantInfoResp(this.adminRoles, this.allowedClusters);
}

factory TenantInfoResp.fromJson(Map name) {
return TenantInfoResp(name["adminRoles"], name["allowedClusters"]);
}
}
3 changes: 2 additions & 1 deletion lib/ui/pulsar/pulsar_instance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:paas_dashboard_flutter/ui/pulsar/tab/pulsar_basic.dart';
import 'package:paas_dashboard_flutter/ui/pulsar/tab/pulsar_details.dart';
import 'package:paas_dashboard_flutter/ui/pulsar/tab/pulsar_message_query.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_cluster_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_instance_view_model.dart';
import 'package:provider/provider.dart';

Expand Down Expand Up @@ -38,7 +39,7 @@ class _PulsarInstanceState extends State<PulsarInstanceScreen> {
body: TabBarView(
children: [
ChangeNotifierProvider(
create: (context) => vm.deepCopy(),
create: (context) => PulsarClusterViewModel(vm.pulsarInstancePo),
child: PulsarBasicWidget(),
).build(context),
ChangeNotifierProvider(
Expand Down
87 changes: 84 additions & 3 deletions lib/ui/pulsar/tab/pulsar_basic.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,93 @@
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/spinner_util.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_cluster_view_model.dart';
import 'package:provider/provider.dart';

class PulsarBasicWidget extends StatelessWidget {
class PulsarBasicWidget extends StatefulWidget {
PulsarBasicWidget();

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

class PulsarBasicScreenState extends State<PulsarBasicWidget> {
final searchTextController = TextEditingController();

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

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

@override
Widget build(BuildContext context) {
return ListView(
children: <Widget>[],
final vm = Provider.of<PulsarClusterViewModel>(context);
if (vm.loading) {
WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
SpinnerUtil.create();
});
}
ExceptionUtil.processLoadExceptionPageable(vm, context);
ExceptionUtil.processOpExceptionPageable(vm, context);
var topicsFuture = SingleChildScrollView(
child: DataTable(
showCheckboxColumn: false,
columns: [
DataColumn(label: Text(S.of(context).brokersName)),
DataColumn(label: Text(S.of(context).isLeader)),
DataColumn(label: Text(S.of(context).versionName)),
],
rows: vm.displayList
.map((data) => DataRow(cells: [
DataCell(
Text(data.instance),
),
DataCell(
Text(data.isLeader),
),
DataCell(
Text(data.version),
),
]))
.toList()),
);
var refreshButton = TextButton(
onPressed: () {
vm.fetchPulsarCluster();
},
child: Text(S.of(context).refresh));
var body = ListView(
children: <Widget>[
Container(
height: 50,
child: ListView(
scrollDirection: Axis.horizontal,
shrinkWrap: true,
children: [refreshButton],
),
),
Text(
'Pulsar Cluster',
style: TextStyle(fontSize: 22),
),
topicsFuture
],
);

return body;
}
}
45 changes: 45 additions & 0 deletions lib/vm/pulsar/pulsar_cluster_view_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import 'package:paas_dashboard_flutter/api/pulsar/pulsar_cluster_api.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_cluster.dart';
import 'package:paas_dashboard_flutter/persistent/po/pulsar_instance_po.dart';

import '../base_load_list_page_view_model.dart';

class PulsarClusterViewModel extends BaseLoadListPageViewModel<ClusterResp> {
List<ClusterResp> instances = <ClusterResp>[];

final PulsarInstancePo pulsarInstancePo;

PulsarClusterViewModel(this.pulsarInstancePo);

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;
}

Future<void> fetchPulsarCluster() async {
try {
this.fullList = await PulsarClusterAPi.cluster(host, port);
this.displayList = this.fullList;
} on Exception catch (e) {
loadException = e;
loading = false;
}
loadSuccess();
notifyListeners();
}

Future<void> filter(String str) async {
notifyListeners();
}
}
2 changes: 1 addition & 1 deletion macos/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 6eac6b3292e5142cfc23bdeb71848a40ec51c14c

COCOAPODS: 1.10.1
COCOAPODS: 1.10.2

0 comments on commit 164c9e5

Please sign in to comment.