<commit_before>
var http = require('http'),
	thrift = require('thrift'),
	fs = require('fs');

var FARService = require('./thrift_bindings/FARService.js'),
	FlowService = require('./thrift_bindings/FlowService.js'),
	flowservices_types = require('./thrift_bindings/flowservices_types.js');
var FlowMonitor = require('./thrift_bindings/FlowMonitor.js'),
	flowmonitor_types = require('./thrift_bindings/flowmonitor_types.js');
var SubscriptionService;
var ttransport, tprotocol;

try {
	ttransport = require('thrift/lib/thrift/transport');
} catch (e) {
	ttransport = require('thrift/transport');
}
try {
	tprotocol = require('thrift/lib/thrift/protocol');
} catch (e) {
	tprotocol = require('thrift/protocol');
}

(function () {

	this.config = {};
	this.configure = function (config) {
		this.config = config;
	};

	this.manager = function (method, params, done) {

		params = params || [];

		var conn = thrift.createConnection(
			this.config.manager.host,
			this.config.manager.port, {
			transport: ttransport.TFramedTransport,
			protocol: tprotocol.TBinaryProtocol
		});

		conn.on('error', function (error) {
			console.log('FlowManager: ', error);
			done('Could not connect to FlowMonitor.');
		});
		
		var Manager = thrift.createClient(FlowService, conn);

		console.log(method, params);

		var identifier;

		switch (method) {
			case 'start':
				identifier = new flowservices_types.FlowDescriptor({
					identifier: new flowservices_types.FlowIdentifier({
						app: params[0],
						flow: params[1],
						version: parseInt(params[2], 10),
						accountId: 'demo'
					}),
					"arguments": []
				});
				Manager.start(null, identifier, done);
				conn.end();
				
			break;
			case 'stop':
				identifier = new flowservices_types.FlowIdentifier({
					app: params[0],
					flow: params[1],
					version: parseInt(params[2], 10),
					accountId: 'demo'
				});
				Manager.stop(null, identifier, done);
				conn.end();
				
			break;
			case 'status':
				identifier = new flowservices_types.FlowIdentifier({
					app: params[0],
					flow: params[1],
					version: parseInt(params[2], 10),
					accountId: 'demo'
				});
				Manager.status(null, identifier, done);
				conn.end();
				
			break;
			case 'setInstances':
				identifier = new flowservices_types.FlowIdentifier({
					app: params[0],
					flow: params[1],
					version: params[2],
					accountId: 'demo'
				});
				var flowlet_id = params[3];
				var instances = params[4];

				Manager.setInstances(null, identifier, flowlet_id, instances, done);

				/*
					function (error, response) {

					var interval = setInterval(function () {
						Manager.flowletstatus(null, identifier, flowlet_id, function (error, status) {

							if (error) {
								clearInterval(interval);
								done(error);
							} else {

								switch (status.name) {
									case 'RECONFIGURE':
									case 'ADJUSTING_RESOURCES':<commit_msg>Not showing option to remove an instance if there is currently 1 or fewer. Also, sending auth token<commit_after>
var http = require('http'),
	thrift = require('thrift'),
	fs = require('fs');

var FARService = require('./thrift_bindings/FARService.js'),
	FlowService = require('./thrift_bindings/FlowService.js'),
	flowservices_types = require('./thrift_bindings/flowservices_types.js');
var FlowMonitor = require('./thrift_bindings/FlowMonitor.js'),
	flowmonitor_types = require('./thrift_bindings/flowmonitor_types.js');
var SubscriptionService;
var ttransport, tprotocol;

try {
	ttransport = require('thrift/lib/thrift/transport');
} catch (e) {
	ttransport = require('thrift/transport');
}
try {
	tprotocol = require('thrift/lib/thrift/protocol');
} catch (e) {
	tprotocol = require('thrift/protocol');
}

(function () {

	this.config = {};
	this.configure = function (config) {
		this.config = config;
	};

	this.manager = function (method, params, done) {

		params = params || [];

		var auth_token = new flowservices_types.DelegationToken({ token: null });

		var conn = thrift.createConnection(
			this.config.manager.host,
			this.config.manager.port, {
			transport: ttransport.TFramedTransport,
			protocol: tprotocol.TBinaryProtocol
		});

		conn.on('error', function (error) {
			console.log('FlowManager: ', error);
			done('Could not connect to FlowMonitor.');
		});
		
		var Manager = thrift.createClient(FlowService, conn);

		console.log(method, params);

		var identifier;

		switch (method) {
			case 'start':
				identifier = new flowservices_types.FlowDescriptor({
					identifier: new flowservices_types.FlowIdentifier({
						app: params[0],
						flow: params[1],
						version: parseInt(params[2], 10),
						accountId: 'demo'
					}),
					"arguments": []
				});
				Manager.start(auth_token, identifier, done);
				conn.end();
				
			break;
			case 'stop':
				identifier = new flowservices_types.FlowIdentifier({
					app: params[0],
					flow: params[1],
					version: parseInt(params[2], 10),
					accountId: 'demo'
				});
				Manager.stop(auth_token, identifier, done);
				conn.end();
				
			break;
			case 'status':
				identifier = new flowservices_types.FlowIdentifier({
					app: params[0],
					flow: params[1],
					version: parseInt(params[2], 10),
					accountId: 'demo'
				});
				Manager.status(auth_token, identifier, done);
				conn.end();
				
			break;
			case 'setInstances':
				identifier = new flowservices_types.FlowIdentifier({
					app: params[0],
					flow: params[1],
					version: params[2],
					accountId: 'demo'
				});
				var flowlet_id = params[3];
				var instances = params[4];

				Manager.setInstances(auth_token, identifier, flowlet_id, instances, done);

				/*
					function (error, response) {

					var interval = setInterval(function () {
						Manager.flowletstatus(null, identifier, flowlet_id, function (error, status) {

							if (error) {
								clearInterval(interval);
								done(error);
							} else {

								switch (status.name) {
									case 'RECONFIGURE':
									case 'ADJUSTING_RESOURCES':
{'fraction_non_alphanumeric': 0.14207103551775888, 'fraction_numerical': 0.006836751709187927, 'mean_word_length': 3.426568265682657, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1592842', 'n_tokens_mistral': 2338, 'n_tokens_neox': 1833, 'n_words': 402}