ew file: docs/tests.md
All checks were successful
Casa Home Assistant CI/CD Pipeline / deploy (push) Successful in 17s
All checks were successful
Casa Home Assistant CI/CD Pipeline / deploy (push) Successful in 17s
This commit is contained in:
55
docs/tests.md
Normal file
55
docs/tests.md
Normal file
@@ -0,0 +1,55 @@
|
||||
# tests
|
||||
|
||||
## logs from loki
|
||||
|
||||
```python
|
||||
# loki-get-last-to-sensor.py
|
||||
import sys, os, requests, datetime, time, json
|
||||
|
||||
url = os.environ.get("LOKI_ADDRESS")
|
||||
|
||||
if not url:
|
||||
print(json.dumps({"state": "error", "log_message": "LOKI_ADDRESS is not set or empty"}))
|
||||
sys.exit(1)
|
||||
|
||||
query = os.environ.get("LOKI_QUERY")
|
||||
if not query:
|
||||
print(json.dumps({"state": "error", "log_message": "LOKI_ADDRESS is not set or empty"}))
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
end = int(time.time()) * 1_000_000_000
|
||||
start = (int(time.time()) - 24*60*60) * 1_000_000_000
|
||||
|
||||
|
||||
params = {
|
||||
"query": query,
|
||||
"limit": 1,
|
||||
"direction": "backward",
|
||||
"start": str(start),
|
||||
"end": str(end),
|
||||
}
|
||||
|
||||
resp = requests.get(url, params=params)
|
||||
data = resp.json()
|
||||
|
||||
try:
|
||||
ns = int(data["data"]["result"][0]["values"][0][0])
|
||||
ts = ns / 1_000_000_000
|
||||
dt = datetime.datetime.fromtimestamp(ts).strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
print(json.dumps({
|
||||
"state": dt,
|
||||
}))
|
||||
|
||||
except Exception:
|
||||
print(json.dumps({"state": "unknown"}))
|
||||
```
|
||||
|
||||
```yaml
|
||||
# example of the sensor
|
||||
name: git_limbosolutions_com_last_pbs_backup
|
||||
command: >
|
||||
LOKI_ADDRESS=${LOKI_ADDRESS} LOKI_QUERY="{job=\"git-limbosolutions-com/pbs-backup\"} |= \"INFO: Finished Backup\"" python3 /config/scripts/loki-get-last-to-sensor.py
|
||||
value_template: "{{ value }}"
|
||||
```
|
||||
Reference in New Issue
Block a user