-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrades.service.ts
157 lines (124 loc) · 3.74 KB
/
trades.service.ts
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
/*
* This file is just for demo purpose. it is copied from example of devextreme
* and it is not followed all standards of code comes from bollerpate project.
* TODO: Product and Trade should be converted to types and stored in @core/types folder
* This service generates Products and Trades and emulate updates comming from the server
* Real service should use websocket to get real-time updates from the server
*
*/
import { Injectable, NgZone } from '@angular/core';
import ArrayStore from 'devextreme/data/array_store';
export type ProductTypes = 'Energy' | 'Furures';
// it should be another service that gets data from the server
const systems = ['Solar', 'Magelan'];
const updatesPerSecond = 100;
export class Product {
ProductID: number;
ProductName: string;
ProductType: ProductTypes;
Quantity: number;
Amount: number;
TradeCount: number;
}
export class Trade {
TradeID: number;
ShipSystem: string;
ProductID: number;
UnitPrice: number;
TradeDate: Date;
Quantity: number;
}
// this solution just example generate random trades and updates every 50ms
function generateTestData(): [Product[], Trade[]] {
const systems = ['Solar', 'Magelan'];
const products: Product[] = [];
for (let i = 1; i <= 100; i++) {
products.push({
ProductID: i,
ProductName: `Product ${i}`,
ProductType: i % 2 ? 'Energy' : 'Furures',
Quantity: 0,
Amount: 0,
TradeCount: 0,
});
}
const productsStore = new ArrayStore({
data: products,
key: 'ProductID',
});
const trades: Trade[] = [];
const tradesStore = new ArrayStore({
key: 'TradeID',
data: trades,
});
return [products, trades];
}
@Injectable({
providedIn: 'root',
})
export class TradesService {
private products: Product[];
private trades: Trade[];
private productsStore: ArrayStore<Product, any>;
private tradesStore: ArrayStore<Trade, any>;
constructor(private _zone: NgZone) {
const [products, trades] = generateTestData();
this.products = products;
this.trades = trades;
this.productsStore = new ArrayStore({
data: this.products,
key: 'ProductID',
});
this.tradesStore = new ArrayStore({
key: 'TradeID',
data: this.trades,
});
}
getProducts() {
return this.productsStore;
}
getTrades() {
return this.tradesStore;
}
getTradeCount() {
return this.trades.length;
}
// this is just for demo purpose it creates random trades and updates tradesStore and productsStore
addTrade() {
const product = this.products[Math.round(Math.random() * 99)];
const trade: Trade = {
TradeID: this.trades.length ? this.trades[this.trades.length - 1].TradeID + 1 : 20001,
ShipSystem: systems[Math.round(Math.random() * (systems.length - 1))],
ProductID: product.ProductID,
UnitPrice: Math.round(Math.random() * 100) + 1,
TradeDate: new Date(),
Quantity: Math.round(Math.random() * 20) + 1,
};
this.tradesStore.push([{ type: 'insert', data: trade }]);
this.productsStore.push([
{
type: 'update',
key: trade.ProductID,
data: {
TradeCount: product.TradeCount + 1,
Quantity: product.Quantity + trade.Quantity,
Amount: product.Amount + trade.UnitPrice * trade.Quantity,
},
},
]);
}
// this is just for demo purpose it should get data from the server with websocket
getTradeUpdates() {
// comment runOutsideAngular to see the difference in number of changeDetection circles
this._zone.runOutsideAngular(() => {
setInterval(() => {
if (this.getTradeCount() > 500000) {
return;
}
for (let i = 0; i < updatesPerSecond / 20; i++) {
this.addTrade();
}
}, 50);
});
}
}