File size: 15,650 Bytes
1ce80f7
 
 
 
 
1c34a81
 
1ce80f7
1c34a81
1ce80f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c34a81
1ce80f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c34a81
1ce80f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c34a81
1ce80f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c34a81
1ce80f7
 
 
1c34a81
 
1ce80f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c34a81
1ce80f7
 
 
 
 
 
 
 
1c34a81
 
1ce80f7
 
 
 
 
 
 
 
 
 
 
 
 
1c34a81
 
1ce80f7
 
 
 
 
1c34a81
1ce80f7
1c34a81
1ce80f7
 
 
 
 
1c34a81
1ce80f7
 
 
 
 
 
 
 
1c34a81
1ce80f7
 
 
 
 
 
 
 
1c34a81
 
 
 
 
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
import { useState } from 'react'
import { Archive, ArrowLeft, Plus, RefreshCw, Save } from 'lucide-react'
import { useApi } from '../../api/ApiProvider'
import { useApiQuery } from '../../api/useApiQuery'
import { canImportData } from '../../api/permissions'
import './model-workspace.css'

const statusLabel = status => (status ?? 'unknown').replaceAll('_', ' ')

export function ModelWorkspace({ model, onNotify, onCreateProjection, onBack }) {
  const { request, user } = useApi()
  const canEdit = canImportData(user?.roles ?? [])
  const modelId = model?.id
  const detailQuery = useApiQuery(modelId ? `/v1/models/${encodeURIComponent(modelId)}` : null)
  const versionsQuery = useApiQuery(modelId ? `/v1/models/${encodeURIComponent(modelId)}/versions` : null)
  const detail = detailQuery.data
  const versions = versionsQuery.data?.versions ?? detail?.versions ?? []
  const [selectedVersionId, setSelectedVersionId] = useState('')
  const selectedVersion = versions.find(version => version.id === selectedVersionId)
    ?? versions.find(version => version.is_current)
    ?? detail?.current_version
    ?? null
  const versionId = selectedVersion?.id
  const structureQuery = useApiQuery(versionId ? `/v1/model-versions/${encodeURIComponent(versionId)}/structure` : null)
  const variablesQuery = useApiQuery(versionId ? `/v1/model-versions/${encodeURIComponent(versionId)}/variables` : null)
  const [versionLabel, setVersionLabel] = useState('')
  const [changeSummary, setChangeSummary] = useState('')
  const [basis, setBasis] = useState('')
  const [createBusy, setCreateBusy] = useState(false)
  const [createError, setCreateError] = useState('')
  const [editingNotes, setEditingNotes] = useState(false)
  const [notesByVersion, setNotesByVersion] = useState({})
  const [saveBusy, setSaveBusy] = useState(false)
  const [actionError, setActionError] = useState('')
  const notes = selectedVersion
    ? notesByVersion[selectedVersion.id] ?? selectedVersion.notes ?? ''
    : ''

  const createDraftVersion = async () => {
    if (!canEdit || !modelId || createBusy) return
    setCreateBusy(true)
    setCreateError('')
    try {
      const created = await request(`/v1/models/${encodeURIComponent(modelId)}/versions`, {
        method: 'POST',
        body: JSON.stringify({
          ...(versionLabel.trim() ? { version_label: versionLabel.trim() } : {}),
          ...(selectedVersion?.id ? { parent_version_id: selectedVersion.id } : {}),
          ...((selectedVersion?.basis ?? basis.trim()) ? { basis: selectedVersion?.basis ?? basis.trim() } : {}),
          change_summary: changeSummary.trim() || null,
        }),
      })
      setVersionLabel('')
      setChangeSummary('')
      setSelectedVersionId(created.id)
      versionsQuery.refetch()
      detailQuery.refetch()
      onNotify(`Draft ${created.version_label} created. It is not runnable until the backend provides a validation or approval workflow.`)
    } catch (error) {
      setCreateError(error instanceof Error ? error.message : 'Could not create a model draft.')
    } finally {
      setCreateBusy(false)
    }
  }

  const saveNotes = async () => {
    if (!canEdit || !selectedVersion || selectedVersion.status !== 'draft' || saveBusy) return
    setSaveBusy(true)
    setActionError('')
    try {
      await request(`/v1/model-versions/${encodeURIComponent(selectedVersion.id)}`, {
        method: 'PATCH',
        body: JSON.stringify({ notes }),
      })
      setNotesByVersion(current => ({ ...current, [selectedVersion.id]: notes }))
      versionsQuery.refetch()
      detailQuery.refetch()
      setEditingNotes(false)
      onNotify('Draft version notes saved.')
    } catch (error) {
      setActionError(error instanceof Error ? error.message : 'Could not save the draft notes.')
    } finally {
      setSaveBusy(false)
    }
  }

  const archiveDraft = async () => {
    if (!canEdit || !selectedVersion || selectedVersion.status !== 'draft' || saveBusy) return
    if (!window.confirm(`Archive draft ${selectedVersion.version_label}? This draft will disappear from the version list.`)) return
    setSaveBusy(true)
    setActionError('')
    try {
      await request(`/v1/model-versions/${encodeURIComponent(selectedVersion.id)}`, { method: 'DELETE' })
      setSelectedVersionId('')
      versionsQuery.refetch()
      detailQuery.refetch()
      onNotify(`Draft ${selectedVersion.version_label} archived.`)
    } catch (error) {
      setActionError(error instanceof Error ? error.message : 'Could not archive the draft.')
    } finally {
      setSaveBusy(false)
    }
  }

  const structure = structureQuery.data
  const variables = variablesQuery.data?.variables ?? []
  const formulaGroups = structure?.formula_groups ?? []

  return (
    <div className="model-workspace flex flex-col gap-4">

      <header className="flex flex-wrap items-center justify-between gap-3 rounded-[10px] border border-[#dce3e9] bg-white p-4">

        <div className="min-w-0">

          <span className="text-[10px] font-bold uppercase tracking-wide text-[#71809f]">Model detail · {detail?.product_code ?? model?.product ?? '—'}</span>

          <h2 className="mt-1 text-2xl font-bold text-[#101b37]">{detail?.name ?? model?.name ?? 'Model'}</h2>

          <p className="mt-1 text-xs text-[#71809f]">{detail?.description || 'No description'} · Model ID {detail?.id ?? modelId}</p>

        </div>

        <div className="flex flex-wrap gap-2">

          {onBack && <button className="model-open-button" onClick={onBack}><ArrowLeft size={15} />Back to models</button>}

          <button className="model-open-button" onClick={() => { detailQuery.refetch(); versionsQuery.refetch(); if (versionId) { structureQuery.refetch(); variablesQuery.refetch() } }}><RefreshCw size={15} />Refresh</button>

          <button className="model-open-button" onClick={onCreateProjection}>Create Projection Set</button>

        </div>

      </header>

      {detailQuery.error && <p role="alert">Could not load model detail: {detailQuery.error.message}</p>}

      <div className="grid grid-cols-[minmax(0,1.3fr)_minmax(280px,.7fr)] items-start gap-4 max-[900px]:grid-cols-1">

        <main className="flex min-w-0 flex-col gap-4">

          <section className="mw-card">

            <div className="flex flex-wrap items-center justify-between gap-3">

              <div><h2 className="text-base font-bold">Versions</h2><p className="mt-1 text-xs text-[#71809f]">Draft versions are not runnable yet. Formulas are not editable from the UI.</p></div>

              <span className="text-xs text-[#71809f]">{versions.length} versions</span>

            </div>

            {versionsQuery.loading && <p role="status">Loading model versions…</p>}

            {versionsQuery.error && <p role="alert">Could not load versions: {versionsQuery.error.message}</p>}

            <div className="mt-3 flex flex-col gap-2">{versions.map(version => (

              <button key={version.id} aria-pressed={selectedVersion?.id === version.id} className={`w-full rounded-lg border p-3 text-left ${selectedVersion?.id === version.id ? 'border-[#80c51a] bg-[#f5faee]' : 'border-[#e2e8ed] bg-white'}`} onClick={() => { setSelectedVersionId(version.id); setEditingNotes(false) }}>

                <span className="flex flex-wrap items-center justify-between gap-2"><b>{version.version_label}</b><span className="rounded-full bg-[#edf1f5] px-2 py-1 text-[10px] uppercase">{statusLabel(version.status)}{version.status === 'draft' ? ' · not runnable yet' : ''}</span></span>

                <small className="mt-1 block text-[#71809f]">{version.basis ?? 'No basis'} · {version.is_current ? 'Current' : 'Historical'} · {version.created_at ? new Date(version.created_at).toLocaleDateString() : 'Date unavailable'}</small>

              </button>

            ))}{!versionsQuery.loading && !versionsQuery.error && !versions.length && <p>No versions are listed for this model.</p>}</div>

          </section>



          <section className="mw-card">

            <div className="flex flex-wrap items-center justify-between gap-3"><div><h2 className="text-base font-bold">Version structure</h2><p className="mt-1 text-xs text-[#71809f]">{selectedVersion ? `${selectedVersion.version_label} · ${statusLabel(selectedVersion.status)}` : 'Select a model version'}</p></div></div>

            {structureQuery.loading && <p role="status">Loading model structure…</p>}

            {structureQuery.error && <p role="alert">Could not load structure: {structureQuery.error.message}</p>}

            {structure && <div className="mt-3">

              {selectedVersion?.status === 'draft' && <p className="mb-3 rounded-md bg-[#fff7df] p-2 text-xs text-[#805d10]">This draft is not runnable yet. The backend does not expose model-version approval or validation.</p>}

              <div className="grid grid-cols-2 gap-2 sm:grid-cols-4">{Object.entries(structure.facts ?? {}).map(([label, value]) => <div className="rounded-md bg-[#f5f7f8] p-2" key={label}><small className="block text-[#71809f]">{label.replaceAll('_', ' ')}</small><b>{value}</b></div>)}</div>

              <h3 className="mt-4 font-bold">Hierarchy</h3>

              <div className="mt-2 flex flex-col">{(structure.hierarchy ?? []).map((item, index) => <div className="flex items-center justify-between gap-2 border-b border-[#edf0f2] py-2 text-xs" key={`${item.id ?? item.label}-${index}`} style={{ paddingLeft: `${Math.min(item.depth ?? 0, 8) * 14}px` }}><span>{item.label}</span><small className="text-[#71809f]">{item.level} · {item.meta}</small></div>)}</div>

              <h3 className="mt-4 font-bold">Formula groups</h3>

              <div className="mt-2 flex flex-col gap-2">{formulaGroups.map(group => <article className="rounded-md border border-[#e2e8ed] p-2 text-xs" key={group.id}><b>{group.name}</b><p className="text-[#71809f]">{group.formula_count} formulas · {group.version_label} · {group.lineage_state}</p><small>{group.description}</small></article>)}{!formulaGroups.length && <p>No formula groups reported.</p>}</div>

              {structure.validation && <details className="mt-3"><summary className="cursor-pointer text-xs font-bold">Validation · {structure.validation.status}</summary><pre className="mt-2 overflow-auto rounded bg-[#f5f7f8] p-2 text-[10px]">{JSON.stringify(structure.validation, null, 2)}</pre></details>}

            </div>}

          </section>



          <section className="mw-card">

            <div><h2 className="text-base font-bold">Variables ({variablesQuery.data?.total ?? variables.length})</h2><p className="mt-1 text-xs text-[#71809f]">Definitions used by this model version.</p></div>

            {variablesQuery.loading && <p role="status">Loading variables…</p>}

            {variablesQuery.error && <p role="alert">Could not load variables: {variablesQuery.error.message}</p>}

            <div className="mt-3 overflow-auto"><table className="w-full border-collapse text-left text-xs"><thead><tr className="border-b border-[#e2e8ed] text-[#71809f]"><th className="p-2">Variable</th><th className="p-2">Kind / type</th><th className="p-2">Unit</th><th className="p-2">Required</th><th className="p-2">Source</th></tr></thead><tbody>{variables.map(variable => <tr className="border-b border-[#edf0f2]" key={variable.id}><td className="p-2"><b>{variable.display_name}</b><small className="block text-[#71809f]">{variable.variable_name} · {variable.description}</small></td><td className="p-2">{variable.kind} / {variable.data_type}</td><td className="p-2">{variable.unit ?? '—'}</td><td className="p-2">{variable.required ? 'Yes' : 'No'}</td><td className="p-2">{variable.source?.type ?? '—'}</td></tr>)}</tbody></table></div>

          </section>

        </main>



        <aside className="flex flex-col gap-4">

          <section className="mw-card">

            <h2 className="text-base font-bold">Model details</h2>

            <dl className="mt-3 grid grid-cols-[auto_1fr] gap-x-3 gap-y-2 text-xs"><dt className="text-[#71809f]">Status</dt><dd>{statusLabel(detail?.status ?? model?.status)}</dd><dt className="text-[#71809f]">Owner</dt><dd>{detail?.owner?.full_name ?? model?.owner ?? 'Unavailable'}</dd><dt className="text-[#71809f]">Runs</dt><dd>{detail?.run_count ?? model?.runs ?? 0}</dd><dt className="text-[#71809f]">Updated</dt><dd>{detail?.updated_at ? new Date(detail.updated_at).toLocaleString() : model?.updated ?? 'Unavailable'}</dd></dl>

            {detail?.access?.length > 0 && <div className="mt-3 border-t pt-3"><h3 className="font-bold">Access</h3>{detail.access.map(item => <p className="mt-1 text-xs" key={item.user.id}>{item.user.full_name} · {item.role}</p>)}</div>}

          </section>

          <section className="mw-card flex flex-col gap-3">

            <div><h2 className="text-base font-bold">Create draft version</h2><p className="mt-1 text-xs text-[#71809f]">Copies the selected version's structure. Draft versions cannot run yet.</p></div>

            {!canEdit && <p className="text-xs text-[#71809f]">Your role is read-only for model registries.</p>}

            <label className="grid gap-1 text-xs">Version label<input className="rounded border p-2" maxLength={30} value={versionLabel} onChange={event => setVersionLabel(event.target.value)} placeholder="Optional, e.g. v0.2" /></label>

            {!selectedVersion && <label className="grid gap-1 text-xs">Basis<input className="rounded border p-2" required value={basis} onChange={event => setBasis(event.target.value)} placeholder="Required for the first version" /></label>}

            <label className="grid gap-1 text-xs">Change summary<input className="rounded border p-2" value={changeSummary} onChange={event => setChangeSummary(event.target.value)} /></label>

            {createError && <p role="alert" className="text-xs text-red-700">{createError}</p>}

            <button className="model-open-button" disabled={!canEdit || createBusy || (!selectedVersion && !basis.trim())} onClick={() => void createDraftVersion()}><Plus size={15} />{createBusy ? 'Creating…' : 'Create draft version'}</button>

          </section>

          {selectedVersion && <section className="mw-card flex flex-col gap-3">

            <div><h2 className="text-base font-bold">Draft notes</h2><p className="mt-1 text-xs text-[#71809f]">Only draft versions can be edited or archived here.</p></div>

            {actionError && <p role="alert" className="text-xs text-red-700">{actionError}</p>}

            {selectedVersion.status === 'draft' ? <>

              <label className="grid gap-1 text-xs">Notes<textarea className="min-h-24 rounded border p-2" disabled={!editingNotes} value={notes} onChange={event => setNotesByVersion(current => ({ ...current, [selectedVersion.id]: event.target.value }))} /></label>

              {canEdit && <div className="flex flex-wrap gap-2">{editingNotes ? <button className="model-open-button" disabled={saveBusy} onClick={() => void saveNotes()}><Save size={15} />Save notes</button> : <button className="model-open-button" onClick={() => setEditingNotes(true)}>Edit notes</button>}<button className="model-open-button" disabled={saveBusy} onClick={() => void archiveDraft()}><Archive size={15} />Archive draft</button></div>}

            </> : <p className="text-xs text-[#71809f]">This version is {statusLabel(selectedVersion.status)}; edit and archive actions are unavailable.</p>}

          </section>}

        </aside>

      </div>
    </div>
  )
}