pi-hole / web

Pi-hole Dashboard for stats and more
https://pi-hole.net
Other
2.01k stars 556 forks source link

API : invalid response type #202

Closed nlamirault closed 7 years ago

nlamirault commented 7 years ago
Expected Behaviour:

When i've got Top Ads, result looks like a map, while when TopAds are empty results seems to be an array.

See: https://github.com/nlamirault/pihole_exporter/issues/1

Actual Behaviour:

Without TopAds it seems to be an array. With TopAds, it seems to be a map :

{"domains_being_blocked":"101934","dns_queries_today":"180","ads_blocked_today":"13","ads_percentage_today":"7.2222222222222","domains_over_time":{"17":"22","18":"158"},"ads_over_time":{"17":"0","18":"13"},"top_queries":{"www.google.com":"9","www.googleapis.com":"5","www.google.fr":"5","api.github.com":"4","apis.google.com":"4","ssl.gstatic.com":"4","e12.whatsapp.net":"4","github.com":"4","clients3.google.com":"3","avatars3.githubusercontent.com":"3"},"top_ads":{"stats.g.doubleclick.net":"2","s.adroll.com":"1","www.googletagmanager.com":"1","munchkin.marketo.net":"1","dx.steelhousemedia.com":"1","cdn.segment.io":"1","collector.githubapp.com":"1","googleads.g.doubleclick.net":"1","static.doubleclick.net":"1","www.googleadservices.com":"1"},"query[AAAA]":"83","query[A]":"97","8.8.4.4":"60","8.8.8.8":"110","top_sources":{"192.168.1.18":"147","192.168.1.14":"33"}}
Steps to reproduce this issue:

Launch a fresh installation of PiHole.

nlamirault commented 7 years ago

According to PHP documentation, there is 2 possible outputs :

echo "Empty array output as array: ", json_encode($b), "\n";
RESULT: Empty array output as array: []
echo "Empty array output as object: ", json_encode($b, JSON_FORCE_OBJECT), "\n\n";
RESULT: Empty array output as object: {}

Change function in https://github.com/pi-hole/AdminLTE/blob/master/api.php#L63 in 👍

echo json_encode($data, JSON_FORCE_OBJECT);

could solve problem ...

DL6ER commented 7 years ago

Just tested it. Your proposed change causes problems in the current version of the devel branch. The main diagram (Queries over Time) does not load anymore because of invalid data format. I'm currently looking into this and will open a PR as soon as I find a solution.

DL6ER commented 7 years ago

Although I've been able to solve the issue with the main diagram, it causes additional trouble with "Recent Queries" which might not be able to be solved straightforwardly. You can have a look at what I did to make it working on the main page here.

DL6ER commented 7 years ago

@nlamirault The change you suggested cause unpleasant trouble with DataTables. Also, I don't want to break anything else users might have invented that relies on our API how it is.

However, what do you think about having an option to get the behavior you want/need?

Example: api.php?xyz []

api.php?xyz&jsonForceObject {}

api.php?getAllQueries {"data":[["2016-11-24T00:00:04","A","xyz.de","x.y.z.w","OK",""],

api.php?getAllQueries&jsonForceObject {"data":{"0":{"0":"2016-11-24T00:00:04","1":"A","2":"xyz.de","3":"x.y.z.w","4":"OK","5":""},

nlamirault commented 7 years ago

@DL6ER Perfect.

DL6ER commented 7 years ago

Update has been released