Ramasani commited on
Commit
c4098fc
·
1 Parent(s): 10032c3

Fix job status display and backend save_page ID issue

Browse files
Files changed (2) hide show
  1. static/app.js +81 -21
  2. storage.py +3 -2
static/app.js CHANGED
@@ -12,6 +12,9 @@ document.addEventListener('DOMContentLoaded', () => {
12
  })
13
  })
14
 
 
 
 
15
  document.getElementById('scrape-form').addEventListener('submit', async (e) => {
16
  e.preventDefault()
17
  const urlsText = document.getElementById('urls').value
@@ -20,7 +23,7 @@ document.addEventListener('DOMContentLoaded', () => {
20
  if (!urls.length) return
21
 
22
  const result = document.getElementById('scrape-result')
23
- result.textContent = 'Submitting...'
24
 
25
  try {
26
  const res = await fetch('/scrape', {
@@ -29,35 +32,87 @@ document.addEventListener('DOMContentLoaded', () => {
29
  body: JSON.stringify({ urls })
30
  })
31
  const data = await res.json()
32
- result.innerHTML = `<p>Job ID: <strong>${data.job_id}</strong></p><p>Status: ${data.status}</p><p>Total URLs: ${data.total}</p>`
 
33
  } catch (err) {
34
  result.innerHTML = `<p class="error">Error: ${err.message}</p>`
35
  }
36
  })
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  document.getElementById('check-job-btn').addEventListener('click', async () => {
39
  const jobId = document.getElementById('job-id-input').value.trim()
40
  if (!jobId) return
41
 
42
  const result = document.getElementById('job-result')
43
- result.textContent = 'Loading...'
44
 
45
- try {
46
- const res = await fetch(`/scrape/${jobId}`)
47
- const data = await res.json()
48
-
49
- let html = `<p>Status: ${data.status}</p><p>Progress: ${data.completed}/${data.total}</p>`
50
- if (data.results) {
51
- html += '<h3>Results:</h3><ul>'
52
- data.results.forEach(r => {
53
- html += `<li>${r.url} - ${r.error || r.title || 'No title'}</li>`
54
- })
55
- html += '</ul>'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  }
57
- result.innerHTML = html
58
- } catch (err) {
59
- result.innerHTML = `<p class="error">Error: ${err.message}</p>`
60
  }
 
 
 
61
  })
62
 
63
  async function loadPages() {
@@ -76,7 +131,7 @@ document.addEventListener('DOMContentLoaded', () => {
76
  <td><a href="#" onclick="showPageDetail(${p.id}); return false">${truncate(p.url, 50)}</a></td>
77
  <td>${p.title || '-'}</td>
78
  <td>${p.prediction ? `<span class="category ${p.prediction.category}">${p.prediction.category}</span>` : '-'}</td>
79
- <td>${p.error ? `<span class="error">${p.error}</span>` : '<span class="success">OK</span>'}</td>
80
  </tr>
81
  `).join('') || '<tr><td colspan="5">No pages found</td></tr>'
82
  } catch (err) {
@@ -95,12 +150,13 @@ document.addEventListener('DOMContentLoaded', () => {
95
  const modal = document.getElementById('page-detail')
96
  modal.innerHTML = `
97
  <button class="close-btn" onclick="closeModal()">&times;</button>
98
- <h3>${page.url}</h3>
99
  <p><strong>Title:</strong> ${page.title || 'N/A'}</p>
100
- <p><strong>Status:</strong> ${page.error || 'Success'}</p>
101
  ${page.prediction ? `
102
  <p><strong>Category:</strong> <span class="category ${page.prediction.category}">${page.prediction.category}</span></p>
103
  <p><strong>Confidence:</strong> ${(page.prediction.confidence * 100).toFixed(1)}%</p>
 
104
  ` : ''}
105
  ${page.features ? `
106
  <h4>Features:</h4>
@@ -194,6 +250,10 @@ document.addEventListener('DOMContentLoaded', () => {
194
  }
195
 
196
  loadStats()
 
197
 
198
- window.truncate = (str, len) => str.length > len ? str.slice(0, len) + '...' : str
 
 
 
199
  })
 
12
  })
13
  })
14
 
15
+ let scrapeInterval = null
16
+ let jobInterval = null
17
+
18
  document.getElementById('scrape-form').addEventListener('submit', async (e) => {
19
  e.preventDefault()
20
  const urlsText = document.getElementById('urls').value
 
23
  if (!urls.length) return
24
 
25
  const result = document.getElementById('scrape-result')
26
+ result.innerHTML = 'Submitting...'
27
 
28
  try {
29
  const res = await fetch('/scrape', {
 
32
  body: JSON.stringify({ urls })
33
  })
34
  const data = await res.json()
35
+ result.innerHTML = `<p>Job ID: <strong>${data.job_id}</strong></p><p>Status: <strong>${data.status}</strong></p><p>Progress: ${data.completed}/${data.total}</p>`
36
+ startScrapePolling(data.job_id)
37
  } catch (err) {
38
  result.innerHTML = `<p class="error">Error: ${err.message}</p>`
39
  }
40
  })
41
 
42
+ function startScrapePolling(jobId) {
43
+ if (scrapeInterval) clearInterval(scrapeInterval)
44
+ const result = document.getElementById('scrape-result')
45
+
46
+ scrapeInterval = setInterval(async () => {
47
+ try {
48
+ const res = await fetch(`/scrape/${jobId}`)
49
+ const data = await res.json()
50
+
51
+ let html = `<p>Job ID: <strong>${data.job_id}</strong></p><p>Status: <strong>${data.status}</strong></p><p>Progress: ${data.completed}/${data.total}</p>`
52
+ if (data.results && data.results.length > 0) {
53
+ html += '<h3>Results:</h3><table><thead><tr><th>URL</th><th>Status</th><th>Category</th></tr></thead><tbody>'
54
+ data.results.forEach(r => {
55
+ const statusText = r.error ? `<span class="error">${truncate(r.error, 30)}</span>` : '<span class="success">Success</span>'
56
+ const category = r.prediction ? `<span class="category ${r.prediction.category}">${r.prediction.category}</span>` : '-'
57
+ html += `<tr><td>${truncate(r.url, 40)}</td><td>${statusText}</td><td>${category}</td></tr>`
58
+ })
59
+ html += '</tbody></table>'
60
+ }
61
+ result.innerHTML = html
62
+
63
+ if (data.status === 'completed' || data.status === 'failed') {
64
+ clearInterval(scrapeInterval)
65
+ scrapeInterval = null
66
+ }
67
+ } catch (err) {
68
+ clearInterval(scrapeInterval)
69
+ scrapeInterval = null
70
+ result.innerHTML += `<p class="error">Error: ${err.message}</p>`
71
+ }
72
+ }, 1000)
73
+ }
74
+
75
  document.getElementById('check-job-btn').addEventListener('click', async () => {
76
  const jobId = document.getElementById('job-id-input').value.trim()
77
  if (!jobId) return
78
 
79
  const result = document.getElementById('job-result')
80
+ if (jobInterval) clearInterval(jobInterval)
81
 
82
+ const checkStatus = async () => {
83
+ try {
84
+ const res = await fetch(`/scrape/${jobId}`)
85
+ if (!res.ok) {
86
+ result.innerHTML = '<p class="error">Job not found</p>'
87
+ clearInterval(jobInterval)
88
+ return
89
+ }
90
+ const data = await res.json()
91
+
92
+ let html = `<p>Status: <strong>${data.status}</strong></p><p>Progress: ${data.completed}/${data.total}</p>`
93
+ if (data.results && data.results.length > 0) {
94
+ html += '<h3>Results:</h3><table><thead><tr><th>URL</th><th>Status</th><th>Category</th><th>Confidence</th></tr></thead><tbody>'
95
+ data.results.forEach(r => {
96
+ const statusText = r.error ? `<span class="error">${truncate(r.error, 30)}</span>` : '<span class="success">Success</span>'
97
+ const category = r.prediction ? `<span class="category ${r.prediction.category}">${r.prediction.category}</span>` : '-'
98
+ const confidence = r.prediction ? `${(r.prediction.confidence * 100).toFixed(1)}%` : '-'
99
+ html += `<tr><td>${truncate(r.url, 40)}</td><td>${statusText}</td><td>${category}</td><td>${confidence}</td></tr>`
100
+ })
101
+ html += '</tbody></table>'
102
+ }
103
+ result.innerHTML = html
104
+
105
+ if (data.status === 'completed' || data.status === 'failed') {
106
+ clearInterval(jobInterval)
107
+ }
108
+ } catch (err) {
109
+ result.innerHTML = `<p class="error">Error: ${err.message}</p>`
110
+ clearInterval(jobInterval)
111
  }
 
 
 
112
  }
113
+
114
+ checkStatus()
115
+ jobInterval = setInterval(checkStatus, 1000)
116
  })
117
 
118
  async function loadPages() {
 
131
  <td><a href="#" onclick="showPageDetail(${p.id}); return false">${truncate(p.url, 50)}</a></td>
132
  <td>${p.title || '-'}</td>
133
  <td>${p.prediction ? `<span class="category ${p.prediction.category}">${p.prediction.category}</span>` : '-'}</td>
134
+ <td>${p.error ? `<span class="error">${truncate(p.error, 30)}</span>` : '<span class="success">OK</span>'}</td>
135
  </tr>
136
  `).join('') || '<tr><td colspan="5">No pages found</td></tr>'
137
  } catch (err) {
 
150
  const modal = document.getElementById('page-detail')
151
  modal.innerHTML = `
152
  <button class="close-btn" onclick="closeModal()">&times;</button>
153
+ <h3>${truncate(page.url, 60)}</h3>
154
  <p><strong>Title:</strong> ${page.title || 'N/A'}</p>
155
+ <p><strong>Status:</strong> ${page.error ? page.error : 'Success'}</p>
156
  ${page.prediction ? `
157
  <p><strong>Category:</strong> <span class="category ${page.prediction.category}">${page.prediction.category}</span></p>
158
  <p><strong>Confidence:</strong> ${(page.prediction.confidence * 100).toFixed(1)}%</p>
159
+ <p><strong>Relevance:</strong> ${(page.prediction.relevance_score * 100).toFixed(1)}%</p>
160
  ` : ''}
161
  ${page.features ? `
162
  <h4>Features:</h4>
 
250
  }
251
 
252
  loadStats()
253
+ setInterval(loadStats, 5000)
254
 
255
+ window.truncate = (str, len) => {
256
+ if (!str) return '-'
257
+ return str.length > len ? str.slice(0, len) + '...' : str
258
+ }
259
  })
storage.py CHANGED
@@ -124,11 +124,12 @@ class Database:
124
  text_length = len(html) if html else 0
125
 
126
  with self.get_conn() as conn:
127
- cursor = conn.execute("""
128
  INSERT OR REPLACE INTO scraped_pages (url, html, status_code, title, text_length, content_hash, error)
129
  VALUES (?, ?, ?, ?, ?, ?, ?)
130
  """, (url, html, status_code, title, text_length, content_hash, error))
131
- return cursor.lastrowid
 
132
 
133
  def get_page(self, page_id: int) -> Optional[Dict[str, Any]]:
134
  with self.get_conn() as conn:
 
124
  text_length = len(html) if html else 0
125
 
126
  with self.get_conn() as conn:
127
+ conn.execute("""
128
  INSERT OR REPLACE INTO scraped_pages (url, html, status_code, title, text_length, content_hash, error)
129
  VALUES (?, ?, ?, ?, ?, ?, ?)
130
  """, (url, html, status_code, title, text_length, content_hash, error))
131
+ row = conn.execute("SELECT id FROM scraped_pages WHERE url = ?", (url,)).fetchone()
132
+ return row["id"] if row else 0
133
 
134
  def get_page(self, page_id: int) -> Optional[Dict[str, Any]]:
135
  with self.get_conn() as conn: