Skip to content

Commit 158bcb1

Browse files
author
Furkan Yavuz
authored
Merge pull request #76 from open-template-hub/develop
Release/system info endpoint
2 parents c780b28 + 34e3bfc commit 158bcb1

9 files changed

Lines changed: 93 additions & 10 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
name: Cron System Info Updater Workflow
2+
3+
on:
4+
schedule:
5+
- cron: '0 5 * * *'
6+
7+
jobs:
8+
cron-system-info-updater:
9+
name: 'Cron System Info Updater'
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Run System Info Updater Endpoint
13+
run: |
14+
curl -H "Origin: https://opentemplatehub.com" -H "Authorization: Bearer ${{ secrets.ADMIN_AUTH_TOKEN }}" https://oth-server-orchestra-live.herokuapp.com/analytics/system-info/update?key=npm-downloads

app/controller/system-info.controller.ts

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import { Context } from '@open-template-hub/common';
66
import { Environment } from '../../environment';
77
import { SystemInfoRepository } from '../repository/system-info.repository';
8+
import axios from 'axios';
89

910
export class SystemInfoController {
1011

@@ -14,11 +15,40 @@ export class SystemInfoController {
1415
this.environment = new Environment();
1516
}
1617

17-
getNpmDownloads = async ( context: Context, key: any ) => {
18+
getSystemInfo = async ( context: Context, key: any ) => {
1819
const systemInfoRepository = await new SystemInfoRepository().initialize(
1920
context.mongodb_provider.getConnection()
2021
);
2122

22-
return systemInfoRepository.getNpmDownloads( key );
23+
return systemInfoRepository.getSystemInfo( key );
2324
};
25+
26+
updateSystemInfo = async ( context: Context, key: any ) => {
27+
const systemInfoRepository = await new SystemInfoRepository().initialize(
28+
context.mongodb_provider.getConnection()
29+
);
30+
31+
if ( key === 'npm-downloads' ) {
32+
const count = await this.getNpmPackagesDownloadCount();
33+
return systemInfoRepository.incrementSystemInfo( key, count );
34+
}
35+
};
36+
37+
getNpmPackagesDownloadCount = async () => {
38+
let count = 0;
39+
40+
for ( const npmPackage of this.NPM_PACKAGES ) {
41+
const url = `https://api.npmjs.org/downloads/point/last-day/@open-template-hub/${ npmPackage }`;
42+
console.info( url );
43+
44+
const response = await axios.get<any>( url );
45+
count += response.data.downloads;
46+
}
47+
48+
return count;
49+
};
50+
51+
NPM_PACKAGES: string[] = [
52+
'server-generator', 'app-generator', 'animated-code-editor', 'led', 'card', 'button', 'hero', 'ui-library-template', 'common'
53+
];
2454
}

app/repository/event-config.repository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ export class EventConfigRepository {
3636
}
3737
] );
3838

39-
let newMessagesArray: string[] = [];
4039
for ( const element of dataModel ) {
40+
let newMessagesArray: string[] = [];
4141
if ( element.messages?.length > 1 ) {
4242
for ( let message of element.messages ) {
4343
if ( message.language === language ) {

app/repository/event.repository.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class EventRepository {
4141
try {
4242
let queryResult = await this.dataModel.aggregate([
4343
{ $match: query },
44-
{ $sort: { timestamp: -1 } },
44+
{ $sort: { timestamp: -1 } },
4545
{ $facet: {
4646
data: [
4747
{ $match: { } },
@@ -50,7 +50,7 @@ export class EventRepository {
5050
],
5151
meta: [
5252
{ $count: "count" },
53-
{ $addFields: {
53+
{ $addFields: {
5454
skip: skip,
5555
limit: limit
5656
}}
@@ -66,7 +66,7 @@ export class EventRepository {
6666
}
6767

6868
return queryResult
69-
}
69+
}
7070
return { }
7171
} catch ( error ) {
7272
console.error( '> filterEvents error: ', error );

app/repository/system-info.repository.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,29 @@ export class SystemInfoRepository {
1313
return this;
1414
};
1515

16-
getNpmDownloads = async ( key: any ) => {
16+
getSystemInfo = async ( key: any ) => {
1717
try {
1818
return await this.dataModel.findOne( { key } );
1919
} catch ( error ) {
20-
console.error( '> getNpmDownloads error: ', error );
20+
console.error( '> getSystemInfo error: ', error );
21+
throw error;
22+
}
23+
};
24+
25+
updateSystemInfo = async ( key: any, value: any ) => {
26+
try {
27+
return await this.dataModel.findOneAndUpdate( { key }, { value } );
28+
} catch ( error ) {
29+
console.error( '> updateSystemInfo error: ', error );
30+
throw error;
31+
}
32+
};
33+
34+
incrementSystemInfo = async ( key: any, incrementBy: any ) => {
35+
try {
36+
return await this.dataModel.findOneAndUpdate( { key }, { $inc: { value: incrementBy } } );
37+
} catch ( error ) {
38+
console.error( '> incrementSystemInfo error: ', error );
2139
throw error;
2240
}
2341
};

app/route/system-info.route.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
*/
44

55
import {
6-
ResponseCode,
6+
authorizedBy,
7+
ResponseCode, UserRole,
78
} from '@open-template-hub/common';
89
import { Request, Response } from 'express';
910
import Router from 'express-promise-router';
1011
import { SystemInfoController } from '../controller/system-info.controller';
1112

1213
const subRoutes = {
1314
root: '/',
15+
update: '/update'
1416
};
1517

1618
export const router = Router();
@@ -19,7 +21,17 @@ router.get(
1921
subRoutes.root,
2022
async ( req: Request, res: Response ) => {
2123
const systemInfoController = new SystemInfoController();
22-
let response = await systemInfoController.getNpmDownloads( res.locals.ctx, req.query.key );
24+
let response = await systemInfoController.getSystemInfo( res.locals.ctx, req.query.key );
25+
res.status( ResponseCode.OK ).json( response.value );
26+
}
27+
);
28+
29+
router.get(
30+
subRoutes.update,
31+
authorizedBy( [ UserRole.ADMIN ] ),
32+
async ( req: Request, res: Response ) => {
33+
const systemInfoController = new SystemInfoController();
34+
let response = await systemInfoController.updateSystemInfo( res.locals.ctx, req.query.key );
2335
res.status( ResponseCode.OK ).json( response.value );
2436
}
2537
);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
### Get System Info
22
GET http://localhost:4005/system-info?key=npm-downloads
33
Content-Type: application/json
4+
5+
### Update System Info
6+
GET http://localhost:4005/system-info/update?key=npm-downloads
7+
Content-Type: application/json
8+
Authorization: Bearer token

docs/OUTDATED.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Open Template Hub - Analytics Server Template v4
1313

1414
Following packages are not updated in the develop branch yet. So, if you want to update outdated packages on your own risk, update the package.json and install dependencies.
1515

16+
| Package | Current | Wanted | Latest | Location |
17+
| --- | --- | --- | --- | --- |
18+
| axios | 0.27.2 | 0.27.2 | 1.1.3 | node_modules/axios |
1619

1720
<table align="right"><tr><td><a href="https://opentemplatehub.com"><img src="https://raw.githubusercontent.com/open-template-hub/open-template-hub.github.io/master/assets/logo/brand-logo.png" width="50px" alt="oth"/></a></td><td><b>Open Template Hub © 2021</b></td></tr></table>
1821

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@types/cors": "^2.8.12",
1515
"@types/express": "^4.17.13",
1616
"@types/mongoose": "^5.11.97",
17+
"axios": "^0.27.2",
1718
"body-parser": "^1.20.0",
1819
"cors": "^2.8.5",
1920
"dotenv": "^16.0.1",

0 commit comments

Comments
 (0)