From e919906f9952a19e235659e47c1cf421552e0063 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Mon, 25 Jan 2016 17:55:48 +0100 Subject: [PATCH] Update time also for finished builds, but only every minute --- app/components/queued-jobs.js | 9 ++--- app/components/running-jobs.js | 8 ++--- app/controllers/repo.js | 24 ++++--------- app/controllers/repos.js | 14 ++++---- app/models/repo.js | 7 +++- app/services/update-times.js | 54 ++++++++++++++++++++++++++++++ app/utils/duration-calculations.js | 6 ++-- 7 files changed, 82 insertions(+), 40 deletions(-) create mode 100644 app/services/update-times.js diff --git a/app/components/queued-jobs.js b/app/components/queued-jobs.js index 633900f5..f0c7c03a 100644 --- a/app/components/queued-jobs.js +++ b/app/components/queued-jobs.js @@ -3,6 +3,8 @@ import config from 'travis/config/environment'; export default Ember.Component.extend({ store: Ember.inject.service(), + updateTimesService: Ember.inject.service('updateTimes'), + init() { this._super.apply(this, arguments); if (!Ember.testing) { @@ -11,11 +13,6 @@ export default Ember.Component.extend({ }, updateTimes() { - var jobs; - if (jobs = this.get('jobs')) { - return jobs.forEach(function(job) { - return job.updateTimes(); - }); - } + this.get('updateTimesService').push(this.get('jobs')); } }); diff --git a/app/components/running-jobs.js b/app/components/running-jobs.js index 68a8ebe5..8140faa1 100644 --- a/app/components/running-jobs.js +++ b/app/components/running-jobs.js @@ -4,6 +4,7 @@ import config from 'travis/config/environment'; export default Ember.Component.extend(Polling, { store: Ember.inject.service(), + updateTimesService: Ember.inject.service('updateTimes'), pollHook(store) { return this.get('store').find('job', {}); @@ -17,11 +18,6 @@ export default Ember.Component.extend(Polling, { }, updateTimes() { - var jobs; - if (jobs = this.get('jobs')) { - return jobs.forEach(function(job) { - return job.updateTimes(); - }); - } + this.get('updateTimesService').push(this.get('jobs')); } }); diff --git a/app/controllers/repo.js b/app/controllers/repo.js index 6208278b..db3710b2 100644 --- a/app/controllers/repo.js +++ b/app/controllers/repo.js @@ -4,6 +4,7 @@ import config from 'travis/config/environment'; export default Ember.Controller.extend({ + updateTimesService: Ember.inject.service('updateTimes'), popup: Ember.inject.service(), jobController: Ember.inject.controller('job'), @@ -49,27 +50,16 @@ export default Ember.Controller.extend({ init() { this._super.apply(this, arguments); if (!Ember.testing) { - return Visibility.every(this.config.intervals.updateTimes, this.updateTimes.bind(this)); + Visibility.every(this.config.intervals.updateTimes, this.updateTimes.bind(this)); } }, updateTimes() { - return Ember.run(this, function() { - var build, builds, jobs; - if (builds = this.get('builds')) { - builds.forEach(function(b) { - return b.updateTimes(); - }); - } - if (build = this.get('build')) { - build.updateTimes(); - } - if (build && (jobs = build.get('jobs'))) { - return jobs.forEach(function(j) { - return j.updateTimes(); - }); - } - }); + let updateTimesService = this.get('updateTimesService'); + + updateTimesService.push(this.get('build')); + updateTimesService.push(this.get('builds')); + updateTimesService.push(this.get('jobs')); }, deactivate() { diff --git a/app/controllers/repos.js b/app/controllers/repos.js index 71b64b70..0ce24511 100644 --- a/app/controllers/repos.js +++ b/app/controllers/repos.js @@ -51,6 +51,7 @@ var sortCallback = function(repo1, repo2) { var Controller = Ember.Controller.extend({ ajax: Ember.inject.service(), + updateTimesService: Ember.inject.service('updateTimes'), actions: { activate: function(name) { @@ -97,7 +98,7 @@ var Controller = Ember.Controller.extend({ init() { this._super.apply(this, arguments); if (!Ember.testing) { - return Visibility.every(this.config.intervals.updateTimes, this.updateTimes.bind(this)); + Visibility.every(this.config.intervals.updateTimes, this.updateTimes.bind(this)); } }, @@ -133,12 +134,13 @@ var Controller = Ember.Controller.extend({ }.property(), updateTimes() { - var repos; - if (repos = this.get('repos')) { - return repos.forEach(function(r) { - return r.updateTimes(); - }); + let records = this.get('repos'); + + if(Config.useV3API) { + let callback = (record) => { return record.get('lastBuild'); }; + records = records.filter(callback).map(callback); } + this.get('updateTimesService').push(records); }, activate(tab, params) { diff --git a/app/models/repo.js b/app/models/repo.js index 3bf30ce7..90cf1da3 100644 --- a/app/models/repo.js +++ b/app/models/repo.js @@ -55,7 +55,12 @@ if (Config.useV3API) { duration = durationFromHelper(this.get('lastBuildStartedAt'), this.get('lastBuildFinishedAt')); } return duration; - }.property('_lastBuildDuration', 'lastBuildStartedAt', 'lastBuildFinishedAt') + }.property('_lastBuildDuration', 'lastBuildStartedAt', 'lastBuildFinishedAt'), + + isFinished: function() { + let state = this.get('lastBuildState'); + return ['passed', 'failed', 'errored', 'canceled'].contains(state); + }.property('lastBuildState') }); } diff --git a/app/services/update-times.js b/app/services/update-times.js new file mode 100644 index 00000000..765fc387 --- /dev/null +++ b/app/services/update-times.js @@ -0,0 +1,54 @@ +import Ember from 'ember'; +import Config from 'travis/config/environment'; + +export default Ember.Service.extend({ + records: [], + allowFinishedBuilds: false, + + init() { + Visibility.every(Config.intervals.updateTimes, this.updateTimes.bind(this)); + setInterval(this.resetAllowFinishedBuilds.bind(this), 60000); + + return this._super(...arguments); + }, + + resetAllowFinishedBuilds() { + this.set('allowFinishedBuilds', true); + }, + + updateTimes() { + let records = this.get('records'); + + records.filter((record) => { + return this.get('allowFinishedBuilds') || !record.get('isFinished'); + }).forEach((record) => { + record.updateTimes(); + }); + + this.set('records', []); + + if(this.get('allowFinishedBuilds')) { + this.set('allowFinishedBuilds', false); + } + }, + + pushObject(record) { + let records = this.get('records'); + + if(!records.contains(record)) { + records.pushObject(record); + } + }, + + push(model) { + if(!model) { return; } + + if(model.forEach) { + model.forEach( (element) => { + this.pushObject(element); + }); + } else { + this.pushObject(model); + } + } +}); diff --git a/app/utils/duration-calculations.js b/app/utils/duration-calculations.js index d0e607ca..40aaff78 100644 --- a/app/utils/duration-calculations.js +++ b/app/utils/duration-calculations.js @@ -14,9 +14,7 @@ export default Ember.Mixin.create({ }.property('_duration', 'finishedAt', 'startedAt', 'notStarted', '_finishedAt', '_startedAt'), updateTimes() { - if (!this.get('isFinished')) { - this.notifyPropertyChange('duration'); - return this.notifyPropertyChange('finishedAt'); - } + this.notifyPropertyChange('duration'); + return this.notifyPropertyChange('finishedAt'); } });