Skip to content

Commit a0aebfb

Browse files
committed
feat(cache): rewrite caching script
1 parent b476247 commit a0aebfb

File tree

2 files changed

+96
-26
lines changed

2 files changed

+96
-26
lines changed

backend/src/gee/caching_script.py

Lines changed: 95 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,99 @@
1-
import json
2-
3-
content = [
4-
{"timestamp": 1493251200, "value": 0.6265267304234295},
5-
{"timestamp": 1494720000, "value": 0.68603163673333},
6-
{"timestamp": 1494979200, "value": 0.755257128311451},
7-
]
8-
var_name = "msavi_daily_cache"
9-
file_name = "../cache/temp_cache.py"
10-
INDENT = " "
11-
12-
def combine_chache_with_update():
13-
# update existing cache
14-
return
15-
16-
def write_year(file, year, results):
17-
file.write(f"{INDENT}# Year {year}\n")
18-
19-
def write_results_to_cache(results: list, var_name: str, file_name: str):
20-
with open(file_name, "w") as file:
21-
file.write(f"{var_name} = [\n")
22-
for day in results:
23-
file.write(f"{INDENT}{json.dumps(day)},\n")
1+
import os
2+
from enum import Enum
3+
4+
from ..constants import IndexType
5+
from ..cache import ndvi_cache
6+
from ..cache import msavi_cache
7+
8+
9+
TIMESTAMP = "timestamp"
10+
CACHE_PATH = "../cache/"
11+
12+
13+
class ChosenCache(Enum):
14+
NDVI = ndvi_cache.ndvi_daily_cache
15+
MSAVI = msavi_cache.msavi_daily_cache
16+
17+
18+
index_type_to_cache = {
19+
IndexType.NDVI: ChosenCache.NDVI,
20+
IndexType.MSAVI: ChosenCache.MSAVI,
21+
}
22+
23+
24+
class CacheName(Enum):
25+
NDVI = "ndvi_daily_cache"
26+
MSAVI = "msavi_daily_cache"
27+
28+
29+
index_type_to_cache_name = {
30+
IndexType.NDVI: CacheName.NDVI,
31+
IndexType.MSAVI: CacheName.MSAVI,
32+
}
33+
34+
35+
class CacheFileName(Enum):
36+
NDVI = "ndvi_cache.py"
37+
MSAVI = "msavi_cache.py"
38+
39+
40+
index_type_to_cache_file = {
41+
IndexType.NDVI: CacheFileName.NDVI,
42+
IndexType.MSAVI: CacheFileName.MSAVI,
43+
}
44+
45+
46+
def load_cache_file(index_type):
47+
try:
48+
cache_list = index_type_to_cache[index_type].value
49+
if isinstance(cache_list, list) and all(
50+
isinstance(item, dict) for item in cache_list
51+
):
52+
return cache_list
53+
except (SyntaxError, ValueError, IndexError):
54+
pass
55+
56+
return []
57+
58+
59+
def save_cache_to_file(cache, index_type):
60+
"""Save the updated cache to the file, maintaining the variable structure."""
61+
dirname = os.path.dirname(__file__)
62+
filepath = os.path.join(
63+
dirname, CACHE_PATH, index_type_to_cache_file[index_type].value
64+
)
65+
with open(filepath, "w") as file:
66+
cache_name = index_type_to_cache_name[index_type].value
67+
file.write(f"{cache_name} = [\n")
68+
for entry in cache:
69+
file.write(f" {entry},\n")
2470
file.write("]\n")
25-
file.close()
2671

27-
return
72+
73+
def update_cache_file(data, index_type: IndexType):
74+
"""Update the cache with new data while maintaining chronological order."""
75+
cache = load_cache_file(index_type)
76+
existing_timestamps = {entry[TIMESTAMP] for entry in cache}
77+
78+
# Add only new entries
79+
new_entries = [
80+
entry for entry in data if entry[TIMESTAMP] not in existing_timestamps
81+
]
82+
cache.extend(new_entries)
83+
84+
# Sort the cache by timestamp
85+
cache.sort(key=lambda x: x[TIMESTAMP])
86+
87+
# Save back to the file
88+
save_cache_to_file(cache, index_type)
2889

2990

30-
# write_results_to_cache(content, var_name, file_name)
91+
def update_cache(input: list[dict], index_type: IndexType):
92+
try:
93+
if isinstance(input, list) and all(isinstance(item, dict) for item in input):
94+
update_cache_file(input, index_type)
95+
print("Cache updated successfully.")
96+
else:
97+
print("Invalid input format. Expected a list of dictionaries.")
98+
except ValueError as e:
99+
print(f"Error parsing input data: {e}")

backend/src/service.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ def sat_index_service(
169169
sat_index_time_series = get_sat_index_info(
170170
masked_images, LocationPolygon[location.value].value, index_type
171171
)
172+
# call caching script here
172173

173174
# Get cached range
174175
if cache_start_date:

0 commit comments

Comments
 (0)