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

Feature/cross insights #66

Merged
merged 3 commits into from
Feb 24, 2025
Merged
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
4 changes: 4 additions & 0 deletions internal/api/server/access/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
)

func IsSchemaForbiddenForUser(schemaUseCase *usecases.SchemaUseCase, schemaName string, r *http.Request) bool {
if schemaName == "" {
return false
}

user := session.GetUserFromContext(r)
if user == nil {
return false
Expand Down
2 changes: 1 addition & 1 deletion internal/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ info:
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
version: 1.18.7
version: 1.19.0
externalDocs:
description: Find out more about spec
url: https://moonlogs.pages.dev
Expand Down
3 changes: 2 additions & 1 deletion internal/storage/mongodb_adapter/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ func (s *RecordStorage) GetRecordsByQuery(ctx context.Context, record entities.R

if record.ID != 0 {
filter["schema_id"] = record.SchemaID
} else {
}
if record.SchemaName != "" {
filter["schema_name"] = record.SchemaName
}

Expand Down
4 changes: 3 additions & 1 deletion internal/storage/sqlite_adapter/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ func (s *RecordStorage) GetRecordsByQuery(ctx context.Context, record entities.R
if record.ID != 0 {
queryBuilder.WriteString("schema_id = ?")
queryParams = append(queryParams, record.SchemaID)
} else {
}

if record.SchemaName != "" {
queryBuilder.WriteString("schema_name = ?")
queryParams = append(queryParams, record.SchemaName)
}
Expand Down
1 change: 1 addition & 0 deletions web/src/app/chains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from "./profile";
export * from "./action";
export * from "./alerting-rule";
export * from "./notification-profile";
export * from "./insights";
11 changes: 11 additions & 0 deletions web/src/app/chains/insights.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { chainRoute } from "atomic-router";
import { chainAuthorized, insightsRoute } from "@/shared/routing";
import { schemaModel } from "@/entities/schema";

chainRoute({
route: chainAuthorized(insightsRoute),
beforeOpen: {
effect: schemaModel.effects.getSchemasFx,
mapParams: () => ({}),
},
});
2 changes: 2 additions & 0 deletions web/src/app/pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { combine } from "effector";
import { NotificationProfileCreatePage } from "@/pages/notification-profile-create";
import { NotificationProfleEditPage } from "@/pages/notification-profile-edit";
import { NotificationProfileListPage } from "@/pages/notification-profile-list";
import { InsightsPage } from "@/pages/insights";

export const Pages = () => {
const $layoutVisible = combine(
Expand Down Expand Up @@ -68,6 +69,7 @@ export const Pages = () => {
NotificationProfileListPage();
NotificationProfileCreatePage();
NotificationProfleEditPage();
InsightsPage();
},
layoutVisible: $layoutVisible,
});
Expand Down
1 change: 1 addition & 0 deletions web/src/features/insights-builder/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { InsightsBuilder } from "./ui";
144 changes: 144 additions & 0 deletions web/src/features/insights-builder/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import { schemaModel } from "@/entities/schema";
import { getLogs, Log } from "@/shared/api";
import { $intl } from "@/shared/lib";
import { attach, createEffect, createEvent, createStore, sample } from "effector";

type InsightFilter = {
fieldName: string;
fieldValue: string;
};

export const $filterList = createStore<InsightFilter[]>([{ fieldName: "", fieldValue: "" }]);

const buildInsight = createEvent();
const addFilter = createEvent();
const removeFilter = createEvent<number>();
const fieldNameChanged = createEvent<{ fieldName: string; idx: number }>();
const fieldValueChanged = createEvent<{ fieldValue: string; idx: number }>();

export const events = {
buildInsight,
addFilter,
removeFilter,
fieldNameChanged,
fieldValueChanged,
};

sample({
source: $filterList,
clock: addFilter,
fn: (filters) => {
return [...filters, { fieldName: "", fieldValue: "" }];
},
target: $filterList,
});

sample({
source: $filterList,
clock: removeFilter,
fn: (filters, idx) => [...filters.slice(0, idx), ...filters.slice(idx + 1)],
target: $filterList,
});

sample({
source: $filterList,
clock: fieldNameChanged,
fn: (filters, { fieldName, idx }) => {
return filters.map((filter, index) => (index === idx ? { ...filter, fieldName: fieldName } : filter));
},
target: $filterList,
});

sample({
source: $filterList,
clock: fieldValueChanged,
fn: (filters, { fieldValue, idx }) => {
return filters.map((filter, index) => (index === idx ? { ...filter, fieldValue: fieldValue } : filter));
},
target: $filterList,
});

export const $fieldsOptions = schemaModel.$schemas.map((schemas) => {
const allFields = schemas.flatMap((schema) => {
const fields = schema.fields || [];
return fields.map((f) => ({ title: f.title, name: f.name, id: f.name }));
});

return Array.from(new Map(allFields.map((f) => [f.id, f])).values());
});

const getLogsFx = attach({
source: $filterList,
effect: createEffect((filterList: InsightFilter[]) => {
const query = filterList.reduce(
(acc, filter) => {
acc[filter.fieldName] = filter.fieldValue;
return acc;
},
{} as Record<string, string>,
);

return getLogs({ schema_name: "", query, limit: 1000 });
}),
});

sample({
clock: buildInsight,
target: getLogsFx,
});

export const $insightLogs = createStore<Log[]>([]);

sample({
source: $intl,
clock: getLogsFx.doneData,
fn: (intl, logsResponse) => {
const logs = logsResponse.data.reverse();
const formattedLogs = logs.map((log) => {
return { ...log, created_at: intl.format(new Date(log.created_at)) };
});

return formattedLogs;
},
target: $insightLogs,
});

export type InsightSchema = {
schemaTitle: string;
schemaName: string;
schemaId: number;
schemaColor: string;
};

export const $insightsSchemas = createStore<InsightSchema[]>([]);

const COLORS: Record<number, string> = {
0: "green-500",
1: "blue-500",
2: "gray-500",
3: "yellow-500",
4: "red-500",
5: "pink-500",
6: "purple-500",
7: "lime-500",
8: "cyan-500",
9: "orange-500",
};

sample({
source: { logs: $insightLogs, schemas: schemaModel.$schemas },
fn: ({ logs, schemas }) => {
const uniqueSchemaIDs = new Set(logs.map((l) => l.schema_id));
const relevantSchemas = schemas.filter((s) => uniqueSchemaIDs.has(s.id));

return relevantSchemas.map((s) => {
return {
schemaTitle: s.title,
schemaName: s.name,
schemaId: s.id,
schemaColor: COLORS[Number(s.id.toString()[0])],
};
});
},
target: $insightsSchemas,
});
Loading