improve sorting or builds by grouping them by commit

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2019-01-15 20:09:26 +01:00
parent 785ea911a3
commit ab488cb9dd
No known key found for this signature in database
GPG Key ID: 4EF064D0E6D63020
4 changed files with 92 additions and 5 deletions

View File

@ -15,12 +15,12 @@ gulp.task('devel', () => {
gulp.task('test', () =>
gulp.src('test/**/*.js', {read: false})
.pipe(mocha({reporter: 'list'}))
.pipe(mocha({sort: true, reporter: 'list'}))
)
gulp.task('testw', () =>
gulp.src('test/**/*.js', {read: false})
.pipe(mocha({reporter: 'list', watch: true}))
.pipe(mocha({sort: true, reporter: 'list', watch: true}))
)
gulp.task('build', ['test'])

View File

@ -32,7 +32,8 @@
"nyc": "^13.1.0",
"sinon": "^7.1.1",
"sinon-chai": "^3.3.0",
"supertest": "^3.3.0"
"supertest": "^3.3.0",
"tmp": "^0.0.33"
},
"scripts": {
"start": "node server.js",

View File

@ -37,11 +37,33 @@ class Builds {
})
}
/* This sorts so that builds are shown grouped by commit,
* but still in chronological order based on $loki. */
sortBuildsByCommit (builds) {
let bc = {}, b
/* put builds under commit keys */
for (let i=0; i<builds.length; i++) {
b = builds[i]
bc[b.commit] || (bc[b.commit] = [])
bc[b.commit].push(b)
}
bc = Object.values(bc)
bc.sort((o1, o2) => {
let o1_min = Math.min(o1.map((o) => o.$loki))
let o2_min = Math.min(o2.map((o) => o.$loki))
return o1_min - o2_min
})
/* flatten */
return [].concat.apply([], bc)
}
async getBuilds (pr) {
const builds = await this.builds.chain()
let builds = await this.builds.chain()
.find({pr})
.compoundsort(['$loki', 'id'])
.compoundsort(['id', '$loki'])
.data()
/* sort groups of builds for commit based on $loki */
builds = this.sortBuildsByCommit(builds)
/* strip the $loki attribute */
return builds.map((b) => {
const {$loki, ...build} = b

64
test/builds.js Normal file
View File

@ -0,0 +1,64 @@
const tmp = require('tmp')
const expect = require('chai').expect
const sinon = require('sinon')
const sample = require('./sample')
const Builds = require('../src/builds')
tmp.setGracefulCleanup()
const BUILDS = [
{...sample.BUILD, id: '#1', commit: 'abcd1234', platform: 'macos'},
{...sample.BUILD, id: '#2', commit: '1234abcd', platform: 'macos'},
{...sample.BUILD, id: '#1', commit: 'abcd1234', platform: 'linux'},
{...sample.BUILD, id: '#2', commit: 'abcd1234', platform: 'linux'},
{...sample.BUILD, id: '#3', commit: '1234abcd', platform: 'linux'},
{...sample.BUILD, id: '#1', commit: 'abcd1234', platform: 'windows'},
{...sample.BUILD, id: '#2', commit: '1234abcd', platform: 'windows'},
]
let builds, db
describe('Builds', () => {
before(() => {
db = tmp.fileSync({keep: false})
builds = new Builds(db.name, 999)
builds.initDB()
})
after(() => {
builds.db.close()
db.removeCallback()
})
describe('getBuilds', () => {
before(async () => {
/* need to add the builds before they can be sorted */
for (let i=0; i<BUILDS.length; i++) {
let b = BUILDS[i]
await builds.addBuild('PR-1', b)
/* verify the build was added */
let rval = await builds.builds.findOne({id: b.id, platform: b.platform})
expect(rval.commit).to.equal(BUILDS[i].commit)
}
})
it('should sort by commits and ids', async () => {
let rval = await builds.getBuilds('PR-1')
/* remove fields we don't care about for easier comparison */
rval = rval.map((b) => {
const { pr, success, duration, url, pkg_url, meta, ...build } = b
return build
})
expect(rval).to.deep.equal([
{id: '#1', commit: 'abcd1234', platform: 'macos'},
{id: '#1', commit: 'abcd1234', platform: 'linux'},
{id: '#1', commit: 'abcd1234', platform: 'windows'},
{id: '#2', commit: 'abcd1234', platform: 'linux'},
{id: '#2', commit: '1234abcd', platform: 'macos'},
{id: '#2', commit: '1234abcd', platform: 'windows'},
{id: '#3', commit: '1234abcd', platform: 'linux'}
])
})
})
})