Skip to content

Commit

Permalink
Update DiskUsage to newer standard file location and format (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
asnyv authored Jul 13, 2020
1 parent d4335b0 commit 2636ff0
Showing 1 changed file with 60 additions and 24 deletions.
84 changes: 60 additions & 24 deletions webviz_subsurface/plugins/_disk_usage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pathlib
from uuid import uuid4
import datetime
from typing import Optional

import pandas as pd
import dash_html_components as html
Expand All @@ -18,20 +19,32 @@ class DiskUsage(WebvizPluginABC):
---
* **`scratch_dir`:** Path to the scratch directory to show disk usage for.
* **`date`:** Date as string of form YYYY-MM-DD to request an explisit date. Default is to
to use the most recent file avaialable, limited to the last week.
---
?> The `scratch_dir` directory must have a hidden folder `.disk_usage` containing daily
csv files called `disk_usage_user_YYYY-MM-DD.csv`, where YYYY-MM-DD is the date.
The plugin will search backwards from the current date, and throw an error if no file was found
from the last week.
The csv file must have the columns `userid` and `usageKB` (where KB means kilobytes).
All other columns are ignored.
"""

def __init__(self, app, scratch_dir: pathlib.Path):
def __init__(self, app, scratch_dir: pathlib.Path, date: Optional["str"] = None):

super().__init__()

self.scratch_dir = scratch_dir
self.chart_id = "chart-id-{}".format(uuid4())
self.plot_type_id = "plot-type-id-{}".format(uuid4())
self.disk_usage = get_disk_usage(self.scratch_dir)
self.date_input = date
self.disk_usage = get_disk_usage(self.scratch_dir, self.date_input)
self.date = str(self.disk_usage["date"].unique()[0])
self.users = self.disk_usage["userid"]
self.usage = self.disk_usage["usageKB"] / (1024 ** 2)
self.usage_gb = self.disk_usage["usageKB"] / (1024 ** 2)
self.set_callbacks(app)
self.theme = app.webviz_settings["theme"]

@property
def layout(self):
Expand All @@ -43,27 +56,28 @@ def layout(self):
as of {self.date}."
),
dcc.RadioItems(
id=self.plot_type_id,
id=self.uuid("plot_type"),
options=[
{"label": i, "value": i} for i in ["Pie chart", "Bar chart"]
],
value="Pie chart",
),
wcc.Graph(id=self.chart_id),
wcc.Graph(id=self.uuid("chart")),
]
)

def set_callbacks(self, app):
@app.callback(
Output(self.chart_id, "figure"), [Input(self.plot_type_id, "value")]
Output(self.uuid("chart"), "figure"),
[Input(self.uuid("plot_type"), "value")],
)
def _update_plot(plot_type):
if plot_type == "Pie chart":
data = [
{
"values": self.usage,
"values": self.usage_gb,
"labels": self.users,
"text": (self.usage).map("{:.2f} GB".format),
"text": (self.usage_gb).map("{:.2f} GB".format),
"textinfo": "label",
"textposition": "inside",
"hoverinfo": "label+text",
Expand All @@ -75,33 +89,55 @@ def _update_plot(plot_type):
elif plot_type == "Bar chart":
data = [
{
"y": self.usage,
"y": self.usage_gb,
"x": self.users,
"text": (self.usage).map("{:.2f} GB".format),
"text": (self.usage_gb).map("{:.2f} GB".format),
"hoverinfo": "x+text",
"type": "bar",
}
]
layout = {
"yaxis": {"title": "Usage in Gigabytes", "family": "Equinor"},
"xaxis": {"title": "User name", "family": "Equinor"},
"yaxis": {"title": "Usage in Gigabytes"},
"xaxis": {"title": "User name"},
}

layout["height"] = 800
layout["width"] = 1000
layout["font"] = {"family": "Equinor"}
layout["hoverlabel"] = {"font": {"family": "Equinor"}}

return {"data": data, "layout": layout}
return {"data": data, "layout": self.theme.create_themed_layout(layout)}

def add_webvizstore(self):
return [(get_disk_usage, [{"scratch_dir": self.scratch_dir}])]
return [
(
get_disk_usage,
[{"scratch_dir": self.scratch_dir, "date": self.date_input}],
)
]


@CACHE.memoize(timeout=CACHE.TIMEOUT)
@webvizstore
def get_disk_usage(scratch_dir) -> pd.DataFrame:
df = pd.read_csv(scratch_dir / "disk_usage.csv")

last_date = sorted(list(df["date"].unique()))[-1]
return df.loc[df["date"] == last_date]
def get_disk_usage(scratch_dir, date) -> pd.DataFrame:
if date is None:
today = datetime.datetime.today()
for i in range(7):
date = today - datetime.timedelta(days=i)
try:
return pd.read_csv(
scratch_dir
/ ".disk_usage"
/ f"disk_usage_user_{date.strftime('%Y-%m-%d')}.csv"
).assign(date=date.strftime("%Y-%m-%d"))
except FileNotFoundError:
continue
raise FileNotFoundError(
f"No disk usage file found for last week in {scratch_dir}."
)
try:
return pd.read_csv(
scratch_dir / ".disk_usage" / f"disk_usage_user_{date}.csv"
).assign(date=date)
except FileNotFoundError:
raise FileNotFoundError(
f"No disk usage file found for {date} in {scratch_dir}."
)

0 comments on commit 2636ff0

Please sign in to comment.