Skip to content

Commit 6dc233f

Browse files
committed
refactor: return aggregated profiles
1 parent 91ce19a commit 6dc233f

File tree

3 files changed

+38
-17
lines changed

3 files changed

+38
-17
lines changed

backend/src/internal/usecase/queries/getAggregatedProfiles.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,8 @@ import type { PersistencePort } from '../../core/port/PersistencePort.ts';
33

44
export async function getAggregatedProfiles(
55
persistence: PersistencePort,
6-
): Promise<
7-
{
8-
createdAt: string;
9-
emotions: AggregatedEmotionProfile['emotions'];
10-
tonalities: AggregatedEmotionProfile['tonalities'];
11-
totalWeight: number;
12-
}[]
13-
> {
6+
): Promise<AggregatedEmotionProfile[]> {
147
const snapshots = await persistence.getSnapshots();
15-
168
return snapshots
179
.filter((s) => {
1810
const ok = !!s.aggregatedEmotionProfile;
@@ -23,10 +15,5 @@ export async function getAggregatedProfiles(
2315
}
2416
return ok;
2517
})
26-
.map((s) => ({
27-
createdAt: s.createdAt,
28-
emotions: s.aggregatedEmotionProfile.emotions,
29-
tonalities: s.aggregatedEmotionProfile.tonalities,
30-
totalWeight: s.aggregatedEmotionProfile.totalWeight,
31-
}));
18+
.map((s) => s.aggregatedEmotionProfile);
3219
}

frontend/src/components/Chart/useChartData.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { useEffect, useState } from 'react';
2-
import { assertRawEntries } from '../../types/Chart.ts';
2+
import {
3+
assertRawEntries,
4+
type AggregatedEmotionProfile,
5+
} from '../../types/Chart.ts';
36
import { EMOTION_KEYS, TONALITY_KEYS } from './config.ts';
47
import {
58
parseEmotions,
@@ -20,7 +23,12 @@ export function useChartData() {
2023
void (async () => {
2124
try {
2225
const res = await fetch('chart.json');
23-
const raw: unknown = await res.json();
26+
const agg = (await res.json()) as AggregatedEmotionProfile[];
27+
const raw = agg.map((p) => ({
28+
createdAt: p.date,
29+
emotions: p.emotions,
30+
tonalities: p.tonalities,
31+
}));
2432
assertRawEntries(raw);
2533
const emotions = parseEmotions(raw);
2634
const tonalities = parseTonalities(raw);

frontend/src/types/Chart.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,29 @@ export function assertRawEntries(x: unknown): asserts x is RawEntry[] {
1818
throw new Error('Invalid chart.json shape');
1919
}
2020
}
21+
22+
export interface EmotionScores {
23+
anger: number;
24+
fear: number;
25+
trust: number;
26+
sadness: number;
27+
joy: number;
28+
disgust: number;
29+
}
30+
31+
export interface TonalityScores {
32+
positive: number;
33+
negative: number;
34+
optimistic_anticipation: number;
35+
pessimistic_anticipation: number;
36+
positive_surprise: number;
37+
negative_surprise: number;
38+
}
39+
40+
export interface AggregatedEmotionProfile {
41+
date: string;
42+
count: number;
43+
totalWeight: number;
44+
emotions: EmotionScores;
45+
tonalities: TonalityScores;
46+
}

0 commit comments

Comments
 (0)