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

feat: add last updated timestamp to mapping ui #1115

Merged
1 change: 1 addition & 0 deletions keep-ui/app/mapping/mapping.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { MdWarning } from "react-icons/md";
import Loading from "app/loading";
import {MappingRule} from "./models";
import {useState} from "react";

export default function Mapping() {
const { data: mappings, isLoading } = useMappings();

Expand Down
2 changes: 2 additions & 0 deletions keep-ui/app/mapping/models.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export interface MappingRule {
file_name?: string;
created_by?: string;
created_at: Date;
updated_by?: string;
last_updated_at: Date;
disabled: boolean;
override: boolean;
condition?: string;
Expand Down
45 changes: 44 additions & 1 deletion keep-ui/app/mapping/rules-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import {
createColumnHelper,
flexRender,
getCoreRowModel,
useReactTable,
useReactTable, ExpandedState,
} from "@tanstack/react-table";
import { MdRemoveCircle, MdModeEdit } from "react-icons/md";
import { useSession } from "next-auth/react";
import { getApiURL } from "utils/apiUrl";
import { useMappings } from "utils/hooks/useMappingRules";
import { toast } from "react-toastify";
import {useState} from "react";

const columnHelper = createColumnHelper<MappingRule>();

Expand All @@ -32,6 +33,7 @@ interface Props {
export default function RulesTable({ mappings, editCallback }: Props) {
const { data: session } = useSession();
const { mutate } = useMappings();
const [expanded, setExpanded] = useState<ExpandedState>({});

const columns = [
columnHelper.display({
Expand Down Expand Up @@ -100,7 +102,9 @@ export default function RulesTable({ mappings, editCallback }: Props) {
const table = useReactTable({
columns,
data: mappings.sort((a, b) => b.priority - a.priority),
state: { expanded },
getCoreRowModel: getCoreRowModel(),
onExpandedChange: setExpanded,
});

const deleteRule = (ruleId: number) => {
Expand Down Expand Up @@ -148,16 +152,55 @@ export default function RulesTable({ mappings, editCallback }: Props) {
</TableHead>
<TableBody>
{table.getRowModel().rows.map((row) => (
<>
<TableRow
className="even:bg-tremor-background-muted even:dark:bg-dark-tremor-background-muted hover:bg-slate-100"
key={row.id}
onClick={() => row.toggleExpanded()}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(cell.column.columnDef.cell, cell.getContext())}
</TableCell>
))}
</TableRow>
{row.getIsExpanded() && (
<TableRow className="pl-2.5">
<TableCell colSpan={columns.length}>
<div className="flex space-x-2 divide-x">
<div className="flex items-center space-x-2">
<span className="font-bold">Created At:</span>
<span>
{new Date(
row.original.created_at + "Z"
).toLocaleString()}
</span>
</div>
<div className="flex items-center space-x-2 pl-2.5">
<span className="font-bold">Created By:</span>
<span>{row.original.created_by}</span>
</div>
{row.original.last_updated_at && (
<>
<div className="flex items-center space-x-2 pl-2.5">
<span className="font-bold">Updated At:</span>
<span>
{new Date(
row.original.last_updated_at + "Z"
).toLocaleString()}
</span>
</div>
<div className="flex items-center space-x-2 pl-2.5">
<span className="font-bold">Updated By:</span>
<span>{row.original.updated_by}</span>
</div>
</>
)}
</div>
</TableCell>
</TableRow>
)}
</>
))}
</TableBody>
</Table>
Expand Down
20 changes: 20 additions & 0 deletions keep/api/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,26 @@ def create_db_and_tables():
logger.exception("Failed to migrate rule table")
pass
logger.info("Migrated Rule table")
# add updated_by and last_updated_at to the mapping rule table
logger.info("Migrating MappingRule table")
try:
if session.bind.dialect.name == "postgresql":
session.exec("ALTER TABLE mappingrule ADD COLUMN updated_by VARCHAR(255);")
session.exec("ALTER TABLE mappingrule ADD COLUMN last_updated_at TIMESTAMP;")
elif session.bind.dialect.name == "mssql":
session.exec("ALTER TABLE mappingrule ADD updated_by NVARCHAR(255);")
session.exec("ALTER TABLE mappingrule ADD last_updated_at DATETIME;")
else:
session.exec("ALTER TABLE mappingrule ADD COLUMN updated_by VARCHAR(255);")
session.exec("ALTER TABLE mappingrule ADD COLUMN last_updated_at DATETIME;")
except Exception as e:
# that's ok
if "Duplicate column name" in str(e):
pass
# else, log
else:
logger.exception("Failed to migrate mapping rule table")
pass
session.commit()
logger.info("Migrated succesfully")
except Exception:
Expand Down
4 changes: 4 additions & 0 deletions keep/api/models/db/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class MappingRule(SQLModel, table=True):
sa_column=Column(JSON),
nullable=False,
) # max_length=204800)
updated_by: Optional[str] = Field(max_length=255, default=None)
last_updated_at: datetime = Field(default_factory=datetime.utcnow)


class MappRuleDtoBase(BaseModel):
Expand All @@ -40,6 +42,8 @@ class MappingRuleDtoOut(MappRuleDtoBase, extra="ignore"):
created_by: Optional[str]
created_at: datetime
attributes: list[str] = []
updated_by: Optional[str] | None
last_updated_at: Optional[datetime] | None


class MappingRuleDtoIn(MappRuleDtoBase):
Expand Down
3 changes: 3 additions & 0 deletions keep/api/routes/mapping.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import logging

from fastapi import APIRouter, Depends, HTTPException
Expand Down Expand Up @@ -108,6 +109,8 @@ def update_rule(
existing_rule.matchers = rule.matchers
existing_rule.file_name = rule.file_name
existing_rule.priority = rule.priority
existing_rule.updated_by = authenticated_entity.email
existing_rule.last_updated_at = datetime.datetime.now(tz=datetime.timezone.utc)
if rule.rows is not None:
existing_rule.rows = rule.rows
session.commit()
Expand Down
Loading