Skip to content

Commit 8275b3c

Browse files
committed
minor-bugs-fixes
1 parent ab363b3 commit 8275b3c

4 files changed

Lines changed: 36 additions & 42 deletions

File tree

internal/api/handlers/dashboard.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,16 @@ func GetDecisions(dockerClient *docker.Client, cfg *config.Config, ttlCache ...*
6262
return func(c *gin.Context) {
6363
dockerClient = resolveDockerClient(c, dockerClient)
6464

65-
// Check cache first
65+
// Resolve pagination before building cache key so key includes limit/offset.
6666
summary := c.Query("summary") == "true"
67-
cacheKey := "decisions"
67+
defaultLimit := config.EffectiveLimit(cfg.DecisionListLimit, constants.MaxListLimit)
68+
limit, offset := parsePaginationParams(c, defaultLimit, constants.MaxListLimit)
69+
70+
var cacheKey string
6871
if summary {
6972
cacheKey = "decisions-summary"
73+
} else {
74+
cacheKey = fmt.Sprintf("decisions:limit=%d:offset=%d", limit, offset)
7075
}
7176
if len(ttlCache) > 0 && ttlCache[0] != nil {
7277
if cached, ok := ttlCache[0].Get(cacheKey); ok {
@@ -80,8 +85,6 @@ func GetDecisions(dockerClient *docker.Client, cfg *config.Config, ttlCache ...*
8085

8186
logger.Info("Getting CrowdSec decisions via cscli")
8287

83-
defaultLimit := config.EffectiveLimit(cfg.DecisionListLimit, constants.MaxListLimit)
84-
limit, offset := parsePaginationParams(c, defaultLimit, constants.MaxListLimit)
8588
fetchLimit := defaultLimit
8689
if c.Query("limit") != "" || c.Query("offset") != "" {
8790
fetchLimit = constants.MaxListLimit

internal/history/store.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -699,20 +699,29 @@ func (s *Store) GetDecisionHistoryRecord(ctx context.Context, id int64) (*models
699699
// GetHistoryStats returns aggregate counts for the history dashboard.
700700
func (s *Store) GetHistoryStats(ctx context.Context) (*models.HistoryStats, error) {
701701
var stats models.HistoryStats
702-
queries := []struct {
703-
dest *int
704-
query string
705-
}{
706-
{&stats.TotalDecisions, "SELECT COUNT(1) FROM decision_history"},
707-
{&stats.ActiveDecisions, "SELECT COUNT(1) FROM decision_history WHERE is_stale = 0"},
708-
{&stats.TotalAlerts, "SELECT COUNT(1) FROM alert_history"},
709-
{&stats.ActiveAlerts, "SELECT COUNT(1) FROM alert_history WHERE is_stale = 0"},
710-
{&stats.RepeatedOffenderCount, "SELECT COUNT(1) FROM repeated_offender_events"},
711-
}
712-
for _, q := range queries {
713-
if err := s.db.QueryRowContext(ctx, q.query).Scan(q.dest); err != nil {
714-
return nil, err
715-
}
702+
query := `
703+
SELECT
704+
(SELECT COUNT(1) FROM decision_history),
705+
(SELECT COUNT(1) FROM decision_history WHERE is_stale = 0),
706+
(SELECT COUNT(1) FROM alert_history),
707+
(SELECT COUNT(1) FROM alert_history WHERE is_stale = 0),
708+
(SELECT COUNT(DISTINCT value || '|' || scope)
709+
FROM (
710+
SELECT value, scope FROM decision_history
711+
WHERE datetime(created_at) >= datetime('now', '-30 days')
712+
GROUP BY value, scope
713+
HAVING COUNT(1) >= 3
714+
))
715+
`
716+
err := s.db.QueryRowContext(ctx, query).Scan(
717+
&stats.TotalDecisions,
718+
&stats.ActiveDecisions,
719+
&stats.TotalAlerts,
720+
&stats.ActiveAlerts,
721+
&stats.RepeatedOffenderCount,
722+
)
723+
if err != nil {
724+
return nil, err
716725
}
717726
return &stats, nil
718727
}

web/src/components/history/ReapplyDecisionDialog.tsx

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useForm, Controller } from 'react-hook-form'
33
import { toast } from 'sonner'
44
import { RotateCcw, Loader2 } from 'lucide-react'
55
import api from '@/lib/api'
6-
import type { DecisionHistoryRecord, RepeatedOffender } from '@/lib/api'
6+
import type { DecisionHistoryRecord } from '@/lib/api'
77
import { Button } from '@/components/ui/button'
88
import {
99
Dialog,
@@ -31,7 +31,7 @@ interface ReapplyFormValues {
3131
}
3232

3333
interface ReapplyDecisionDialogProps {
34-
record: DecisionHistoryRecord | RepeatedOffender | null
34+
record: DecisionHistoryRecord | null
3535
open: boolean
3636
onClose: () => void
3737
onSuccess: () => void
@@ -47,12 +47,7 @@ const DURATION_PRESETS = [
4747
{ value: 'custom', label: 'Custom…' },
4848
]
4949

50-
function getRecordId(record: DecisionHistoryRecord | RepeatedOffender): number | null {
51-
if ('id' in record) return record.id
52-
return null
53-
}
54-
55-
function getRecordValue(record: DecisionHistoryRecord | RepeatedOffender): string {
50+
function getRecordValue(record: DecisionHistoryRecord): string {
5651
return record.value
5752
}
5853

@@ -72,11 +67,7 @@ export function ReapplyDecisionDialog({ record, open, onClose, onSuccess }: Reap
7267

7368
const onSubmit = async (data: ReapplyFormValues) => {
7469
if (!record) return
75-
const id = getRecordId(record)
76-
if (!id) {
77-
toast.error('Cannot reapply: no record ID available')
78-
return
79-
}
70+
const id = record.id
8071

8172
const duration = data.duration === 'custom' ? data.customDuration : data.duration
8273
if (!duration) {

web/src/pages/History.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export default function History() {
124124
const [decisionValue, setDecisionValue] = useState('')
125125
const [decisionScenario, setDecisionScenario] = useState('')
126126
const [decisionSelected, setDecisionSelected] = useState<Set<number>>(new Set())
127-
const [reapplyRecord, setReapplyRecord] = useState<DecisionHistoryRecord | RepeatedOffender | null>(null)
127+
const [reapplyRecord, setReapplyRecord] = useState<DecisionHistoryRecord | null>(null)
128128
const [bulkReapplyOpen, setBulkReapplyOpen] = useState(false)
129129

130130
// Alerts tab state
@@ -663,16 +663,7 @@ export default function History() {
663663
: <span className="text-muted-foreground text-xs">-</span>
664664
}
665665
</TableCell>
666-
<TableCell className="text-right">
667-
<Button
668-
variant="ghost"
669-
size="sm"
670-
className="h-7 gap-1 text-xs"
671-
onClick={() => setReapplyRecord(o)}
672-
>
673-
<RotateCcw className="h-3 w-3" /> Re-apply
674-
</Button>
675-
</TableCell>
666+
<TableCell className="text-right" />
676667
</TableRow>
677668
))
678669
)}

0 commit comments

Comments
 (0)