11import axios from "axios" ;
2- import Backbone from "backbone" ;
32import { getAppRoot } from "onload/loadConfig" ;
4- import Utils from "utils/utils" ;
53
64import { rethrowSimple } from "@/utils/simple-error" ;
75
86let webhookData = undefined ;
97
10- async function getWebHookData ( ) {
8+ async function getWebhookData ( ) {
119 if ( webhookData === undefined ) {
1210 try {
1311 const { data } = await axios . get ( `${ getAppRoot ( ) } api/webhooks` ) ;
@@ -19,57 +17,16 @@ async function getWebHookData() {
1917 return webhookData ;
2018}
2119
22- const WebhookView = Backbone . View . extend ( {
23- el : "#webhook-view" ,
24-
25- initialize : function ( options ) {
26- const toolId = options . toolId || "" ;
27- const toolVersion = options . toolVersion || "" ;
28-
29- this . $el . attr ( "tool_id" , toolId ) ;
30- this . $el . attr ( "tool_version" , toolVersion ) ;
31-
32- getWebHookData ( ) . then ( ( data ) => {
33- const filteredData = filterData ( data , options ) ;
34- if ( filteredData . length > 0 ) {
35- this . render ( weightedRandomPick ( filteredData ) ) ;
36- }
37- } ) ;
38- } ,
39-
40- render : function ( model ) {
41- this . $el . html ( `<div id="${ model . id } "></div>` ) ;
42- Utils . appendScriptStyle ( model ) ;
43- return this ;
44- } ,
45- } ) ;
46-
47- function filterData ( data , options ) {
48- let filteredData = data ;
49- if ( options . type ) {
50- filteredData = filterType ( data , options . type ) ;
20+ export async function loadWebhooks ( type ) {
21+ const webhooks = await getWebhookData ( ) ;
22+ if ( type ) {
23+ return webhooks . filter ( ( item ) => item . type && item . type . indexOf ( type ) !== - 1 ) ;
24+ } else {
25+ return webhooks ;
5126 }
52- return filteredData ;
53- }
54-
55- const load = ( options ) => {
56- getWebHookData ( ) . then ( ( data ) => {
57- options . callback ( filterData ( data , options ) ) ;
58- } ) ;
59- } ;
60-
61- function filterType ( data , type ) {
62- return data . filter ( ( item ) => {
63- const itype = item . type ;
64- if ( itype ) {
65- return itype . indexOf ( type ) !== - 1 ;
66- } else {
67- return false ;
68- }
69- } ) ;
7027}
7128
72- function weightedRandomPick ( data ) {
29+ export function pickWebhook ( data ) {
7330 const weights = data . map ( ( d ) => d . weight ) ;
7431 const sum = weights . reduce ( ( a , b ) => a + b ) ;
7532
@@ -87,8 +44,3 @@ function weightedRandomPick(data) {
8744
8845 return data . at ( table [ Math . floor ( Math . random ( ) * table . length ) ] ) ;
8946}
90-
91- export default {
92- WebhookView : WebhookView ,
93- load : load ,
94- } ;
0 commit comments