-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcheck-metadata.py
More file actions
70 lines (48 loc) · 1.67 KB
/
Copy pathcheck-metadata.py
File metadata and controls
70 lines (48 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import json
import sys
from datetime import datetime, timezone
from functools import cache
from pathlib import Path
import requests
def get_agency_url(agency: str):
path = Path("./metadata.json")
if not path.exists():
raise RuntimeError("Metadata file not found")
config = json.loads(path.read_text())
return config[agency]
@cache
def get_metadata(url: str):
response = requests.get(url, timeout=30)
response.raise_for_status()
return response.json()
def _check_metadata_ts(url: str, ts_field: str):
now = datetime.now(tz=timezone.utc)
data = get_metadata(url)
ts = data["db"][ts_field]
timestamp = datetime.fromisoformat(ts)
if not all((timestamp.year == now.year, timestamp.month == now.month, timestamp.day == now.day)):
raise RuntimeError(f"Timestamp mismatch in {ts_field}: {ts}")
def check_metadata_file_ts(url: str):
_check_metadata_ts(url, "file_ts")
def check_metadata_load_ts(url: str):
_check_metadata_ts(url, "load_ts")
def check_metadata_users(url: str):
data = get_metadata(url)
users = data["db"]["users"]
if users < 1:
raise RuntimeError("Database has no users")
def check_metadata_eligibility(url: str):
data = get_metadata(url)
eligibility = data["db"]["eligibility"]
if len(eligibility) < 1:
raise RuntimeError("Database has no eligibility")
if __name__ == "__main__":
args = sys.argv
if len(args) < 2:
raise RuntimeError("Usage: check-metadata AGENCY")
agency = args[1]
url = get_agency_url(agency)
check_metadata_file_ts(url)
check_metadata_load_ts(url)
check_metadata_users(url)
check_metadata_eligibility(url)