Skip to content

Commit

Permalink
Prettified Code!
Browse files Browse the repository at this point in the history
  • Loading branch information
sheyabernstein authored and actions-user committed Feb 27, 2024
1 parent 467c6de commit 56f487c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 31 deletions.
26 changes: 12 additions & 14 deletions MMM-pihole-stats.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ Module.register("MMM-pihole-stats", {
initialLoadDelay: 0,
},

formatInt (n) {
formatInt(n) {
return n.toLocaleString();
},

formatFloat (n) {
formatFloat(n) {
if (this.config.floatingPoints) {
const x = 10 ** this.config.floatingPoints;
return Math.round(parseFloat(n) * x) / x;
Expand All @@ -36,7 +36,7 @@ Module.register("MMM-pihole-stats", {
},

// Define start sequence.
start () {
start() {
Log.info(`Starting module: ${this.name}`);

this.domains_being_blocked = null;
Expand All @@ -50,7 +50,7 @@ Module.register("MMM-pihole-stats", {
},

// Override dom generator.
getDom () {
getDom() {
const wrapper = document.createElement("div");

if (!this.loaded) {
Expand All @@ -61,8 +61,7 @@ Module.register("MMM-pihole-stats", {

const header = document.createElement("div");
header.className = "small bright";
header.innerHTML
= `${this.formatInt(this.ads_blocked_today)}
header.innerHTML = `${this.formatInt(this.ads_blocked_today)}
ads blocked today. (
${this.formatFloat(this.ads_percentage_today)}
%)`;
Expand Down Expand Up @@ -112,8 +111,7 @@ Module.register("MMM-pihole-stats", {

const footer = document.createElement("div");
footer.className = "xsmall";
footer.innerHTML
= `${this.formatInt(this.dns_queries_today)}
footer.innerHTML = `${this.formatInt(this.dns_queries_today)}
DNS queries,
${this.formatInt(this.domains_being_blocked)}
domains blacklisted.`;
Expand All @@ -122,16 +120,16 @@ Module.register("MMM-pihole-stats", {
return wrapper;
},

updateStats () {
updateStats() {
Log.info(`${this.name}: Getting data`);

this.sendSocketNotification("GET_PIHOLE", {
config: this.config
config: this.config,
});
},

// Handle node helper response
socketNotificationReceived (notification, payload) {
socketNotificationReceived(notification, payload) {
if (notification === "PIHOLE_DATA") {
this.processSummary(payload);
this.loaded = true;
Expand All @@ -142,7 +140,7 @@ Module.register("MMM-pihole-stats", {
this.updateDom(this.config.animationSpeed);
},

scheduleUpdate (delay) {
scheduleUpdate(delay) {
let nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
Expand All @@ -155,7 +153,7 @@ Module.register("MMM-pihole-stats", {
}, nextLoad);
},

processSummary (data) {
processSummary(data) {
if (!data) {
// Did not receive usable new data.
return;
Expand All @@ -167,7 +165,7 @@ Module.register("MMM-pihole-stats", {
this.ads_percentage_today = data.ads_percentage_today || "0.0";
},

processSources (data) {
processSources(data) {
if (!data) {
// Did not receive usable new data.
return;
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ Without `config.showSources` enabled:

## Dependencies

- [MagicMirror²](https://github.com/MagicMirrorOrg/MagicMirror)
- [Pi-hole](https://pi-hole.net)
- [MagicMirror²](https://github.com/MagicMirrorOrg/MagicMirror)
- [Pi-hole](https://pi-hole.net)

## Installation

1. Clone this repo into `~/MagicMirror/modules` directory.
`git clone https://github.com/sheyabernstein/MMM-pihole-stats`
`git clone https://github.com/sheyabernstein/MMM-pihole-stats`
2. Obtain an API token from your PiHole installation by navigating to [http://pi.hole/admin/settings.php?tab=api](http://pi.hole/admin/settings.php?tab=api) and clicking `Show API token`
3. Configure your `~/MagicMirror/config/config.js`

Expand All @@ -40,7 +40,7 @@ Here is an example entry for `config.js`:
## Configuration Options

| **Option** | **Default** | **Description** |
|--------------------------|--------------------------------|-------------------------------------------------------------------------------------------------------------------------------------|
| ------------------------ | ------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------- |
| `apiURL` | `http://pi.hole/admin/api.php` | URL to Pi-hole admin, including HTTP protocol |
| `apiToken` | | API Token from Pi-hole (required for `showSources`) |
| `showSources` | `true` | Show request sources (clients) |
Expand All @@ -52,6 +52,6 @@ Here is an example entry for `config.js`:

## Notes

- Feb 28, 2024 update: This module needs no external packages anymore.
- Feb 27, 2024 update: This module now requires `npm install` when installing.
- Sep 27, 2020 update: Configuring the Pi-hole server to allow CORS is no longer needed.
- Feb 28, 2024 update: This module needs no external packages anymore.
- Feb 27, 2024 update: This module now requires `npm install` when installing.
- Sep 27, 2020 update: Configuring the Pi-hole server to allow CORS is no longer needed.
28 changes: 18 additions & 10 deletions node_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ const Log = require("logger");
const NodeHelper = require("node_helper");

module.exports = NodeHelper.create({
start () {
start() {
Log.info(`Starting node_helper for module [${this.name}]`);
},

socketNotificationReceived (notification, payload) {
socketNotificationReceived(notification, payload) {
if (notification === "GET_PIHOLE") {
const config = payload.config;

Expand All @@ -20,19 +20,21 @@ module.exports = NodeHelper.create({

if (config.showSources && config.sourcesCount > 0) {
if (config.showSources && !config.apiToken) {
Log.error(`${this.name}: Can't load sources because the apiKey is not set.`);
Log.error(
`${this.name}: Can't load sources because the apiKey is not set.`,
);
} else {
this.getPiholeData(
config,
{ getQuerySources: config.sourcesCount },
"PIHOLE_SOURCES"
"PIHOLE_SOURCES",
);
}
}
}
},

isValidURL (url) {
isValidURL(url) {
try {
new URL(url);
return true;
Expand All @@ -41,7 +43,7 @@ module.exports = NodeHelper.create({
}
},

buildURL (config, params) {
buildURL(config, params) {
params = params || {};

if (config.apiToken && !params.hasOwnProperty("auth")) {
Expand All @@ -54,7 +56,7 @@ module.exports = NodeHelper.create({
return url.toString();
},

async getPiholeData (config, params, notification) {
async getPiholeData(config, params, notification) {
const self = this,
url = self.buildURL(config, params),
headers = { Referer: url };
Expand All @@ -66,14 +68,20 @@ module.exports = NodeHelper.create({
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
if (response.headers.get('content-type').includes('application/json')) {
if (
response.headers
.get("content-type")
.includes("application/json")
) {
const data = await response.json();
self.sendSocketNotification(notification, data);
} else {
throw new Error(`Expected JSON but received ${response.headers.get('content-type')}`);
throw new Error(
`Expected JSON but received ${response.headers.get("content-type")}`,
);
}
} catch (error) {
Log.error(self.name + " ERROR:", error);
}
},
});
});

0 comments on commit 56f487c

Please sign in to comment.