Skip to content

Commit

Permalink
Merge pull request #51 from electrolux-oss/50/read-only-mode
Browse files Browse the repository at this point in the history
#50 feat: readOnly mode for metric settings
  • Loading branch information
gluckzhang authored Aug 16, 2024
2 parents a912a63 + ea9b089 commit cfe4745
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "root",
"version": "0.1.7",
"version": "0.1.8",
"private": true,
"engines": {
"node": "18 || 20"
Expand Down
2 changes: 1 addition & 1 deletion plugins/infrawallet-backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@electrolux-oss/plugin-infrawallet-backend",
"version": "0.1.7",
"version": "0.1.8",
"backstage": {
"role": "backend-plugin"
},
Expand Down
14 changes: 14 additions & 0 deletions plugins/infrawallet-backend/src/service/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,25 @@ export async function createRouter(options: RouterOptions): Promise<express.Rout
});

router.put('/:walletName/metrics_setting', async (request, response) => {
const readOnly = config.getOptionalBoolean('infraWallet.settings.readOnly') ?? false;

if (readOnly) {
response.status(403).json({ error: 'API not enabled in read-only mode', status: 403 });
return;
}

const updatedMetricSetting = await updateOrInsertWalletMetricSetting(database, request.body as MetricSetting);
response.json({ updated: updatedMetricSetting, status: 200 });
});

router.delete('/:walletName/metrics_setting', async (request, response) => {
const readOnly = config.getOptionalBoolean('infraWallet.settings.readOnly') ?? false;

if (readOnly) {
response.status(403).json({ error: 'API not enabled in read-only mode', status: 403 });
return;
}

const deletedMetricSetting = await deleteWalletMetricSetting(database, request.body as MetricSetting);
response.json({ deleted: deletedMetricSetting, status: 200 });
});
Expand Down
1 change: 1 addition & 0 deletions plugins/infrawallet/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Config {
settings: {
defaultGroupBy?: string; // if not set, `none` will be used
defaultShowLastXMonths?: number; // if not set, 3 will be used
readOnly?: boolean; // false by default
};
};
}
2 changes: 1 addition & 1 deletion plugins/infrawallet/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@electrolux-oss/plugin-infrawallet",
"version": "0.1.7",
"version": "0.1.8",
"backstage": {
"role": "frontend-plugin"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { alertApiRef, useApi } from '@backstage/core-plugin-api';
import { alertApiRef, configApiRef, useApi } from '@backstage/core-plugin-api';
import AddIcon from '@material-ui/icons/Add';
import CancelIcon from '@material-ui/icons/Close';
import DeleteIcon from '@material-ui/icons/DeleteOutlined';
Expand Down Expand Up @@ -28,12 +28,15 @@ import {
} from '@mui/x-data-grid';

export const MetricConfigurationComponent: FC<{ wallet?: Wallet }> = ({ wallet }) => {
const configApi = useApi(configApiRef);
const alertApi = useApi(alertApiRef);
const infraWalletApi = useApi(infraWalletApiRef);
const [rows, setRows] = useState<GridRowsProp>([]);
const [metricConfigs, setMetricConfigs] = useState<MetricConfig[]>();
const [rowModesModel, setRowModesModel] = useState<GridRowModesModel>({});

const readOnly = configApi.getOptionalBoolean('infraWallet.settings.readOnly') ?? false;

function EditToolbar() {
const handleClick = () => {
const id = uuidv4();
Expand Down Expand Up @@ -136,7 +139,7 @@ export const MetricConfigurationComponent: FC<{ wallet?: Wallet }> = ({ wallet }
field: 'metric_provider',
headerName: 'Provider',
width: 220,
editable: true,
editable: !readOnly,
type: 'singleSelect',
valueOptions: () => {
const options: ValueOptions[] = [];
Expand All @@ -155,7 +158,7 @@ export const MetricConfigurationComponent: FC<{ wallet?: Wallet }> = ({ wallet }
field: 'config_name',
headerName: 'ConfigName',
width: 180,
editable: true,
editable: !readOnly,
type: 'singleSelect',
valueOptions: params => {
const options: ValueOptions[] = [];
Expand All @@ -173,21 +176,24 @@ export const MetricConfigurationComponent: FC<{ wallet?: Wallet }> = ({ wallet }
field: 'metric_name',
headerName: 'MetricName',
width: 220,
editable: true,
editable: !readOnly,
},
{
field: 'description',
headerName: 'Description',
width: 220,
editable: true,
editable: !readOnly,
},
{
field: 'query',
headerName: 'Query',
flex: 1,
editable: true,
editable: !readOnly,
},
{
];

if (!readOnly) {
columns.push({
field: 'actions',
type: 'actions',
headerName: 'Actions',
Expand Down Expand Up @@ -227,8 +233,8 @@ export const MetricConfigurationComponent: FC<{ wallet?: Wallet }> = ({ wallet }
<GridActionsCellItem icon={<DeleteIcon />} label="Delete" onClick={handleDeleteClick(row)} color="inherit" />,
];
},
},
];
});
}

const getWalletMetricSettings = useCallback(async () => {
if (wallet) {
Expand Down Expand Up @@ -281,7 +287,7 @@ export const MetricConfigurationComponent: FC<{ wallet?: Wallet }> = ({ wallet }
onRowEditStop={handleRowEditStop}
processRowUpdate={processRowUpdate}
slots={{
toolbar: EditToolbar as GridSlots['toolbar'],
toolbar: readOnly ? null : (EditToolbar as GridSlots['toolbar']),
}}
/>
</Box>
Expand Down

0 comments on commit cfe4745

Please sign in to comment.