@@ -235,7 +235,13 @@ proto.reportStatusDownload = function(deploymentKey, label, clientUniqueId) {
235235 return this . getPackagesInfo ( deploymentKey , label )
236236 . then ( ( packages ) => {
237237 return Promise . all ( [
238- models . PackagesMetrics . addOneOnDownloadById ( packages . id ) ,
238+ models . PackagesMetrics . findOne ( { where : { package_id : packages . id } } )
239+ . then ( ( metrics ) => {
240+ if ( metrics ) {
241+ return metrics . increment ( 'downloaded' ) ;
242+ }
243+ return ;
244+ } ) ,
239245 models . LogReportDownload . create ( {
240246 package_id : packages . id ,
241247 client_unique_id : clientUniqueId
@@ -248,40 +254,62 @@ proto.reportStatusDeploy = function (deploymentKey, label, clientUniqueId, other
248254 return this . getPackagesInfo ( deploymentKey , label )
249255 . then ( ( packages ) => {
250256 var constConfig = require ( '../const' ) ;
251- var status = _ . get ( others , "status" ) ;
257+ var statusText = _ . get ( others , "status" ) ;
258+ var status = 0 ;
259+ if ( _ . eq ( statusText , "DeploymentSucceeded" ) ) {
260+ status = constConfig . DEPLOYMENT_SUCCEEDED ;
261+ } else if ( _ . eq ( statusText , "DeploymentFailed" ) ) {
262+ status = constConfig . DEPLOYMENT_FAILED ;
263+ }
252264 var packageId = packages . id ;
253265 var previous_deployment_key = _ . get ( others , 'previousDeploymentKey' ) ;
254266 var previous_label = _ . get ( others , 'previousLabelOrAppVersion' ) ;
255- if ( _ . eq ( status , "DeploymentSucceeded" ) ) {
256- return Promise . all ( [
257- models . LogReportDeploy . create ( {
258- package_id : packageId ,
259- client_unique_id : clientUniqueId ,
260- previous_label : previous_label ,
261- previous_deployment_key : previous_deployment_key ,
262- status : constConfig . DEPLOYMENT_SUCCEEDED
263- } )
264- . then ( ( ) => {
265- if ( previous_deployment_key && previous_label ) {
266-
267- }
268- } ) ,
269- models . PackagesMetrics . addOneOnInstalledById ( packageId ) ,
270- models . PackagesMetrics . addOneOnActiveById ( packageId ) ,
271- ] ) ;
272- } else if ( _ . eq ( status , "DeploymentFailed" ) ) {
267+ if ( status > 0 ) {
273268 return Promise . all ( [
274269 models . LogReportDeploy . create ( {
275270 package_id : packageId ,
276271 client_unique_id : clientUniqueId ,
277272 previous_label : previous_label ,
278273 previous_deployment_key : previous_deployment_key ,
279- status : constConfig . DEPLOYMENT_FAILED
274+ status : status
280275 } ) ,
281- models . PackagesMetrics . addOneOnInstalledById ( packageId ) ,
282- models . PackagesMetrics . addOneOnFailedById ( packageId ) ,
283- ] ) ;
284- } else {
276+ models . PackagesMetrics . findOne ( { where : { package_id : packageId } } )
277+ . then ( ( metrics ) => {
278+ if ( _ . isEmpty ( metrics ) ) {
279+ return ;
280+ }
281+ if ( constConfig . DEPLOYMENT_SUCCEEDED ) {
282+ return metrics . increment ( [ 'installed' , 'active' ] , { by : 1 } ) ;
283+ } else {
284+ return metrics . increment ( [ 'installed' , 'failed' ] , { by : 1 } ) ;
285+ }
286+ } )
287+ ] )
288+ . then ( ( ) => {
289+ if ( previous_deployment_key && previous_label ) {
290+ return models . Deployments . findOne ( { where : { deployment_key : previous_deployment_key } } )
291+ . then ( ( dep ) => {
292+ if ( _ . isEmpty ( dep ) ) {
293+ return ;
294+ }
295+ return models . Packages . findOne ( { where : { deployment_id : dep . id , label : previous_label } } )
296+ . then ( ( p ) => {
297+ if ( _ . isEmpty ( p ) ) {
298+ return ;
299+ }
300+ return models . PackagesMetrics . findOne ( { where :{ package_id : p . id } } ) ;
301+ } ) ;
302+ } )
303+ . then ( ( metrics ) => {
304+ if ( metrics ) {
305+ return metrics . decrement ( 'active' ) ;
306+ }
307+ return ;
308+ } ) ;
309+ }
310+ return ;
311+ } ) ;
312+ } else {
285313 return ;
286314 }
287315 } ) ;
0 commit comments