power-stats.ipynb 8.9 KB
Newer Older
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
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Power Stats \n",
    "\n",
    "Use RestAPI to read power consumption info for cluster nodes and generate usage reports.  this is based on the [pandas time series tutorial by Jennifer Walker](https://www.dataquest.io/blog/tutorial-time-series-analysis-with-pandas/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import pprint\n",
    "import datetime\n",
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.dates as mdates"
   ]
  },
29
30
31
32
33
34
35
36
37
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.rcParams[\"figure.figsize\"] = (20,6)"
   ]
  },
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
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set up credentials to query RestAPI. Bright controls access based on the user identity.  The user's cert.pem and cert.key are automatically generated but the cacert.pem needs to be constructed from the  certs returned by the master."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cert_file='~/.cm/cert.pem'\n",
    "key_file='~/.cm/cert.key'\n",
    "ca_file='cacert.pem'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "cert=(os.path.expanduser(cert_file), os.path.expanduser(key_file))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gather Cluster Power Data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "startdate = '2020/01/01 00:00:00'\n",
79
    "enddate = '2021/02/21 00:00:00'"
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "params = (\n",
    "    ('start', startdate),\n",
    "    ('measurable', 'Pwr_Consumption'),\n",
    "    ('indent', '1'),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
101
    "response = requests.get('https://master:8081/rest/v1/monitoring/dump', params=params, cert=cert, verify=False)"
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Simply read the json response into a dataframe for futher parsing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame(response.json()[\"data\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
124
125
126
127
128
    "## Clean Data and Resample\n",
    "\n",
    "Some of data values report unrealistic power values.  Any reading over 10kW is considered invalid.  \n",
    "\n",
    "Shouldn't do that until later since it implicitly filters out NaN"
129
130
131
132
133
134
135
136
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
137
    "#df = df.loc[df['raw'] <  10000]"
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
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create a datatime type column from the reported sample times."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['datetime'] = pd.to_datetime(df.time, format=\"%Y/%m/%d %H:%M:%S\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create an index for the hourly "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hourly_idx=pd.date_range(startdate, enddate, freq='H')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "debug=False\n",
    "\n",
    "# prepare data frame to append to, use zeros for default column \n",
    "m6_hourly_pwr=pd.DataFrame(np.zeros((1,len(hourly_idx))).T, index=hourly_idx, columns=['sum'])\n",
    "\n",
183
    "for num, entity in enumerate(sorted(df.entity.unique())):\n",
184
185
186
187
    "    if entity not in ['c0108', 'c0009']:\n",
    "        node_pwr=df[df.entity==entity].set_index(\"datetime\")\n",
    "        node_pwr=node_pwr[['raw']].resample('H').mean()\n",
    "        node_pwr=node_pwr[startdate:enddate].fillna(method=\"ffill\")\n",
188
    "        node_pwr=node_pwr[startdate:enddate].fillna(method=\"bfill\")\n",
189
190
191
192
193
194
195
    "        if debug:\n",
    "          print(node_pwr)\n",
    "          missing = node_pwr['raw'].isnull().sum()\n",
    "          print(\"{}: {} missing {}\\n\".format(num, entity, missing))\n",
    "        m6_hourly_pwr[entity]= node_pwr[startdate:enddate]\n"
   ]
  },
196
197
198
199
200
201
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Plot Per-node Hourly\n",
    "\n",
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
    "This is just to see the data for each node in one plot and get a feel for how the nodes behave relative to each other.  Plot nodes in individual subplotes to decern individual behavior of specific nodes. It does give a sense of how the total power adds up. \n",
    "\n",
    "Inspect the nodes in the first rack."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_nodes=36\n",
    "fig, axes = plt.subplots(num_nodes,1, figsize=(20,30))\n",
    "for i in range(num_nodes):\n",
    "    m6_hourly_pwr['2020-02-01':'2021-02-21'].iloc[:,i+1:i+2].plot(ax=axes[i], legend=\"left\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Overview plot reveals missing power data for a number of nodes.  Inspect one up close."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "m6_hourly_pwr['2020-02-01':'2021-02-21'].iloc[:,1:2].plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Identify nodes that have missing data\n",
    "\n",
    "Identify nodes by ones that have NaN values over the past month."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "nan_mask = m6_hourly_pwr['2021-02-01':'2021-02-02'].isna()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "power_missing = nan_mask[nan_mask].apply(lambda row: row[row == True].index, axis=1)[1]"
260
261
262
263
264
265
266
267
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
268
269
270
271
    "num_nodes=len(power_missing)\n",
    "fig, axes = plt.subplots(num_nodes,1, figsize=(20,30))\n",
    "for i, node in enumerate(power_missing):\n",
    "    m6_hourly_pwr[node].plot(ax=axes[i], legend=\"left\")"
272
273
   ]
  },
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Plot Power Usage Graph\n",
    "\n",
    "\n",
    "Pick the start and end date for the plots from the data range selected above. Generate the sum and plot only it's values.\n",
    "\n",
    "We skip over the first month of collection because it is uncommonly noisy."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
292
    "kW = m6_hourly_pwr['2020-02-01':'2021-02-21'].sum(axis=1)/1000"
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ax = kW.plot()\n",
    "ax.set_ylabel(\"Power (kW)\")\n",
    "ax.set_title(\"Cheaha compute and login node hourly power use\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Resample hourly sum to support the seven day average."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "kW_d = kW.resample('D').mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Compute the centered 7-day rolling mean\n",
    "# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rolling.html\n",
    "kW_7d = kW_d.rolling(7, center=True).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot houry, daily, 7-day rolling mean\n",
    "fig, ax = plt.subplots()\n",
    "ax.plot(kW, marker='.', markersize=2, color='gray', linestyle='None', label='Hourly Average')\n",
    "ax.plot(kW_d, color='brown', linewidth=2, label='1-day Average')\n",
    "ax.plot(kW_7d, color='black', linewidth=4, label='7-day Rolling Average')\n",
    "label='Trend (7 day Rolling Mean)'\n",
    "ax.legend()\n",
    "ax.set_ylabel('Power (kW)')\n",
    "ax.set_title('Cheaha Trends in Electricity Consumption');"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}