forked from dcos/demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
180 lines (165 loc) · 4.65 KB
/
main.go
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
// Copyright 2016 Mesosphere. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"flag"
"fmt"
"os"
"strconv"
"strings"
"sync"
"github.com/Shopify/sarama"
log "github.com/Sirupsen/logrus"
)
const (
VERSION string = "0.1.0"
MAX_ACCOUNT_NUM int = 1000
)
var (
version bool
wg sync.WaitGroup
cities []string
// the aggregate transaction volume [source][target]:
volume [MAX_ACCOUNT_NUM][MAX_ACCOUNT_NUM]int
// FQDN/IP + port of a Kafka broker:
broker string
// the ingestion queue:
iqueue chan Transaction
// aggregate volume alert threshold:
threshold int
// defines how the output is rendered:
prodout bool
)
type Transaction struct {
City string
Source string
Target string
Amount int
}
func about() {
fmt.Printf("\nThis is the fintrans money laundering detection consumer in version %s\n", VERSION)
}
func init() {
cities = []string{
"London",
"NYC",
"SF",
"Moscow",
"Tokyo",
}
// the commend line parameters:
flag.BoolVar(&version, "version", false, "Display version information")
flag.StringVar(&broker, "broker", "", "The FQDN or IP address and port of a Kafka broker. Example: broker-1.kafka.mesos:9382 or 10.0.3.178:9398")
flag.Usage = func() {
fmt.Printf("Usage: %s [args]\n\n", os.Args[0])
fmt.Println("Arguments:")
flag.PrintDefaults()
}
flag.Parse()
// the optional environment variables:
threshold = 10000
if at := os.Getenv("ALERT_THRESHOLD"); at != "" {
if ati, err := strconv.Atoi(at); err == nil {
threshold = ati
}
}
prodout = true
if po := os.Getenv("PROD_OUTPUT"); po != "" {
if strings.ToLower(po) == "false" {
prodout = false
}
}
// creating the aggregate transaction volume matrix with format [source][target]:
volume = [MAX_ACCOUNT_NUM][MAX_ACCOUNT_NUM]int{}
// creating the buffered channel holding up to 100 transactions:
iqueue = make(chan Transaction, 100)
}
func detect() {
for {
t := <-iqueue
log.WithFields(log.Fields{"func": "detect"}).Info(fmt.Sprintf("Dequeued %#v", t))
source := -1
if s, err := strconv.Atoi(t.Source); err == nil {
source = s
}
target := -1
if t, err := strconv.Atoi(t.Target); err == nil {
target = t
}
if source != -1 && target != -1 { // we have a valid transaction
volume[source][target] = volume[source][target] + t.Amount
log.WithFields(log.Fields{"func": "detect"}).Info(fmt.Sprintf("%s -> %s totalling %d now", t.Source, t.Target, volume[source][target]))
if volume[source][target] > threshold {
if prodout {
fmt.Println(fmt.Sprintf("POTENTIAL MONEY LAUNDERING: %s -> %s totalling %d now ", t.Source, t.Target, volume[source][target]))
} else {
fmt.Println(fmt.Sprintf("\x1b[31;1mPOTENTIAL MONEY LAUNDERING:\x1b[0m %s -> %s totalling %d now ", t.Source, t.Target, volume[source][target]))
}
}
}
log.WithFields(log.Fields{"func": "detect"}).Info(fmt.Sprintf("Current queue length: %d", len(iqueue)))
}
}
func consume(topic string) {
var consumer sarama.Consumer
defer wg.Done()
if c, err := sarama.NewConsumer([]string{broker}, nil); err != nil {
log.WithFields(log.Fields{"func": "consume"}).Error(err)
return
} else {
consumer = c
}
defer func() {
if err := consumer.Close(); err != nil {
log.WithFields(log.Fields{"func": "consume"}).Error(err)
}
}()
if partitionConsumer, err := consumer.ConsumePartition(topic, 0, sarama.OffsetNewest); err != nil {
log.WithFields(log.Fields{"func": "consume"}).Error(err)
return
} else {
defer func() {
if err := partitionConsumer.Close(); err != nil {
log.WithFields(log.Fields{"func": "consume"}).Error(err)
}
}()
for {
msg := <-partitionConsumer.Messages()
traw := strings.Split(string(msg.Value), " ")
amount := 0
if a, err := strconv.Atoi(traw[2]); err == nil {
amount = a
}
t := Transaction{City: msg.Topic, Source: traw[0], Target: traw[1], Amount: amount}
iqueue <- t
log.WithFields(log.Fields{"func": "consume"}).Info(fmt.Sprintf("Queued %#v", t))
}
}
}
func main() {
if version {
about()
os.Exit(0)
}
if broker == "" {
flag.Usage()
os.Exit(1)
}
wg.Add(len(cities))
go detect()
for _, city := range cities {
go consume(city)
}
wg.Wait()
}