Skip to content

Commit

Permalink
feat: eval performances (#436)
Browse files Browse the repository at this point in the history
Co-authored-by: Vince Loewe <[email protected]>
  • Loading branch information
hughcrt and vincelwt authored Jul 23, 2024
1 parent 5dd36bc commit 1e8a3d9
Show file tree
Hide file tree
Showing 54 changed files with 1,159 additions and 1,026 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-push-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
service: [backend, radar, ml]
service: [backend, realtime-evaluators, ml]
steps:
- name: Check out the private Ops repo
uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion ops
Submodule ops updated from 0d1f13 to eb7857
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"scripts": {
"start": "npm run migrate:db && tsx src/index.ts",
"start:radar": "tsx src/radar.ts",
"start:realtime-evaluators": "tsx src/realtime-evaluators.ts",
"dev:realtime-evaluators": "tsx --env-file=.env --watch src/realtime-evaluators.ts",
"migrate:db": "tsx src/migrate.ts",
"build": "tsup src/index.ts --format esm",
Expand Down
6 changes: 2 additions & 4 deletions packages/backend/src/api/v1/evaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ evaluators.get("/:id", async (ctx: Context) => {
})

evaluators.post("/", async (ctx: Context) => {
console.log(ctx.request.body)
const requestBody = z.object({
ownerId: z.string().optional(),
name: z.string(),
Expand All @@ -53,7 +52,7 @@ evaluators.post("/", async (ctx: Context) => {
type: z.string(),
mode: z.string(),
params: z.record(z.any()),
filters: z.string(),
filters: z.array(z.any()),
})

const { projectId } = ctx.state
Expand All @@ -63,7 +62,6 @@ evaluators.post("/", async (ctx: Context) => {
const [insertedEvaluator] = await sql`
insert into evaluator ${sql({
...evaluator,
filters: deserializeLogic(evaluator.filters),
projectId,
})}
returning *
Expand All @@ -79,7 +77,7 @@ evaluators.patch("/:id", async (ctx: Context) => {
type: z.string(),
mode: z.string(),
params: z.record(z.any()),
filters: z.record(z.any()),
filters: z.array(z.any()),
})

const { projectId } = ctx.state
Expand Down
99 changes: 76 additions & 23 deletions packages/backend/src/api/v1/projects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,40 +164,93 @@ projects.patch(
async (ctx: Context) => {
const bodySchema = z.object({
name: z.string().optional(),
filters: z.array(z.any()).optional(),
});
const { name, filters } = bodySchema.parse(ctx.request.body);
const { projectId } = ctx.params;
const { userId } = ctx.state;
})
const { name } = bodySchema.parse(ctx.request.body)
const { projectId } = ctx.params
const { userId } = ctx.state

// TODO: this should be in a middleware
const hasProjectAccess = await checkProjectAccess(projectId, userId);
const hasProjectAccess = await checkProjectAccess(projectId, userId)
if (!hasProjectAccess) {
ctx.throw(401, "Unauthorized");
ctx.throw(401, "Unauthorized")
}


if (name) {
await sql`
update project
set name = ${name}
where id = ${projectId}
`;
update project set name = ${name} where id = ${projectId}
`
}

if (filters) {
await sql`
insert into ingestion_rule (project_id, type, filters)
values (${projectId}, 'filtering', ${filters})
on conflict (project_id, type)
do update set filters = excluded.filters
`;
ctx.status = 200
ctx.body = {}
},
)

projects.get(
"/:projectId/rules",
checkAccess("projects", "update"),
async (ctx: Context) => {
const { projectId } = ctx.params
const { userId } = ctx.state

const hasProjectAccess = await checkProjectAccess(projectId, userId)
if (!hasProjectAccess) {
ctx.throw(401, "Unauthorized")
}

ctx.status = 200;
ctx.body = {};
}
);
const rules =
await sql`select * from ingestion_rule where project_id = ${projectId}`

ctx.body = rules
},
)

projects.post(
"/:projectId/rules",
checkAccess("projects", "update"),
async (ctx: Context) => {
const bodySchema = z.object({
type: z.enum(["filtering", "masking"]).default("filtering"),
filters: z.array(z.any()).optional(),
})
const { type, filters } = bodySchema.parse(ctx.request.body)
const { projectId } = ctx.params
const { userId } = ctx.state

const hasProjectAccess = await checkProjectAccess(projectId, userId)
if (!hasProjectAccess) {
ctx.throw(401, "Unauthorized")
}

await sql`
insert into ingestion_rule (project_id, type, filters)
values (${projectId}, ${type}, ${filters})
on conflict (project_id, type)
do update set filters = excluded.filters
`

ctx.status = 200
ctx.body = {}
},
)

projects.delete(
"/:projectId/rules",
checkAccess("projects", "update"),
async (ctx: Context) => {
const { projectId } = ctx.params
const { userId } = ctx.state

const hasProjectAccess = await checkProjectAccess(projectId, userId)
if (!hasProjectAccess) {
ctx.throw(401, "Unauthorized")
}

await sql`delete from ingestion_rule where project_id = ${projectId}`

ctx.status = 200
ctx.body = {}
},
)

export default projects
Loading

0 comments on commit 1e8a3d9

Please sign in to comment.