-
Notifications
You must be signed in to change notification settings - Fork 6
/
PairwiseComparisonQual.ts
185 lines (168 loc) · 5.38 KB
/
PairwiseComparisonQual.ts
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import * as noflo from 'noflo'
import { init as contactableInit, makeContactable, shutdown as contactableShutdown } from 'rsf-contactable'
import {
whichToInit,
genericPairwise,
ValidateFn,
PairwiseResultFn
} from '../libs/shared'
import { Contactable, Statement, ContactableConfig, PairwiseQualified, PairwiseChoice, ContactableInitConfig } from 'rsf-types'
import { ProcessHandler, NofloComponent } from '../libs/noflo-types'
const defaultPairwiseQualifiedCb = (pairwiseQualified: PairwiseQualified): void => { }
const coreLogic = async (
contactables: Contactable[],
statements: Statement[],
question: string,
maxTime: number,
eachCb: (pairwiseQualified: PairwiseQualified) => void = defaultPairwiseQualifiedCb,
maxResponsesText: string,
allCompletedText: string,
timeoutText: string,
invalidResponseText: string
): Promise<PairwiseQualified[]> => {
const validate: ValidateFn = (): boolean => {
return true
}
const convertToPairwiseResult: PairwiseResultFn<PairwiseQualified> = (
msg: string,
personalResultsSoFar: PairwiseQualified[],
contactable: Contactable,
pairsTexts: PairwiseChoice[]
): PairwiseQualified => {
const responsesSoFar = personalResultsSoFar.length
return {
choices: pairsTexts[responsesSoFar],
quality: msg,
contact: contactable.config(),
timestamp: Date.now()
}
}
return await genericPairwise<PairwiseQualified>(
contactables,
statements,
question,
maxTime,
eachCb,
validate,
convertToPairwiseResult,
maxResponsesText,
allCompletedText,
timeoutText,
invalidResponseText
)
}
const process: ProcessHandler = async (input, output) => {
if (!input.hasData('question', 'statements', 'max_time', 'contactable_configs', 'bot_configs')) {
return
}
const question: string = input.getData('question')
const statements: Statement[] = input.getData('statements')
const maxTime: number = input.getData('max_time')
const botConfigs: ContactableInitConfig = input.getData('bot_configs')
const contactableConfigs: ContactableConfig[] = input.getData('contactable_configs')
const maxResponsesText: string = input.getData('max_responses_text')
const allCompletedText: string = input.getData('all_completed_text')
const invalidResponseText: string = input.getData('invalid_response_text')
const timeoutText: string = input.getData('timeout_text')
let contactables: Contactable[]
try {
await contactableInit(whichToInit(contactableConfigs), botConfigs)
contactables = contactableConfigs.map(makeContactable)
} catch (e) {
output.send({
error: e
})
output.done()
return
}
try {
const results: PairwiseQualified[] = await coreLogic(
contactables,
statements,
question,
maxTime,
(pairwiseQualified: PairwiseQualified): void => {
output.send({ pairwise_qual: pairwiseQualified })
},
maxResponsesText,
allCompletedText,
timeoutText,
invalidResponseText
)
await contactableShutdown()
output.send({
results
})
} catch (e) {
await contactableShutdown()
output.send({
error: e
})
}
output.done()
}
const getComponent = (): NofloComponent => {
const c: NofloComponent = new noflo.Component()
/* META */
c.description = 'Iterate through all the combinations in a list of statements getting peoples to free relate them'
c.icon = 'compress'
/* IN PORTS */
c.inPorts.add('question', {
datatype: 'string',
description: 'a human readable string clarifying what you would like them to consider about a pair and respond to',
required: true
})
c.inPorts.add('statements', {
datatype: 'array', // rsf-types/Statement[]
description: 'the list of statements (as objects with property "text") to create all possible pairs out of, and relate',
required: true
})
c.inPorts.add('max_time', {
datatype: 'int',
description: 'the number of seconds to wait until stopping this process automatically',
required: true
})
c.inPorts.add('contactable_configs', {
datatype: 'array', // rsf-types/ContactableConfig[]
description: 'an array of rsf-contactable compatible config objects',
required: true
})
c.inPorts.add('bot_configs', {
datatype: 'object',
description: 'an object of rsf-contactable compatible bot config objects',
required: true
})
c.inPorts.add('max_responses_text', {
datatype: 'string',
description: 'msg override: the message sent when participant hits response limit'
})
c.inPorts.add('all_completed_text', {
datatype: 'string',
description: 'msg override: the message sent to all participants when the process completes, by completion by all participants'
})
c.inPorts.add('invalid_response_text', {
datatype: 'string',
description: 'msg override: the message sent when participant use an invalid response'
})
c.inPorts.add('timeout_text', {
datatype: 'string',
description: 'msg override: the message sent to all participants when the process completes because the timeout is reached'
})
/* OUT PORTS */
c.outPorts.add('pairwise_qual', {
datatype: 'object' // rsf-types/PairwiseQualified
})
c.outPorts.add('results', {
datatype: 'array' // rsf-types/PairwiseQualified[]
})
c.outPorts.add('error', {
datatype: 'all'
})
/* DEFINE PROCESS */
c.process(process)
return c
}
export {
coreLogic,
getComponent
}