/
KeenStore.js
231 lines (192 loc) · 5.94 KB
/
KeenStore.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import KeenAnalysis from 'keen-analysis';
import KeenTracking from 'keen-tracking';
import { observable } from 'mobx';
import moment from 'moment';
const COLLECTIONS = {
PAGEVIEWS: 'pageviews',
TIMENODES: 'timenodes'
};
// 2 minutes in milliseconds
const ACTIVE_TIMENODES_POLLING_INTERVAL = 5 * 60 * 1000;
const TEN_MIN = 10 * 60 * 1000;
const FIVE_SEC = 5000;
export class KeenStore {
@observable
activeTimeNodes = null;
@observable
activeTimeNodesTimeNodeSpecificProvider = null;
timeNodeSpecificProviderNetId = null;
projectId = '';
writeKey = '';
readKey = '';
analysisClient = null;
trackingClient = null;
networkId = null;
isBlacklisted = false;
_web3Service = null;
initialized;
@observable
historyActiveTimeNodes = [];
@observable
gettingActiveTimeNodesHistory = false;
@observable
historyPollingInterval = FIVE_SEC;
constructor(projectId, writeKey, readKey, web3Service, versions) {
this.projectId = projectId;
this.writeKey = writeKey;
this.readKey = readKey;
this._web3Service = web3Service;
this.versions = versions;
this.initialized = this.initialize();
}
async initialize() {
await this._web3Service.init();
this.networkId = this._web3Service.network.id;
this.analysisClient = new KeenAnalysis({
projectId: this.projectId,
readKey: this.readKey
});
this.trackingClient = new KeenTracking({
projectId: this.projectId,
writeKey: this.writeKey
});
this.sendPageView();
this._pollActiveTimeNodesCount();
}
sendPageView() {
this.trackingClient.recordEvent(COLLECTIONS.PAGEVIEWS, {
title: document.title
});
}
async sendActiveTimeNodeEvent(nodeAddress, dayAddress, networkId = this.networkId) {
await this.initialized;
nodeAddress = this._web3Service.web3.sha3(nodeAddress).toString();
dayAddress = this._web3Service.web3.sha3(dayAddress).toString();
networkId = networkId.toString();
const event = {
nodeAddress,
dayAddress,
networkId,
eacVersions: this.versions,
nodeType: 'dapp'
};
this.trackingClient.recordEvent(COLLECTIONS.TIMENODES, event);
}
setTimeNodeSpecificProviderNetId(netId) {
this.timeNodeSpecificProviderNetId = parseInt(netId);
}
async refreshActiveTimeNodesCount() {
this.activeTimeNodes = await this.getActiveTimeNodesCount(this.networkId);
if (this.timeNodeSpecificProviderNetId === this.networkId) {
this.activeTimeNodesTimeNodeSpecificProvider = this.activeTimeNodes;
} else if (this.timeNodeSpecificProviderNetId === null) {
this.activeTimeNodesTimeNodeSpecificProvider = null;
} else {
this.activeTimeNodesTimeNodeSpecificProvider = await this.getActiveTimeNodesCount(
this.timeNodeSpecificProviderNetId
);
}
}
async refreshActiveTimeNodesHistory() {
if (!this.isBlacklisted) {
this.gettingActiveTimeNodesHistory = true;
const isAllZeroes = array => array.every(counter => counter === 0);
const history = await this._getActiveTimeNodesHistory();
/*
Keen sometimes returns all zeroes instead of the real value.
We keep getting the values from Keen every 5 seconds until we get the proper values.
After that we switch to a longer time interval.
NOTE: This check can be remove if Keen fixes sending incorrect values
*/
if (!isAllZeroes(history)) {
this.historyPollingInterval = TEN_MIN; // Set a longer polling interval
this.historyActiveTimeNodes = history;
}
this.gettingActiveTimeNodesHistory = false;
}
}
async _getActiveTimeNodesHistory() {
await this.initialized;
if (!this.isBlacklisted) {
const count = new KeenAnalysis.Query('count_unique', {
event_collection: COLLECTIONS.TIMENODES,
target_property: 'nodeAddress',
timeframe: {
start: moment()
.subtract(24, 'hours')
.toISOString(),
end: moment().toISOString()
},
interval: 'hourly',
group_by: 'nodeAddress',
filters: [
{
property_name: 'networkId',
operator: 'eq',
property_value: this.timeNodeSpecificProviderNetId
},
{
property_name: 'eacVersions.contracts',
operator: 'eq',
property_value: this.versions.contracts
}
]
});
let response = null;
try {
response = await this.analysisClient.run(count);
} catch (e) {
this.isBlacklisted = true;
}
if (
response === null ||
!response.hasOwnProperty('result') ||
response.hasOwnProperty('err')
) {
return null;
}
return response.result.map(res => res.value[0].result);
}
}
async getActiveTimeNodesCount(networkId, timeframe = 'this_5_minutes') {
await this.initialized;
if (!this.isBlacklisted) {
const count = new KeenAnalysis.Query('count', {
event_collection: COLLECTIONS.TIMENODES,
target_property: 'nodeAddress',
timeframe,
group_by: 'nodeAddress',
filters: [
{
property_name: 'networkId',
operator: 'eq',
property_value: networkId
},
{
property_name: 'eacVersions.contracts',
operator: 'eq',
property_value: this.versions.contracts
}
]
});
let response = null;
try {
response = await this.analysisClient.run(count);
} catch (e) {
this.isBlacklisted = true;
}
if (
response === null ||
!response.hasOwnProperty('result') ||
response.hasOwnProperty('err')
) {
return null;
}
return response.result.length;
}
}
async _pollActiveTimeNodesCount() {
await this.refreshActiveTimeNodesCount();
setInterval(() => this.refreshActiveTimeNodesCount(), ACTIVE_TIMENODES_POLLING_INTERVAL);
}
}