Skip to content
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
8 changes: 6 additions & 2 deletions docs/basic-command-line-interface-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,17 @@ robotdashboard -r index=0,index=1:4;9,index=10
robotdashboard --removeruns 'run_start=2024-07-30 15:27:20.184407,index=20'
robotdashboard -r alias=some_cool_alias,tag=prod,tag=dev -r alias=alias12345
robotdashboard -r limit=10
robotdashboard -r age=10d # (y)ear/(d)ay/(h)our/(m)inute/(s)econd supported
robotdashboard -r age=-10d
```
- Optional: `-r` or `--removeruns` specifies one or more runs to remove.
- Multiple values are separated by commas (,).
- Must specify data types: index, run_start, alias, tag or limit.
- Index ranges use `:` for ranges and `;` for lists.
- Quotation marks are required when spaces exist in identifiers.
- With limit=10 only the 10 most recent runs will be kept, all others will be removed.
- Quotation marks are required when spaces exist in identifiers.
- With limit=10 only the 10 most recent runs will be kept, all others will be removed.
- With age=10d only runs _**older**_ than 10 days will be removed
- With age=-10d only runs _**younger**_ than 10 days will be removed

## Customizing the Dashboard

Expand Down
3 changes: 3 additions & 0 deletions robotframework_dashboard/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,9 @@ def _parse_arguments(self):
" • '-r run_start=2024-07-30 15:27:20.184407' -> remove specified run\n"
" • '-r alias=some_alias,tag=prod'\n"
" • '-r limit=10' -> keep only the 10 most recent runs\n"
" • '-r age=10d' -> remove runs older than 10 days\n"
Comment thread
timdegroot1996 marked this conversation as resolved.
" • '-r age=-10d' -> remove runs younger than 10 days\n"
" • (y)ear/(d)ay/(h)our/(m)inute/(s)econd supported\n"
),
action="append",
nargs="*",
Expand Down
55 changes: 54 additions & 1 deletion robotframework_dashboard/database.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import sqlite3
import re
from pathlib import Path
from .queries import *
from .abstractdb import AbstractDatabaseProcessor
from time import time
from datetime import datetime, timezone
from datetime import datetime, timezone, timedelta
from typing import Union

# Explicit adapter for datetime -> ISO string, replacing the deprecated default
Expand Down Expand Up @@ -399,6 +400,8 @@ def remove_runs(self, remove_runs: list):
console += self._remove_by_tag(run, run_starts, run_tags)
elif "limit=" in run:
console += self._remove_by_limit(run, run_starts)
elif "age=" in run:
console += self._remove_by_age(run, run_starts)
else:
print(
f" ERROR: incorrect usage of the remove_run feature ({run}), check out robotdashboard --help for instructions"
Expand Down Expand Up @@ -490,6 +493,37 @@ def _remove_by_limit(self, run: str, run_starts: list):
console += f" Removed run from the database: index={index}, run_start={run_starts[index]}\n"
return console

def _remove_by_age(self, run_query: str, run_starts: list):
console = ""
try:
clean_query = run_query.replace("age=", "")
mod, delta = self.parse_time_range(clean_query)
except ValueError as e:
return f" ERROR: {e}"
Comment thread
timdegroot1996 marked this conversation as resolved.
cutoff = datetime.now(timezone.utc)-delta
targets = []
for r in run_starts:
try:
run_dt = datetime.fromisoformat(r)
if run_dt.tzinfo is None:
run_dt = run_dt.replace(tzinfo=timezone.utc)
if mod == "+":
if run_dt < cutoff:
targets.append(r)
elif mod == "-":
if run_dt > cutoff:
targets.append(r)
except ValueError as e:
print(f" WARNING: Skipping invalid timestamp: '{r}' ({e})")
if not targets:
console += f" WARNING: no runs were removed as no runs were within range {clean_query}"
return console
for run_to_remove in targets:
self._remove_run(run_to_remove)
print(f" Removed run from the database: run_start={run_to_remove}")
console += f" Removed run from the database: run_start={run_to_remove}\n"
return console

def _remove_run(self, run_start: str):
"""Helper function to remove the data from all tables"""
self.connection.cursor().execute(DELETE_FROM_RUNS.format(run_start=run_start))
Expand All @@ -510,6 +544,25 @@ def vacuum_database(self):
print(f" Vacuumed the database in {round(end - start, 2)} seconds")
return console

def parse_time_range(self, range_str: str):
# Regex groups : [modifier] [value] [unit]
# e.g., +10d, -4h, 1y
match = re.match(r"([+-])?(\d+)([smhdy])", range_str)
if not match:
raise ValueError("Invalid format. Use e.g., 10d, +5h, -1y")
modifier, value, unit = match.groups()
value = int(value)
units = {
's': 'seconds',
'm': 'minutes',
'h': 'hours',
'd': 'days',
'y': 'days'
}
# assume year is 365 days
delta_kwargs = {units[unit]: value * (365 if unit == 'y' else 1)}
return modifier or '+', timedelta(**delta_kwargs)

def update_output_path(self, log_path: str):
"""Function to update the output_path using the log path that the server has used"""
console = ""
Expand Down
3 changes: 3 additions & 0 deletions robotframework_dashboard/js/admin_page/admin_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ function remove_outputs() {
const removeTags = document.getElementById("removeTags").value.split(",")
const removeLimit = document.getElementById("removeLimit").value
if (removeLimit != "") { data["limit"] = removeLimit }
const removeAge = document.getElementById("removeAge").value
if (removeAge != "") { data["age"] = removeAge }
for (const removeTag of removeTags) {
if (removeTag == "") { continue }
tags.push(removeTag)
Expand All @@ -126,6 +128,7 @@ function remove_outputs() {
document.getElementById("removeAliases").value = ""
document.getElementById("removeTags").value = ""
document.getElementById("removeLimit").value = ""
document.getElementById("removeAge").value = ""
const body = JSON.stringify(data)
send_request("DELETE", "/remove-outputs", body, "removeSpinner")
}
Expand Down
10 changes: 10 additions & 0 deletions robotframework_dashboard/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@
"tags": ["tag1", "tag2", "tag3"],
},
{"limit": 10},
{"age": "10d"},
{"age": "-10d"},
{"all": True},
],
"openapi_examples": {
Expand Down Expand Up @@ -140,6 +142,11 @@
"tags": ["tag1", "tag2", "tag3"],
},
},
"age": {
"summary": "Remove runs based on age threshold",
"description": "Remove runs older than a threshold (e.g., '10d') or younger than a threshold (e.g., '-10d'). Supports (y)ear/(d)ay/(h)our/(m)inute/(s)econd.",
"value": {"age": "10d"},
},
"limit": {
"summary": "Remove all but the N most recent runs",
"description": "Keep only the specified number of most recent runs, deleting the rest.",
Expand Down Expand Up @@ -246,6 +253,7 @@ class RemoveOutputs(BaseModel):
tags: Optional[List[str]] = None
all: Optional[bool] = False
limit: Optional[int] = None
age: Optional[str] = None
model_config = remove_outputs_model_config


Expand Down Expand Up @@ -607,6 +615,8 @@ async def remove_outputs_from_database(
if remove_output.tags != None:
for run in remove_output.tags:
remove_runs.append(f"tag={run}")
if remove_output.age != None:
remove_runs.append(f"age={remove_output.age}")
if remove_output.limit != None:
remove_runs.append(f"limit={remove_output.limit}")
paths_before = self.robotdashboard.get_run_paths()
Expand Down
12 changes: 11 additions & 1 deletion robotframework_dashboard/templates/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,16 @@ <h3>Remove output.xml(s) From Database</h3>
<input class="form-control form-control-sm" id="removeTags"></input>
</div>
</div>
<div class="row">
<div class="col-2 d-flex">
<p class="mt-auto">outputs to remove by age</p>
</div>
<div class="col-8">
<label class="form-label form-label-sm mt-1" for="removeAge">E.g. "10d" (delete runs older than 10 days),
or -10d (delete younger than 10 days). (y)ear/(d)ay/(h)our/(m)inute/(s)econd supported</label>
<input class="form-control form-control-sm" id="removeAge"></input>
</div>
</div>
<div class="row">
<div class="col-2 d-flex">
<p class="mt-auto">outputs to remove by limit</p>
Expand Down Expand Up @@ -401,4 +411,4 @@ <h5 class="modal-title" id="confirmModalLabel">Please Confirm</h5>
<!-- placeholder_javascript -->
</body>

</html>
</html>
3 changes: 3 additions & 0 deletions tests/robot/resources/cli_output/help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ database:
• '-r run_start=2024-07-30 15:27:20.184407' -> remove specified run
• '-r alias=some_alias,tag=prod'
• '-r limit=10' -> keep only the 10 most recent runs
• '-r age=10d' -> remove runs older than 10 days
• '-r age=-10d' -> remove runs younger than 10 days
• (y)ear/(d)ay/(h)our/(m)inute/(s)econd supported
-c PATH, --databaseclass PATH
Path to a custom database class (.py) to override the built-in SQLite engine.
• See docs for implementation details
Expand Down
Loading