-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.test.js
153 lines (150 loc) · 7.54 KB
/
solution.test.js
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
const {
roundTimeStampToHour, groupByIp, removeExcessiveClicks,
groupByTimestamp, findExpansiveClickPerTime,
findExpansiveClickPerTimeIpLookup, aggregateClicks
} = require('./solution_Daisy_Lin.js');
const data = require('./clicks.json');
describe('roundTimeStampToHour', () => {
test('same hour but different minutes should be normalized to the same hour', () => {
const t1 = roundTimeStampToHour("3/11/2016 06:32:42");
const t2 = roundTimeStampToHour("3/11/2016 06:59:59");
expect(t1 === t2).toBeTruthy();
});
test('timestamp with different hour should be unequal', () => {
const t1 = roundTimeStampToHour("3/11/2016 06:32:42");
const t2 = roundTimeStampToHour("3/11/2016 07:32:42");
expect(t1 !== t2).toBeTruthy();
});
test('timestamp with same hour but different day should be unequal', () => {
const t1 = roundTimeStampToHour("3/11/2016 06:32:42");
const t2 = roundTimeStampToHour("3/12/2016 06:32:42");
expect(t1 !== t2).toBeTruthy();
});
test('group the array by ip', () => {
const testData = [
{"ip": "22.22.22.22", "timestamp": "3/11/2016 02:02:58", "amount": 7.00},
{"ip": "11.11.11.11", "timestamp": "3/11/2016 02:12:32", "amount": 6.50},
{"ip": "11.11.11.11", "timestamp": "3/11/2016 02:13:11", "amount": 7.25},
{"ip": "44.44.44.44", "timestamp": "3/11/2016 02:13:54", "amount": 8.75},
{"ip": "22.22.22.22", "timestamp": "3/11/2016 05:02:45", "amount": 11.00},
{"ip": "22.22.22.22", "timestamp": "3/12/2016 06:35:12", "amount": 2.00},
];
const ipLookups = groupByIp(testData);
const expectedResult = {
"22.22.22.22": [
{"ip": "22.22.22.22", "timestamp": "3/11/2016 02:02:58", "amount": 7.00},
{"ip": "22.22.22.22", "timestamp": "3/11/2016 05:02:45", "amount": 11.00},
{"ip": "22.22.22.22", "timestamp": "3/12/2016 06:35:12", "amount": 2.00}
],
"11.11.11.11": [
{"ip": "11.11.11.11", "timestamp": "3/11/2016 02:12:32", "amount": 6.50},
{"ip": "11.11.11.11", "timestamp": "3/11/2016 02:13:11", "amount": 7.25},
],
"44.44.44.44": [
{"ip": "44.44.44.44", "timestamp": "3/11/2016 02:13:54", "amount": 8.75},
]
};
expect(ipLookups).toEqual(expectedResult)
});
test('remove the ip has more than 10 clicks', () => {
const lookups = {
"22.22.22.22": [
{"ip": "22.22.22.22", "timestamp": "3/11/2016 02:02:58", "amount": 7.00},
{"ip": "22.22.22.22", "timestamp": "3/11/2016 05:02:45", "amount": 11.00},
{"ip": "22.22.22.22", "timestamp": "3/12/2016 06:35:12", "amount": 2.00},
{"ip": "22.22.22.22", "timestamp": "3/11/2016 02:02:58", "amount": 7.00},
{"ip": "22.22.22.22", "timestamp": "3/11/2016 02:02:58", "amount": 7.00},
{"ip": "22.22.22.22", "timestamp": "3/11/2016 02:02:58", "amount": 7.00},
{"ip": "22.22.22.22", "timestamp": "3/11/2016 02:02:58", "amount": 7.00},
{"ip": "22.22.22.22", "timestamp": "3/11/2016 02:02:58", "amount": 7.00},
{"ip": "22.22.22.22", "timestamp": "3/11/2016 02:02:58", "amount": 7.00},
{"ip": "22.22.22.22", "timestamp": "3/11/2016 02:02:58", "amount": 7.00},
{"ip": "22.22.22.22", "timestamp": "3/11/2016 02:02:58", "amount": 7.00}
],
"11.11.11.11": [
{"ip": "11.11.11.11", "timestamp": "3/11/2016 02:12:32", "amount": 6.50},
{"ip": "11.11.11.11", "timestamp": "3/11/2016 02:13:11", "amount": 7.25},
],
"44.44.44.44": [
{"ip": "44.44.44.44", "timestamp": "3/11/2016 02:13:54", "amount": 8.75},
]
};
const lookUpCln = removeExcessiveClicks(lookups);
const expectedResult = {
"11.11.11.11": [
{"ip": "11.11.11.11", "timestamp": "3/11/2016 02:12:32", "amount": 6.50},
{"ip": "11.11.11.11", "timestamp": "3/11/2016 02:13:11", "amount": 7.25},
],
"44.44.44.44": [
{"ip": "44.44.44.44", "timestamp": "3/11/2016 02:13:54", "amount": 8.75},
]
};
expect(lookUpCln).toEqual(expectedResult)
});
test('group the array by round up timestamp', () => {
const testData = [
{"ip": "22.22.22.22", "timestamp": "3/11/2016 02:02:58", "amount": 7.00},
{"ip": "11.11.11.11", "timestamp": "3/11/2016 02:13:11", "amount": 7.25},
{"ip": "22.22.22.22", "timestamp": "3/11/2016 05:02:45", "amount": 11.00},
{"ip": "22.22.22.22", "timestamp": "3/12/2016 06:35:12", "amount": 2.00}];
const expectedResult = {
'1457679600000': [
{"ip": "22.22.22.22", "timestamp": "3/11/2016 02:02:58", "amount": 7.00},
{"ip": "11.11.11.11", "timestamp": "3/11/2016 02:13:11", "amount": 7.25}
],
'1457690400000': [
{"ip": "22.22.22.22", "timestamp": "3/11/2016 05:02:45", "amount": 11.00}
],
'1457780400000': [
{"ip": "22.22.22.22", "timestamp": "3/12/2016 06:35:12", "amount": 2.00}
]
};
expect(groupByTimestamp(testData)).toEqual(expectedResult);
});
test('find the most expansive click', () => {
const testData = [
{"ip": "22.22.22.22", "timestamp": "3/11/2016 02:02:58", "amount": 7.00},
{"ip": "11.11.11.11", "timestamp": "3/11/2016 02:13:11", "amount": 7.25}
];
expect(findExpansiveClickPerTime(testData)).toEqual({
"ip": "11.11.11.11",
"timestamp": "3/11/2016 02:13:11",
"amount": 7.25
})
});
test('return the earliest one to result when there is a tie', () => {
const testData = [
{"ip": "22.22.22.22", "timestamp": "3/11/2016 02:02:58", "amount": 7.00},
{"ip": "11.11.11.11", "timestamp": "3/11/2016 02:45:11", "amount": 7.25},
{"ip": "11.11.11.11", "timestamp": "3/11/2016 02:13:11", "amount": 7.25}
];
expect(findExpansiveClickPerTime(testData)).toEqual({
"ip": "11.11.11.11",
"timestamp": "3/11/2016 02:13:11",
"amount": 7.25
})
});
test('return the ip lookup table of the most expansive clicks array from each period', () => {
const testData = [
{"ip": "44.44.44.44", "timestamp": "3/11/2016 02:13:54", "amount": 8.75},
{"ip": "22.22.22.22", "timestamp": "3/12/2016 06:35:12", "amount": 2.00},
{"ip": "22.22.22.22", "timestamp": "3/11/2016 05:02:45", "amount": 11.00},
];
const expectedResult = {
"44.44.44.44": [
{"ip": "44.44.44.44", "timestamp": "3/11/2016 02:13:54", "amount": 8.75}
],
"22.22.22.22": [
{"ip": "22.22.22.22", "timestamp": "3/12/2016 06:35:12", "amount": 2.00},
{"ip": "22.22.22.22", "timestamp": "3/11/2016 05:02:45", "amount": 11.00},
]
};
const ipLookUps = groupByIp(testData);
const resultDate = findExpansiveClickPerTimeIpLookup(ipLookUps);
expect(resultDate).toEqual(expectedResult)
});
test('return the flatten array and should be the subset of the original', () => {
const expectedResult = aggregateClicks(data);
expect(data).toEqual(expect.arrayContaining(expectedResult))
})
});