Legg til PPQ kostnadsanalyse-webapp
This commit is contained in:
commit
4fbbb5fa55
1 file changed
+582
@@ -0,0 +1,582 @@
|
||||
<!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: 2.5rem;
|
||||
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: 40px;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s;
|
||||
}
|
||||
|
||||
.upload-area:hover {
|
||||
border-color: #6d4aff;
|
||||
background: #f8f5ff;
|
||||
}
|
||||
|
||||
.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;
|
||||
transition: all 0.3s;
|
||||
}
|
||||
|
||||
button.analyze-btn:hover {
|
||||
background: #5a3fd6;
|
||||
transform: translateY(-2px);
|
||||
}
|
||||
|
||||
.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;
|
||||
}
|
||||
|
||||
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: 200px;
|
||||
box-shadow: 0 4px 15px rgba(109,74,255,0.3);
|
||||
}
|
||||
|
||||
.metric-value {
|
||||
font-size: 2rem;
|
||||
font-weight: bold;
|
||||
margin-bottom: 5px;
|
||||
}
|
||||
|
||||
.metric-label {
|
||||
opacity: 0.9;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.finding-box {
|
||||
background: #fff3cd;
|
||||
border-left: 4px solid #ffc107;
|
||||
padding: 15px 20px;
|
||||
margin: 20px 0;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.warning-box {
|
||||
background: #f8d7da;
|
||||
border-left: 4px solid #dc3545;
|
||||
padding: 15px 20px;
|
||||
margin: 20px 0;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.success-box {
|
||||
background: #d4edda;
|
||||
border-left: 4px solid #28a745;
|
||||
padding: 15px 20px;
|
||||
margin: 20px 0;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.privacy-note {
|
||||
background: rgba(255,255,255,0.1);
|
||||
color: white;
|
||||
padding: 15px;
|
||||
border-radius: 8px;
|
||||
text-align: center;
|
||||
margin-top: 20px;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.metric-card {
|
||||
display: block;
|
||||
margin: 10px 0;
|
||||
}
|
||||
|
||||
table {
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
|
||||
th, td {
|
||||
padding: 8px 10px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<header>
|
||||
<h1>📊 PPQ Cost Analyzer</h1>
|
||||
<p>Analyser PayPerQ-bruken din - alle data held seg i nettlesaren din</p>
|
||||
</header>
|
||||
|
||||
<section class="upload-section" id="uploadSection">
|
||||
<h2>📁 Last opp CSV-fil</h2>
|
||||
<p style="margin-bottom: 20px; color: #666;">Drag & drop eller klikk for å velje fil</p>
|
||||
|
||||
<input type="file" id="fileInput" accept=".csv" style="display:none">
|
||||
<div class="upload-area" id="uploadArea">
|
||||
<div style="font-size: 3rem; margin-bottom: 10px;">📄</div>
|
||||
<div><strong>PayPerQ-Usage_*.csv</strong></div>
|
||||
</div>
|
||||
|
||||
<div class="file-info" id="fileInfo"></div>
|
||||
|
||||
<button class="analyze-btn" id="analyzeBtn" style="display:none">Analyser data</button>
|
||||
</section>
|
||||
|
||||
<section class="report-section" id="reportSection">
|
||||
<div class="privacy-note">
|
||||
🔒 **Privacy**: All analyse skjer lokalt i nettlesaren. Ingen data blir sende til serverar.
|
||||
</div>
|
||||
|
||||
<h2>📊 Sammendrag</h2>
|
||||
<div id="summaryMetrics"></div>
|
||||
|
||||
<h2>💰 Kostnad per modell</h2>
|
||||
<table id="modelTable"></table>
|
||||
|
||||
<h2>📅 Dagleg fordeling</h2>
|
||||
<table id="dailyTable"></table>
|
||||
|
||||
<h2>⚠️ Anomalier</h2>
|
||||
<div id="anomalies"></div>
|
||||
|
||||
<h2>🕐 Timefordeling (aktivitet)</h2>
|
||||
<table id="hourlyTable"></table>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>
|
||||
<script>
|
||||
// Globale variablar
|
||||
let csvData = [];
|
||||
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');
|
||||
|
||||
// Drag & drop eventar
|
||||
uploadArea.addEventListener('click', () => fileInput.click());
|
||||
|
||||
uploadArea.addEventListener('dragover', (e) => {
|
||||
e.preventDefault();
|
||||
uploadArea.classList.add('dragover');
|
||||
});
|
||||
|
||||
uploadArea.addEventListener('dragleave', () => {
|
||||
uploadArea.classList.remove('dragover');
|
||||
});
|
||||
|
||||
uploadArea.addEventListener('drop', (e) => {
|
||||
e.preventDefault();
|
||||
uploadArea.classList.remove('dragover');
|
||||
const files = e.dataTransfer.files;
|
||||
if (files.length > 0) handleFile(files[0]);
|
||||
});
|
||||
|
||||
fileInput.addEventListener('change', (e) => {
|
||||
if (e.target.files.length > 0) handleFile(e.target.files[0]);
|
||||
});
|
||||
|
||||
analyzeBtn.addEventListener('click', generateReport);
|
||||
|
||||
function handleFile(file) {
|
||||
if (!file.name.endsWith('.csv')) {
|
||||
alert('Vennligst last opp ei .csv-fil');
|
||||
return;
|
||||
}
|
||||
|
||||
fileInfo.style.display = 'block';
|
||||
fileInfo.innerHTML = `✅ <strong>${file.name}</strong> (${formatSize(file.size)})`;
|
||||
analyzeBtn.style.display = 'inline-block';
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.onload = (e) => {
|
||||
csvData = parseCSV(e.target.result);
|
||||
};
|
||||
reader.readAsText(file);
|
||||
}
|
||||
|
||||
function parseCSV(csvText) {
|
||||
const lines = csvText.trim().split('\n');
|
||||
const headers = lines[0].split(',').map(h => h.trim());
|
||||
const data = [];
|
||||
|
||||
for (let i = 1; i < lines.length && !lines[i].includes('Total Records'); i++) {
|
||||
const values = lines[i].split(',');
|
||||
if (values.length === headers.length) {
|
||||
const row = {};
|
||||
headers.forEach((header, idx) => {
|
||||
let val = values[idx]?.trim();
|
||||
|
||||
// Konverter tall
|
||||
if (header.includes('Tokens') || header.includes('Price')) {
|
||||
val = parseFloat(val) || 0;
|
||||
}
|
||||
|
||||
row[header] = val;
|
||||
});
|
||||
data.push(row);
|
||||
}
|
||||
}
|
||||
|
||||
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 num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ' ');
|
||||
}
|
||||
|
||||
function generateReport() {
|
||||
if (csvData.length === 0) {
|
||||
alert('Ingen data funnen');
|
||||
return;
|
||||
}
|
||||
|
||||
// Beregn statistsikar
|
||||
const stats = calculateStats(csvData);
|
||||
|
||||
// Render summary
|
||||
renderSummary(stats);
|
||||
|
||||
// Render modell-tabell
|
||||
renderModelTable(stats.models);
|
||||
|
||||
// Render dagleg tabell
|
||||
renderDailyTable(stats.daily);
|
||||
|
||||
// Render anomalier
|
||||
renderAnomalies(csvData);
|
||||
|
||||
// Render timefordeling
|
||||
renderHourlyTable(csvData);
|
||||
|
||||
// Vis rapport
|
||||
uploadSection.style.display = 'none';
|
||||
reportSection.classList.add('active');
|
||||
|
||||
// Scroll to top
|
||||
window.scrollTo(0, 0);
|
||||
}
|
||||
|
||||
function calculateStats(data) {
|
||||
const models = {};
|
||||
const daily = {};
|
||||
const hourly = {};
|
||||
|
||||
data.forEach(row => {
|
||||
// Per modell
|
||||
const model = row.Model;
|
||||
if (!models[model]) {
|
||||
models[model] = { count: 0, totalCost: 0, totalInput: 0, totalOutput: 0 };
|
||||
}
|
||||
models[model].count++;
|
||||
models[model].totalCost += row['Price (USD)'];
|
||||
models[model].totalInput += row['Input Tokens'];
|
||||
models[model].totalOutput += row['Output Tokens'];
|
||||
|
||||
// Per dag
|
||||
const date = row.Date;
|
||||
if (!daily[date]) {
|
||||
daily[date] = { cost: 0, count: 0 };
|
||||
}
|
||||
daily[date].cost += row['Price (USD)'];
|
||||
daily[date].count++;
|
||||
|
||||
// Per time
|
||||
const hour = parseInt(row.Time.split(':')[0]);
|
||||
if (!hourly[hour]) {
|
||||
hourly[hour] = { cost: 0, count: 0 };
|
||||
}
|
||||
hourly[hour].cost += row['Price (USD)'];
|
||||
hourly[hour].count++;
|
||||
});
|
||||
|
||||
return {
|
||||
totalCost: data.reduce((sum, r) => sum + r['Price (USD)'], 0),
|
||||
totalRequests: data.length,
|
||||
totalInput: data.reduce((sum, r) => sum + r['Input Tokens'], 0),
|
||||
totalOutput: data.reduce((sum, r) => sum + r['Output Tokens'], 0),
|
||||
uniqueDates: new Set(data.map(r => r.Date)).size,
|
||||
models,
|
||||
daily,
|
||||
hourly
|
||||
};
|
||||
}
|
||||
|
||||
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(stats.totalInput / 1000000).replace(/\.00$/, '')}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>
|
||||
`;
|
||||
}
|
||||
|
||||
function renderModelTable(models) {
|
||||
const sorted = Object.entries(models)
|
||||
.sort((a, b) => b[1].totalCost - a[1].totalCost);
|
||||
|
||||
let html = '<tr><th>Modell</th><th>Føresp.</th><th>Total ($)</th><th>Gj.snitt ($)</th></tr>';
|
||||
|
||||
sorted.forEach(([name, stats]) => {
|
||||
const avg = stats.totalCost / stats.count;
|
||||
html += `<tr>
|
||||
<td><strong>${name}</strong></td>
|
||||
<td>${formatNumber(stats.count)}</td>
|
||||
<td>$${stats.totalCost.toFixed(2)}</td>
|
||||
<td>$${avg.toFixed(4)}</td>
|
||||
</tr>`;
|
||||
});
|
||||
|
||||
document.getElementById('modelTable').innerHTML = html;
|
||||
}
|
||||
|
||||
function renderDailyTable(daily) {
|
||||
const sorted = Object.entries(daily).sort((a, b) => a[0].localeCompare(b[0]));
|
||||
|
||||
let html = '<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>`;
|
||||
});
|
||||
|
||||
document.getElementById('dailyTable').innerHTML = html;
|
||||
}
|
||||
|
||||
function renderAnomalies(data) {
|
||||
const anomaliesContainer = document.getElementById('anomalies');
|
||||
const boxes = [];
|
||||
|
||||
// Gratis førespurnadar
|
||||
const free = data.filter(r => r['Price (USD)'] === 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} (${r.Input Tokens} tokens)`)
|
||||
});
|
||||
}
|
||||
|
||||
// Monsterkontekstar
|
||||
const big = data.filter(r => r['Input Tokens'] > 100000);
|
||||
if (big.length > 0) {
|
||||
boxes.push({
|
||||
type: 'warning-box',
|
||||
title: `${big.length} "monsterkontekstar" (>100k tokens)`,
|
||||
items: big.sort((a,b) => b['Input Tokens'] - a['Input Tokens'])
|
||||
.slice(0, 5)
|
||||
.map(r => `${r.Model}: ${r['Input Tokens']} tokens, $${r['Price (USD)'].toFixed(2)}`)
|
||||
});
|
||||
}
|
||||
|
||||
// Høgaste kostnadar
|
||||
const topCosts = [...data].sort((a,b) => b['Price (USD)'] - a['Price (USD)']).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)'].toFixed(2)}`)
|
||||
});
|
||||
|
||||
// Tomme førespurnadar
|
||||
const empty = data.filter(r => r['Input Tokens'] === 0 || r['Output Tokens'] === 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>';
|
||||
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;
|
||||
}
|
||||
|
||||
function renderHourlyTable(data) {
|
||||
const hourly = {};
|
||||
data.forEach(r => {
|
||||
const hour = parseInt(r.Time.split(':')[0]);
|
||||
if (!hourly[hour]) {
|
||||
hourly[hour] = { cost: 0, count: 0 };
|
||||
}
|
||||
hourly[hour].cost += r['Price (USD)'];
|
||||
hourly[hour].count++;
|
||||
});
|
||||
|
||||
const sorted = Object.entries(hourly).sort((a, b) => parseInt(a[0]) - parseInt(b[0]));
|
||||
|
||||
let html = '<tr><th>Time</th><th>Kostnad ($)</th><th>Førespurnadar</th></tr>';
|
||||
let peakHour = null, peakCost = 0;
|
||||
|
||||
sorted.forEach(([hour, stats]) => {
|
||||
if (stats.cost > peakCost) {
|
||||
peakCost = stats.cost;
|
||||
peakHour = hour;
|
||||
}
|
||||
html += `<tr>
|
||||
<td>${parseInt(hour):02d}:00</td>
|
||||
<td>$${stats.cost.toFixed(2)}</td>
|
||||
<td>${formatNumber(stats.count)}</td>
|
||||
</tr>`;
|
||||
});
|
||||
|
||||
document.getElementById('hourlyTable').innerHTML = html;
|
||||
|
||||
if (peakHour !== null) {
|
||||
const peakBox = document.createElement('div');
|
||||
peakBox.className = 'success-box';
|
||||
peakBox.innerHTML = `
|
||||
<strong>Høgaste kostnad:</strong> ${(parseInt(peakHour)):02d}:00 med $${peakCost.toFixed(2)}
|
||||
`;
|
||||
document.getElementById('hourlyTable').appendChild(peakBox);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user