File size: 11,933 Bytes
a96bcc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
046aaba
a96bcc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
046aaba
 
 
 
a96bcc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55788de
 
 
 
 
 
 
 
 
 
 
a96bcc0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f76a91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a96bcc0
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>mtDNA Location • Output</title>
  <!-- Optional: keep the same favicon / fonts as your existing site -->
  <link rel="icon" href="{{ url_for('static', filename='images/favicon.png') }}" />
  <link rel="stylesheet" href="{{ url_for('static', filename='Output.css') }}" media="screen">
</head>

<body
  data-is-vip="{{ 'true' if isvip else 'false' }}"
  data-ws-url="{{ ws_url|default('') }}"
>
<!-- data-job-id="{{ job_id|default('') }}" -->
  <div class="container">
    <!-- Header -->
    <header class="row">
      <div>
        <h1>mtDNA Location • Output</h1>
        <div class="muted small">
          Job ID: <span id="job-id">{{ job_id }}
          </span> • Started: <span id="job-started-at">{{ started_at or '' }}
          </span> • Permitted #run: <span id="job-total-queries">{{ total_queries or '' }}</span></div>
      </div>
      <div class="toolbar">
        <span id="ws-status" class="badge connecting" aria-live="polite" role="status">
          <span class="badge-dot" aria-hidden="true"></span>
          <span class="badge-text">Connecting…</span>
        </span>
        <a class="btn btn-ghost" href="{{ url_for('home') }}">← Back</a>
      </div>
    </header>

    <div class="spacer"></div>

    <section class="row grid-2">
      <!-- Left column: run status + logs -->
      <div class="card">
        <h2>Run status</h2>
        <div class="kvs" aria-live="polite">
          <div class="kv">
            <div class="label">Overall status</div>
            <div id="overall-status" class="value">{{ status or 'queued' }}</div>
          </div>
          <div class="kv">
            <div class="label">Progress</div>
            <div class="value"><span id="progress-value">0</span>%</div>
          </div>
          <div class="kv">
            <div class="label">Processed</div>
            <div class="value"><span id="processed-count">0</span>/<span id="total-count">0</span></div>
          </div>
          <div class="kv">
            <div class="label">Tier</div>
            <div class="value" id="user-tier">{{ 'Premium' if isvip else 'Freemium' }}</div>
          </div>
        </div>

        <div class="spacer"></div>

        <div class="progress" aria-hidden="true"><span id="progress-bar" style="width:0%"></span></div>

        <div class="spacer"></div>

        <div class="toolbar">
          <!-- Wire these later to your WebSocket/SSE/REST actions -->
          <button id="btn-cancel" class="btn btn-danger" data-action="cancel">Cancel job</button>
          <button id="btn-pause" class="btn" data-action="pause">Pause</button>
          <button id="btn-resume" class="btn" data-action="resume">Resume</button>
        </div>

        <div class="spacer"></div>

        <h3>Live log</h3>
        <pre id="live-log" class="log mono" aria-live="polite" aria-atomic="false">
          <span id="log-placeholder" class="logline muted">Waiting for messages…</span>
        </pre>
      </div>

      <!-- Right column: summary + downloads + alerts -->
      <div class="card">
        <h2>Summary</h2>
        <ul class="muted summary">
          <li>Accession IDs received: <strong id="sum-accessions">0</strong></li>
          <li>Unique isolates resolved: <strong id="sum-isolates">0</strong></li>
          <li>Errors / warnings: <strong id="sum-errors">0</strong></li>
        </ul>
        <div id="alerts"></div>

        <div class="spacer"></div>

        <h3>Downloads</h3>
        <div class="row download">
          <a id="dl-csv" class="btn" href="#" download>Results (.csv)</a>
          <a id="dl-json" class="btn" href="#" download>Results (.json)</a>
        </div>

        <div class="spacer"></div>
        <h3>Share with</h3>
        <div class="row share">
          <input id="share-link" class="mono" value="" readonly />
          <button id="copy-share" class="btn">Send</button>
        </div>
      </div>
    </section>

    <div class="spacer"></div>

    <!-- Results table -->
    <section class="card">
      <h2>Results</h2>
      <div class="muted small" id="results-help">Rows will stream in as they complete.</div>
      
      <div class="spacer"></div>
      
      <div style="overflow:auto;">
        <table id="results-table" style="table-layout:fixed;width:100%" aria-describedby="results-help">
          <colgroup>
            <col width="3%">
            <col width="8%">
            <col width="26%">
            <col width="8%">
            <col width="25%">
            <col width="8%">
            <col width="15%">
            <col width="7%">
          </colgroup>
          <thead>
            <tr>
              <th scope="col" class="col-idx">#</th>
              <th scope="col" class="col-id">Sample ID</th>
              <th scope="col" class="col-country">Sources</th>
              <th scope="col" class="col-sampletype">Predicted Country</th>
              <th scope="col" class="col-countryex">Country Explanation</th>
              <th scope="col" class="col-sampleex">Predicted Type</th>
              <th scope="col" class="col-sources">Type Explanation</th>
              <th scope="col" class="col-time">Time</th>
            </tr>
          </thead>
          <tbody id="results-body">
            <tr id="results-placeholder">
              <td colspan="8" class="muted">No results yet.</td>
            </tr>
          </tbody>
        </table>
      </div>
    </section>

    <div class="spacer"></div>

    <!-- Visuals / future widgets area -->
    <section class="row visualisation">
      <div class="card">
        <h3>Geography preview</h3>
        <div id="map-mount">Map placeholder</div>
      </div>
      <div class="card">
        <h3>Progress over time</h3>
        <div id="chart-mount">Chart placeholder</div>
      </div>
    </section>

    <div class="spacer"></div>

    <footer class="muted small">
      <span id="tier-text">{{ 'We love you ♥ Premium' if isvip else 'Freemium' }}</span> • mtDNA Location
    </footer>
  </div>

  <!-- TEMPLATES (for client-side rendering) -->
  <template id="tpl-log-line"><span class="logline"></span>\n</template>
  <template id="tpl-result-row">
    <tr>
      <td class="mono"></td>
      <td></td>
      <td class="mono small muted"></td>
      <td></td>
      <td class="small"></td>
    </tr>
  </template>

  <!-- Load a v3 client that matches Flask-SocketIO (python-socketio 5.x) -->
  <script src="https://cdn.socket.io/3.1.3/socket.io.min.js"></script>
  <script>
  (function () {
    // ---- your existing JS (kept) ----
    const els = {
      wsStatus: document.getElementById('ws-status'),
      wsBadgeText: document.querySelector('#ws-status .badge-text'),
      progressBar: document.getElementById('progress-bar'),
      progressValue: document.getElementById('progress-value'),
      processed: document.getElementById('processed-count'),
      total: document.getElementById('total-count'),
      overallStatus: document.getElementById('overall-status'),
      log: document.getElementById('live-log'),
      tbody: document.getElementById('results-body'),
      placeholder: document.getElementById('results-placeholder'),
      dlCSV: document.getElementById('dl-csv'),
      dlJSON: document.getElementById('dl-json'),
    };
    const jobId = "{{ job_id }}";  // provided by Flask template (✓) 

    function setConnection(state, text) {
      els.wsStatus?.classList.remove('connecting','connected','disconnected');
      els.wsStatus?.classList.add(state);
      if (els.wsBadgeText) els.wsBadgeText.textContent = text;
    }
    function appendLog(line) {
      if (!els.log) return;

      const ph = document.getElementById('log-placeholder');
      if (ph) ph.remove(); // drop the placeholder once

      const span = document.createElement('span');
      span.className = 'logline';
      span.textContent = line;
      els.log.appendChild(span);
      els.log.appendChild(document.createTextNode('\n'));
      els.log.scrollTop = els.log.scrollHeight;
    }
    function appendRow(r) {
      if (els.placeholder) { els.placeholder.remove(); els.placeholder = null; }
      const tr = document.createElement('tr');
      tr.innerHTML = `
        <td class="mono">${r.idx ?? ''}</td>
        <td>${r.sample_id ?? ''}</td>
        <td class="mono small muted">${r.sources ?? ''}</td>
        <td>${r.predicted_country ?? ''}</td>
        <td class="small">${r.country_explanation ?? ''}</td>
        <td>${r.predicted_sample_type ?? ''}</td>
        <td class="small">${r.sample_type_explanation ?? ''}</td>
        <td>${(String(r.time_cost ?? r['Time cost'] ?? '').match(/\d+(?:\.\d+)?/) || [''])[0]}</td>`;
      els.tbody?.appendChild(tr);

      // increment processed + progress % (your existing logic) 
      if (els.processed && els.total) {
        const done = (parseInt(els.processed.textContent || '0', 10) || 0) + 1;
        els.processed.textContent = String(done);
        const total = parseInt(els.total.textContent || '0', 10) || 0;
        if (total > 0) {
          const pct = Math.min(100, Math.round((done / total) * 100));
          els.progressValue.textContent = pct;
          els.progressBar.style.width = pct + '%';
        }
      }
      const sumIso = document.getElementById('sum-isolates');
      if (sumIso) sumIso.textContent = String((parseInt(sumIso.textContent || '0', 10) || 0) + 1);
    }

    setConnection('connecting', 'Connecting…');

    // Create the socket (v3 client)
    // const socket = io({ transports: ['websocket', 'polling'], reconnectionAttempts: 5, timeout: 10000 });
    // ✅ replace WITH THIS:
    const qs = window.location.search;                 // includes ?__sign=...
    const endpoint = qs ? ('/?' + qs.slice(1)) : '/';  // build "/?__sign=..."
    const socket = io(endpoint, {
      path: '/socket.io',
      transports: ['websocket','polling'],  // allow fallback, then upgrade
      withCredentials: true,
      reconnectionAttempts: 5,
      timeout: 10000
    });

    socket.on('connect', () => {
      setConnection('connected', 'Connected');
      appendLog('[WS] connected');
      socket.emit('join', { job_id: jobId }); // your page already emits join (✓) 
    });
    socket.on('connect_error', (err) => appendLog('[WS] connect_error: ' + (err?.message || err)));
    socket.on('disconnect', (reason) => {
      setConnection('disconnected', 'Disconnected');
      appendLog('[WS] disconnected: ' + reason);
    });

    socket.on('status', (data) => {
      if (els.overallStatus) els.overallStatus.textContent = data.state || 'unknown';
      if (data.total != null) {
        if (els.total) els.total.textContent = data.total;
        const sumAcc = document.getElementById('sum-accessions');
        if (sumAcc) sumAcc.textContent = data.total; // update “Accession IDs received” counter
      }
      appendLog(`[STATUS] ${data.state}${data.elapsed ? ' in ' + data.elapsed : ''}`);
    });

    socket.on('log', (data) => appendLog(`[LOG] ${data.msg}`));
    socket.on('row', appendRow);

    window.addEventListener('beforeunload', () => {
      socket.emit('leave', { job_id: jobId });
    });
  })();

  document.addEventListener('click', function (e) {
    const a = e.target.closest('a[href]');
    if (!a) return;

    const href = a.getAttribute('href');
    // Only adjust internal links (not http/https or mailto/etc.)
    if (!href || /^(?:[a-z]+:)?\/\//i.test(href) || href.startsWith('#')) return;

    // Preserve the current ?__sign=... and any other params
    const qs = window.location.search; // includes "?__sign=..."
    // If the link already has a query, merge them
    const joined = href + (href.includes('?') ? '&' + qs.slice(1) : qs);

    e.preventDefault();
    window.location.href = joined;
  });
  </script>
</body>
</html>