forked from guillecanovas/lab5-soa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplication.kt
94 lines (80 loc) · 3.49 KB
/
Application.kt
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
package soa.camel
import io.micrometer.core.instrument.Counter
import io.micrometer.core.instrument.MeterRegistry
import org.apache.camel.ProducerTemplate
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.model.dataformat.JsonLibrary
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.stereotype.Component
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.ResponseBody
@SpringBootApplication
class Application
fun main(args: Array<String>) {
runApplication<Application>(*args)
}
const val DIRECT_ROUTE = "direct:twitter"
const val COUNT_ROUTE = "direct:extractor"
const val LOG_ROUTE = "direct:log"
@Controller
class SearchController(private val producerTemplate: ProducerTemplate) {
@RequestMapping("/")
fun index() = "index"
@RequestMapping(value = ["/search"])
@ResponseBody
fun search(@RequestParam("q") q: String?): Any =
producerTemplate.requestBodyAndHeader(DIRECT_ROUTE, "mandalorian", "keywords", q)
}
@Component
class Router(meterRegistry: MeterRegistry) : RouteBuilder() {
private val perKeywordMessages = TaggedCounter("per-keyword-messages", "keyword", meterRegistry)
override fun configure() {
from(DIRECT_ROUTE)
.process { exchange ->
//Get the query
val keyword = exchange.getIn().getHeader("keywords")
//println(keyword)
if (keyword is String) {
//Pattern max:n of the query where n is a number
val maxPattern = Regex("max:[0-9]+")
//Search for coincidences of the pattern in the query
var ans : MatchResult? = maxPattern.find(keyword, 0)
//If there is any coincidence, change the query to match twitter-search
//query format
ans?.value?.let{
val keywordTS = keyword.replace(maxPattern, "?count=" + it.filter(Char::isDigit))
exchange.getIn().setHeader("keywords", keywordTS)
}
}
}
.toD("twitter-search:\${header.keywords}")
.wireTap(LOG_ROUTE)
.wireTap(COUNT_ROUTE)
from(LOG_ROUTE)
.marshal().json(JsonLibrary.Gson)
.to("file://log?fileName=\${date:now:yyyy/MM/dd/HH-mm-ss.SSS}.json")
from(COUNT_ROUTE)
.split(body())
.process { exchange ->
val keyword = exchange.getIn().getHeader("keywords")
if (keyword is String) {
//Delete query options (?count=n) to only gather metrics from keywords
//println(keyword.replace(Regex("\\?count=[0-9]+"), ""))
keyword.replace(Regex("\\?count=[0-9]+"), "").trim().split(" ").map {
perKeywordMessages.increment(it)
}
}
}
}
}
class TaggedCounter(private val name: String, private val tagName: String, private val registry: MeterRegistry) {
private val counters: MutableMap<String, Counter> = HashMap()
fun increment(tagValue: String) {
counters.getOrPut(tagValue) {
Counter.builder(name).tags(tagName, tagValue).register(registry)
}.increment()
}
}