Add explicit payload transformations
This commit is contained in:
+13
-7
@@ -25,7 +25,8 @@ const fileInput = ref<HTMLInputElement | null>(null)
|
||||
const placingPreset = ref<SystemPreset | null>(null)
|
||||
const selectedCapabilityIds = ref<string[]>([])
|
||||
type TransferKind = 'received' | 'sent' | 'stored'
|
||||
interface TransferRecord { id: string; kind: TransferKind; scenarioName: string; stepNumber: number; stepExplanation: string; senderId: string; destinationId: string; payload: string; timestamp: string }
|
||||
interface TransferChange { key: string; status: 'added' | 'removed' | 'changed' | 'same'; incoming?: unknown; outgoing?: unknown }
|
||||
interface TransferRecord { id: string; kind: TransferKind; scenarioName: string; stepNumber: number; stepExplanation: string; senderId: string; destinationId: string; payload: string; incoming: string; outgoing: string; changes: TransferChange[]; timestamp: string }
|
||||
const transferHistory = ref<TransferRecord[]>([])
|
||||
const dataTab = ref<TransferKind>('received')
|
||||
|
||||
@@ -43,7 +44,7 @@ const isRunning = computed(() => playing.value || stepIndex.value >= 0)
|
||||
function persist() { localStorage.setItem(STORAGE_KEY, JSON.stringify(landscape.value)) }
|
||||
function persistPresets() { localStorage.setItem(PRESET_STORAGE_KEY, JSON.stringify(customPresets.value)) }
|
||||
function selectSystem(id: string) { selectedSystemId.value = id; selectedConnectionId.value = '' }
|
||||
function selectConnection(id: string) { selectedConnectionId.value = id; selectedSystemId.value = '' }
|
||||
function selectConnection(id: string) { selectedConnectionId.value = id; selectedSystemId.value = ''; const connection = landscape.value.connections.find((item) => item.id === id); if (connection && !connection.transformation) connection.transformation = { incoming: connection.payload, outgoing: connection.payload, note: '' } }
|
||||
function addSystem() {
|
||||
const id = uid('system'); landscape.value.systems.push({ id, name: 'New system', description: 'Describe this system.', category: 'service', color: '#6d7cff', position: [160, 160], capabilities: [] }); selectSystem(id); persist(); rebuildGraph()
|
||||
}
|
||||
@@ -58,7 +59,7 @@ function deleteSystem(id: string) {
|
||||
}
|
||||
function addConnection() {
|
||||
if (landscape.value.systems.length < 2) return
|
||||
const connection: ConnectionModel = { id: uid('connection'), sourceId: landscape.value.systems[0].id, targetId: landscape.value.systems[1].id, label: 'New connection', description: '', payload: '{}' }
|
||||
const connection: ConnectionModel = { id: uid('connection'), sourceId: landscape.value.systems[0].id, targetId: landscape.value.systems[1].id, label: 'New connection', description: '', payload: '{}', transformation: { incoming: '{}', outgoing: '{}', note: '' } }
|
||||
landscape.value.connections.push(connection); selectConnection(connection.id); persist(); rebuildGraph()
|
||||
}
|
||||
function deleteConnection(id: string) {
|
||||
@@ -98,16 +99,20 @@ function recordTransfers(step: ScenarioModel['steps'][number]) {
|
||||
for (const connectionId of step.connectionIds) {
|
||||
const connection = landscape.value.connections.find((item) => item.id === connectionId)
|
||||
if (!connection) continue
|
||||
const payload = step.payload || connection.payload
|
||||
const incoming = connection.transformation?.incoming || step.payload || connection.payload
|
||||
const outgoing = connection.transformation?.outgoing || incoming
|
||||
const changes = comparePayloads(incoming, outgoing)
|
||||
const payload = outgoing
|
||||
transferHistory.value.push(
|
||||
{ id: uid('transfer'), kind: 'sent', scenarioName: selectedScenario.value.name, stepNumber: stepIndex.value + 1, stepExplanation: step.explanation, senderId: connection.sourceId, destinationId: connection.targetId, payload, timestamp: now },
|
||||
{ id: uid('transfer'), kind: 'received', scenarioName: selectedScenario.value.name, stepNumber: stepIndex.value + 1, stepExplanation: step.explanation, senderId: connection.sourceId, destinationId: connection.targetId, payload, timestamp: now },
|
||||
{ id: uid('transfer'), kind: 'stored', scenarioName: selectedScenario.value.name, stepNumber: stepIndex.value + 1, stepExplanation: step.explanation, senderId: connection.sourceId, destinationId: connection.targetId, payload, timestamp: now }
|
||||
...(['sent', 'received', 'stored'] as TransferKind[]).map((kind) => ({ id: uid('transfer'), kind, scenarioName: selectedScenario.value!.name, stepNumber: stepIndex.value + 1, stepExplanation: step.explanation, senderId: connection.sourceId, destinationId: connection.targetId, payload, incoming, outgoing, changes, timestamp: now }))
|
||||
)
|
||||
}
|
||||
}
|
||||
function comparePayloads(incoming: string, outgoing: string): TransferChange[] { try { const before = JSON.parse(incoming); const after = JSON.parse(outgoing); const keys = new Set([...Object.keys(before || {}), ...Object.keys(after || {})]); return [...keys].sort().map((key) => ({ key, status: !(key in before) ? 'added' : !(key in after) ? 'removed' : JSON.stringify(before[key]) !== JSON.stringify(after[key]) ? 'changed' : 'same', incoming: before[key], outgoing: after[key] })) } catch { return [] } }
|
||||
function enableTransformation() { if (selectedConnection.value && !selectedConnection.value.transformation) selectedConnection.value.transformation = { incoming: selectedConnection.value.payload, outgoing: selectedConnection.value.payload, note: '' } }
|
||||
function recordsForSelectedSystem(kind: TransferKind) { if (!selectedSystem.value) return []; return transferHistory.value.filter((record) => kind === 'sent' ? record.senderId === selectedSystem.value?.id : record.destinationId === selectedSystem.value?.id) }
|
||||
function systemName(id: string) { return landscape.value.systems.find((system) => system.id === id)?.name || id }
|
||||
const latestSelectedRecord = computed(() => selectedSystem.value ? transferHistory.value.filter((record) => record.destinationId === selectedSystem.value?.id).slice(-1)[0] : undefined)
|
||||
function animatePacket(duration: number) {
|
||||
stopAnimation(); const started = performance.now()
|
||||
const tick = (now: number) => { packetProgress.value = Math.min(1, (now - started) / duration); drawPackets(); if (packetProgress.value < 1) animationFrame = requestAnimationFrame(tick); else if (playing.value) setTimeout(advanceStep, 180 / speed.value) }
|
||||
@@ -187,5 +192,6 @@ onUnmounted(stopAnimation)
|
||||
<footer class="playback panel"><div class="playback-scenario"><span class="eyebrow">SIMULATION</span><strong>{{ selectedScenario?.name || 'No scenario selected' }}</strong></div><button class="control" @click="resetPlayback">↺</button><button class="control play" @click="togglePlay">{{ playing ? 'Ⅱ' : '▶' }}</button><button class="control" @click="advanceStep">›</button><div class="timeline"><div class="timeline-track"><span v-for="(step, index) in (selectedScenario?.steps || [])" :key="step.id" class="timeline-step" :class="{complete: index < stepIndex, current: index === stepIndex}" :style="{width: `${100 / Math.max(1, selectedScenario?.steps.length || 1)}%`}"><i></i><small>{{ index + 1 }}</small></span></div><div class="timeline-label"><span>{{ currentStep?.explanation || 'Ready to run' }}</span><span>{{ stepIndex + 1 < 0 ? 0 : stepIndex + 1 }} / {{ selectedScenario?.steps.length || 0 }}</span></div></div><label class="speed">Speed <select v-model.number="speed"><option :value="0.5">0.5×</option><option :value="1">1×</option><option :value="2">2×</option><option :value="4">4×</option></select></label></footer>
|
||||
<section class="event-drawer" v-if="eventLog.length"><span class="eyebrow">EVENT LOG</span><span v-for="event in eventLog.slice(0, 3)" :key="event">{{ event }}</span><span v-if="currentStep" class="payload"><b>Payload</b> {{ currentStep.payload }}</span></section>
|
||||
<div v-if="placingPreset" class="modal-backdrop" @click.self="placingPreset = null"><section class="preset-modal panel"><div class="inspector-header"><div><span class="eyebrow">PLACE PRESET</span><h2>{{ placingPreset.name }}</h2><p>{{ placingPreset.description }}</p></div><button class="danger-icon" @click="placingPreset = null">×</button></div><p class="muted">Choose the capabilities to place. The new canvas system will have its own editable copies.</p><label v-for="capability in placingPreset.capabilities" :key="capability.id" class="capability-choice" :title="capability.description"><input v-model="selectedCapabilityIds" type="checkbox" :value="capability.id" /><span><b>{{ capability.name }}</b><small>{{ capability.description }}</small></span></label><div class="modal-actions"><button class="ghost" @click="placingPreset = null">Cancel</button><button class="primary" :disabled="!selectedCapabilityIds.length" @click="placePreset">Place system</button></div></section></div>
|
||||
<section v-if="selectedConnection" class="transformation-panel panel"><div><span class="eyebrow">SIMULATED TRANSFORMATION</span><strong>{{ selectedConnection.label }}</strong><p class="muted">Configure the data change explicitly; playback never calls a real service.</p></div><label>Incoming JSON<textarea v-model="selectedConnection.transformation!.incoming" rows="4" class="code" @focus="enableTransformation"></textarea></label><label>Outgoing JSON<textarea v-model="selectedConnection.transformation!.outgoing" rows="4" class="code" @focus="enableTransformation"></textarea></label><label>Explanation<input v-model="selectedConnection.transformation!.note" placeholder="What changed?" @focus="enableTransformation" /></label></section><section v-if="latestSelectedRecord" class="transformation-view panel"><span class="eyebrow">LAST RECEIVED PAYLOAD</span><div class="json-columns"><div><b>Incoming</b><pre>{{ latestSelectedRecord.incoming }}</pre></div><div><b>Outgoing</b><pre>{{ latestSelectedRecord.outgoing }}</pre></div></div><div class="change-list"><span v-for="change in latestSelectedRecord.changes" :key="change.key" :class="`change-${change.status}`"><b>{{ change.status }}</b> {{ change.key }}: {{ change.incoming }} → {{ change.outgoing }}</span></div></section>
|
||||
</main>
|
||||
</template>
|
||||
|
||||
@@ -2,6 +2,7 @@ import { createApp } from 'vue'
|
||||
import App from './App.vue'
|
||||
import './style.css'
|
||||
import './inspector.css'
|
||||
import './transformation.css'
|
||||
import './style.css'
|
||||
|
||||
createApp(App).mount('#app')
|
||||
|
||||
@@ -23,6 +23,7 @@ export interface ConnectionModel {
|
||||
label: string
|
||||
description: string
|
||||
payload: string
|
||||
transformation?: { incoming: string; outgoing: string; note: string }
|
||||
}
|
||||
|
||||
export interface ScenarioStep {
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
.transformation-panel,.transformation-view{display:grid;grid-template-columns:1fr 1fr 1fr 1fr;gap:12px;padding:14px;align-items:start}.transformation-panel>div{grid-column:1/-1}.transformation-panel label{color:#8996af;font-size:11px;font-weight:700}.transformation-panel textarea,.transformation-panel input{display:block;width:100%;margin-top:6px;padding:8px;background:#0d1320;border:1px solid #303d58;color:#a9d8ce;border-radius:5px}.transformation-panel textarea{font-family:ui-monospace,monospace;font-size:10px}.transformation-view{grid-template-columns:1fr 1fr}.transformation-view>.eyebrow{grid-column:1/-1}.json-columns{display:grid;grid-template-columns:1fr 1fr;gap:12px;min-width:0}.json-columns b{font-size:10px;color:#8795ff}.json-columns pre{white-space:pre-wrap;overflow:auto;background:#0d1320;color:#a9d8ce;border:1px solid #2b3852;border-radius:5px;padding:9px;font-size:10px;min-height:70px}.change-list{grid-column:1/-1;display:flex;gap:7px;flex-wrap:wrap}.change-list span{font-size:10px;border-radius:4px;padding:5px 7px;background:#202a40;color:#c7d2e5}.change-list .change-added{border-left:3px solid #26c6a6}.change-list .change-removed{border-left:3px solid #e78091}.change-list .change-changed{border-left:3px solid #f2a65a}.change-list .change-same{border-left:3px solid #586681}@media(max-width:800px){.transformation-panel{grid-template-columns:1fr}.transformation-view,.json-columns{grid-template-columns:1fr}}
|
||||
Reference in New Issue
Block a user