DebasishDhal99 commited on
Commit
59275a8
·
1 Parent(s): 18474d2

Remove redundant lines, fix variable names, make requests async

Browse files
frontend/src/App.js CHANGED
@@ -1,11 +1,11 @@
1
  // import logo from './logo.svg';
2
  import './App.css';
3
- import Map from './components/Map';
4
  import React, { useState,
5
  // useEffect,
6
  // useCallback
7
  } from 'react';
8
- // import { MapContainer, TileLayer, useMapEvents } from 'react-leaflet';
9
 
10
 
11
  function App() {
@@ -44,7 +44,7 @@ function App() {
44
  <button type="submit">Search</button>
45
  </form>
46
  </div>
47
- <Map
48
  onMapClick={handleMapClick}
49
  searchQuery={submittedQuery}
50
  contentType={contentType}
 
1
  // import logo from './logo.svg';
2
  import './App.css';
3
+ import WikiMap from './components/Map';
4
  import React, { useState,
5
  // useEffect,
6
  // useCallback
7
  } from 'react';
8
+
9
 
10
 
11
  function App() {
 
44
  <button type="submit">Search</button>
45
  </form>
46
  </div>
47
+ <WikiMap
48
  onMapClick={handleMapClick}
49
  searchQuery={submittedQuery}
50
  contentType={contentType}
frontend/src/components/Map.js CHANGED
@@ -48,7 +48,7 @@ const ResizeHandler = ({ trigger }) => {
48
  }, [trigger, map]);
49
  return null;
50
  };
51
- const Map = ( { onMapClick, searchQuery, contentType, setSearchQuery, setSubmittedQuery } ) => {
52
  const [baseLayer, setBaseLayer] = useState("base"); // "base" | "satellite"
53
 
54
  const [markerPosition, setMarkerPosition] = useState(null);
@@ -182,7 +182,7 @@ const Map = ( { onMapClick, searchQuery, contentType, setSearchQuery, setSubmitt
182
 
183
  if (contentType === 'summary') {
184
  setWikiContent(data);
185
- if (data && data.latitude && data.longitude) {
186
  setMarkerPosition([data.latitude, data.longitude]);
187
  }
188
  } else if (contentType === 'full') {
@@ -210,9 +210,8 @@ const Map = ( { onMapClick, searchQuery, contentType, setSearchQuery, setSubmitt
210
  const blob = new Blob([htmlContent], { type: 'text/html' });
211
  const blobUrl = URL.createObjectURL(blob);
212
  setIframeSrc(blobUrl);
213
- // const dataUrl = 'data:text/html;charset=utf-8,' + encodeURIComponent(htmlContent);
214
- // setIframeSrc(dataUrl);;
215
- if (data && data.latitude && data.longitude) {
216
  setMarkerPosition([data.latitude, data.longitude]);
217
  }
218
  }
@@ -1376,4 +1375,4 @@ const Map = ( { onMapClick, searchQuery, contentType, setSearchQuery, setSubmitt
1376
  };
1377
 
1378
 
1379
- export default Map;
 
48
  }, [trigger, map]);
49
  return null;
50
  };
51
+ const WikiMap = ( { onMapClick, searchQuery, contentType, setSearchQuery, setSubmittedQuery } ) => {
52
  const [baseLayer, setBaseLayer] = useState("base"); // "base" | "satellite"
53
 
54
  const [markerPosition, setMarkerPosition] = useState(null);
 
182
 
183
  if (contentType === 'summary') {
184
  setWikiContent(data);
185
+ if (data?.latitude && data?.longitude) {
186
  setMarkerPosition([data.latitude, data.longitude]);
187
  }
188
  } else if (contentType === 'full') {
 
210
  const blob = new Blob([htmlContent], { type: 'text/html' });
211
  const blobUrl = URL.createObjectURL(blob);
212
  setIframeSrc(blobUrl);
213
+
214
+ if (data?.latitude && data?.longitude) {
 
215
  setMarkerPosition([data.latitude, data.longitude]);
216
  }
217
  }
 
1375
  };
1376
 
1377
 
1378
+ export default WikiMap;
frontend/src/utils/mapUtils.js CHANGED
@@ -88,9 +88,6 @@ function formatArea(area, unit = 'sqm', format = "normal") {
88
  case "ha":
89
  value = area / 1e4;
90
  return (format === "scientific" ? value.toExponential(2) : value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })) + ' ha';
91
- case "sqm":
92
- value = area;
93
- return (format === "scientific" ? value.toExponential(2) : value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })) + ' m²';
94
  case "acres":
95
  value = area / 4046.8564224;
96
  return (format === "scientific" ? value.toExponential(2) : value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })) + ' acres';
@@ -100,6 +97,7 @@ function formatArea(area, unit = 'sqm', format = "normal") {
100
  case "sqft":
101
  value = area * 10.76391041671;
102
  return (format === "scientific" ? value.toExponential(2) : value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })) + ' ft²';
 
103
  default:
104
  value = area;
105
  return (format === "scientific" ? value.toExponential(2) : value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })) + ' m²';
@@ -113,25 +111,20 @@ function formatPerimeter(perimeter, unit = 'sqm', format = "normal") {
113
  let value;
114
  switch (unit) {
115
  case "km2":
116
- value = perimeter / 1000;
117
- return (format === "scientific" ? value.toExponential(2) : value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })) + ' km';
118
  case "ha":
119
  value = perimeter / 1000;
120
  return (format === "scientific" ? value.toExponential(2) : value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })) + ' km';
121
- case "m2":
122
- value = perimeter;
123
- return (format === "scientific" ? value.toExponential(2) : value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })) + ' m';
124
  case "mi2":
125
  value = perimeter / 1609.344;
126
  return (format === "scientific" ? value.toExponential(2) : value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })) + ' mi';
127
  case "sqft":
128
  value = perimeter * 3.280839895013123; // meters to feet
129
  return (format === "scientific" ? value.toExponential(2) : value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })) + ' ft';
 
130
  default:
131
  value = perimeter;
132
  return (format === "scientific" ? value.toExponential(2) : value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })) + ' m';
133
  }
134
  }
135
 
136
- export {generateGeodesicPoints, calculatePolygonArea, getPolygonCentroid, formatArea, formatPerimeter};
137
- // calculatePolygonArea
 
88
  case "ha":
89
  value = area / 1e4;
90
  return (format === "scientific" ? value.toExponential(2) : value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })) + ' ha';
 
 
 
91
  case "acres":
92
  value = area / 4046.8564224;
93
  return (format === "scientific" ? value.toExponential(2) : value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })) + ' acres';
 
97
  case "sqft":
98
  value = area * 10.76391041671;
99
  return (format === "scientific" ? value.toExponential(2) : value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })) + ' ft²';
100
+ case "sqm":
101
  default:
102
  value = area;
103
  return (format === "scientific" ? value.toExponential(2) : value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })) + ' m²';
 
111
  let value;
112
  switch (unit) {
113
  case "km2":
 
 
114
  case "ha":
115
  value = perimeter / 1000;
116
  return (format === "scientific" ? value.toExponential(2) : value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })) + ' km';
 
 
 
117
  case "mi2":
118
  value = perimeter / 1609.344;
119
  return (format === "scientific" ? value.toExponential(2) : value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })) + ' mi';
120
  case "sqft":
121
  value = perimeter * 3.280839895013123; // meters to feet
122
  return (format === "scientific" ? value.toExponential(2) : value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })) + ' ft';
123
+ case "m2":
124
  default:
125
  value = perimeter;
126
  return (format === "scientific" ? value.toExponential(2) : value.toLocaleString(undefined, { maximumFractionDigits: 2, minimumFractionDigits: 2 })) + ' m';
127
  }
128
  }
129
 
130
+ export {generateGeodesicPoints, calculatePolygonArea, getPolygonCentroid, formatArea, formatPerimeter};
 
main.py CHANGED
@@ -58,7 +58,8 @@ async def get_wiki_summary(summary_page_name: str, background_tasks: BackgroundT
58
  # print("Cache hit for summary:", page_name) #Working
59
  return JSONResponse(content=summary_cache[summary_page_name], status_code=200)
60
  try:
61
- response = requests.get(f"https://en.wikipedia.org/api/rest_v1/page/summary/{summary_page_name}", timeout=10)
 
62
 
63
  if response.status_code != 200:
64
  return JSONResponse(
@@ -102,7 +103,8 @@ async def search_wiki_full_page(full_page: str, background_tasks: BackgroundTask
102
  # print("Cache hit for full_page:", full_page) #Working
103
  return JSONResponse(content=full_page_cache[full_page], status_code=200)
104
 
105
- response = requests.get(f"https://en.wikipedia.org/wiki/{full_page}", timeout=10)
 
106
  try:
107
  if response.status_code != 200:
108
  return JSONResponse(
@@ -217,9 +219,10 @@ async def get_nearby_wiki_pages(payload: NearbyWikiPage):
217
  f"&gsradius={radius}"
218
  f"&gslimit={limit}"
219
  "&format=json")
220
- # print(url)
221
  try:
222
- response = requests.get(url, timeout=10)
 
223
  if response.status_code != 200:
224
  return JSONResponse(
225
  content={"error": "Failed to fetch nearby pages"},
@@ -258,7 +261,6 @@ async def get_nearby_wiki_pages(payload: NearbyWikiPage):
258
  tasks = [fetch_url(client, url) for url in urls]
259
  results = await asyncio.gather(*tasks)
260
 
261
- # print(results)
262
  for result in results:
263
 
264
  for unit in result.get("data", {}).get("query", {}).get("geosearch", []):
 
58
  # print("Cache hit for summary:", page_name) #Working
59
  return JSONResponse(content=summary_cache[summary_page_name], status_code=200)
60
  try:
61
+ async with httpx.AsyncClient() as client:
62
+ response = await client.get(f"https://en.wikipedia.org/api/rest_v1/page/summary/{summary_page_name}", timeout=10)
63
 
64
  if response.status_code != 200:
65
  return JSONResponse(
 
103
  # print("Cache hit for full_page:", full_page) #Working
104
  return JSONResponse(content=full_page_cache[full_page], status_code=200)
105
 
106
+ async with httpx.AsyncClient() as client:
107
+ response = await client.get(f"https://en.wikipedia.org/wiki/{full_page}", timeout=10)
108
  try:
109
  if response.status_code != 200:
110
  return JSONResponse(
 
219
  f"&gsradius={radius}"
220
  f"&gslimit={limit}"
221
  "&format=json")
222
+
223
  try:
224
+ async with httpx.AsyncClient() as client:
225
+ response = await client.get(url, timeout=10)
226
  if response.status_code != 200:
227
  return JSONResponse(
228
  content={"error": "Failed to fetch nearby pages"},
 
261
  tasks = [fetch_url(client, url) for url in urls]
262
  results = await asyncio.gather(*tasks)
263
 
 
264
  for result in results:
265
 
266
  for unit in result.get("data", {}).get("query", {}).get("geosearch", []):