-
Notifications
You must be signed in to change notification settings - Fork 3
/
BaseAggregator.scala
188 lines (151 loc) · 5.52 KB
/
BaseAggregator.scala
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
186
187
188
import org.apache.spark.sql.expressions.MutableAggregationBuffer
import org.apache.spark.sql.expressions.UserDefinedAggregateFunction
import org.apache.spark.sql.Row
import org.apache.spark.sql.types.{DecimalType,StructType}
import java.math.{BigDecimal => JBigDecimal}
abstract class BaseAggregator(colName: String) extends UserDefinedAggregateFunction {
type Price = (JBigDecimal, Timestamp)
override def inputSchema: StructType = StructType(
StructField("day", StringType) ::
StructField("hour", IntegerType) ::
StructField("min", IntegerType) ::
StructField("sec", IntegerType) ::
StructField("milli", IntegerType) ::
StructField("tradHour", IntegerType) ::
StructField("tradMin", IntegerType) ::
StructField("price", new DecimalType(12,6)) ::
StructField("thresh", IntegerType) :: Nil)
override def bufferSchema: StructType = StructType(
StructField("price", new DecimalType(12,6)) ::
StructField("priceTs", TimestampType) :: Nil)
override def dataType: DataType = StructType(
StructField(colName, new DecimalType(12,6)) ::
StructField(colName + "_ts", TimestampType) :: Nil)
override def initialize(buffer: MutableAggregationBuffer): Unit = {
buffer(0) = null
buffer(1) = null
}
override def deterministic : Boolean = true
// This is how to update your buffer schema given an input.
override def update(buffer: MutableAggregationBuffer, input: Row): Unit = {
val day = input.getAs[String](0)
val hour = input.getAs[Int](1)
val min = input.getAs[Int](2)
val sec = input.getAs[Int](3)
val milli = input.getAs[Int](4)
val tradHour = input.getAs[Int](5)
val tradMin = input.getAs[Int](6)
val price = input.getAs[JBigDecimal](7)
val x = input.getAs[Int](8)
val nfpTs = getNfpTs(day, tradHour, tradMin)
val tickTs = getTickTs(day, hour, min, sec, milli)
//nfpTsPlusThresh
val maxTs = tsPlusMins(nfpTs, x)
//if the tickTs is between nfpTs and maxTs
//big filtering
println(s"nfpTs is $nfpTs while tickTs is $tickTs")
if (tickTs.after(nfpTs) && tickTs.before(maxTs)) {
buffer(0) = price
buffer(1) = tickTs
}
}
override def merge(buffer: MutableAggregationBuffer, row: Row): Unit = {
val result = getPricePoint(buffer, row)
buffer(0) = result._1
buffer(1) = result._2
}
// This is where you output the final value, given the final value of your bufferSchema.
override def evaluate(buffer: Row): Any = {
(buffer(0), buffer(1))
}
def getTickTs(day: String, hour: Int, min: Int, sec: Int, milli:Int) : Timestamp = {
new Timestamp(new LocalDateTime(
day.substring(0,4).toInt,
day.substring(4,6).toInt,
day.substring(6,8).toInt,
hour, min, sec, milli
).toDate.getTime)
}
def getPricePoint(a : Price, b: Price): Price
def getNfpTs(day: String, nfpHour: Int, nfpMin: Int) : Timestamp = {
new Timestamp(new LocalDateTime(
day.substring(0,4).toInt,
day.substring(4,6).toInt,
day.substring(6,8).toInt,
nfpHour, nfpMin, 0, 0
).toDate.getTime)
}
def tsPlusMins(ts: Timestamp, thresh: Int) : Timestamp = {
val localDT = new LocalDateTime(ts.getTime).plusMinutes(thresh)
new Timestamp(localDT.toDate.getTime)
}
// def getEarliestAndLatest(a : Price, b: Price) : Price = {
// var open : (JBigDecimal, Timestamp) = null
// var close : (JBigDecimal, Timestamp) = null
// val tickPrice = b._1
// val tickTs = b._2
// if (tickTs != null){
// //get min time
// open = if (a._2 == null || tickTs.before(a._2)) (tickPrice, tickTs) else (a._1, a._2)
// //get the max time
// close = if (a._2 == null || tickTs.after(a._4)) (tickPrice, tickTs) else (a._3, a._4)
// } else {
// open = (a._1, a._2)
// close = (a._3, a._4)
// }
// (open._1, open._2, close._1, close._2)
// }
implicit def castInternalBufferToPrice(buffer: MutableAggregationBuffer) : Price = {
(buffer.getAs[JBigDecimal](0), buffer.getAs[Timestamp](1))
}
implicit def castInternalBufferToPrice(row: Row) : Price = {
(row.getAs[JBigDecimal](0), row.getAs[Timestamp](1))
}
}
class FirstAggregator extends BaseAggregator("open_price") {
def getPricePoint(a : Price, b: Price) : Price = {
if (a != null) a else b
}
}
class OpenAggregator extends BaseAggregator("open_price") {
def getPricePoint(a : Price, b: Price) : Price = {
var open : (JBigDecimal, Timestamp) = null
val tickPrice = b._1
val tickTs = b._2
if (tickTs != null){
//get min time
open = if (a._2 == null || tickTs.before(a._2)) (tickPrice, tickTs) else (a._1, a._2)
} else {
open = (a._1, a._2)
}
(open._1, open._2)
}
}
class CloseAggregator extends BaseAggregator("close_price") {
def getPricePoint(a : Price, b: Price) : Price = {
var close : (JBigDecimal, Timestamp) = null
val tickPrice = b._1
val tickTs = b._2
if (tickTs != null){
//get max time
close = if (a._2 == null || tickTs.after(a._2)) (tickPrice, tickTs) else (a._1, a._2)
} else {
close = (a._1, a._2)
}
(close._1, close._2)
}
}
class MaxPriceAggregator extends BaseAggregator("high_price") {
def getPricePoint(a : Price, b: Price) : Price = {
var max : (JBigDecimal, Timestamp) = null
val tickPrice = b._1
val tickTs = b._2
if (tickPrice != null){
//get max price
max = if (a._1 == null || (tickPrice.compareTo(a._1) >= 0)) (tickPrice, tickTs) else (a._1, a._2)
} else {
max = (a._1, a._2)
}
(max._1, max._2)
}
}