571 lines
21 KiB
HTML
571 lines
21 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="nn">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>PPQ Cost Analyzer</title>
|
|
<style>
|
|
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
min-height: 100vh;
|
|
padding: 20px;
|
|
}
|
|
.container { max-width: 1200px; margin: 0 auto; }
|
|
header { text-align: center; color: white; margin-bottom: 30px; }
|
|
header h1 { font-size: 2rem; margin-bottom: 10px; }
|
|
header p { opacity: 0.9; }
|
|
.upload-section {
|
|
background: white;
|
|
border-radius: 12px;
|
|
padding: 40px;
|
|
text-align: center;
|
|
margin-bottom: 30px;
|
|
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
|
|
}
|
|
.upload-area {
|
|
border: 3px dashed #ccc;
|
|
border-radius: 8px;
|
|
padding: 60px 20px;
|
|
cursor: pointer;
|
|
transition: all 0.3s;
|
|
}
|
|
.upload-area:hover, .upload-area.dragover {
|
|
border-color: #6d4aff;
|
|
background: #f0ebff;
|
|
}
|
|
.file-info {
|
|
display: none;
|
|
margin-top: 20px;
|
|
padding: 15px;
|
|
background: #e8f5e9;
|
|
border-radius: 8px;
|
|
color: #2e7d32;
|
|
}
|
|
button.analyze-btn {
|
|
background: #6d4aff;
|
|
color: white;
|
|
border: none;
|
|
padding: 15px 40px;
|
|
font-size: 1.1rem;
|
|
border-radius: 8px;
|
|
cursor: pointer;
|
|
margin-top: 20px;
|
|
display: none;
|
|
}
|
|
button.analyze-btn:hover { background: #5a3fd6; }
|
|
button.analyze-btn:disabled { background: #ccc; cursor: not-allowed; }
|
|
.report-section {
|
|
background: white;
|
|
border-radius: 12px;
|
|
padding: 40px;
|
|
box-shadow: 0 10px 40px rgba(0,0,0,0.2);
|
|
display: none;
|
|
}
|
|
.report-section.active { display: block; }
|
|
h2 {
|
|
color: #6d4aff;
|
|
margin: 30px 0 20px 0;
|
|
padding-bottom: 10px;
|
|
border-bottom: 2px solid #eee;
|
|
}
|
|
h2:first-of-type { margin-top: 0; }
|
|
table {
|
|
width: 100%;
|
|
border-collapse: collapse;
|
|
margin: 20px 0;
|
|
font-size: 0.95rem;
|
|
overflow-x: auto;
|
|
}
|
|
th, td {
|
|
padding: 12px 15px;
|
|
text-align: left;
|
|
border-bottom: 1px solid #ddd;
|
|
}
|
|
th { background: #f8f5ff; color: #6d4aff; font-weight: 600; }
|
|
tr:hover { background: #faf9fc; }
|
|
.metric-card {
|
|
display: inline-block;
|
|
background: linear-gradient(135deg, #6d4aff 0%, #8b5cf6 100%);
|
|
color: white;
|
|
padding: 20px 30px;
|
|
border-radius: 8px;
|
|
margin: 10px;
|
|
min-width: 180px;
|
|
box-shadow: 0 4px 15px rgba(109,74,255,0.3);
|
|
}
|
|
.metric-value { font-size: 1.8rem; font-weight: bold; margin-bottom: 5px; }
|
|
.metric-label { opacity: 0.9; font-size: 0.9rem; }
|
|
.finding-box, .warning-box, .success-box {
|
|
padding: 15px 20px;
|
|
margin: 20px 0;
|
|
border-radius: 4px;
|
|
border-left: 4px solid;
|
|
}
|
|
.finding-box { background: #fff3cd; border-color: #ffc107; }
|
|
.warning-box { background: #f8d7da; border-color: #dc3545; }
|
|
.success-box { background: #d4edda; border-color: #28a745; }
|
|
.debug-log {
|
|
background: #1a1a2e;
|
|
color: #0f0;
|
|
padding: 15px;
|
|
border-radius: 8px;
|
|
margin: 20px 0;
|
|
font-family: monospace;
|
|
font-size: 0.85rem;
|
|
max-height: 200px;
|
|
overflow-y: auto;
|
|
display: none;
|
|
}
|
|
.privacy-note {
|
|
background: rgba(255,255,255,0.15);
|
|
color: white;
|
|
padding: 15px;
|
|
border-radius: 8px;
|
|
text-align: center;
|
|
margin-top: 20px;
|
|
font-size: 0.9rem;
|
|
}
|
|
.status-msg {
|
|
padding: 10px;
|
|
margin: 10px 0;
|
|
border-radius: 8px;
|
|
display: none;
|
|
}
|
|
.status-error { background: #f8d7da; color: #721c24; }
|
|
.status-success { background: #d4edda; color: #155724; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<header>
|
|
<h1>📊 PPQ Cost Analyzer</h1>
|
|
<p>Drag & drop CSV-fila di her - alle data held seg lokalt!</p>
|
|
</header>
|
|
|
|
<section class="upload-section" id="uploadSection">
|
|
<div class="upload-area" id="uploadArea">
|
|
<div style="font-size: 3rem; margin-bottom: 15px;">📄</div>
|
|
<div style="font-size: 1.2rem; margin-bottom: 10px;"><strong>Drag & drop CSV-fila her</strong></div>
|
|
<div style="color: #666;">eller klikk for å velje fil</div>
|
|
</div>
|
|
|
|
<input type="file" id="fileInput" accept=".csv" style="display:none">
|
|
|
|
<div class="status-msg" id="statusMsg"></div>
|
|
|
|
<div class="file-info" id="fileInfo"></div>
|
|
|
|
<button class="analyze-btn" id="analyzeBtn">Analyser data</button>
|
|
|
|
<div class="debug-log" id="debugLog">
|
|
<strong>Debug:</strong><br>
|
|
</div>
|
|
</section>
|
|
|
|
<section class="report-section" id="reportSection">
|
|
<div class="privacy-note">
|
|
🔒 Alle data blir handtert lokalt i nettlesaren din. Ingen data blir sende ut.
|
|
</div>
|
|
|
|
<h2>📊 Sammendrag</h2>
|
|
<div id="summaryMetrics"></div>
|
|
|
|
<h2>💰 Kostnad per modell</h2>
|
|
<div id="modelTableContainer"></div>
|
|
|
|
<h2>📅 Dagleg fordeling</h2>
|
|
<div id="dailyTableContainer"></div>
|
|
|
|
<h2>⚠️ Anomalier</h2>
|
|
<div id="anomalies"></div>
|
|
</section>
|
|
</div>
|
|
|
|
<script>
|
|
// Debug-funksjon
|
|
function log(msg) {
|
|
const debugLog = document.getElementById('debugLog');
|
|
debugLog.style.display = 'block';
|
|
debugLog.innerHTML += `[${new Date().toLocaleTimeString()}] ${msg}<br>`;
|
|
debugLog.scrollTop = debugLog.scrollHeight;
|
|
console.log('[PPQ Analyzer]', msg);
|
|
}
|
|
|
|
function showStatus(msg, isError = false) {
|
|
const statusEl = document.getElementById('statusMsg');
|
|
statusEl.style.display = 'block';
|
|
statusEl.className = 'status-msg ' + (isError ? 'status-error' : 'status-success');
|
|
statusEl.textContent = msg;
|
|
}
|
|
|
|
// Globale variablar
|
|
let csvData = [];
|
|
let rawData = '';
|
|
|
|
const uploadArea = document.getElementById('uploadArea');
|
|
const fileInput = document.getElementById('fileInput');
|
|
const fileInfo = document.getElementById('fileInfo');
|
|
const analyzeBtn = document.getElementById('analyzeBtn');
|
|
const reportSection = document.getElementById('reportSection');
|
|
const uploadSection = document.getElementById('uploadSection');
|
|
|
|
log('Init start...');
|
|
|
|
// Klikk for å open filveljar
|
|
uploadArea.addEventListener('click', () => {
|
|
log('Upload area clicked');
|
|
fileInput.click();
|
|
});
|
|
|
|
// Drag & drop
|
|
uploadArea.addEventListener('dragover', (e) => {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
uploadArea.classList.add('dragover');
|
|
log('Drag over');
|
|
});
|
|
|
|
uploadArea.addEventListener('dragleave', (e) => {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
uploadArea.classList.remove('dragover');
|
|
log('Drag leave');
|
|
});
|
|
|
|
uploadArea.addEventListener('drop', (e) => {
|
|
e.preventDefault();
|
|
e.stopPropagation();
|
|
uploadArea.classList.remove('dragover');
|
|
log('Drop event');
|
|
|
|
const files = e.dataTransfer.files;
|
|
log(`Dropped ${files.length} file(s)`);
|
|
|
|
if (files.length > 0) {
|
|
handleFile(files[0]);
|
|
}
|
|
});
|
|
|
|
// Filveljar
|
|
fileInput.addEventListener('change', (e) => {
|
|
log(`File input change, ${e.target.files.length} file(s)`);
|
|
if (e.target.files.length > 0) {
|
|
handleFile(e.target.files[0]);
|
|
}
|
|
});
|
|
|
|
// Analyse-knapp
|
|
analyzeBtn.addEventListener('click', () => {
|
|
log('Analyze button clicked');
|
|
generateReport();
|
|
});
|
|
|
|
function handleFile(file) {
|
|
log(`Handling file: ${file.name} (${formatSize(file.size)} bytes)`);
|
|
|
|
if (!file.name.toLowerCase().endsWith('.csv')) {
|
|
showStatus('Feil! Vennligst last opp ei .csv-fil', true);
|
|
log('Error: Not a CSV file');
|
|
return;
|
|
}
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = (event) => {
|
|
rawData = event.target.result;
|
|
log(`File read successfully, ${rawData.length} characters`);
|
|
|
|
// Sjekk første raden for debugging
|
|
const firstLines = rawData.split('\n').slice(0, 3);
|
|
log('First 3 lines:');
|
|
firstLines.forEach((line, i) => log(` Line ${i}: ${line.substring(0, 100)}`));
|
|
|
|
fileInfo.style.display = 'block';
|
|
fileInfo.innerHTML = `✅ <strong>${file.name}</strong><br>Storleik: ${formatSize(file.size)} | Ready to analyze`;
|
|
|
|
analyzeBtn.style.display = 'inline-block';
|
|
analyzeBtn.disabled = false;
|
|
|
|
showStatus('Filen er lasta opp! Trykk på "Analyser data" for å starte.', false);
|
|
};
|
|
|
|
reader.onerror = (error) => {
|
|
log('FileReader error: ' + error);
|
|
showStatus('Feil ved lesing av fil', true);
|
|
};
|
|
|
|
log('Starting file read...');
|
|
reader.readAsText(file, 'UTF-8');
|
|
}
|
|
|
|
function parseCSV(csvText) {
|
|
log('Parsing CSV...');
|
|
const lines = csvText.trim().split('\n').filter(line => line.trim());
|
|
log(`Found ${lines.length} lines total`);
|
|
|
|
if (lines.length < 2) {
|
|
log('Error: Not enough lines in CSV');
|
|
return [];
|
|
}
|
|
|
|
const headers = lines[0].split(',').map(h => h.trim().replace(/"/g, ''));
|
|
log(`Headers: ${JSON.stringify(headers)}`);
|
|
|
|
const data = [];
|
|
let errors = 0;
|
|
|
|
for (let i = 1; i < lines.length; i++) {
|
|
const line = lines[i];
|
|
|
|
// Skip total row
|
|
if (line.includes('Total Records') || line.trim() === '') continue;
|
|
|
|
// Handle quoted fields
|
|
const values = [];
|
|
let inQuotes = false;
|
|
let current = '';
|
|
|
|
for (let char of line) {
|
|
if (char === '"') {
|
|
inQuotes = !inQuotes;
|
|
} else if (char === ',' && !inQuotes) {
|
|
values.push(current.trim().replace(/"/g, ''));
|
|
current = '';
|
|
} else {
|
|
current += char;
|
|
}
|
|
}
|
|
values.push(current.trim().replace(/"/g, ''));
|
|
|
|
if (values.length === headers.length) {
|
|
const row = {};
|
|
headers.forEach((header, idx) => {
|
|
let val = values[idx];
|
|
|
|
// Konverter tallfelt
|
|
if (header.includes('Tokens') || header === 'Price (USD)') {
|
|
val = parseFloat(val) || 0;
|
|
}
|
|
|
|
row[header] = val;
|
|
});
|
|
|
|
data.push(row);
|
|
} else {
|
|
errors++;
|
|
if (errors < 3) {
|
|
log(`Parse error line ${i}: expected ${headers.length} fields, got ${values.length}`);
|
|
}
|
|
}
|
|
}
|
|
|
|
log(`Parsed ${data.length} rows, ${errors} errors`);
|
|
return data;
|
|
}
|
|
|
|
function formatSize(bytes) {
|
|
if (bytes < 1024) return bytes + ' B';
|
|
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(2) + ' KB';
|
|
return (bytes / (1024 * 1024)).toFixed(2) + ' MB';
|
|
}
|
|
|
|
function formatNumber(num) {
|
|
return Math.floor(num).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ' ');
|
|
}
|
|
|
|
function generateReport() {
|
|
log('Generating report...');
|
|
|
|
if (!rawData || rawData.length === 0) {
|
|
log('No data loaded!');
|
|
showStatus('Ingen data funnen. Last opp ei CSV-fil først.', true);
|
|
return;
|
|
}
|
|
|
|
csvData = parseCSV(rawData);
|
|
log(`Loaded ${csvData.length} data rows`);
|
|
|
|
if (csvData.length === 0) {
|
|
showStatus('Kunne ikkje lese data frå fila', true);
|
|
return;
|
|
}
|
|
|
|
const stats = calculateStats(csvData);
|
|
log('Stats calculated');
|
|
|
|
renderSummary(stats);
|
|
renderModelTable(stats.models);
|
|
renderDailyTable(stats.daily);
|
|
renderAnomalies(csvData);
|
|
|
|
uploadSection.style.display = 'none';
|
|
reportSection.classList.add('active');
|
|
|
|
log('Report rendered successfully');
|
|
window.scrollTo(0, 0);
|
|
}
|
|
|
|
function calculateStats(data) {
|
|
const models = {};
|
|
const daily = {};
|
|
|
|
data.forEach(row => {
|
|
// Per modell
|
|
const model = row.Model || 'unknown';
|
|
if (!models[model]) {
|
|
models[model] = { count: 0, totalCost: 0, totalInput: 0, totalOutput: 0 };
|
|
}
|
|
models[model].count++;
|
|
models[model].totalCost += row['Price (USD)'] || 0;
|
|
models[model].totalInput += row['Input Tokens'] || 0;
|
|
models[model].totalOutput += row['Output Tokens'] || 0;
|
|
|
|
// Per dag
|
|
const date = row.Date || 'unknown';
|
|
if (!daily[date]) {
|
|
daily[date] = { cost: 0, count: 0 };
|
|
}
|
|
daily[date].cost += row['Price (USD)'] || 0;
|
|
daily[date].count++;
|
|
});
|
|
|
|
return {
|
|
totalCost: data.reduce((sum, r) => sum + (r['Price (USD)'] || 0), 0),
|
|
totalRequests: data.length,
|
|
totalInput: data.reduce((sum, r) => sum + (r['Input Tokens'] || 0), 0),
|
|
totalOutput: data.reduce((sum, r) => sum + (r['Output Tokens'] || 0), 0),
|
|
uniqueDates: new Set(data.map(r => r.Date)).size,
|
|
models,
|
|
daily
|
|
};
|
|
}
|
|
|
|
function renderSummary(stats) {
|
|
const container = document.getElementById('summaryMetrics');
|
|
container.innerHTML = `
|
|
<div class="metric-card">
|
|
<div class="metric-value">$${stats.totalCost.toFixed(2)}</div>
|
|
<div class="metric-label">Totalkostnad</div>
|
|
</div>
|
|
<div class="metric-card">
|
|
<div class="metric-value">${formatNumber(stats.totalRequests)}</div>
|
|
<div class="metric-label">Førespurnadar</div>
|
|
</div>
|
|
<div class="metric-card">
|
|
<div class="metric-value">${formatNumber(Math.round(stats.totalInput / 1000000))}M</div>
|
|
<div class="metric-label">Input-tokenar</div>
|
|
</div>
|
|
<div class="metric-card">
|
|
<div class="metric-value">$${(stats.totalCost / stats.uniqueDates).toFixed(2)}</div>
|
|
<div class="metric-label">Per dag</div>
|
|
</div>
|
|
`;
|
|
log('Summary rendered');
|
|
}
|
|
|
|
function renderModelTable(models) {
|
|
const sorted = Object.entries(models).sort((a, b) => b[1].totalCost - a[1].totalCost);
|
|
|
|
let html = '<table><tr><th>Modell</th><th>Føresp.</th><th>Total ($)</th><th>Gj.snitt ($)</th></tr>';
|
|
|
|
sorted.forEach(([name, stats]) => {
|
|
const avg = stats.count > 0 ? stats.totalCost / stats.count : 0;
|
|
html += `<tr>
|
|
<td><strong>${name}</strong></td>
|
|
<td>${formatNumber(stats.count)}</td>
|
|
<td>$${stats.totalCost.toFixed(2)}</td>
|
|
<td>$${avg.toFixed(4)}</td>
|
|
</tr>`;
|
|
});
|
|
|
|
html += '</table>';
|
|
document.getElementById('modelTableContainer').innerHTML = html;
|
|
log(`Model table rendered with ${sorted.length} models`);
|
|
}
|
|
|
|
function renderDailyTable(daily) {
|
|
const sorted = Object.entries(daily).sort((a, b) => a[0].localeCompare(b[0]));
|
|
|
|
let html = '<table><tr><th>Dato</th><th>Kostnad ($)</th><th>Førespurnadar</th></tr>';
|
|
|
|
sorted.forEach(([date, stats]) => {
|
|
html += `<tr>
|
|
<td>${date}</td>
|
|
<td>$${stats.cost.toFixed(2)}</td>
|
|
<td>${formatNumber(stats.count)}</td>
|
|
</tr>`;
|
|
});
|
|
|
|
html += '</table>';
|
|
document.getElementById('dailyTableContainer').innerHTML = html;
|
|
log('Daily table rendered');
|
|
}
|
|
|
|
function renderAnomalies(data) {
|
|
const anomaliesContainer = document.getElementById('anomalies');
|
|
const boxes = [];
|
|
|
|
// Gratis førespurnadar
|
|
const free = data.filter(r => (r['Price (USD)'] || 0) === 0);
|
|
if (free.length > 0) {
|
|
boxes.push({
|
|
type: 'success-box',
|
|
title: `${free.length} gratis førespurnadar`,
|
|
items: free.slice(0, 5).map(r => `${r.Model} (${formatNumber(r['Input Tokens'])} tokens)`)
|
|
});
|
|
}
|
|
|
|
// Monsterkontekstar (>100k input)
|
|
const big = data.filter(r => (r['Input Tokens'] || 0) > 100000);
|
|
if (big.length > 0) {
|
|
boxes.push({
|
|
type: 'warning-box',
|
|
title: `${big.length} "monsterkontekstar" (>100k tokens)`,
|
|
items: big.sort((a,b) => (b['Input Tokens']||0) - (a['Input Tokens']||0))
|
|
.slice(0, 5)
|
|
.map(r => `${r.Model}: ${formatNumber(r['Input Tokens'])} tokens, $${(r['Price (USD)']||0).toFixed(2)}`)
|
|
});
|
|
}
|
|
|
|
// Høgaste kostnadar
|
|
const topCosts = [...data].sort((a,b) => (b['Price (USD)']||0) - (a['Price (USD)']||0)).slice(0, 3);
|
|
boxes.push({
|
|
type: 'finding-box',
|
|
title: 'Topp 3 dyraste enkeltforespurnadar',
|
|
items: topCosts.map(r => `${r.Model} den ${r.Date} kl. ${r.Time}: $${(r['Price (USD)']||0).toFixed(2)}`)
|
|
});
|
|
|
|
// Tomme førespurnadar
|
|
const empty = data.filter(r => (r['Input Tokens'] || 0) === 0 || (r['Output Tokens'] || 0) === 0);
|
|
if (empty.length > 0) {
|
|
boxes.push({
|
|
type: 'warning-box',
|
|
title: `${empty.length} førespurnadar med 0 tokens`,
|
|
items: empty.slice(0, 5).map(r => `${r.Model} den ${r.Date} kl. ${r.Time}`)
|
|
});
|
|
}
|
|
|
|
if (boxes.length === 0) {
|
|
anomaliesContainer.innerHTML = '<div class="success-box">Ingen anomaliar funne ✨</div>';
|
|
log('No anomalies detected');
|
|
return;
|
|
}
|
|
|
|
let html = '';
|
|
boxes.forEach(box => {
|
|
html += `<div class="${box.type}">
|
|
<h3>${box.title}</h3>
|
|
<ul style="margin: 10px 0; padding-left: 20px;">
|
|
${box.items.map(item => `<li>${item}</li>`).join('')}
|
|
</ul>
|
|
</div>`;
|
|
});
|
|
|
|
anomaliesContainer.innerHTML = html;
|
|
log(`Anomalies rendered: ${boxes.length} sections`);
|
|
}
|
|
</script>
|
|
</body>
|
|
</html> |