ChandimaPrabath commited on
Commit
40df71d
·
1 Parent(s): 4e9654b
Files changed (1) hide show
  1. LoadBalancer.py +63 -10
LoadBalancer.py CHANGED
@@ -78,37 +78,45 @@ class LoadBalancer:
78
 
79
  def get_reports(self):
80
  reports = self.instances_api.fetch_reports()
 
 
 
 
 
81
  for instance_url in self.instances[:]: # Copy list to avoid modification during iteration
82
  if instance_url in reports:
83
  report = reports[instance_url]
84
  logging.info(f"Report from {instance_url}: {report}")
85
- self.process_report(instance_url, report)
86
  else:
87
  logging.error(f"Failed to get report from {instance_url}. Removing instance.")
88
  self.remove_instance(instance_url)
89
 
90
- def process_report(self, instance_url, report):
 
 
 
91
  film_store = report.get('film_store', {})
92
  tv_store = report.get('tv_store', {})
93
  cache_size = report.get('cache_size')
94
 
95
  logging.info(f"Processing report from {instance_url}")
96
- # logging.info(f"Film Store: {film_store}")
97
- # logging.info(f"TV Store: {tv_store}")
98
- # logging.info(f"Cache Size: {cache_size}")
99
 
100
- # Process films
101
  for title, path in film_store.items():
102
  url = f"{instance_url}/api/film/{title.replace(' ', '%20')}"
103
- self.update_film_store_json(title, url)
104
 
105
-
106
- # Process TV shows
107
  for title, seasons in tv_store.items():
 
 
108
  for season, episodes in seasons.items():
 
 
109
  for episode, path in episodes.items():
110
  url = f"{instance_url}/api/tv/{title.replace(' ', '%20')}/{season.replace(' ', '%20')}/{episode.replace(' ', '%20')}"
111
- self.update_tv_store_json(title, season, episode, url)
112
 
113
  logging.info("Film and TV Stores processed successfully.")
114
  self.update_instances_health(instance=instance_url, cache_size=cache_size)
@@ -124,6 +132,51 @@ class LoadBalancer:
124
  logging.info("Stopping polling.")
125
  self.stop_event.set()
126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  ######################################################################
128
 
129
  @staticmethod
 
78
 
79
  def get_reports(self):
80
  reports = self.instances_api.fetch_reports()
81
+
82
+ # Initialize temporary JSON data holders
83
+ temp_film_store = {}
84
+ temp_tv_store = {}
85
+
86
  for instance_url in self.instances[:]: # Copy list to avoid modification during iteration
87
  if instance_url in reports:
88
  report = reports[instance_url]
89
  logging.info(f"Report from {instance_url}: {report}")
90
+ self.process_report(instance_url, report, temp_film_store, temp_tv_store)
91
  else:
92
  logging.error(f"Failed to get report from {instance_url}. Removing instance.")
93
  self.remove_instance(instance_url)
94
 
95
+ # Compare and update actual JSON files after processing all reports
96
+ self.update_json_files(temp_film_store, temp_tv_store)
97
+
98
+ def process_report(self, instance_url, report, temp_film_store, temp_tv_store):
99
  film_store = report.get('film_store', {})
100
  tv_store = report.get('tv_store', {})
101
  cache_size = report.get('cache_size')
102
 
103
  logging.info(f"Processing report from {instance_url}")
 
 
 
104
 
105
+ # Update temporary film store
106
  for title, path in film_store.items():
107
  url = f"{instance_url}/api/film/{title.replace(' ', '%20')}"
108
+ temp_film_store[title] = url
109
 
110
+ # Update temporary TV store
 
111
  for title, seasons in tv_store.items():
112
+ if title not in temp_tv_store:
113
+ temp_tv_store[title] = {}
114
  for season, episodes in seasons.items():
115
+ if season not in temp_tv_store[title]:
116
+ temp_tv_store[title][season] = {}
117
  for episode, path in episodes.items():
118
  url = f"{instance_url}/api/tv/{title.replace(' ', '%20')}/{season.replace(' ', '%20')}/{episode.replace(' ', '%20')}"
119
+ temp_tv_store[title][season][episode] = url
120
 
121
  logging.info("Film and TV Stores processed successfully.")
122
  self.update_instances_health(instance=instance_url, cache_size=cache_size)
 
132
  logging.info("Stopping polling.")
133
  self.stop_event.set()
134
 
135
+ def update_json_files(self, temp_film_store, temp_tv_store):
136
+ # Load current data
137
+ film_store_data = self.read_json(self.FILM_STORE_JSON_PATH)
138
+ tv_store_data = self.read_json(self.TV_STORE_JSON_PATH)
139
+
140
+ # Compare and update film store
141
+ for title in list(film_store_data.keys()):
142
+ if title not in temp_film_store:
143
+ del film_store_data[title]
144
+
145
+ for title, url in temp_film_store.items():
146
+ film_store_data[title] = url
147
+
148
+ with open(self.FILM_STORE_JSON_PATH, 'w') as json_file:
149
+ json.dump(film_store_data, json_file, indent=2)
150
+
151
+ print(f'Film store updated.')
152
+
153
+ # Compare and update TV store
154
+ for title in list(tv_store_data.keys()):
155
+ if title not in temp_tv_store:
156
+ del tv_store_data[title]
157
+ else:
158
+ for season in list(tv_store_data[title].keys()):
159
+ if season not in temp_tv_store[title]:
160
+ del tv_store_data[title][season]
161
+ else:
162
+ for episode in list(tv_store_data[title][season].keys()):
163
+ if episode not in temp_tv_store[title][season]:
164
+ del tv_store_data[title][season][episode]
165
+
166
+ for title, seasons in temp_tv_store.items():
167
+ if title not in tv_store_data:
168
+ tv_store_data[title] = {}
169
+ for season, episodes in seasons.items():
170
+ if season not in tv_store_data[title]:
171
+ tv_store_data[title][season] = {}
172
+ for episode, url in episodes.items():
173
+ tv_store_data[title][season][episode] = url
174
+
175
+ with open(self.TV_STORE_JSON_PATH, 'w') as json_file:
176
+ json.dump(tv_store_data, json_file, indent=2)
177
+
178
+ print(f'TV store updated.')
179
+
180
  ######################################################################
181
 
182
  @staticmethod