1515from typing import List , Dict , Any , Optional
1616from collections import defaultdict
1717import hashlib
18-
18+ from math import radians , cos , sin , asin , sqrt
1919import numpy as np
2020from sklearn .cluster import DBSCAN
21-
21+ import uuid
2222from app .logging .setup_logging import get_logger
23+ from app .database .images import db_get_all_images
2324
2425# Initialize logger
2526logger = get_logger (__name__ )
@@ -80,7 +81,6 @@ def find_nearest_city(
8081 Returns:
8182 City name if within range, None otherwise
8283 """
83- from math import radians , cos , sin , asin , sqrt
8484
8585 def haversine_distance (lat1 : float , lon1 : float , lat2 : float , lon2 : float ) -> float :
8686 """Calculate distance between two points in km using Haversine formula."""
@@ -104,6 +104,16 @@ def haversine_distance(lat1: float, lon1: float, lat2: float, lon2: float) -> fl
104104 return nearest_city
105105
106106
107+ # function to count total memories which has location
108+ # this can also be done in tsx.
109+ def find_total_location_memories (data : list ) -> int :
110+ tlm = 0 # total location memories
111+ for memory in data :
112+ if memory ["location_name" ] is not None :
113+ tlm += 1
114+ return tlm
115+
116+
107117class MemoryClustering :
108118 """
109119 Clusters images into memories based on location and time proximity.
@@ -385,7 +395,7 @@ def _create_simple_memory(
385395 title = date_obj .strftime ("%B %Y" )
386396 else :
387397 title = "Undated Photos"
388- location_name = ""
398+ location_name = None
389399 center_lat = 0
390400 center_lon = 0
391401
@@ -944,3 +954,46 @@ def _generate_memory_id(
944954 hash_input = f"lat:{ lat_rounded } |lon:{ lon_rounded } "
945955 hash_digest = hashlib .sha256 (hash_input .encode ()).hexdigest ()[:8 ]
946956 return f"mem_nodate_{ hash_digest } "
957+
958+
959+ def generate_clusters_for_weekends () -> List [Dict ]:
960+ images = db_get_all_images ()
961+
962+ # sort by date
963+ images .sort (key = lambda x : x ["metadata" ]["date_created" ], reverse = True )
964+
965+ weekend_memories = {}
966+
967+ for img in images :
968+ metadata = img .get ("metadata" )
969+ if not metadata :
970+ continue
971+ date_str = metadata .get ("date_created" )
972+ if not date_str :
973+ continue
974+ try :
975+ dt = datetime .fromisoformat (date_str )
976+ except (ValueError , TypeError ):
977+ continue
978+
979+ # get year and week number
980+ year , week , _ = dt .isocalendar ()
981+
982+ week_key = f"{ year } -W{ week } "
983+
984+ if week_key not in weekend_memories :
985+ weekend_memories [week_key ] = {
986+ "mem_id" : str (uuid .uuid4 ()),
987+ "images" : [],
988+ "end_date" : date_str .split ("T" )[0 ],
989+ "start_date" : "" ,
990+ }
991+ image_info = {
992+ "id" : img ["id" ],
993+ "path" : img ["path" ],
994+ "thumbnailPath" : img ["thumbnailPath" ],
995+ }
996+ weekend_memories [week_key ]["start_date" ] = date_str .split ("T" )[0 ]
997+ weekend_memories [week_key ]["images" ].append (image_info )
998+
999+ return list (weekend_memories .values ())
0 commit comments