|
|
exports.up = function(r, conn) { |
|
|
return Promise.all([ |
|
|
r |
|
|
.tableCreate('coreMetrics') |
|
|
.run(conn) |
|
|
.catch(err => { |
|
|
console.log(err); |
|
|
throw err; |
|
|
}), |
|
|
]) |
|
|
.then(() => { |
|
|
return r |
|
|
.table('coreMetrics') |
|
|
.indexCreate('date', r.row('date')) |
|
|
.run(conn) |
|
|
.catch(err => { |
|
|
console.log(err); |
|
|
throw err; |
|
|
}); |
|
|
}) |
|
|
.then(() => { |
|
|
const saveCoreMetrics = (data, index) => { |
|
|
const d = new Date(); |
|
|
d.setDate(d.getDate() - index); |
|
|
return r |
|
|
.table('coreMetrics') |
|
|
.insert({ |
|
|
date: d, |
|
|
...data, |
|
|
}) |
|
|
.run(conn); |
|
|
}; |
|
|
|
|
|
const getCount = (table, filter, index, field) => { |
|
|
if (filter) { |
|
|
return r |
|
|
.table(table) |
|
|
.filter(filter) |
|
|
.filter( |
|
|
r.row(field).during( |
|
|
|
|
|
r.now().sub(60 * 60 * 24 * 10000), |
|
|
r.now().sub(60 * 60 * 24 * index) |
|
|
) |
|
|
) |
|
|
.filter(row => r.not(row.hasFields('deletedAt'))) |
|
|
.count() |
|
|
.run(conn); |
|
|
} |
|
|
|
|
|
return r |
|
|
.table(table) |
|
|
.filter( |
|
|
r.row(field).during( |
|
|
|
|
|
r.now().sub(60 * 60 * 24 * 10000), |
|
|
r.now().sub(60 * 60 * 24 * index) |
|
|
) |
|
|
) |
|
|
.filter(row => r.not(row.hasFields('deletedAt'))) |
|
|
.count() |
|
|
.run(conn); |
|
|
}; |
|
|
|
|
|
|
|
|
const getPu = (table, field, index) => { |
|
|
|
|
|
return getCount('users', null, index, 'createdAt') |
|
|
.then(userCount => { |
|
|
|
|
|
const tableCount = r |
|
|
.table(table) |
|
|
.filter( |
|
|
r.row(field).during( |
|
|
|
|
|
r.now().sub(60 * 60 * 24 * 10000), |
|
|
r.now().sub(60 * 60 * 24 * index) |
|
|
) |
|
|
) |
|
|
.filter(row => r.not(row.hasFields('deletedAt'))) |
|
|
.count() |
|
|
.run(conn); |
|
|
|
|
|
return Promise.all([userCount, tableCount]); |
|
|
}) |
|
|
.then(([userCount, tableCount]) => { |
|
|
return parseFloat((tableCount / userCount).toFixed(3)); |
|
|
}); |
|
|
}; |
|
|
|
|
|
|
|
|
const arr = Array.apply(null, { length: 60 }).map(() => 0); |
|
|
const backfill = arr.map((o, i) => { |
|
|
const cpu = getPu('usersCommunities', 'createdAt', i); |
|
|
const mpu = getPu('messages', 'timestamp', i); |
|
|
const tpu = getPu('threads', 'createdAt', i); |
|
|
const users = getCount('users', null, i, 'createdAt'); |
|
|
const communities = getCount('communities', null, i, 'createdAt'); |
|
|
const threads = getCount('threads', null, i, 'createdAt'); |
|
|
const dmThreads = getCount( |
|
|
'directMessageThreads', |
|
|
null, |
|
|
i, |
|
|
'createdAt' |
|
|
); |
|
|
const threadMessages = getCount( |
|
|
'messages', |
|
|
{ threadType: 'story' }, |
|
|
i, |
|
|
'timestamp' |
|
|
); |
|
|
const dmMessages = getCount( |
|
|
'messages', |
|
|
{ |
|
|
threadType: 'directMessageThread', |
|
|
}, |
|
|
i, |
|
|
'timestamp' |
|
|
); |
|
|
|
|
|
return Promise.all([ |
|
|
cpu, |
|
|
mpu, |
|
|
tpu, |
|
|
users, |
|
|
communities, |
|
|
threads, |
|
|
dmThreads, |
|
|
threadMessages, |
|
|
dmMessages, |
|
|
]).then( |
|
|
([ |
|
|
cpu, |
|
|
mpu, |
|
|
tpu, |
|
|
users, |
|
|
communities, |
|
|
threads, |
|
|
dmThreads, |
|
|
threadMessages, |
|
|
dmMessages, |
|
|
]) => { |
|
|
const coreMetrics = { |
|
|
cpu: cpu || 0, |
|
|
mpu: mpu || 0, |
|
|
tpu: tpu || 0, |
|
|
users: users || 0, |
|
|
communities: communities || 0, |
|
|
threads: threads || 0, |
|
|
dmThreads: dmThreads || 0, |
|
|
threadMessages: threadMessages || 0, |
|
|
dmMessages: dmMessages || 0, |
|
|
}; |
|
|
return saveCoreMetrics(coreMetrics, i); |
|
|
} |
|
|
); |
|
|
}); |
|
|
|
|
|
return Promise.all(backfill); |
|
|
}) |
|
|
.catch(err => { |
|
|
console.log(err); |
|
|
throw err; |
|
|
}); |
|
|
}; |
|
|
|
|
|
exports.down = function(r, conn) { |
|
|
return Promise.all([r.tableDrop('coreMetrics').run(conn)]); |
|
|
}; |
|
|
|