var YugabyteDB = {
	params: {},
	metrics: {},
	error_msg: '',

	setParams: function (params) {
		['account_id', 'project_id', 'token', 'cluster_id'].forEach(function (field) {
			if (typeof params !== 'object' || typeof params[field] === 'undefined' || params[field] === '') {
				throw 'Required param is not set: ' + field + '.';
			}
		});

		YugabyteDB.params = params;
	},


	request: function (url, data) {
		if (typeof data === 'undefined' || data === null) {
			data = '';
		}

		var response, request = new HttpRequest();
		if (typeof YugabyteDB.params.proxy !== 'undefined' && YugabyteDB.params.proxy !== '') {
			request.setProxy(YugabyteDB.params.proxy);
		}
		if (YugabyteDB.params.token) {
			request.addHeader('Accept: application/json');
			request.addHeader('Authorization: Bearer ' + YugabyteDB.params.token);
		}

		Zabbix.log(4, '[ YugabyteDB ] Sending request: ' + url);

		response = request.get(url);

		Zabbix.log(4, '[ YugabyteDB ] Received response with status code ' + request.getStatus() + ': ' + response);

		if (request.getStatus() !== 200 || response === null) {
			throw 'Request failed with status code ' + request.getStatus() + ': ' + response;
		}

		try {
			return JSON.parse(response);
		}
		catch (error) {
			throw 'Failed to parse response received from API:' + error;
		}
	},


	get_last_values: function (values) {
		var max_timestamp = 0;
		var last_value = 0;
		for (e in values) {
			if (values[e][0] > max_timestamp) {
				max_timestamp = values[e][0];
				last_value = values[e][1];
			};
		};
		return last_value;
	},


	get_start_time: function() {
		return Math.floor(Date.now() / 1000 - 300);
	}

};

metrics = [
	"YSQL_SELECT_OPS_PER_SEC",
	"YSQL_DELETE_OPS_PER_SEC",
	"YSQL_INSERT_OPS_PER_SEC",
	"YSQL_UPDATE_OPS_PER_SEC",
	"YSQL_OTHER_OPS_PER_SEC",
	"YSQL_TRANSACTION_OPS_PER_SEC",
	"YSQL_SELECT_LATENCY_MS",
	"YSQL_DELETE_LATENCY_MS",
	"YSQL_INSERT_LATENCY_MS",
	"YSQL_UPDATE_LATENCY_MS",
	"YSQL_OTHER_LATENCY_MS",
	"YSQL_TRANSACTION_LATENCY_MS",
	"YCQL_SELECT_OPS_PER_SEC",
	"YCQL_DELETE_OPS_PER_SEC",
	"YCQL_INSERT_OPS_PER_SEC",
	"YCQL_OTHER_OPS_PER_SEC",
	"YCQL_UPDATE_OPS_PER_SEC",
	"YCQL_TRANSACTION_OPS_PER_SEC",
	"YCQL_SELECT_LATENCY_MS",
	"YCQL_DELETE_LATENCY_MS",
	"YCQL_INSERT_LATENCY_MS",
	"YCQL_OTHER_LATENCY_MS",
	"YCQL_UPDATE_LATENCY_MS",
	"YCQL_TRANSACTION_LATENCY_MS"
]

try {
	YugabyteDB.setParams(JSON.parse(value));

	var size = 6

	for (var i = 0; i < Math.ceil(metrics.length/size); i++)  {
		var values = YugabyteDB.request(
			'https://cloud.yugabyte.com/api/public/v1/accounts/' + YugabyteDB.params.account_id + '/projects/' + YugabyteDB.params.project_id + '/clusters/' + YugabyteDB.params.cluster_id + '/metrics?metrics=' + metrics.slice((i * size), (i * size) + size).join('%2C') + '&start_time=' + YugabyteDB.get_start_time()
		);

		values.data.forEach(function (e) {
			YugabyteDB.metrics[e.name] = YugabyteDB.get_last_values(e.values)
		});
	};
}
catch (error) {
	Zabbix.log(3, '[ YugabyteDB ] ERROR: ' + error);
	error_msg = 'Failed to get data from API:' + error;
	return JSON.stringify({'error': error_msg});
}

return JSON.stringify({'metrics': YugabyteDB.metrics, 'error': YugabyteDB.error_msg});