Skip to content

Commit

Permalink
added new api endpoint Statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
o0shojo0o committed Nov 2, 2023
1 parent 8f9d203 commit 5129092
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 19 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,19 @@ pixelit_api:
SEQ_SERVER: http://seqserver:5341
SEQ_APIKEY: xxxxxxxxxxxx
```
<!--
### **WORK IN PROGRESS**
-->
## Develop
Install dependencies with `npm install` and run dev server with `npn run dev`.

## Changelog

### **WORK IN PROGRESS**

- (o0shojo0o) added new api endpoint `Statistics`

### 1.6.0 (2023-10-27)

- (o0shojo0o) added `buildSection` for endpoint `Telemetry`
Expand Down
58 changes: 40 additions & 18 deletions libs/pixelItRepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
const mysql = require('mysql2/promise');
const tools = require('./tools')
const log = require('./logger')
const countries = require("i18n-iso-countries");
countries.registerLocale(require("i18n-iso-countries/langs/en.json"));

const connection = mysql.createPool({
host: process.env.MYSQL_HOST,
Expand Down Expand Up @@ -126,23 +128,6 @@ async function saveStats(telemetry) {
}
};

async function getUserMapData() {
let sqlResult
const result = []
try {
sqlResult = await connection.query(`select JSON_EXTRACT(geoip, '$.ll') as coords from pixel_it_telemetry where last_change >= CURRENT_DATE - INTERVAL 30 DAY`)

for (const x of sqlResult[0]) {
result.push(x.coords)
}

return result
} catch (error) {
log.error('getUserMapData: {error}', { error: error })
return null
}
};

async function saveBMP(bmp) {
try {
bmp.userID = await getUserIDByName(bmp.userName);
Expand Down Expand Up @@ -182,11 +167,48 @@ async function saveBMP(bmp) {
}
}

async function getUserMapData() {
let sqlResult
const result = [];
try {
sqlResult = await connection.query(`select JSON_EXTRACT(geoip, '$.ll') as coords from pixel_it_telemetry where last_change >= CURRENT_DATE - INTERVAL 30 DAY`);

for (const x of sqlResult[0]) {
result.push(x.coords)
}

return result
} catch (error) {
log.error('getUserMapData: {error}', { error: error })
return null
}
};


async function getStatistics() {
const result = {};
try {
result.buildStats = (await connection.query(`SELECT DISTINCT(IF(build_section = '','No_Data',build_section)) AS build, COUNT(*) AS count FROM pixel_it_telemetry where last_change >= CURRENT_DATE - INTERVAL 30 DAY GROUP BY build_section`))[0];
result.versionStats = (await connection.query(`SELECT DISTINCT(version) AS version, COUNT(*) AS count FROM pixel_it_telemetry where last_change >= CURRENT_DATE - INTERVAL 30 DAY GROUP BY version`))[0];
result.countryStats = (await connection.query(`SELECT DISTINCT(JSON_EXTRACT(geoip, '$.country')) AS country, COUNT(*) AS count FROM pixel_it_telemetry where last_change >= CURRENT_DATE - INTERVAL 30 DAY GROUP BY JSON_EXTRACT(geoip, '$.country')`))[0];

for (const countryStat of result.countryStats) {
countryStat.country = countries.getName(countryStat.country, 'en', { select: 'official' });
}

return result
} catch (error) {
log.error('getUserMapData: {error}', { error: error })
return null
}
};

module.exports = {
getBMPByID,
getBMPAll,
getBMPNewst,
getUserMapData,
saveStats,
saveBMP,
getUserMapData,
getStatistics,
};
9 changes: 9 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ app.get('/api/UserMapData', async (req, res) => {
res.send(userMapData);
});

app.get('/api/Statistics', async (req, res) => {
const sourceIP = tools.getIPFromRequest(req);
const rawUrl = tools.getRawURLFromRequest(req);
const statistics = (await cache.getOrSet('Statistics', () => { return repo.getStatistics() }, 30)) ?? {};

log.info('{apiPath}: Statistics successfully delivered', { apiPath: 'Statistics', sourceIP, rawUrl, useragent: req.useragent, rateLimit: req.rateLimit, });
res.send(statistics);
});

app.get('/api/LastVersion', async (req, res) => {
const sourceIP = tools.getIPFromRequest(req);
const rawUrl = tools.getRawURLFromRequest(req);
Expand Down
30 changes: 30 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"express-useragent": "^1.0.15",
"fast-geoip": "^1.1.88",
"helmet": "^7.0.0",
"i18n-iso-countries": "^7.7.0",
"mysql2": "^3.6.2",
"node-cache": "^5.1.2",
"seq-logging": "^2.1.1"
Expand Down

0 comments on commit 5129092

Please sign in to comment.