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

Add notebook for generating power reports

parent 2865a20e
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: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 = '2020/07/10 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=ca_file)
```
%% 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:
Some of data values report unrealistic power values. Any reading over 10kW is considered invalid.
%% Cell type:code id: tags:
```
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()):
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")
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 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':'2020-07-09'].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