Skip to content

Commit

Permalink
Added - Actual url, change vars to strings
Browse files Browse the repository at this point in the history
  • Loading branch information
goldbattle committed Aug 5, 2016
1 parent 75f85a8 commit 9baa3a7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Allow for viewing of stats in tray menu
* Allow for enable/disable stat pulling
* Simple one click to get to the dashboard
* Easy installer for end user (long term)
* Easy installer for end user



Expand Down
6 changes: 3 additions & 3 deletions source/includes/copernicus/Copernicus.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ class Copernicus {
string url_admin;

// Stats that we get from updating
int stat_dns;
int stat_ads_total;
double stat_ads_percent;
string stat_dns;
string stat_ads_total;
string stat_ads_percent;


// Curl callback function
Expand Down
38 changes: 21 additions & 17 deletions source/src/copernicus/Copernicus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ using json = nlohmann::json;
// Default constructor
Copernicus::Copernicus() {
status = true;
url_admin = "https://github.com/goldbattle/copernicus/";
stat_dns = 0;
stat_ads_total = 0;
stat_ads_percent = 0;
url_admin = "http://pi.hole/admin/";
stat_dns = "0";
stat_ads_total = "0";
stat_ads_percent = "0";
}

// This will poll the API can get new stats
Expand All @@ -47,24 +47,24 @@ void Copernicus::update_stats() {
try {

// Host and url
std::string host("date.jsontest.com");
std::string api_path("/");
std::string host("pi.hole");
std::string api_path("/admin/api.php?summary");

// Open the connection to the server
data_raw = network.Get(host, api_path);

// Data returned
//std::cout << "[debug]: response data" << endl << data_raw << endl;
std::cout << "[debug]: response data" << endl << data_raw << endl;

}
// Catch any problems with the http requests
catch(const happyhttp::Wobbly& e) {
// Error out
std::cerr <<"[error]: HTTP Error \"" << e.what() << "\"" << std::endl;
// Set to zero values
stat_dns = 0;
stat_ads_total = 0;
stat_ads_percent = 0;
stat_dns = "Error";
stat_ads_total = "Error";
stat_ads_percent = "Error";
// Return
return;
}
Expand All @@ -79,19 +79,23 @@ void Copernicus::update_stats() {
data_json = json::parse(data_raw);

// Parse api response
stat_dns = data_json["milliseconds_since_epoch"].get<int>();
stat_ads_total = data_json["milliseconds_since_epoch"].get<int>();
stat_ads_percent = data_json["milliseconds_since_epoch"].get<double>();
stat_dns = data_json["dns_queries_today"].get<std::string>();
stat_ads_total = data_json["ads_blocked_today"].get<std::string>();

// Create a stream so we can add the percent
stringstream ss;
ss << data_json["ads_percentage_today"].get<std::string>() << "%";
stat_ads_percent = ss.str();

}
// Unable to parse json, reset values
catch(...) {
// Error out
std::cerr <<"[error]: Unable to parse JSON" << std::endl;
// Set to zero values
stat_dns = 0;
stat_ads_total = 0;
stat_ads_percent = 0;
stat_dns = "Error";
stat_ads_total = "Error";
stat_ads_percent = "Error";
// Return
return;
}
Expand Down Expand Up @@ -131,6 +135,6 @@ string Copernicus::get_ads_blocked_total() {
// Accessors method to percentage of ads
string Copernicus::get_ads_blocked_percent() {
stringstream ss;
ss << "Traffic Percent: " << stat_ads_percent << "%";
ss << "Traffic Percent: " << stat_ads_percent;
return ss.str();
}

0 comments on commit 9baa3a7

Please sign in to comment.