implemented amplitude metrics (#1141)

This commit is contained in:
Danny Coates 2019-02-12 11:50:06 -08:00 committed by GitHub
parent 1a483cad55
commit 9b37e92a81
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 774 additions and 528 deletions

View file

@ -1,4 +1,4 @@
/* global DEFAULTS LIMITS */
/* global LIMITS */
import FileSender from './fileSender';
import FileReceiver from './fileReceiver';
import { copyToClipboard, delay, openLinksInNewTab, percent } from './utils';
@ -50,37 +50,27 @@ export default function(state, emitter) {
emitter.on('logout', () => {
state.user.logout();
state.timeLimit = DEFAULTS.EXPIRE_SECONDS;
state.downloadCount = 1;
metrics.loggedOut({ trigger: 'button' });
emitter.emit('pushState', '/');
});
emitter.on('changeLimit', async ({ file, value }) => {
const ok = await file.changeLimit(value, state.user);
if (!ok) {
return;
}
state.storage.writeFile(file);
metrics.changedDownloadLimit(file);
});
emitter.on('removeUpload', file => {
state.archive.remove(file);
render();
});
emitter.on('delete', async ({ file, location }) => {
emitter.on('delete', async ownedFile => {
try {
metrics.deletedUpload({
size: file.size,
time: file.time,
speed: file.speed,
type: file.type,
ttl: file.expiresAt - Date.now(),
size: ownedFile.size,
time: ownedFile.time,
speed: ownedFile.speed,
type: ownedFile.type,
ttl: ownedFile.expiresAt - Date.now(),
location
});
state.storage.remove(file.id);
await file.del();
state.storage.remove(ownedFile.id);
await ownedFile.del();
} catch (e) {
state.raven.captureException(e);
}
@ -100,20 +90,35 @@ export default function(state, emitter) {
state.archive.addFiles(files, maxSize);
} catch (e) {
if (e.message === 'fileTooBig' && maxSize < LIMITS.MAX_FILE_SIZE) {
state.modal = signupDialog();
} else {
state.modal = okDialog(
state.translate(e.message, {
size: bytes(maxSize),
count: LIMITS.MAX_FILES_PER_ARCHIVE
})
);
return emitter.emit('signup-cta', 'size');
}
state.modal = okDialog(
state.translate(e.message, {
size: bytes(maxSize),
count: LIMITS.MAX_FILES_PER_ARCHIVE
})
);
}
render();
});
emitter.on('upload', async ({ type, dlimit, password }) => {
emitter.on('signup-cta', source => {
state.modal = signupDialog(source);
render();
});
emitter.on('authenticate', async (code, oauthState) => {
try {
await state.user.finishLogin(code, oauthState);
await state.user.syncFileList();
emitter.emit('replaceState', '/');
} catch (e) {
emitter.emit('replaceState', '/error');
setTimeout(render);
}
});
emitter.on('upload', async () => {
if (state.storage.files.length >= LIMITS.MAX_ARCHIVES_PER_USER) {
state.modal = okDialog(
state.translate('tooManyArchives', {
@ -122,8 +127,7 @@ export default function(state, emitter) {
);
return render();
}
const size = state.archive.size;
if (!state.timeLimit) state.timeLimit = DEFAULTS.EXPIRE_SECONDS;
const archive = state.archive;
const sender = new FileSender();
sender.on('progress', updateProgress);
@ -135,41 +139,38 @@ export default function(state, emitter) {
const links = openLinksInNewTab();
await delay(200);
const start = Date.now();
try {
metrics.startedUpload({ size, type });
const ownedFile = await sender.upload(
state.archive,
state.timeLimit,
dlimit,
state.user.bearerToken
);
ownedFile.type = type;
const ownedFile = await sender.upload(archive, state.user.bearerToken);
state.storage.totalUploads += 1;
metrics.completedUpload(ownedFile);
const duration = Date.now() - start;
metrics.completedUpload(archive, duration);
state.storage.addFile(ownedFile);
// TODO integrate password into /upload request
if (password) {
emitter.emit('password', { password, file: ownedFile });
if (archive.password) {
emitter.emit('password', {
password: archive.password,
file: ownedFile
});
}
state.modal = copyDialog(ownedFile.name, ownedFile.url);
} catch (err) {
if (err.message === '0') {
//cancelled. do nothing
metrics.cancelledUpload({ size, type });
const duration = Date.now() - start;
metrics.cancelledUpload(archive, duration);
render();
} else {
// eslint-disable-next-line no-console
console.error(err);
state.raven.captureException(err);
metrics.stoppedUpload({ size, type, err });
metrics.stoppedUpload(archive);
emitter.emit('pushState', '/error');
}
} finally {
openLinksInNewTab(links, false);
state.archive.clear();
state.password = '';
archive.clear();
state.uploading = false;
state.transfer = null;
await state.user.syncFileList();
@ -183,7 +184,6 @@ export default function(state, emitter) {
render();
await file.setPassword(password);
state.storage.writeFile(file);
metrics.addedPassword({ size: file.size });
await delay(1000);
} catch (err) {
// eslint-disable-next-line no-console
@ -220,18 +220,20 @@ export default function(state, emitter) {
state.transfer.on('complete', render);
const links = openLinksInNewTab();
const size = file.size;
const start = Date.now();
try {
const start = Date.now();
metrics.startedDownload({ size: file.size, ttl: file.ttl });
const dl = state.transfer.download({
stream: state.capabilities.streamDownload
});
render();
await dl;
const time = Date.now() - start;
const speed = size / (time / 1000);
state.storage.totalDownloads += 1;
metrics.completedDownload({ size, time, speed });
const duration = Date.now() - start;
metrics.completedDownload({
size,
duration,
password_protected: file.requiresPassword
});
} catch (err) {
if (err.message === '0') {
// download cancelled
@ -239,12 +241,16 @@ export default function(state, emitter) {
render();
} else {
// eslint-disable-next-line no-console
console.error(err);
state.transfer = null;
const location = err.message === '404' ? '/404' : '/error';
if (location === '/error') {
state.raven.captureException(err);
metrics.stoppedDownload({ size, err });
const duration = Date.now() - start;
metrics.stoppedDownload({
size,
duration,
password_protected: file.requiresPassword
});
}
emitter.emit('pushState', location);
}
@ -253,9 +259,9 @@ export default function(state, emitter) {
}
});
emitter.on('copy', ({ url, location }) => {
emitter.on('copy', ({ url }) => {
copyToClipboard(url);
metrics.copiedLink({ location });
// metrics.copiedLink({ location });
});
setInterval(() => {