Lean  $LEAN_TAG$
Nodes.cs
1 /*
2  * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
3  * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15 
16 using System.Collections.Generic;
17 using Newtonsoft.Json;
18 
19 namespace QuantConnect.Api
20 {
21  /// <summary>
22  /// Node class built for API endpoints nodes/read and nodes/create.
23  /// Converts JSON properties from API response into data members for the class.
24  /// Contains all relevant information on a Node to interact through API endpoints.
25  /// </summary>
26  public class Node
27  {
28  /// <summary>
29  /// The nodes cpu clock speed in GHz.
30  /// </summary>
31  [JsonProperty(PropertyName = "speed")]
32  public decimal Speed { get; set; }
33 
34  /// <summary>
35  /// The monthly and yearly prices of the node in US dollars,
36  /// see <see cref="NodePrices"/> for type.
37  /// </summary>
38  [JsonProperty(PropertyName = "price")]
39  public NodePrices Prices { get; set; }
40 
41  /// <summary>
42  /// CPU core count of node.
43  /// </summary>
44  [JsonProperty(PropertyName = "cpu")]
45  public int CpuCount { get; set; }
46 
47  /// <summary>
48  /// Indicate if the node has GPU (1) or not (0)
49  /// </summary>
50  [JsonProperty(PropertyName = "hasGpu")]
51  public int HasGPU { get; set; }
52 
53  /// <summary>
54  /// Size of RAM in Gigabytes.
55  /// </summary>
56  [JsonProperty(PropertyName = "ram")]
57  public decimal Ram { get; set; }
58 
59  /// <summary>
60  /// Name of the node.
61  /// </summary>
62  [JsonProperty(PropertyName = "name")]
63  public string Name { get; set; }
64 
65  /// <summary>
66  /// Node type identifier for configuration.
67  /// </summary>
68  [JsonProperty(PropertyName = "sku")]
69  public string SKU { get; set; }
70 
71  /// <summary>
72  /// Description of the node.
73  /// </summary>
74  [JsonProperty(PropertyName = "description")]
75  public string Description { get; set; }
76 
77  /// <summary>
78  /// User currently using the node.
79  /// </summary>
80  [JsonProperty(PropertyName = "usedBy")]
81  public string UsedBy { get; set; }
82 
83  /// <summary>
84  /// URL of the user using the node
85  /// </summary>
86  [JsonProperty(PropertyName = "userProfile")]
87  public string UserProfile { get; set; }
88 
89  /// <summary>
90  /// Project the node is being used for.
91  /// </summary>
92  [JsonProperty(PropertyName = "projectName")]
93  public string ProjectName { get; set; }
94 
95  /// <summary>
96  /// Id of the project the node is being used for.
97  /// </summary>
98  [JsonProperty(PropertyName = "projectId")]
99  public int? ProjectId { get; set; }
100 
101  /// <summary>
102  /// Indicates if the node is currently busy.
103  /// </summary>
104  [JsonProperty(PropertyName = "busy")]
105  public bool Busy { get; set; }
106 
107  /// <summary>
108  /// Full ID of node.
109  /// </summary>
110  [JsonProperty(PropertyName = "id")]
111  public string Id { get; set; }
112 
113  /// <summary>
114  /// Maximum number of assets recommended for this node.
115  /// </summary>
116  [JsonProperty(PropertyName = "assets")]
117  public int Assets { get; set; }
118 
119  /// <summary>
120  /// Node host.
121  /// </summary>
122  [JsonProperty(PropertyName = "host")]
123  public string Host { get; set; }
124 
125  /// <summary>
126  /// Indicate if this is the active node. The project will use this node if it's not busy.
127  /// </summary>
128  [JsonProperty(PropertyName = "active")]
129  public bool Active { get; set; }
130  }
131 
132  /// <summary>
133  /// Collection of <see cref="Node"/> objects for each target environment.
134  /// </summary>
135  public class NodeList : RestResponse
136  {
137  /// <summary>
138  /// Collection of backtest nodes
139  /// </summary>
140  [JsonProperty(PropertyName = "backtest")]
141  public List<Node> BacktestNodes { get; set; }
142 
143  /// <summary>
144  /// Collection of research nodes
145  /// </summary>
146  [JsonProperty(PropertyName = "research")]
147  public List<Node> ResearchNodes { get; set; }
148 
149  /// <summary>
150  /// Collection of live nodes
151  /// </summary>
152  [JsonProperty(PropertyName = "live")]
153  public List<Node> LiveNodes { get; set; }
154  }
155 
156  /// <summary>
157  /// Rest api response wrapper for node/create, reads in the nodes information into a
158  /// node object
159  /// </summary>
160  public class CreatedNode : RestResponse
161  {
162  /// <summary>
163  /// The created node from node/create
164  /// </summary>
165  [JsonProperty("node")]
166  public Node Node { get; set; }
167  }
168 
169  /// <summary>
170  /// Class for generating a SKU for a node with a given configuration
171  /// Every SKU is made up of 3 variables:
172  /// - Target environment (L for live, B for Backtest, R for Research)
173  /// - CPU core count
174  /// - Dedicated RAM (GB)
175  /// </summary>
176  public class SKU
177  {
178  /// <summary>
179  /// The number of CPU cores in the node
180  /// </summary>
181  public int Cores { get; set; }
182 
183  /// <summary>
184  /// Size of RAM in GB of the Node
185  /// </summary>
186  public int Memory { get; set; }
187 
188  /// <summary>
189  /// Target environment for the node
190  /// </summary>
191  public NodeType Target { get; set; }
192 
193  /// <summary>
194  /// Constructs a SKU object out of the provided node configuration
195  /// </summary>
196  /// <param name="cores">Number of cores</param>
197  /// <param name="memory">Size of RAM in GBs</param>
198  /// <param name="target">Target Environment Live/Backtest/Research</param>
199  public SKU(int cores, int memory, NodeType target)
200  {
201  Cores = cores;
202  Memory = memory;
203  Target = target;
204  }
205 
206  /// <summary>
207  /// Generates the SKU string for API calls based on the specifications of the node
208  /// </summary>
209  /// <returns>String representation of the SKU</returns>
210  public override string ToString()
211  {
212  string result = "";
213 
214  switch (Target)
215  {
216  case NodeType.Backtest:
217  result += "B";
218  break;
219  case NodeType.Research:
220  result += "R";
221  break;
222  case NodeType.Live:
223  result += "L";
224  break;
225  }
226 
227  if (Cores == 0)
228  {
229  result += "-MICRO";
230  }
231  else
232  {
233  result += Cores + "-" + Memory;
234  }
235 
236  return result;
237  }
238  }
239 
240  /// <summary>
241  /// NodeTypes enum for all possible options of target environments
242  /// Used in conjuction with SKU class as a NodeType is a required parameter for SKU
243  /// </summary>
244  public enum NodeType
245  {
246  /// A node for running backtests
247  Backtest, //0
248  /// A node for running research
249  Research, //1
250  /// A node for live trading
251  Live //2
252  }
253 
254  /// <summary>
255  /// Class for deserializing node prices from node object
256  /// </summary>
257  public class NodePrices
258  {
259  /// <summary>
260  /// The monthly price of the node in US dollars
261  /// </summary>
262  [JsonProperty(PropertyName = "monthly")]
263  public int Monthly { get; set; }
264 
265  /// <summary>
266  /// The yearly prices of the node in US dollars
267  /// </summary>
268  [JsonProperty(PropertyName = "yearly")]
269  public int Yearly { get; set; }
270  }
271 
272  /// <summary>
273  /// Supported optimization nodes
274  /// </summary>
275  public static class OptimizationNodes
276  {
277  /// <summary>
278  /// 2 CPUs 8 GB ram
279  /// </summary>
280  public static string O2_8 => "O2-8";
281 
282  /// <summary>
283  /// 4 CPUs 12 GB ram
284  /// </summary>
285  public static string O4_12 => "O4-12";
286 
287  /// <summary>
288  /// 8 CPUs 16 GB ram
289  /// </summary>
290  public static string O8_16 => "O8-16";
291  }
292 }