11const geoip = require ( 'geoip-country' ) ;
22const db = require ( '../lib/db/mysql' ) ;
33const pterostatus = require ( '../lib/pterostatus' )
4- const DBDelay = Number ( process . env . CheckDelayInMS ) / 1000 ;
4+ const DBDelay = Number ( process . env . CheckDelayInMS ) / 1000 ;
55
6- let StatsCollector = function ( ) {
7- return new Promise ( function ( resolve , reject ) {
6+ let StatsCollector = function ( ) {
7+ return new Promise ( function ( resolve , reject ) {
88 const Promisees = [ db . GetAllUsers ( ) , db . GetAllServers ( ) , db . GetAllProducts ( ) , db . GetFailedJobsSinceLastCheck ( DBDelay ) ] ;
99
10- if ( process . env . PteroStatusURL ) {
10+ if ( process . env . PteroStatusURL ) {
1111 Promisees . push ( pterostatus . GetPteroStatusData ( ) )
1212 }
13-
13+
1414 try {
15- Promise . all ( Promisees ) . then ( function ( values ) {
15+ Promise . all ( Promisees ) . then ( function ( values ) {
1616 const Users = values [ 0 ] ;
1717 const Servers = values [ 1 ] ;
1818 const Products = values [ 2 ] ;
1919 const FailedJobs = values [ 3 ] ;
2020 let PteroStatusData = { } ;
21- if ( process . env . PteroStatusURL ) {
22- PteroStatusData = values [ values . length - 1 ] ;
21+ if ( process . env . PteroStatusURL ) {
22+ PteroStatusData = values [ values . length - 1 ] ;
2323 }
24-
24+
2525 let [ TotalCoins , TotalSuspendedUsers , TotalAllowedServers , TotalVerifiedUsers , TotalVerifiedDiscordUsers , TotalCPU , TotalRAM , TotalSWAP , TotalDisk , TotalAllocations , TotalDB , TotalServerCostMonthly , ServerSuspended ] = [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ] ;
2626 let UserCountrys = [ ] ;
2727 let ServerProducts = [ ] ;
2828
29- Users . map ( function ( User ) {
30- if ( User . suspended === 0 ) {
31- TotalCoins += User . credits ;
29+ Users . map ( function ( User ) {
30+ if ( User . suspended === 0 ) {
31+ if ( process . env . ExcludeAdminCoins !== 'true' || User . role !== "admin" ) {
32+ TotalCoins += User . credits ;
33+ }
3234 TotalAllowedServers += User . server_limit ;
3335
34- if ( User . email_verified_at !== null ) {
36+ if ( User . email_verified_at !== null ) {
3537 TotalVerifiedUsers += 1 ;
3638 }
3739
38- if ( User . discord_verified_at !== null ) {
40+ if ( User . discord_verified_at !== null ) {
3941 TotalVerifiedDiscordUsers += 1 ;
4042 }
4143
42- if ( User . ip !== null && geoip . lookup ( User . ip ) ) {
44+ if ( User . ip !== null && geoip . lookup ( User . ip ) ) {
4345 UserCountrys . push ( geoip . lookup ( User . ip ) . country ) ;
44- } else {
45- UserCountrys . push ( "Local" ) ;
46+ } else {
47+ UserCountrys . push ( "Local" ) ;
4648 }
4749
48- } else {
50+ } else {
4951 TotalSuspendedUsers += 1 ;
5052 }
5153 } ) ;
5254
53- Servers . map ( function ( Server ) {
54- if ( Server . suspended === null ) {
55- Products . map ( function ( Product ) {
56- if ( Server . product_id === Product . id ) {
55+ Servers . map ( function ( Server ) {
56+ if ( Server . suspended === null ) {
57+ Products . map ( function ( Product ) {
58+ if ( Server . product_id === Product . id ) {
5759 TotalCPU += Product . cpu ;
5860 TotalRAM += Product . memory ;
5961 TotalSWAP += Product . swap ;
@@ -62,13 +64,13 @@ let StatsCollector = function() {
6264 TotalAllocations += Product . allocations ;
6365 TotalServerCostMonthly += Product . price ;
6466 ServerProducts . push ( Product . name ) ;
65- }
67+ }
6668 } )
67- } else {
69+ } else {
6870 ServerSuspended += 1 ;
6971 }
7072 } ) ;
71-
73+
7274 const UserCountrysObject = UserCountrys . reduce ( function ( acc , curr ) {
7375 return acc [ curr ] ? ++ acc [ curr ] : acc [ curr ] = 1 , acc
7476 } , { } )
@@ -107,14 +109,18 @@ let StatsCollector = function() {
107109 Failed : FailedJobs [ 0 ] [ 'COUNT(*)' ] ,
108110 }
109111 }
110-
111- if ( process . env . PteroStatusURL ) {
112+
113+ if ( process . env . PteroStatusURL ) {
112114 ExportStats [ "PteroStatus" ] = PteroStatusData ;
113115 }
114-
116+
115117 resolve ( ExportStats ) ;
116- } ) . catch ( function ( err ) {
117- console . log ( err ) ;
118+ } ) . catch ( function ( err ) {
119+ if ( err . response . status === 403 ) {
120+ console . log ( "PteroStatus API Key is invalid" ) ;
121+ } else {
122+ console . log ( err ) ;
123+ }
118124 } ) ;
119125 } catch ( error ) {
120126 reject ( error ) ;
0 commit comments