Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Client formatting of webhooks changes.
- Loading branch information
Showing
5 changed files
with
56 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,76 @@ | ||
import Utils from 'utils/utils'; | ||
import Utils from "utils/utils"; | ||
|
||
const Webhooks = Backbone.Collection.extend({ | ||
url: `${Galaxy.root}api/webhooks` | ||
url: `${Galaxy.root}api/webhooks` | ||
}); | ||
|
||
const WebhookView = Backbone.View.extend({ | ||
el: '#webhook-view', | ||
el: "#webhook-view", | ||
|
||
initialize: function (options) { | ||
const toolId = options.toolId || ''; | ||
const toolVersion = options.toolVersion || ''; | ||
initialize: function(options) { | ||
const toolId = options.toolId || ""; | ||
const toolVersion = options.toolVersion || ""; | ||
|
||
this.$el.attr('tool_id', toolId); | ||
this.$el.attr('tool_version', toolVersion); | ||
this.$el.attr("tool_id", toolId); | ||
this.$el.attr("tool_version", toolVersion); | ||
|
||
const webhooks = new Webhooks(); | ||
webhooks.fetch({ | ||
success: data => { | ||
data.reset(filterType(data, options.type)); | ||
const webhooks = new Webhooks(); | ||
webhooks.fetch({ | ||
success: data => { | ||
data.reset(filterType(data, options.type)); | ||
|
||
if (data.length > 0) { | ||
this.render(weightedRandomPick(data)); | ||
} | ||
} | ||
}); | ||
}, | ||
if (data.length > 0) { | ||
this.render(weightedRandomPick(data)); | ||
} | ||
} | ||
}); | ||
}, | ||
|
||
render: function (model) { | ||
const webhook = model.toJSON(); | ||
this.$el.html(`<div id="${webhook.id}"></div>`); | ||
Utils.appendScriptStyle(webhook); | ||
return this; | ||
} | ||
render: function(model) { | ||
const webhook = model.toJSON(); | ||
this.$el.html(`<div id="${webhook.id}"></div>`); | ||
Utils.appendScriptStyle(webhook); | ||
return this; | ||
} | ||
}); | ||
|
||
const load = options => { | ||
const webhooks = new Webhooks(); | ||
webhooks.fetch({ | ||
async: options.async !== undefined ? options.async : true, | ||
success: data => { | ||
if (options.type) { | ||
data.reset(filterType(data, options.type)); | ||
} | ||
options.callback(data); | ||
} | ||
}); | ||
const webhooks = new Webhooks(); | ||
webhooks.fetch({ | ||
async: options.async !== undefined ? options.async : true, | ||
success: data => { | ||
if (options.type) { | ||
data.reset(filterType(data, options.type)); | ||
} | ||
options.callback(data); | ||
} | ||
}); | ||
}; | ||
|
||
function filterType (data, type) { | ||
return data.models.filter(item => item.get('type').indexOf(type) !== -1); | ||
function filterType(data, type) { | ||
return data.models.filter(item => item.get("type").indexOf(type) !== -1); | ||
} | ||
|
||
function weightedRandomPick (data) { | ||
const weights = data.pluck('weight'); | ||
const sum = weights.reduce((a, b) => a + b); | ||
function weightedRandomPick(data) { | ||
const weights = data.pluck("weight"); | ||
const sum = weights.reduce((a, b) => a + b); | ||
|
||
const normalizedWeightsMap = new Map(); | ||
weights.forEach((weight, index) => { | ||
normalizedWeightsMap.set(index, parseFloat((weight / sum).toFixed(2))); | ||
}); | ||
const normalizedWeightsMap = new Map(); | ||
weights.forEach((weight, index) => { | ||
normalizedWeightsMap.set(index, parseFloat((weight / sum).toFixed(2))); | ||
}); | ||
|
||
const table = []; | ||
for (const [index, weight] of normalizedWeightsMap) { | ||
for (let i = 0; i < weight * 100; i++) { | ||
table.push(index); | ||
const table = []; | ||
for (const [index, weight] of normalizedWeightsMap) { | ||
for (let i = 0; i < weight * 100; i++) { | ||
table.push(index); | ||
} | ||
} | ||
} | ||
|
||
return data.at(table[Math.floor(Math.random() * table.length)]); | ||
return data.at(table[Math.floor(Math.random() * table.length)]); | ||
} | ||
|
||
export default { | ||
WebhookView: WebhookView, | ||
load: load | ||
WebhookView: WebhookView, | ||
load: load | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters