Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eventing settings #232

Open
wants to merge 6 commits into
base: v0.22.0-old
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 153 additions & 29 deletions src/components/eventing/EventingConfigure.jsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,166 @@
import React from 'react'
import { AutoComplete, Button, Form, Checkbox } from 'antd';
import { AutoComplete, Button, Form, Checkbox, Select, Input, DatePicker } from 'antd';
import ConditionalFormBlock from "../conditional-form-block/ConditionalFormBlock";
import FormItemLabel from "../form-item-label/FormItemLabel";
import { useSelector } from 'react-redux';
import { getTrackedCollections } from '../../operations/database';
import { MinusCircleFilled, PlusOutlined } from '@ant-design/icons';

const { Option } = AutoComplete;

const EventingConfigure = ({ initialValues, handleSubmit, dbList, loading }) => {
const EventingConfigure = ({ initialValues, handleSubmit, dbList, loading, onDeleteEventingLogs }) => {
const [form] = Form.useForm()
const state = useSelector(state => state)

const [form] = Form.useForm()
const handleSubmitClick = e => {
form.validateFields().then(values => {
handleSubmit(values)
})
}
const handleTruncateEventLogs = () => {
form.validateFields(["truncateSince"]).then(values => {
values.truncateSince = values.truncateSince.unix()

if (!loading) {
form.setFieldsValue(initialValues)
}
onDeleteEventingLogs(values.truncateSince)
})
}

return (
<Form form={form} >
<Form.Item name="enabled" valuePropName="checked">
<Checkbox>
Enable eventing module
const getCollections = (db) => getTrackedCollections(state, db)

const handleSubmitClick = e => {
form.validateFields(["enabled", "dbAlias", "dbTables"]).then(values => {
values = Object.assign({}, initialValues, values)

if (values.enabled) {
values.dbTablesInclusionMap = {}
if (values.dbTables.length === 0) {
values.dbTablesInclusionMap["*"] = "*";
}
else {
values.dbTables.forEach(item => {
if (item) {
values.dbTablesInclusionMap[item.db] = item.col && item.col.length > 0 ? item.col : "*"
}
})
}
}

delete values["dbTables"]
handleSubmit(values)
})
}

if (!loading) {
const dbTables = [];
if (initialValues.enabled && (!initialValues.dbTablesInclusionMap || Object.keys(initialValues.dbTablesInclusionMap).length === 0)) {
dbTables.push({ db: "*", col: ["*"] })
}
if (initialValues.enabled && initialValues.dbTablesInclusionMap) {
Object.keys(initialValues.dbTablesInclusionMap).forEach(db => {
dbTables.push({ db, col: initialValues.dbTablesInclusionMap[db] })
})
}
form.setFieldsValue({ ...initialValues, dbTables })
}

const isAddDatabaseDisabled = (fields) => {
if (Object.keys(fields).length === 0) {
return false;
}
return fields.dbTables.find(x => x.db === "*");
}

return (
<Form form={form}>
<Form.Item name="enabled" valuePropName="checked">
<Checkbox>
Enable eventing module
</Checkbox>
</Form.Item>
<ConditionalFormBlock dependency="enabled" condition={() => form.getFieldValue("enabled")}>
<FormItemLabel name="Eventing DB" description="The database to store event invocation logs" />
<Form.Item name="dbAlias" rules={[{ required: true, message: 'Database is required!' }]}>
<AutoComplete placeholder="Choose an eventing database" style={{ width: 320 }} options={dbList.map(db => ({ value: db }))} />
</Form.Item>
</ConditionalFormBlock>
<Form.Item>
<Button onClick={handleSubmitClick} >
Save
</Form.Item>
<ConditionalFormBlock dependency="enabled" condition={() => form.getFieldValue("enabled")}>
<FormItemLabel name="Eventing DB" description="The database to store event invocation logs" />
<Form.Item name="dbAlias" rules={[{ required: true, message: 'Database is required!' }]}>
<AutoComplete placeholder="Choose an eventing database" style={{ width: 320 }} options={dbList.map(db => ({ value: db }))} />
</Form.Item>
<FormItemLabel name="Enable eventing" description="Select the database & table on which the eventing has to be enabled" />
<Form.List name="dbTables">
{(fields, { add, remove }) => {
return (
<div>
{fields.map((field) => (
<Input.Group compact key={field.key}>
<Form.Item
name={[field.name, 'db']}
key={[field.name, 'db']}
style={{ flexGrow: 1, width: 200, marginRight: 10 }}
rules={[{ required: true, message: "Please enter a database!" }]}
>
<AutoComplete placeholder="Select databases">
{
dbList.map(db => (<AutoComplete.Option key={db} value={db}>{db}</AutoComplete.Option>))
}
</AutoComplete>
</Form.Item>
<Form.Item noStyle shouldUpdate>
{() => (
<Form.Item
name={[field.name, 'col']}
key={[field.name, 'col']}
style={{ flexGrow: 1, width: 200, marginRight: 10 }}
rules={[{ required: true, message: "Please enter atleast 1 collection!" }]}
>
<Select
mode="tags"
placeholder="Collections / Tables"
filterOption={(input, option) => option.value.toLowerCase().indexOf(input.toLowerCase()) >= 0}
>
{
getCollections(form.getFieldValue(["dbTables", field.name, 'db'])).map(data => (
<Option key={data} value={data}>
{data}
</Option>
))
}
</Select>
</Form.Item>
)}
</Form.Item>
<Form.Item>
<MinusCircleFilled onClick={() => remove(field.name)} style={{ fontSize: 20, cursor: "pointer" }} />
</Form.Item>
</Input.Group>
))}
<Form.Item shouldUpdate noStyle>
{() => (
<Button
style={{ marginBottom: "1em" }}
onClick={() => {
form.validateFields([
...fields.map(obj => ["dbTables", obj.name, "db"]),
...fields.map(obj => ["dbTables", obj.name, "col"])])
.then(() => add())
.catch(ex => console.log("Exception", ex))
}}
disabled={isAddDatabaseDisabled(form.getFieldsValue([...fields.map(obj => ["dbTables", obj.name, "db"])]))}
>
<PlusOutlined /> Add database
</Button>
)}
</Form.Item>
</div>
)
}}
</Form.List>
</ConditionalFormBlock>
<Form.Item>
<Button onClick={handleSubmitClick} >
Save
</Button>
</Form.Item>
</Form>
)
</Form.Item>
<ConditionalFormBlock dependency="enabled" condition={() => form.getFieldValue("enabled")}>
<FormItemLabel name="Truncate event logs" description="Delete the event logs from a specific timestamp to clear/save database storage" />
<Form.Item name="truncateSince" rules={[{ required: true, message: "No date selected!" }]}>
<DatePicker showTime format="YYYY-MM-DD HH:mm:ss" />
</Form.Item>
<Button type="primary" danger onClick={handleTruncateEventLogs}>Delete</Button>
</ConditionalFormBlock>
</Form>
)
}

export default EventingConfigure;
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,12 @@ const ConfigureRule = (props) => {
<Select placeholder="Rule">
{rules
.filter((val) => {
if (props.blockDepth === 1) return true
if (props.ruleMetaData.ruleType === securityRuleGroups.EVENTING_FILTERS)
return !["allow", "deny", "authenticated"].includes(val)

if (props.blockDepth === 1)
return true

return !["allow", "deny", "authenticated"].includes(val)
})
.map((val) => (
Expand Down
2 changes: 1 addition & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ export const defaultEventRule = {
}

export const defaultEventFilterRule = {
rule: "allow"
rule: "match"
}

export const defaultEndpointRule = {
Expand Down
13 changes: 13 additions & 0 deletions src/pages/eventing/EventingSettings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import './event.css';
import { saveEventingConfig, getEventingConfig } from '../../operations/eventing';
import { getDbConfigs } from '../../operations/database';
import { projectModules, actionQueuedMessage } from '../../constants';
import client from "../../client";
import { getAPIToken } from '../../operations/projects';

const EventingSettings = () => {
const { projectID } = useParams();
Expand All @@ -18,6 +20,7 @@ const EventingSettings = () => {
const loading = useSelector(state => state.pendingRequests > 0)
const eventingConfig = useSelector(state => getEventingConfig(state))
const dbConfigs = useSelector(state => getDbConfigs(state));
const internalToken = useSelector(state => getAPIToken(state))

// Derived state
const dbList = Object.keys(dbConfigs)
Expand All @@ -30,6 +33,15 @@ const EventingSettings = () => {
.finally(() => decrementPendingRequests());
};

const handleDeleteEventingLogs = (timestamp) => {
const dbAlias = dbConfigs.dbAlias
incrementPendingRequests()
client.eventing.deleteEventLogs(projectID, dbAlias, timestamp, () => internalToken)
.then(() => notify("success", "Logs truncated successfully"))
.catch(ex => notify("error", "Error truncating event logs", ex))
.finally(() => decrementPendingRequests())
}

return (
<div>
<Topbar showProjectSelector />
Expand All @@ -44,6 +56,7 @@ const EventingSettings = () => {
dbList={dbList}
loading={loading}
handleSubmit={handleEventingConfig}
onDeleteEventingLogs={handleDeleteEventingLogs}
/>
</div>
</div>
Expand Down
40 changes: 40 additions & 0 deletions src/services/eventing.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,46 @@ class Eventing {
.catch(ex => reject({title: "Failed to delete eventing-schema", msg: ex.message}))
})
}

deleteEventLogs(projectId, dbAlias, timestamp, getToken) {
let uri = `/v1/api/${projectId}/graphql`
if (spaceCloudClusterOrigin) {
uri = spaceCloudClusterOrigin + uri;
}
const graphqlClient = createGraphQLClient(uri, getToken)
return new Promise((resolve, reject) => {
graphqlClient.mutate({
mutation: gql`
mutation {
delete_invocation_logs(
where: {
invocation_time: {
_lte: $timestamp
}
}
) @${dbAlias}

delete_event_logs(
where: {
event_ts: {
_lte: $timestamp
}
}
) @${dbAlias}
}
`,
variables: { timestamp }
}).then(res => {
const { data, errors } = res
if (errors && errors.length > 0) {
reject(errors[0].message)
return
}

resolve()
}).catch(ex => reject(ex.toString()))
})
}
}

export default Eventing