Skip to content

Commit 2109091

Browse files
committed
added reset statistics functionality
1 parent 597316d commit 2109091

File tree

6 files changed

+90
-1
lines changed

6 files changed

+90
-1
lines changed

Lingarr.Client/src/components/features/dashboard/StatisticsComponent.vue

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@
104104
</div>
105105
</div>
106106
</div>
107+
108+
<div class="flex justify-end">
109+
<ButtonComponent
110+
variant="ghost"
111+
size="sm"
112+
:disabled="loading || resetting"
113+
:loading="resetting"
114+
@click="handleResetStatistics">
115+
Reset statistics
116+
</ButtonComponent>
117+
</div>
107118
</template>
108119
</CardComponent>
109120
</div>
@@ -115,13 +126,15 @@ import { DailyStatistic, MEDIA_TYPE, Statistics } from '@/ts'
115126
import { useI18n } from '@/plugins/i18n'
116127
import services from '@/services'
117128
import CardComponent from '@/components/common/CardComponent.vue'
129+
import ButtonComponent from '@/components/common/ButtonComponent.vue'
118130
import LoaderCircleIcon from '@/components/icons/LoaderCircleIcon.vue'
119131
import LanguageChart from './LanguageChart.vue'
120132
import StatCard from './StatCard.vue'
121133
import MetricCard from './MetricCard.vue'
122134
const { translate } = useI18n()
123135
124136
const loading = ref(true)
137+
const resetting = ref(false)
125138
const error = ref<string | null>(null)
126139
const statistics = ref<Statistics>()
127140
const dailyStats = ref<DailyStatistic[]>()
@@ -174,6 +187,27 @@ const fetchDailyStats = async () => {
174187
}
175188
}
176189
190+
const handleResetStatistics = async () => {
191+
if (!confirm('Are you sure you want to reset all statistics? This action cannot be undone.')) {
192+
return
193+
}
194+
195+
try {
196+
resetting.value = true
197+
error.value = null
198+
await services.statistics.resetStatistics()
199+
await fetchDailyStats()
200+
await fetchStatistics()
201+
} catch (err: unknown) {
202+
if (err instanceof Error) {
203+
error.value = err?.message || 'Failed to reset statistics'
204+
}
205+
console.error('Error resetting statistics:', err)
206+
} finally {
207+
resetting.value = false
208+
}
209+
}
210+
177211
onMounted(async () => {
178212
await fetchDailyStats()
179213
await fetchStatistics()

Lingarr.Client/src/services/statisticsService.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ const service = (http: AxiosStatic, resource = '/api/statistics'): IStatisticsSe
2424
reject(error.response)
2525
})
2626
})
27+
},
28+
29+
resetStatistics(): Promise<void> {
30+
return new Promise((resolve, reject) => {
31+
http.post(`${resource}/reset`)
32+
.then(() => {
33+
resolve()
34+
})
35+
.catch((error: AxiosError) => {
36+
reject(error.response)
37+
})
38+
})
2739
}
2840
})
2941

Lingarr.Client/src/ts/services.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export interface IDirectoryService {
120120
export interface IStatisticsService {
121121
getStatistics<T>(): Promise<T>
122122
getDailyStatistics<T>(days?: number): Promise<T>
123+
resetStatistics(): Promise<void>
123124
}
124125

125126
export interface ILogsService {

Lingarr.Server/Controllers/StatisticsController.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,11 @@ public async Task<ActionResult<IEnumerable<DailyStatistics>>> GetDailyStats(int
3030
var stats = await _statisticsService.GetDailyStatistics(days);
3131
return Ok(stats);
3232
}
33+
34+
[HttpPost("reset")]
35+
public async Task<ActionResult> ResetStatistics()
36+
{
37+
await _statisticsService.ResetStatistics();
38+
return Ok(new { message = "Statistics have been reset successfully" });
39+
}
3340
}

Lingarr.Server/Interfaces/Services/IStatisticsService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ Task<int> UpdateTranslationStatisticsFromLines(
1818
string serviceType,
1919
string? modelName,
2020
BatchTranslatedLine[] translatedLines);
21+
Task ResetStatistics();
2122
}

Lingarr.Server/Services/StatisticsService.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ namespace Lingarr.Server.Services;
1010
public class StatisticsService : IStatisticsService
1111
{
1212
private readonly LingarrDbContext _dbContext;
13+
private readonly ISettingService _settingService;
1314

1415
public StatisticsService(
15-
LingarrDbContext dbContext)
16+
LingarrDbContext dbContext,
17+
ISettingService settingService)
1618
{
1719
_dbContext = dbContext;
20+
_settingService = settingService;
1821
}
1922

2023
public async Task<Statistics> GetStatistics()
@@ -134,4 +137,35 @@ private async Task<int> UpdateTranslationStatisticsInternal(
134137

135138
return await _dbContext.SaveChangesAsync();
136139
}
140+
141+
public async Task ResetStatistics()
142+
{
143+
// Reset the main statistics entity
144+
var stats = await GetOrCreateStatistics();
145+
stats.TotalLinesTranslated = 0;
146+
stats.TotalCharactersTranslated = 0;
147+
stats.TotalFilesTranslated = 0;
148+
stats.TotalMovies = 0;
149+
stats.TotalEpisodes = 0;
150+
stats.TotalSubtitles = 0;
151+
stats.TranslationsByMediaType = new Dictionary<string, int>();
152+
stats.TranslationsByService = new Dictionary<string, int>();
153+
stats.SubtitlesByLanguage = new Dictionary<string, int>();
154+
stats.TranslationsByModel = new Dictionary<string, int>();
155+
156+
// Delete all daily statistics
157+
var dailyStats = await _dbContext.DailyStatistics.ToListAsync();
158+
_dbContext.DailyStatistics.RemoveRange(dailyStats);
159+
160+
// Reset telemetry snapshot settings
161+
var telemetrySettings = new Dictionary<string, string>
162+
{
163+
{ "telemetry_last_reported_lines", "0" },
164+
{ "telemetry_last_reported_files", "0" },
165+
{ "telemetry_last_reported_characters", "0" }
166+
};
167+
await _settingService.SetSettings(telemetrySettings);
168+
169+
await _dbContext.SaveChangesAsync();
170+
}
137171
}

0 commit comments

Comments
 (0)