Skip to content

Commit

Permalink
Implemented rpm counter
Browse files Browse the repository at this point in the history
  • Loading branch information
Th3Shadowbroker committed Dec 16, 2019
1 parent 3584f78 commit 8be1f83
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 86 deletions.
132 changes: 53 additions & 79 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 24 additions & 7 deletions svc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import log4js from 'log4js';
import {handleRequest, handleLegacyRequest, handleSummary, logRequest} from "./routing/routes";
import JsonConfiguration from "./util/JsonConfiguration";
import LaMetric from "./api/LaMetric";
import Stats from "./util/Stats";

// Prepare log
const log = log4js.getLogger(process.env.npm_package_name);
Expand Down Expand Up @@ -31,6 +32,10 @@ config.defaults({
});
config.save();

//Setup stat record
const stats = new Stats();
setInterval(() => stats.reset(), 60 * 1000);

//Prepare express
log.info('Initializing express using port ' + config.get('port') + '...');
const app = express();
Expand All @@ -40,12 +45,21 @@ app.use(logRequest);
app.get('/getEstimation', (req, res) => handleLegacyRequest(req, res));

//Current paths
app.get('/getEstimation/:timerName', (req,res) => handleRequest(req, res));
app.get('/getEstimations', (req,res) => handleSummary(req, res).then(r => res.json(r)).catch( reason => {
log.error(reason.message);
res.status = 500;
res.json(LaMetric.generateResponse('Unable to connect to the timer-api. LaMetric will try to reconnect automatically.','620'));
} ));
app.get('/getEstimation/:timerName', (req,res) => {
handleRequest(req, res);
stats.increase();
});
app.get('/getEstimations', (req,res) => handleSummary(req, res)
.then(r => {
res.json(r);
stats.increase();
})
.catch( reason => {
log.error(reason.message);
res.status = 500;
res.json(LaMetric.generateResponse('Unable to connect to the timer-api. LaMetric will try to reconnect automatically.','620'));
}
));

//Info resources
// Swagger stuff
Expand All @@ -59,8 +73,11 @@ app.get('/getEstimations', (req,res) => handleSummary(req, res).then(r => res.js
app.use('/issue', (req, res) => res.redirect(process.env.npm_package_bugs_url));
app.use('/issues', (req, res) => res.redirect(process.env.npm_package_bugs_url));

// Stats
app.use('/stats', (req, res) => res.json({rpm: stats.rpm, highest: stats.highest}));

// IMPORTANT: KEEP ALWAYS AS LAST ROUTE!!!
app.use('/', (req, res) => res.redirect('https://th3shadowbroker.github.io/Magma-Boss-Timer-LaMetric/'));
app.use('*', (req, res) => res.redirect('https://th3shadowbroker.github.io/Magma-Boss-Timer-LaMetric/'));

try
{
Expand Down
18 changes: 18 additions & 0 deletions svc/util/Stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Stats {

constructor() {
this.highest = 0;
this.rpm = 0;
}

increase() {
this.rpm++;
}

reset() {
if (this.rpm > this.highest) this.highest = this.rpm;
this.rpm = 0;
}

}
export default Stats;

0 comments on commit 8be1f83

Please sign in to comment.