All files / frontend/src/containers/Prefs PrefsLogging.tsx

58.33% Statements 14/24
75% Branches 3/4
60% Functions 3/5
58.33% Lines 14/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                        10x 10x 10x 10x     10x 5x     10x 5x 5x 5x 5x         5x       10x                                         10x                                                                                            
import { useState, useEffect } from 'react';
import Typography from '@mui/material/Typography';
import Select from '@mui/material/Select';
import MenuItem from '@mui/material/MenuItem';
import FormControl from '@mui/material/FormControl';
import InputLabel from '@mui/material/InputLabel';
import Button from '@mui/material/Button';
import Box from '@mui/material/Box';
import { getInstanceSetting, updateInstanceSettings } from '../../api';
import { useSnackbar } from '../../contexts/SnackbarContext';
 
function PrefsLogging() {
  const { addSnackMessage } = useSnackbar();
  const [logRetention, setLogRetention] = useState<number>(30);
  const [loading, setLoading] = useState<boolean>(true);
  const [saving, setSaving] = useState<boolean>(false);
 
  // Load log retention on mount
  useEffect(() => {
    loadLogRetention();
  }, []);
 
  const loadLogRetention = async () => {
    try {
      setLoading(true);
      const retention = await getInstanceSetting('logging.retention');
      setLogRetention((retention as number) || 30);
    } catch (error) {
      console.error('Failed to load log retention:', error);
      addSnackMessage('Failed to load log retention', { severity: 'error' });
    } finally {
      setLoading(false);
    }
  };
 
  const handleSave = async () => {
    try {
      setSaving(true);
 
      await updateInstanceSettings({
        logging: { retention: logRetention } as any
      });
 
      addSnackMessage(`Log retention updated to ${logRetention} days`, {
        severity: 'success'
      });
    } catch (error) {
      console.error('Failed to save log retention:', error);
      addSnackMessage(`Failed to update log retention: ${(error as Error).message}`, {
        severity: 'error'
      });
    } finally {
      setSaving(false);
    }
  };
 
  return (
    <Box sx={{ padding: '20px', height: '100%' }}>
      <Typography variant="h4">Logging Configuration</Typography>
 
      <Box my={2} mx={1}>
        <Typography variant="body2" color="text.secondary" sx={{ mb: 2 }}>
          Configure how long log files are retained before being automatically deleted.
        </Typography>
 
        <Box sx={{ mb: 2 }}>
          <FormControl variant="outlined" sx={{ minWidth: 200 }}>
            <InputLabel>Log Retention</InputLabel>
            <Select
              value={logRetention}
              onChange={(e) => setLogRetention(e.target.value as number)}
              label="Log Retention"
              disabled={loading}
            >
              <MenuItem value={7}>7 days</MenuItem>
              <MenuItem value={30}>30 days</MenuItem>
              <MenuItem value={90}>90 days</MenuItem>
              <MenuItem value={365}>365 days (1 year)</MenuItem>
            </Select>
          </FormControl>
          <Typography variant="caption" color="text.secondary" sx={{ display: 'block', mt: 1 }}>
            Logs older than this period will be automatically deleted to save disk space.
          </Typography>
        </Box>
 
        <Button
          variant="contained"
          onClick={handleSave}
          disabled={loading || saving}
        >
          Save
        </Button>
 
        <Typography variant="caption" color="text.secondary" sx={{ display: 'block', mt: 2 }}>
          Note: This is an application-level setting that affects all logging operations.
        </Typography>
      </Box>
    </Box>
  );
}
 
export default PrefsLogging;