cache fix
Browse files- .vscode/launch.json +1 -1
- scribe_python_client/client.py +98 -97
.vscode/launch.json
CHANGED
|
@@ -85,7 +85,7 @@
|
|
| 85 |
"console": "internalConsole",
|
| 86 |
"args": [
|
| 87 |
"--api-call",
|
| 88 |
-
"get-dataset-tables-
|
| 89 |
"--env", "prod",
|
| 90 |
],
|
| 91 |
"env": {
|
|
|
|
| 85 |
"console": "internalConsole",
|
| 86 |
"args": [
|
| 87 |
"--api-call",
|
| 88 |
+
"get-dataset-tables-dict",
|
| 89 |
"--env", "prod",
|
| 90 |
],
|
| 91 |
"env": {
|
scribe_python_client/client.py
CHANGED
|
@@ -7,7 +7,7 @@ import jwt
|
|
| 7 |
import urllib #Needed for avoiding using requests, which modified the S3 presigned URL
|
| 8 |
from urllib.parse import urlparse, parse_qs
|
| 9 |
import csv
|
| 10 |
-
|
| 11 |
|
| 12 |
from .utils import convert_timestamps, send_post_request, send_get_request, decode_jwt_token, querystr_to_json, att2statement
|
| 13 |
from .lineage_graph import create_lineage_graph
|
|
@@ -49,112 +49,113 @@ class ScribeClient:
|
|
| 49 |
logging.error(f"Error parsing timestamp: {e}")
|
| 50 |
return True
|
| 51 |
return (datetime.datetime.now() - last_update).days > 1
|
| 52 |
-
|
|
|
|
| 53 |
username=None, password=None):
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
def update_from_cached_data(cached_data):
|
| 57 |
-
if "datasets" in cached_data:
|
| 58 |
-
self.datasets = cached_data["datasets"]
|
| 59 |
-
self.dataset_ids = self.create_dataset_id_map(self.datasets)
|
| 60 |
-
else:
|
| 61 |
-
logging.error("No datasets in provided cached data")
|
| 62 |
-
# product_list intentionally not loaded from cache
|
| 63 |
-
if "team_name" in cached_data:
|
| 64 |
-
self.team_name = cached_data["team_name"]
|
| 65 |
-
else:
|
| 66 |
-
logging.error("No team name in provided cached data")
|
| 67 |
-
if "team_info" in cached_data:
|
| 68 |
-
self.team_info = cached_data["team_info"]
|
| 69 |
-
else:
|
| 70 |
-
logging.error("No team info in provided cached data")
|
| 71 |
-
|
| 72 |
-
# Default cache file path
|
| 73 |
-
cache_dir = os.path.join(os.getcwd(), ".scribe")
|
| 74 |
-
cache_file = os.path.join(cache_dir, "cache.json")
|
| 75 |
-
loaded_cache = None
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
# Check file age
|
| 82 |
-
file_mtime = datetime.datetime.fromtimestamp(os.path.getmtime(cache_file))
|
| 83 |
-
if (datetime.datetime.now() - file_mtime).days <= 1:
|
| 84 |
-
with open(cache_file, "r") as f:
|
| 85 |
-
loaded_cache = json.load(f)
|
| 86 |
-
else:
|
| 87 |
-
logging.info("Cache file is older than one day, will refresh cache.")
|
| 88 |
else:
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
else:
|
| 94 |
-
loaded_cache = cached_data
|
| 95 |
-
|
| 96 |
-
if ignore_env:
|
| 97 |
-
self.api_token = api_token
|
| 98 |
-
else:
|
| 99 |
-
self.api_token = api_token or os.getenv('SCRIBE_TOKEN', None)
|
| 100 |
-
if not self.api_token:
|
| 101 |
-
logging.error("No API token provided. Please set the SCRIBE_TOKEN environment variable or pass it as an argument.")
|
| 102 |
-
return
|
| 103 |
-
self.username = username
|
| 104 |
-
self.password = password
|
| 105 |
-
self.env = env
|
| 106 |
-
self.base_url = base_url
|
| 107 |
-
|
| 108 |
-
if env == "prod":
|
| 109 |
-
self.superset_base_url = "https://superset.scribesecurity.com"
|
| 110 |
-
else:
|
| 111 |
-
self.superset_base_url = f"https://internal-superset.{env}.scribesecurity.com/login/"
|
| 112 |
-
|
| 113 |
-
self.refresh_interval = refresh_interval
|
| 114 |
-
self.last_refresh = datetime.datetime.now() - datetime.timedelta(days=5) # Initialize to a time in the past
|
| 115 |
-
|
| 116 |
-
self.jwt_token = None
|
| 117 |
-
self.superset_token = None
|
| 118 |
-
self.dataset_ids = None
|
| 119 |
-
self.datasets = None
|
| 120 |
-
self.product_list = None
|
| 121 |
-
self.team_id = None
|
| 122 |
-
self.team_name = None
|
| 123 |
-
self.team_info = None
|
| 124 |
-
p = None
|
| 125 |
-
if self.api_token:
|
| 126 |
-
self.last_refresh = datetime.datetime.now() # - datetime.timedelta(hours=5)
|
| 127 |
-
self.refresh_data(force=True)
|
| 128 |
-
|
| 129 |
-
# Use loaded_cache if available and not stale, else refresh and save
|
| 130 |
-
if not loaded_cache or self.need_cache_data_update(loaded_cache):
|
| 131 |
-
d = self.get_datasets()
|
| 132 |
-
if not d:
|
| 133 |
-
logging.error("Refresh: Failed to update dataset IDs")
|
| 134 |
else:
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
if not p:
|
| 139 |
-
logging.error("Refresh: Failed to update product list")
|
| 140 |
-
logging.error(f"Using previous product list\n{self.product_list}")
|
| 141 |
else:
|
| 142 |
-
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
| 147 |
|
| 148 |
-
|
|
|
|
| 149 |
try:
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
except Exception as e:
|
| 155 |
-
logging.error(f"Error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
else:
|
| 157 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
def validate_dataset(self, dataset_name):
|
| 160 |
if not self.dataset_ids:
|
|
|
|
| 7 |
import urllib #Needed for avoiding using requests, which modified the S3 presigned URL
|
| 8 |
from urllib.parse import urlparse, parse_qs
|
| 9 |
import csv
|
| 10 |
+
import pathlib
|
| 11 |
|
| 12 |
from .utils import convert_timestamps, send_post_request, send_get_request, decode_jwt_token, querystr_to_json, att2statement
|
| 13 |
from .lineage_graph import create_lineage_graph
|
|
|
|
| 49 |
logging.error(f"Error parsing timestamp: {e}")
|
| 50 |
return True
|
| 51 |
return (datetime.datetime.now() - last_update).days > 1
|
| 52 |
+
|
| 53 |
+
def __init__(self, api_token=None, ignore_env = False, base_url="https://api.scribesecurity.com", refresh_interval=600, cached_data=None, env = "prod",
|
| 54 |
username=None, password=None):
|
| 55 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
def update_from_cached_data(cached_data):
|
| 58 |
+
if "datasets" in cached_data:
|
| 59 |
+
self.datasets = cached_data["datasets"]
|
| 60 |
+
self.dataset_ids = self.create_dataset_id_map(self.datasets)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
else:
|
| 62 |
+
logging.error("No datasets in provided cached data")
|
| 63 |
+
# product_list intentionally not loaded from cache
|
| 64 |
+
if "team_name" in cached_data:
|
| 65 |
+
self.team_name = cached_data["team_name"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
else:
|
| 67 |
+
logging.error("No team name in provided cached data")
|
| 68 |
+
if "team_info" in cached_data:
|
| 69 |
+
self.team_info = cached_data["team_info"]
|
|
|
|
|
|
|
|
|
|
| 70 |
else:
|
| 71 |
+
logging.error("No team info in provided cached data")
|
| 72 |
|
| 73 |
+
# Default cache file path
|
| 74 |
+
cache_dir = os.path.join(os.getcwd(), ".scribe")
|
| 75 |
+
cache_file = os.path.join(cache_dir, "cache.json")
|
| 76 |
+
loaded_cache = None
|
| 77 |
|
| 78 |
+
# Only load cache if not explicitly provided
|
| 79 |
+
if cached_data is None:
|
| 80 |
try:
|
| 81 |
+
if os.path.exists(cache_file):
|
| 82 |
+
# Check file age
|
| 83 |
+
file_mtime = datetime.datetime.fromtimestamp(os.path.getmtime(cache_file))
|
| 84 |
+
if (datetime.datetime.now() - file_mtime).days <= 1:
|
| 85 |
+
with open(cache_file, "r") as f:
|
| 86 |
+
loaded_cache = json.load(f)
|
| 87 |
+
else:
|
| 88 |
+
logging.info("Cache file is older than one day, will refresh cache.")
|
| 89 |
+
else:
|
| 90 |
+
# Ensure .scribe directory exists
|
| 91 |
+
pathlib.Path(cache_dir).mkdir(parents=True, exist_ok=True)
|
| 92 |
except Exception as e:
|
| 93 |
+
logging.error(f"Error loading cache file: {e}")
|
| 94 |
+
else:
|
| 95 |
+
loaded_cache = cached_data
|
| 96 |
+
|
| 97 |
+
if ignore_env:
|
| 98 |
+
self.api_token = api_token
|
| 99 |
+
else:
|
| 100 |
+
self.api_token = api_token or os.getenv('SCRIBE_TOKEN', None)
|
| 101 |
+
if not self.api_token:
|
| 102 |
+
logging.error("No API token provided. Please set the SCRIBE_TOKEN environment variable or pass it as an argument.")
|
| 103 |
+
return
|
| 104 |
+
self.username = username
|
| 105 |
+
self.password = password
|
| 106 |
+
self.env = env
|
| 107 |
+
self.base_url = base_url
|
| 108 |
+
|
| 109 |
+
if env == "prod":
|
| 110 |
+
self.superset_base_url = "https://superset.scribesecurity.com"
|
| 111 |
else:
|
| 112 |
+
self.superset_base_url = f"https://internal-superset.{env}.scribesecurity.com/login/"
|
| 113 |
+
|
| 114 |
+
self.refresh_interval = refresh_interval
|
| 115 |
+
self.last_refresh = datetime.datetime.now() - datetime.timedelta(days=5) # Initialize to a time in the past
|
| 116 |
+
|
| 117 |
+
self.jwt_token = None
|
| 118 |
+
self.superset_token = None
|
| 119 |
+
self.dataset_ids = None
|
| 120 |
+
self.datasets = None
|
| 121 |
+
self.product_list = None
|
| 122 |
+
self.team_id = None
|
| 123 |
+
self.team_name = None
|
| 124 |
+
self.team_info = None
|
| 125 |
+
p = None
|
| 126 |
+
if self.api_token:
|
| 127 |
+
self.last_refresh = datetime.datetime.now() # - datetime.timedelta(hours=5)
|
| 128 |
+
self.refresh_data(force=True)
|
| 129 |
+
|
| 130 |
+
# Use loaded_cache if available and not stale, else refresh and save
|
| 131 |
+
if not loaded_cache or self.need_cache_data_update(loaded_cache):
|
| 132 |
+
d = self.get_datasets()
|
| 133 |
+
if not d:
|
| 134 |
+
logging.error("Refresh: Failed to update dataset IDs")
|
| 135 |
+
else:
|
| 136 |
+
self.datasets = d
|
| 137 |
+
self.dataset_ids = self.create_dataset_id_map(d)
|
| 138 |
+
p = self.get_products(force=True)
|
| 139 |
+
if not p:
|
| 140 |
+
logging.error("Refresh: Failed to update product list")
|
| 141 |
+
logging.error(f"Using previous product list\n{self.product_list}")
|
| 142 |
+
else:
|
| 143 |
+
self.product_list = p
|
| 144 |
+
|
| 145 |
+
t = self.get_team_info()
|
| 146 |
+
if not t:
|
| 147 |
+
logging.error("Failed to get team data.")
|
| 148 |
+
|
| 149 |
+
# Save cache (excluding product_list)
|
| 150 |
+
try:
|
| 151 |
+
pathlib.Path(cache_dir).mkdir(parents=True, exist_ok=True)
|
| 152 |
+
cache_data = self.get_chache_data()
|
| 153 |
+
with open(cache_file, "w") as f:
|
| 154 |
+
json.dump(cache_data, f, indent=2)
|
| 155 |
+
except Exception as e:
|
| 156 |
+
logging.error(f"Error saving cache file: {e}")
|
| 157 |
+
else:
|
| 158 |
+
update_from_cached_data(loaded_cache)
|
| 159 |
|
| 160 |
def validate_dataset(self, dataset_name):
|
| 161 |
if not self.dataset_ids:
|