Merge pull request #1835 from overleaf/hb-analytics-template-ids

Set template ids on cloned projects on creation

GitOrigin-RevId: 6b7ef72d148774c3d5ea7b65cde0e6a8fdfa2ceb
This commit is contained in:
Timothée Alby 2019-06-04 13:08:04 +02:00 committed by sharelatex
parent f9e82003b3
commit 8330c6ae61
6 changed files with 58 additions and 11 deletions

View file

@ -38,7 +38,7 @@ module.exports = ProjectCreationHandler = {
metrics.inc('project-creation')
if (arguments.length === 3) {
callback = attributes
attributes = null
attributes = {}
}
return ProjectDetailsHandler.validateProjectName(projectName, function(
@ -48,7 +48,7 @@ module.exports = ProjectCreationHandler = {
return callback(error)
}
logger.log({ owner_id, projectName }, 'creating blank project')
if (attributes != null) {
if (attributes.overleaf !== undefined && attributes.overleaf != null) {
return ProjectCreationHandler._createBlankProject(
owner_id,
projectName,
@ -69,10 +69,8 @@ module.exports = ProjectCreationHandler = {
if (error != null) {
return callback(error)
}
attributes = {
overleaf: {
history: { id: history != null ? history.overleaf_id : undefined }
}
attributes.overleaf = {
history: { id: history != null ? history.overleaf_id : undefined }
}
return ProjectCreationHandler._createBlankProject(
owner_id,
@ -83,7 +81,8 @@ module.exports = ProjectCreationHandler = {
return callback(error)
}
AnalyticsManger.recordEvent(owner_id, 'project-created', {
projectId: project._id
projectId: project._id,
attributes
})
return callback(error, project)
}

View file

@ -57,6 +57,10 @@ module.exports = TemplatesManager = {
const projectName = ProjectDetailsHandler.fixProjectName(templateName)
const dumpPath = `${settings.path.dumpFolder}/${uuid.v4()}`
const writeStream = fs.createWriteStream(dumpPath)
const attributes = {
fromV1TemplateId: templateId,
fromV1TemplateVersionId: templateVersionId
}
writeStream.on('close', function() {
if (zipReq.response.statusCode !== 200) {
logger.err(
@ -69,6 +73,7 @@ module.exports = TemplatesManager = {
user_id,
projectName,
dumpPath,
attributes,
function(err, project) {
if (err != null) {
logger.err({ err, zipReq }, 'problem building project from zip')

View file

@ -89,11 +89,16 @@ module.exports = ProjectUploadHandler = {
owner_id,
proposedName,
zipPath,
attributes,
callback
) {
if (callback == null) {
callback = function(error, project) {}
}
if (arguments.length === 4) {
callback = attributes
attributes = {}
}
return ProjectDetailsHandler.generateUniqueName(
owner_id,
ProjectDetailsHandler.fixProjectName(proposedName),
@ -104,6 +109,7 @@ module.exports = ProjectUploadHandler = {
return ProjectCreationHandler.createBlankProject(
owner_id,
name,
attributes,
(error, project) => {
if (error != null) {
return callback(error)

View file

@ -264,11 +264,40 @@ describe('ProjectCreationHandler', function() {
)
})
return it('should send a project-imported event when importing a project', function(done) {
it('should send a project-created event with template information if provided', function(done) {
const attributes = {
fromV1TemplateId: 100
}
return this.handler.createBlankProject(
ownerId,
projectName,
1234,
attributes,
(err, project) => {
expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
expect(
this.AnalyticsManager.recordEvent.calledWith(
ownerId,
'project-created',
{ projectId: project._id, attributes }
)
).to.equal(true)
return done()
}
)
})
return it('should send a project-imported event when importing a project', function(done) {
const attributes = {
overleaf: {
history: {
id: 100
}
}
}
return this.handler.createBlankProject(
ownerId,
projectName,
attributes,
(err, project) => {
expect(this.AnalyticsManager.recordEvent.callCount).to.equal(1)
expect(

View file

@ -46,7 +46,7 @@ describe('TemplatesManager', function() {
this.ProjectUploadManager = {
createProjectFromZipArchiveWithName: sinon
.stub()
.callsArgWith(3, null, { _id: this.project_id })
.callsArgWith(4, null, { _id: this.project_id })
}
this.dumpFolder = 'dump/path'
this.ProjectOptionsHandler = {
@ -139,7 +139,11 @@ describe('TemplatesManager', function() {
return this.ProjectUploadManager.createProjectFromZipArchiveWithName.should.have.been.calledWithMatch(
this.user_id,
this.templateName,
this.dumpPath
this.dumpPath,
{
fromV1TemplateId: this.templateId,
fromV1TemplateVersionId: this.templateVersionId
}
)
})

View file

@ -194,6 +194,10 @@ describe('ProjectUploadManager', function() {
this.ProjectDetailsHandler.generateUniqueName = sinon
.stub()
.callsArgWith(2, null, this.name)
// createBlankProject allows taking optional attributes and will callback the last arg
this.ProjectCreationHandler.createBlankProject = sinon
.stub()
.callsArgWith(3, null, this.project)
this.ProjectUploadManager.insertZipArchiveIntoFolder = sinon
.stub()
.callsArg(4)