Skip to content

Commit

Permalink
Merge pull request #8 from tiennou/fixes
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
pieterbrandsen authored Sep 27, 2024
2 parents a4e18bf + 62cc2ff commit 0183be9
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 185 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module.exports = {
"no-console":"off",
"import/extensions":"off",
"no-underscore-dangle":"off",
"no-param-reassign": ["error", { "props": false }]
"no-param-reassign": ["error", { "props": false }],
"no-restricted-syntax": "off",
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Update all .example files and/or folders to match your needs. This step is not r
* `--grafanaDomain`: Overwrite grafana.ini domain
* `--grafanaPort`: port for Grafana to run on
* `--relayPort`: port for relay-ng to run on (default: 2003)
* `--pushStatusPort`: port for the stats-getter push API (default: disabled)

#### Exporting

Expand Down
64 changes: 64 additions & 0 deletions bin/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env node

const { execSync } = require('child_process');
const { join } = require('path');
require('dotenv').config({ path: join(__dirname, '../.env') });

const nodeVersion = process.versions.node;
const nodeVersionMajor = Number(nodeVersion.split('.')[0]);
const { getPort } = nodeVersionMajor >= 14 ? require('get-port-please') : { getPort: async () => 3000 };

const minimist = require('minimist');
const { createLogger, format, transports } = require('winston');

const setup = require('../src/setup/setup');
const start = require('../src/setup/start');

const argv = minimist(process.argv.slice(2));

const { combine, timestamp, prettyPrint } = format;
const logger = createLogger({
transports: [
new transports.Console({
format: combine(format.colorize(), format.simple()),
}),
new transports.File({
filename: 'logs/setup.log',
format: combine(
timestamp(),
prettyPrint(),
),
})],
});

async function main() {
argv.grafanaPort = argv.grafanaPort ?? await getPort({ portRange: [3000, 4000] });
argv.serverPort = argv.serverPort ?? 21025;

const cli = {
cmd: argv._.shift(),
args: argv,
logger,
};

switch (cli.cmd) {
case 'setup':
setup(cli);
break;

case 'start':
start(cli);
break;

case 'stop':
logger.info(`Stopping server from ${process.env.COMPOSE_FILE}`);
execSync('docker-compose stop');
break;

default:
logger.error(`expected command, got "${cli.cmd}"`);
break;
}
}

main();
15 changes: 0 additions & 15 deletions bin/setup.js

This file was deleted.

7 changes: 4 additions & 3 deletions docker-compose.example.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3.5"

volumes:
grafana_data:
graphite_data:
Expand All @@ -23,6 +21,7 @@ services:
- graphite_data:/opt/graphite/storage
networks:
- stats
logging: *default-logging
grafana:
image: grafana/grafana-oss:9.3.6-ubuntu
volumes:
Expand All @@ -38,6 +37,7 @@ services:
retries: 3
networks:
- stats
logging: *default-logging
stats-getter:
build:
context: .
Expand All @@ -51,4 +51,5 @@ services:
- SERVER_PORT=21025
- INCLUDE_PUSH_STATUS_API=false
networks:
- stats
- stats
logging: *default-logging
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"name": "screeps-grafana-go_carbon",
"version": "1.0.5",
"scripts": {
"setup": "node src/setup/setup.js setup",
"start": "node src/setup/index.js --grafanaPort=3000",
"start:test": "node src/setup/index.js --grafanaPort=3000 --force",
"setup": "node bin/server.js setup",
"start": "node bin/server.js start --grafanaPort=3000",
"start:test": "node bin/server.js start --grafanaPort=3000 --force",
"lint": "eslint src/**/*.js && eslint dashboards/**/*.js",
"lint:fix": "eslint src/**/* --fix && eslint dashboards/**/*.js --fix",
"lint:fix": "eslint src/**/*.js --fix && eslint dashboards/**/*.js --fix",
"update-stats-getter": "docker-compose up --detach --build"
},
"dependencies": {
Expand All @@ -23,7 +23,7 @@
"eslint-plugin-import": "^2.26.0"
},
"bin": {
"screeps-grafana-go_carbon": "./bin/setup.js"
"screeps-grafana-go_carbon": "./bin/server.js"
},
"files": [
"src",
Expand Down
2 changes: 1 addition & 1 deletion src/pushStats/apiFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export default class {
if (res) {
console.log(`Got memory from ${info.username} in ${shard} `);
} else {
return undefined
return undefined;
}

const data = await gz(res.data);
Expand Down
16 changes: 7 additions & 9 deletions src/pushStats/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ManageStats {
}
});

console.log(`[${type}] Getting ${getStatsFunctions.length} statistics`)
console.log(`[${type}] Getting ${getStatsFunctions.length} statistics`);

await Promise.all(getStatsFunctions);

Expand All @@ -96,7 +96,7 @@ class ManageStats {
if (Object.keys(groupedStats).length > 0) {
if (!await ManageStats.reportStats({ stats: groupedStats })) return console.log('Error while pushing stats');

console.log(`[${type}] Pushed stats to graphite`)
console.log(`[${type}] Pushed stats to graphite`);

return console.log(this.message);
}
Expand All @@ -106,13 +106,11 @@ class ManageStats {
}
if (type === 'season') {
if (Object.keys(groupedStats).length > 0) {

if (!await ManageStats.reportStats({ stats: groupedStats })) return console.log('Error while pushing stats');

console.log(`[${type}] Pushed stats to graphite`)
console.log(`[${type}] Pushed stats to graphite`);

return console.log(this.message);

}
if (beginningOfMinute) return console.log('No stats to push');
return undefined;
Expand All @@ -126,7 +124,7 @@ class ManageStats {
try {
const groupedAdminStatsUsers = {};
for (const [username, user] of Object.entries(adminUtilsServerStats)) {
groupedAdminStatsUsers[username] = user;
groupedAdminStatsUsers[username] = user;
}

adminUtilsServerStats.users = groupedAdminStatsUsers;
Expand Down Expand Up @@ -195,7 +193,7 @@ class ManageStats {

static async reportStats(stats) {
return new Promise((resolve) => {
console.log("Writing stats " + JSON.stringify(stats) + " to graphite");
console.log(`Writing stats ${JSON.stringify(stats)} to graphite`);
client.write({ [`${process.env.PREFIX ? `${process.env.PREFIX}.` : ''}screeps`]: stats }, (err) => {
if (err) {
console.log(err);
Expand All @@ -205,13 +203,13 @@ class ManageStats {
lastUpload = new Date().getTime();
resolve(true);
});
//resolve(true);
// resolve(true);
});
}

pushStats(userinfo, stats, shard) {
if (Object.keys(stats).length === 0) return;
let username = userinfo.replaceName !== undefined ? userinfo.replaceName : userinfo.username;
const username = userinfo.replaceName !== undefined ? userinfo.replaceName : userinfo.username;
this.groupedStats[(userinfo.prefix ? `${userinfo.prefix}.` : '') + username] = { [shard]: stats };

console.log(`Pushing stats for ${(userinfo.prefix ? `${userinfo.prefix}.` : '') + username} in ${shard}`);
Expand Down
80 changes: 0 additions & 80 deletions src/setup/index.js

This file was deleted.

Loading

0 comments on commit 0183be9

Please sign in to comment.