Skip to content

Instantly share code, notes, and snippets.

@albertywu
Last active July 16, 2019 14:02
Show Gist options
  • Select an option

  • Save albertywu/742731a9e561e60d8ba7d2bca1c26d75 to your computer and use it in GitHub Desktop.

Select an option

Save albertywu/742731a9e561e60d8ba7d2bca1c26d75 to your computer and use it in GitHub Desktop.
Fraud Detector
/*
Notes:
DataSource
- a stream of data
- hasNext(), getNext()
- examples: Location, Transactions, Contacts, ...
- an algorithm's output is a DataSource
Transaction
- is a type of DataSource (stream)
Algorithm
- takes a stream of data sources as input
- outputs a stream of transactions with metadata
- an algorithm output stream is a DataSource
- can specify its data sources
*/
interface DataSource<T> {
hasNext(): Boolean,
getNext(): T
}
interface Algo<Out> {
stream(): DataSource<Out>
}
class AppendLocationAlgo implements Algo<(Transaction, Location)> {
...
}
class AppendContactsAlgo implements Algo<(Transaction, Contact)> {
...
}
class SuspectFraudAlgo implements Algo<(Transaction, Boolean)> {
...
}
function getAlgo(id: AlgoId, sources: Map[DataSourceId, DataSource]): Algo {
switch (id) {
case Algo.AppendLocation:
return AppendLocationAlgo({
dependsOn: [],
sources: [sources.locations, sources.transactions]
})
case Algo.AppendContacts:
return new AppendContactsAlgo({
dependsOn: [],
sources: [sources.contacts, sources.transactions]
})
case Algo.SuspectFraud:
return SuspectFraudAlgo({
dependsOn: [Algo.AppendLocation, Algo.AppendContacts],
sources: [sources.locations, sources.contacts, sources.transactions]
})
case default:
throw new Error('invalid algoId specified')
}
}
class FraudDetector {
private inputs: Map[DataSourceId, DataSource]
private outputs: Map[AlgoId, DataSource]
constructor(options: FraudDetectorOptions) {
// entityId could correspond to a user, merchant, etc...
// the data sources will vary depending on entity
this.inputs = getDataSourceInputs(options.entityId)
}
setAlgo(algoId: AlgoId): void {
const algo = getAlgo(algoId, this.dataSources)
this.outputs[algoId] = algo.stream()
}
removeAlgo(algo: AlgoId): void {
// remove algo / cleanup
}
listen(algoId: AlgoId): DataSource {
return this.outputs[algoId]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment