Skip to content

Commit

Permalink
Feature/4 step param (#1)
Browse files Browse the repository at this point in the history
* implement tbotnz#4 with optional 'step' param

* update readme
  • Loading branch information
nmanzi authored Oct 11, 2021
1 parent bf819a2 commit a2c774e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ simple micro service for converting your RRD's to web services
### examples
- last 24 hours ```curl 127.0.0.1:9000/?rrd_path=tests/port-id15.rrd```
- epoch date time filter ```curl 127.0.0.1:9000/?rrd_path=tests/port-id15.rrd&epoch_start_time=1622109000&epoch_end_time=1624787400```
- epoch date filter with daily step ```curl 127.0.0.1:9000/?rrd_path=tests/port-id15.rrd&epoch_start_time=1630425600&epoch_end_time=1633017600&step=86400```

### rrdtool
- tested with version 1.7
9 changes: 5 additions & 4 deletions backend/RRD_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

class RRD_parser:

def __init__(self, rrd_file=None, start_time=None, end_time=None):
def __init__(self, rrd_file=None, start_time=None, end_time=None, step=None):
self.rrd_file = rrd_file
self.ds = None
self.step = None
self.step = step
self.time_format = "%Y-%m-%d %H:%M:%S"
self.check_dependc()
self.start_time = start_time
Expand Down Expand Up @@ -53,15 +53,16 @@ def get_data_source(self):
ds_val = match_obj.group(1)
if ds_val not in DS_VALS:
DS_VALS.append(ds_val)
self.step = STEP_VAL

self.step = STEP_VAL if self.step is None else self.step
self.ds = DS_VALS

def get_rrd_json(self, ds):
""" gets RRD json from rrd tool """

rrd_xport_command = f"rrdtool xport --step {self.step} DEF:data={self.rrd_file}:{ds}:AVERAGE XPORT:data:{ds} --showtime"
if self.start_time:
rrd_xport_command = f"rrdtool xport DEF:data={self.rrd_file}:{ds}:AVERAGE XPORT:data:{ds} --showtime --start {self.start_time} --end {self.end_time}"
rrd_xport_command = f"rrdtool xport DEF:data={self.rrd_file}:{ds}:AVERAGE XPORT:data:{ds} --showtime --start {self.start_time} --end {self.end_time} --step {self.step}"
result = subprocess.check_output(
rrd_xport_command,
shell=True
Expand Down
10 changes: 8 additions & 2 deletions rrdrest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
"/",
summary="Get the data from a RRD file, takes in a rrd file path"
)
async def get_rrd(rrd_path: str, epoch_start_time: Optional[int] = None, epoch_end_time: Optional[int] = None):
async def get_rrd(
rrd_path: str,
epoch_start_time: Optional[int] = None,
epoch_end_time: Optional[int] = None,
step: Optional[int] = None
):
is_file = os.path.isfile(rrd_path)
if is_file:
if (epoch_start_time and not epoch_end_time) or (epoch_end_time and not epoch_start_time):
Expand All @@ -25,7 +30,8 @@ async def get_rrd(rrd_path: str, epoch_start_time: Optional[int] = None, epoch_e
rr = RRD_parser(
rrd_file=rrd_path,
start_time=epoch_start_time,
end_time=epoch_end_time
end_time=epoch_end_time,
step=step
)
r = rr.compile_result()
return r
Expand Down

0 comments on commit a2c774e

Please sign in to comment.