Skip to content

Instantly share code, notes, and snippets.

@tuxedocat
Created April 28, 2016 08:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tuxedocat/75e58330c5c133eff8ce657644914d68 to your computer and use it in GitHub Desktop.
Save tuxedocat/75e58330c5c133eff8ce657644914d68 to your computer and use it in GitHub Desktop.
diskusage chart
#!/bin/bash
du -ah --max-depth 3 \
| sort -rh \
| egrep "./(\w+/){2,3}" \
| sed "s@./@$(pwd)/@" > diskusage
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import os
import pathlib
import click
from click.testing import CliRunner
from typing import Tuple, List, Dict, Callable
def _to_megabytes(human_readable: str = None) -> str:
UNITS = {'T': 10 ** 6, 'G': 10 ** 3, 'M': 1.0, 'K': 10 ** -3}
unit = human_readable[-1]
num = human_readable[:-1]
if unit in UNITS:
return float(num) * UNITS[unit] # As megabytes
else:
return None
def parselinegen(commonprefix: str = None) -> Callable:
def parseline(line: Tuple[str, str] = None) -> Dict[str, str]:
row = {}
row.update({'MB': _to_megabytes(line[0])})
fullpath = line[1]
row.update({'fullpath': fullpath})
restpath = fullpath.replace(commonprefix, '')
row.update({'restpath': restpath})
_splitted = restpath.split('/')
for i, d in enumerate(_splitted):
if d:
row.update({'level_{}'.format(i): d})
return row
return parseline
def parse(file: pathlib.Path = None) -> List[Dict]:
with file.open('r', encoding='UTF-8') as f:
lines = [tuple(l.strip().split('\t')) for l in f.readlines()]
commonprefix = os.path.commonprefix([i[1] for i in lines])
_parseline_func = parselinegen(commonprefix)
parsed_dicts = list(map(_parseline_func, lines))
return parsed_dicts
def plot(data: List[Dict] = None, plot_type: str = None, n: int = 20, title: str = None) -> None:
df = pd.DataFrame(data)
if plot_type == 'bar':
bar_chart(df, n=n, title=title)
else:
raise NotImplementedError
def bar_chart(df: pd.DataFrame = None, n: int = 20, title: str = None) -> None:
sns.set(context='paper', font='Tahoma', font_scale=0.5)
f, ax = plt.subplots(figsize=(10, 25))
_df = df.head(n)
g = sns.factorplot(x='MB', y='restpath', kind="bar", data=_df)
plt.title(title)
plt.ylabel('')
plt.savefig('diskusage.png', dpi=300, bbox_inches='tight')
return None
def main(input, plot_type, num_plot):
input_file = pathlib.Path(input)
if not input_file.exists():
raise OSError('File not found')
rows = parse(input_file)
plot(rows, plot_type=plot_type, n=num_plot, title='Disk Usage')
@click.command()
@click.argument('input')
@click.option('--plot-type', '-t', default='bar')
@click.option('--num-plot', '-n', default=20)
def cli(**kwargs):
main(**kwargs)
def test():
runner = CliRunner()
command = ['./sample', '-t', 'bar', '-n', 30]
result = runner.invoke(cli, command)
print(result.output)
print(result.exception)
assert result.exit_code == 0
assert result.exception is None
if __name__ == '__main__':
cli()
11T /lustre/work/moooo/prrr/meowbbcd2c04
8.4T /lustre/work/fooo/bam/meowc2dfced2
5.8T /lustre/work/foo/bar/meowfa6d4761
3.9T /lustre/work/foo/bar/meow3a5a374e
3.7T /lustre/work/foo/bar/meowfb56310c
3.7T /lustre/work/foo/bar/meow050c9eff
3.5T /lustre/work/foo/bar/meowb3ae747a
3.3T /lustre/work/foo/bar/meow15f58e6b
3.3T /lustre/work/foo/bar/meow9bbe31f3
2.7T /lustre/work/foo/bar/meow4a174b82
2.4T /lustre/work/foo/bar/meow17471474
2.1T /lustre/work/foo/bar/meowf47f5669
2.0T /lustre/work/foo/bar/meowfa8707de
1.9T /lustre/work/foo/bar/meow358c6292
1.8T /lustre/work/foo/bar/meowe2ecee04
1.8T /lustre/work/foo/bar/meow3a4c7965
1.6T /lustre/work/foo/bar/meowc1b28d83
1.4T /lustre/work/foo/bar/meowcd4e9230
1.2T /lustre/work/foo/bar/meow46d19e8e
1.1T /lustre/work/foo/bar/meow288141b4
1006G /lustre/work/foo/bar/meow6a0758f3
991G /lustre/work/foo/bar/meow624103e3
958G /lustre/work/foo/bar/meow1d8bc43b
956G /lustre/work/foo/bar/meowad56e555
783G /lustre/work/foo/bar/meowc02b3318
745G /lustre/work/foo/bar/meowca23381f
732G /lustre/work/foo/bar/meow498e5b07
617G /lustre/work/foo/bar/meow4893c6e1
600G /lustre/work/foo/bar/meow6ee09f1e
583G /lustre/work/foo/bar/meow1e61fda3
525G /lustre/work/foo/bar/meow77a41290
504G /lustre/work/foo/bar/meow69af1de5
471G /lustre/work/foo/bar/meowbe6d3c4a
466G /lustre/work/foo/bar/meow50790180
409G /lustre/work/foo/bar/meow12c2ba17
378G /lustre/work/foo/bar/meow6c7a7188
358G /lustre/work/foo/bar/meow5134bdb1
354G /lustre/work/foo/bar/meowfe45927e
337G /lustre/work/foo/bar/meow3a897ee2
299G /lustre/work/foo/bar/meowa15b01cb
299G /lustre/work/foo/bar/meow651df071
292G /lustre/work/foo/bar/meowb3a05ac9
274G /lustre/work/foo/bar/meow9b8ecd9b
251G /lustre/work/foo/bar/meow483e2dca
245G /lustre/work/foo/bar/meowc853e931
244G /lustre/work/foo/bar/meow27b620b5
183G /lustre/work/foo/bar/meow68ca93c7
182G /lustre/work/foo/bar/meow35d35c00
182G /lustre/work/foo/bar/meowb1f6b989
167G /lustre/work/foo/bar/meow084fbc92
148G /lustre/work/foo/bar/meow63c12d55
122G /lustre/work/foo/bar/meow4579d7f9
120G /lustre/work/foo/bar/meow8e374ab9
115G /lustre/work/foo/bar/meowf7666dde
114G /lustre/work/foo/bar/meow5173c21f
104G /lustre/work/foo/bar/meow1a942430
101G /lustre/work/foo/bar/meow6ce83588
99G /lustre/work/foo/bar/meowc192ed7c
94G /lustre/work/foo/bar/meowa65d2363
92G /lustre/work/foo/bar/meow2c875194
89G /lustre/work/foo/bar/meowa4839f8e
84G /lustre/work/foo/bar/meowbdde643e
82G /lustre/work/foo/bar/meow97cc7b86
69G /lustre/work/foo/bar/meow69632ec1
68G /lustre/work/foo/bar/meow8fa6ab92
48G /lustre/work/foo/bar/meow27945059
48G /lustre/work/foo/bar/meow11af595d
48G /lustre/work/foo/bar/meow288cade7
46G /lustre/work/foo/bar/meow7a901c8f
44G /lustre/work/foo/bar/meow95861a6c
43G /lustre/work/foo/bar/meow27bcbec7
38G /lustre/work/foo/bar/meow30edf49d
35G /lustre/work/foo/bar/meowd941810f
33G /lustre/work/foo/bar/meowd0c4d9e3
31G /lustre/work/foo/bar/meowbd441869
29G /lustre/work/foo/bar/meowfeacb918
16G /lustre/work/foo/bar/meow7b6692a4
16G /lustre/work/foo/bar/meow2a788f73
15G /lustre/work/foo/bar/meow77b798e7
14G /lustre/work/foo/bar/meow3e025fd3
12G /lustre/work/foo/bar/meow83c250e6
8.9G /lustre/work/foo/bar/meow5fbbca96
8.3G /lustre/work/foo/bar/meowfc4f8f23
7.8G /lustre/work/foo/bar/meow239f35a8
7.6G /lustre/work/foo/bar/meow66aa9840
7.6G /lustre/work/foo/bar/meowa0f2401c
6.8G /lustre/work/foo/bar/meow4a93c364
6.5G /lustre/work/foo/bar/meow2013cd92
6.0G /lustre/work/foo/bar/meow5e926d18
5.4G /lustre/work/foo/bar/meow69e1a63c
5.2G /lustre/work/foo/bar/meow3d5645f2
4.8G /lustre/work/foo/bar/meowccc7a8a5
4.4G /lustre/work/foo/bar/meow9c785f01
3.9G /lustre/work/foo/bar/meow1cbe9601
2.0G /lustre/work/foo/bar/meow0c641d41
1.9G /lustre/work/foo/bar/meowa36ffeb3
1.9G /lustre/work/foo/bar/meow5015d9d5
1.7G /lustre/work/foo/bar/meow9d75ed94
1.3G /lustre/work/foo/bar/meow0ccfc349
1.1G /lustre/work/foo/bar/meow64a6d8da
1.1G /lustre/work/foo/bar/meow6c38ac13
971M /lustre/work/foo/bar/meowb2523a2c
684M /lustre/work/foo/bar/meowc74e3fef
631M /lustre/work/foo/bar/meow67c2bf9f
606M /lustre/work/foo/bar/meowa3d613b9
503M /lustre/work/foo/bar/meow940ff6f5
281M /lustre/work/foo/bar/meowc124683b
243M /lustre/work/foo/bar/meow17e07dbc
234M /lustre/work/foo/bar/meow68534dc7
229M /lustre/work/foo/bar/meowc3d7b9cb
159M /lustre/work/foo/bar/meowe7fcc404
130M /lustre/work/foo/bar/meowfabc1c87
114M /lustre/work/foo/bar/meow6816f8c8
113M /lustre/work/foo/bar/meow03d19212
83M /lustre/work/foo/bar/meow6e9db1f1
81M /lustre/work/foo/bar/meowc84fec3b
81M /lustre/work/foo/bar/meow6b587160
62M /lustre/work/foo/bar/meow1ad4272c
59M /lustre/work/foo/bar/meowc0bd3c9f
32M /lustre/work/foo/bar/meow21e78483
31M /lustre/work/foo/bar/meow86b2c1b0
31M /lustre/work/foo/bar/meowe633c48e
16M /lustre/work/foo/bar/meow93c63972
16M /lustre/work/foo/bar/meow88d99969
15M /lustre/work/foo/bar/meowfafd69a5
13M /lustre/work/foo/bar/meow64bded2c
11M /lustre/work/foo/bar/meowaa60da34
7.4M /lustre/work/foo/bar/meowb56c83e1
7.4M /lustre/work/foo/bar/meowab4d90f0
4.9M /lustre/work/foo/bar/meowcd79ac2e
4.9M /lustre/work/foo/bar/meowf8f0f131
4.9M /lustre/work/foo/bar/meow5d290d40
3.3M /lustre/work/foo/bar/meow812079ac
2.5M /lustre/work/foo/bar/meowf9ed4dd3
84K /lustre/work/foo/bar/meow7ce2a1ae
32K /lustre/work/foo/bar/meowcf94d1be
32K /lustre/work/foo/bar/meow759bdcac
16K /lustre/work/foo/bar/meow93b8efd3
8.0K /lustre/work/foo/bar/meow7791b568
8.0K /lustre/work/foo/bar/meow9a51df54
8.0K /lustre/work/foo/bar/meowd5329114
4.0K /lustre/work/foo/bar/meow65e3ee81
4.0K /lustre/work/foo/bar/meow76d392c3
4.0K /lustre/work/foo/bar/meow4b2bb2c6
4.0K /lustre/work/foo/bar/meow4aa6734b
4.0K /lustre/work/foo/bar/meowa8e4936f
4.0K /lustre/work/foo/bar/meow237c631d
4.0K /lustre/work/foo/bar/meow1eac908c
4.0K /lustre/work/foo/bar/meow750f8ac1
4.0K /lustre/work/foo/bar/meowa10da766
4.0K /lustre/work/foo/bar/meowb058305d
4.0K /lustre/work/foo/bar/meowf2fcc9cf
4.0K /lustre/work/foo/bar/meowb43e6c07
4.0K /lustre/work/foo/bar/meow01abd0dd
0 /lustre/work/foo/bar/meow790d24fd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment