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

Added replset Status Command #133

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions example/get_replica_status.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:mongo_dart/mongo_dart.dart';

/// At the moment you can only run getStatus() command on primary
/// On secondary we get a "MongoDart Error: No master connection" because we have no read permission on it
/// To test with secondary you can comment our the lines 29 and 31 in connection_manager.dart
main() async {
var db = new Db("mongodb://127.0.0.1:27017/admin");
await db.open();

Map<String, dynamic> rep_status = await db.getStatus();
//print(rep_status);
if (rep_status.containsKey("members")) {
List<dynamic> members = rep_status['members'];
int members_count = members.length;
print("Members Count: " + members_count.toString());
members.forEach((dynamic value) {
print("-----------------------------------");
//print(value);
int _id = value['_id'];
String name = value['name'];
double health = value['health'];
String stateStr = value['stateStr'];
int uptime = value['uptime'];

print("Member id: " + _id.toString());
print("Member name: " + name);
print("Member health: " + health.toString());
print("Member State: " + stateStr.toString());
print("Member State: " + secToTime(uptime));
});
}

await db.close();
}

String secToTime(int seconds) {
double hours = seconds / 3600;
double mins = seconds / 60 % 60;
int secs = seconds % 60;
return hours.floor().toString() +
"h " +
mins.floor().toString() +
"m " +
secs.floor().toString() +
"s";
}
5 changes: 5 additions & 0 deletions lib/src/database/db.dart
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,11 @@ class Db {
connection: connection);
}

Future<Map<String, dynamic>> getStatus({_Connection connection}) {
return executeDbCommand(DbCommand.createStatusCommand(this),
connection: connection);
}

Future<Map<String, dynamic>> isMaster({_Connection connection}) {
return executeDbCommand(DbCommand.createIsMasterCommand(this),
connection: connection);
Expand Down
4 changes: 4 additions & 0 deletions lib/src/database/dbcommand.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ class DbCommand extends MongoQueryMessage {
return createQueryDbCommand(db, {'buildInfo': 1});
}

static DbCommand createStatusCommand(Db db) {
return createQueryDbCommand(db, {'replSetGetStatus': 1 });
}

static DbCommand createGetLastErrorCommand(Db db, WriteConcern concern) {
return createQueryDbCommand(db, concern.command);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/network/connection_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ class _ConnectionManager {
_log.fine(() => replyMessage.documents[0].toString());
var master = replyMessage.documents[0]["ismaster"] == true;
connection.isMaster = master;
if (master) {
//if (master) {
_masterConnection = connection;
}
//}
connection.serverCapabilities
.getParamsFromIstMaster(replyMessage.documents[0]);
if (db._authenticationScheme == null) {
Expand Down
25 changes: 25 additions & 0 deletions test/database_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:uuid/uuid.dart';
const dbName = "test-mongo-dart";

const DefaultUri = 'mongodb://localhost:27017/$dbName';
const DefaultAdminUri = 'mongodb://localhost:27017/admin';

Db db;
Uuid uuid = new Uuid();
Expand Down Expand Up @@ -65,6 +66,11 @@ Future getBuildInfo() async {
expect(result["ok"], 1);
}

Future getStatus() async {
var result = await db.getStatus();
expect(result["ok"], 1);
}

Future testIsMaster() async {
var result = await db.isMaster();
expect(result["ok"], 1);
Expand Down Expand Up @@ -1142,6 +1148,11 @@ Future testFindOneWhileStateIsOpening() {
}

main() {
Future initializeAdminDatabase() async {
db = new Db(DefaultAdminUri);
await db.open();
}

Future initializeDatabase() async {
db = new Db(DefaultUri);
await db.open();
Expand All @@ -1151,6 +1162,20 @@ main() {
await db.close();
}

group("Admin", () {
setUp(() async {
await initializeAdminDatabase();
});

tearDown(() async {
await cleanupDatabase();
});

group('DBCommand:', () {
test('getStatus', getStatus);
});
});

group("A", () {
setUp(() async {
await initializeDatabase();
Expand Down