File size: 7,875 Bytes
42a0d15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<template>
  <div class="graph-view">
    <div class="graph-header">
      <h1>Knowledge Graph</h1>
      <p>Explore relationships between auditees, findings, recommendations, and legal frameworks.</p>
    </div>

    <!-- Query Panel -->
    <div class="query-panel">
      <n-card title="Graph Queries" :bordered="false">
        <div class="query-grid">
          <n-button
            v-for="query in presetQueries"
            :key="query.id"
            quaternary
            class="query-btn"
            @click="runQuery(query.cypher)"
          >
            <template #icon>
              <n-icon><search-outline /></n-icon>
            </template>
            {{ query.label }}
          </n-button>
        </div>
        <n-divider />
        <n-input
          v-model:value="customQuery"
          type="textarea"
          placeholder="Enter Cypher query..."
          :autosize="{ minRows: 3, maxRows: 6 }"
          class="cypher-input"
        />
        <div class="query-actions">
          <n-button type="primary" @click="runQuery(customQuery)" :loading="isLoading">
            <template #icon><n-icon><play-outline /></n-icon></template>
            Run Query
          </n-button>
          <n-button quaternary @click="customQuery = ''">
            Clear
          </n-button>
        </div>
      </n-card>
    </div>

    <!-- Graph Visualization -->
    <div class="graph-visualization">
      <n-card :bordered="false" class="graph-card">
        <div v-if="isLoading" class="graph-loading">
          <n-spin size="large" />
          <p>Loading graph data...</p>
        </div>
        <div v-else-if="graphData.nodes.length === 0" class="graph-empty">
          <n-empty description="Run a query to visualize the knowledge graph">
            <template #icon>
              <n-icon size="48"><share-social-outline /></n-icon>
            </template>
          </n-empty>
        </div>
        <div v-else ref="graphContainer" class="graph-container">
          <v-chart class="chart" :option="chartOption" autoresize />
        </div>
      </n-card>
    </div>

    <!-- Results Panel -->
    <div v-if="queryResults.length > 0" class="results-panel">
      <n-card title="Query Results" :bordered="false">
        <n-data-table
          :columns="resultColumns"
          :data="queryResults"
          :pagination="{ pageSize: 10 }"
          size="small"
        />
      </n-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { use } from 'echarts/core'
import { CanvasRenderer } from 'echarts/renderers'
import { GraphChart } from 'echarts/charts'
import { TooltipComponent, LegendComponent } from 'echarts/components'
import VChart from 'vue-echarts'
import { api } from '@/api/client'
import type { GraphData, GraphNode, GraphEdge } from '@/types'
import { SearchOutline, PlayOutline, ShareSocialOutline } from '@vicons/ionicons5'

use([CanvasRenderer, GraphChart, TooltipComponent, LegendComponent])

const isLoading = ref(false)
const customQuery = ref('')
const graphData = ref<GraphData>({ nodes: [], edges: [] })
const queryResults = ref<any[]>([])

const presetQueries = [
  {
    id: 'pending-recs',
    label: 'Pending Recommendations by Ministry',
    cypher: 'MATCH (a:Auditee)<-[:AUDITS]-(r:AuditReport)-[:CONTAINS]->(f:Finding)-[:RECOMMENDS]->(rec:Recommendation) WHERE rec.status = "Pending" RETURN a.name, rec.description, rec.deadline ORDER BY rec.deadline',
  },
  {
    id: 'county-findings',
    label: 'Findings by County (2023/24)',
    cypher: 'MATCH (a:Auditee)-[:AUDITS]-(r:AuditReport)-[:CONTAINS]->(f:Finding) WHERE r.fiscal_year = "2023/24" AND a.type = "county" RETURN a.name, count(f) as findings ORDER BY findings DESC',
  },
  {
    id: 'legal-refs',
    label: 'Most Cited Legal Provisions',
    cypher: 'MATCH (f:Finding)-[:CITES_LAW]->(l:LegalProvision) RETURN l.section, count(f) as citations ORDER BY citations DESC LIMIT 10',
  },
  {
    id: 'adverse-opinions',
    label: 'Adverse/Disclaimer Opinions',
    cypher: 'MATCH (r:AuditReport) WHERE r.audit_opinion IN ["adverse", "disclaimer"] RETURN r.title, r.auditee, r.fiscal_year, r.audit_opinion',
  },
  {
    id: 'amount-entities',
    label: 'Largest Financial Irregularities',
    cypher: 'MATCH (f:Finding)-[:INVOLVES_AMOUNT]->(a:FinancialAmount) RETURN f.description, a.value, a.currency ORDER BY a.value DESC LIMIT 20',
  },
]

const chartOption = computed(() => ({
  tooltip: {
    trigger: 'item',
    formatter: (params: any) => {
      if (params.dataType === 'node') {
        return `<strong>${params.data.label}</strong><br/>Type: ${params.data.type}`
      }
      return `${params.data.source} -> ${params.data.target}`
    },
  },
  legend: {
    data: ['Auditee', 'Report', 'Finding', 'Legal', 'Amount'],
    bottom: 0,
  },
  series: [
    {
      type: 'graph',
      layout: 'force',
      data: graphData.value.nodes.map((n: GraphNode) => ({
        id: n.id,
        name: n.label,
        label: n.label,
        type: n.type,
        symbolSize: n.type === 'auditee' ? 40 : n.type === 'report' ? 30 : 20,
        itemStyle: {
          color: getNodeColor(n.type),
        },
        category: n.type,
      })),
      links: graphData.value.edges.map((e: GraphEdge) => ({
        source: e.source,
        target: e.target,
        label: {
          show: true,
          formatter: e.type,
        },
      })),
      categories: [
        { name: 'Auditee' },
        { name: 'Report' },
        { name: 'Finding' },
        { name: 'Legal' },
        { name: 'Amount' },
      ],
      roam: true,
      label: {
        show: true,
        position: 'right',
        fontSize: 12,
      },
      force: {
        repulsion: 300,
        edgeLength: 100,
      },
      emphasis: {
        focus: 'adjacency',
        lineStyle: {
          width: 4,
        },
      },
    },
  ],
}))

const resultColumns = [
  { title: 'Property', key: 'property' },
  { title: 'Value', key: 'value' },
]

const getNodeColor = (type: string) => {
  const colors: Record<string, string> = {
    auditee: '#1a5fb4',
    report: '#26a269',
    finding: '#e5a50a',
    legal: '#c01c28',
    amount: '#9b59b6',
  }
  return colors[type] || '#7f8c8d'
}

const runQuery = async (cypher: string) => {
  if (!cypher.trim()) return

  isLoading.value = true
  try {
    const response = await api.graphQuery(cypher)
    graphData.value = response
    queryResults.value = response.nodes.map((n: GraphNode) => ({
      property: n.label,
      value: JSON.stringify(n.properties),
    }))
  } catch (error) {
    console.error('Graph query failed:', error)
  } finally {
    isLoading.value = false
  }
}
</script>

<style scoped lang="scss">
.graph-view {
  padding-bottom: 48px;
}

.graph-header {
  margin-bottom: 32px;

  h1 {
    font-size: 28px;
    font-weight: 700;
    margin-bottom: 8px;
  }

  p {
    color: var(--text-secondary);
    font-size: 15px;
  }
}

.query-panel {
  margin-bottom: 24px;
}

.query-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 8px;
  margin-bottom: 16px;
}

.query-btn {
  justify-content: flex-start;
  text-align: left;
  height: auto;
  padding: 8px 12px;
  font-size: 13px;
}

.cypher-input {
  font-family: 'SF Mono', Monaco, monospace;
  font-size: 13px;
}

.query-actions {
  display: flex;
  gap: 12px;
  margin-top: 12px;
}

.graph-visualization {
  margin-bottom: 24px;
}

.graph-card {
  min-height: 500px;
}

.graph-loading,
.graph-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 500px;
  gap: 16px;
}

.graph-container {
  height: 500px;
}

.chart {
  width: 100%;
  height: 100%;
}

.results-panel {
  margin-top: 24px;
}

@media (max-width: 768px) {
  .query-grid {
    grid-template-columns: 1fr;
  }
}
</style>