Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement #4 'step' parameter #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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