File size: 4,690 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
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(
// go back to all time, for some reason minval isn't working
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(
// go back to all time, for some reason minval isn't working
r.now().sub(60 * 60 * 24 * 10000),
r.now().sub(60 * 60 * 24 * index)
)
)
.filter(row => r.not(row.hasFields('deletedAt')))
.count()
.run(conn);
};
// cpu, tpu, mpu
const getPu = (table, field, index) => {
// get the count of the users within a given range
return getCount('users', null, index, 'createdAt')
.then(userCount => {
// get the count of whatever entity within a given range
const tableCount = r
.table(table)
.filter(
r.row(field).during(
// go back to all time, for some reason minval isn't working
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));
});
};
// array of 30 things
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)]);
};
|