Skip to content
Snippets Groups Projects
Commit a5b9275b authored by John-Paul Robinson's avatar John-Paul Robinson
Browse files

Add validation of per-node power data to ensure power report is accurate

Plot power data on a per-node bases and create a plot for all nodes missing
data in the past month.
parent 3143fc83
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Power Stats
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 id: tags:
```
import requests
import pprint
import datetime
import os
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
```
%% Cell type:code id: tags:
```
plt.rcParams["figure.figsize"] = (20,6)
```
%% Cell type:markdown id: tags:
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 id: tags:
```
cert_file='~/.cm/cert.pem'
key_file='~/.cm/cert.key'
ca_file='cacert.pem'
```
%% Cell type:code id: tags:
```
cert=(os.path.expanduser(cert_file), os.path.expanduser(key_file))
```
%% Cell type:markdown id: tags:
## Gather Cluster Power Data
%% Cell type:code id: tags:
```
startdate = '2020/01/01 00:00:00'
enddate = '2021/02/21 00:00:00'
```
%% Cell type:code id: tags:
```
params = (
('start', startdate),
('measurable', 'Pwr_Consumption'),
('indent', '1'),
)
```
%% Cell type:code id: tags:
```
response = requests.get('https://master:8081/rest/v1/monitoring/dump', params=params, cert=cert, verify=False)
```
%% Cell type:markdown id: tags:
Simply read the json response into a dataframe for futher parsing.
%% Cell type:code id: tags:
```
df = pd.DataFrame(response.json()["data"])
```
%% Cell type:markdown id: tags:
## Clean Data and Resample
Some of data values report unrealistic power values. Any reading over 10kW is considered invalid.
Shouldn't do that until later since it implicitly filters out NaN
%% Cell type:code id: tags:
```
df = df.loc[df['raw'] < 10000]
#df = df.loc[df['raw'] < 10000]
```
%% Cell type:markdown id: tags:
Create a datatime type column from the reported sample times.
%% Cell type:code id: tags:
```
df['datetime'] = pd.to_datetime(df.time, format="%Y/%m/%d %H:%M:%S")
```
%% Cell type:markdown id: tags:
Create an index for the hourly
%% Cell type:code id: tags:
```
hourly_idx=pd.date_range(startdate, enddate, freq='H')
```
%% Cell type:code id: tags:
```
debug=False
# prepare data frame to append to, use zeros for default column
m6_hourly_pwr=pd.DataFrame(np.zeros((1,len(hourly_idx))).T, index=hourly_idx, columns=['sum'])
for num, entity in enumerate(df.entity.unique()):
for num, entity in enumerate(sorted(df.entity.unique())):
if entity not in ['c0108', 'c0009']:
node_pwr=df[df.entity==entity].set_index("datetime")
node_pwr=node_pwr[['raw']].resample('H').mean()
node_pwr=node_pwr[startdate:enddate].fillna(method="ffill")
node_pwr=node_pwr[startdate:enddate].fillna(method="bfill")
if debug:
print(node_pwr)
missing = node_pwr['raw'].isnull().sum()
print("{}: {} missing {}\n".format(num, entity, missing))
m6_hourly_pwr[entity]= node_pwr[startdate:enddate]
```
%% Cell type:markdown id: tags:
## Plot Per-node Hourly
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. There is too much data to decern individual behavior of specific nodes
but it does give a sense of how the total power adds up.
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.
Inspect the nodes in the first rack.
%% Cell type:code id: tags:
```
num_nodes=36
fig, axes = plt.subplots(num_nodes,1, figsize=(20,30))
for i in range(num_nodes):
m6_hourly_pwr['2020-02-01':'2021-02-21'].iloc[:,i+1:i+2].plot(ax=axes[i], legend="left")
```
%% Cell type:markdown id: tags:
Overview plot reveals missing power data for a number of nodes. Inspect one up close.
%% Cell type:code id: tags:
```
m6_hourly_pwr['2020-02-01':'2021-02-21'].iloc[:,1:2].plot()
```
%% Cell type:markdown id: tags:
## Identify nodes that have missing data
Identify nodes by ones that have NaN values over the past month.
%% Cell type:code id: tags:
```
nan_mask = m6_hourly_pwr['2021-02-01':'2021-02-02'].isna()
```
%% Cell type:code id: tags:
```
power_missing = nan_mask[nan_mask].apply(lambda row: row[row == True].index, axis=1)[1]
```
%% Cell type:code id: tags:
```
m6_hourly_pwr['2020-02-01':'2020-07-09'].iloc[:,1:].plot(legend=False)
num_nodes=len(power_missing)
fig, axes = plt.subplots(num_nodes,1, figsize=(20,30))
for i, node in enumerate(power_missing):
m6_hourly_pwr[node].plot(ax=axes[i], legend="left")
```
%% Cell type:markdown id: tags:
# Plot Power Usage Graph
Pick the start and end date for the plots from the data range selected above. Generate the sum and plot only it's values.
We skip over the first month of collection because it is uncommonly noisy.
%% Cell type:code id: tags:
```
kW = m6_hourly_pwr['2020-02-01':'2021-02-21'].sum(axis=1)/1000
```
%% Cell type:code id: tags:
```
ax = kW.plot()
ax.set_ylabel("Power (kW)")
ax.set_title("Cheaha compute and login node hourly power use")
```
%% Cell type:markdown id: tags:
Resample hourly sum to support the seven day average.
%% Cell type:code id: tags:
```
kW_d = kW.resample('D').mean()
```
%% Cell type:code id: tags:
```
# Compute the centered 7-day rolling mean
# https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rolling.html
kW_7d = kW_d.rolling(7, center=True).mean()
```
%% Cell type:code id: tags:
```
# Plot houry, daily, 7-day rolling mean
fig, ax = plt.subplots()
ax.plot(kW, marker='.', markersize=2, color='gray', linestyle='None', label='Hourly Average')
ax.plot(kW_d, color='brown', linewidth=2, label='1-day Average')
ax.plot(kW_7d, color='black', linewidth=4, label='7-day Rolling Average')
label='Trend (7 day Rolling Mean)'
ax.legend()
ax.set_ylabel('Power (kW)')
ax.set_title('Cheaha Trends in Electricity Consumption');
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment