summaryrefslogtreecommitdiffstats
path: root/src/report.cpp
blob: cd1c1dc8d327693541433e59de0256780f01120a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <map>
#include <sstream>
#include "ocelot.h"
#include "misc_functions.h"
#include "report.h"
#include "response.h"
#include "user.h"

std::string report(params_type &params, user_list &users_list, client_opts_t &client_opts) {
	std::stringstream output;
	std::string action = params["get"];
	if (action == "") {
		output << "Invalid action\n";
	} else if (action == "stats") {
		time_t uptime = time(NULL) - stats.start_time;
		int up_d = uptime / 86400;
		uptime -= up_d * 86400;
		int up_h = uptime / 3600;
		uptime -= up_h * 3600;
		int up_m = uptime / 60;
		int up_s = uptime - up_m * 60;
		std::string up_ht = up_h <= 9 ? '0' + inttostr(up_h) : inttostr(up_h);
		std::string up_mt = up_m <= 9 ? '0' + inttostr(up_m) : inttostr(up_m);
		std::string up_st = up_s <= 9 ? '0' + inttostr(up_s) : inttostr(up_s);

		output << "Uptime: " << up_d << " days, " << up_ht << ':' << up_mt << ':' << up_st << '\n'
		<< stats.opened_connections << " connections opened\n"
		<< stats.open_connections << " open connections\n"
		<< stats.connection_rate << " connections/s\n"
		<< stats.requests << " requests handled\n"
		<< stats.request_rate << " requests/s\n"
		<< stats.succ_announcements << " successful announcements\n"
		<< (stats.announcements - stats.succ_announcements) << " failed announcements\n"
		<< stats.scrapes << " scrapes\n"
		<< stats.leechers << " leechers tracked\n"
		<< stats.seeders << " seeders tracked\n"
		<< stats.bytes_read << " bytes read\n"
		<< stats.bytes_written << " bytes written\n";
	} else if (action == "user") {
		std::string key = params["key"];
		if (key == "") {
			output << "Invalid action\n";
		} else {
			user_list::const_iterator u = users_list.find(key);
			if (u != users_list.end()) {
				output << u->second->get_leeching() << " leeching\n"
				<< u->second->get_seeding() << " seeding\n";
			}
		}
	} else {
		output << "Invalid action\n";
	}
	output << "success";
	return response(output.str(), client_opts);
}