-
Notifications
You must be signed in to change notification settings - Fork 108
/
Copy pathutils.ts
202 lines (188 loc) · 5.91 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { differenceInHours } from "date-fns";
import IndexedDBHelper from "@modules/app/indexedDb";
import { panelLogger } from "@modules/logger";
import {
DBTDocumentation,
DBTDocumentationColumn,
DbtGenericTests,
DocumentationStateProps,
Source,
TestMetadataAcceptedValuesKwArgs,
TestMetadataRelationshipsKwArgs,
} from "./state/types";
import { GenerationDBDataProps } from "./types";
import { DataPilotChatAction } from "../dataPilot/types";
export const addDefaultActions = (
data: Record<string, unknown>,
command: string,
): DataPilotChatAction[] => {
return [
{
title: "Regenerate",
data,
command,
userPrompt: "Regenerate documentation for {type} {name}",
datapilotTitle: "Improving documentation based on the user suggestion",
},
{
title: "Make it shorter",
data,
command,
userPrompt: "Make documentation shorter for {type} {name}",
datapilotTitle: "Improving documentation based on the user suggestion",
},
{
title: "Make it longer",
data,
command,
userPrompt: "Make documentation longer for {type} {name}",
datapilotTitle: "Improving documentation based on the user suggestion",
},
{
title: "Make it fun",
data,
command,
userPrompt: "Make documentation fun for {type} {name}",
datapilotTitle: "Improving documentation based on the user suggestion",
},
{
title: "Generate for business user",
data,
command,
userPrompt: "Regenerate documentation for {type} {name} as business user",
datapilotTitle: "Improving documentation based on the user suggestion",
},
];
};
export const addDocGeneration = async (
project: string,
model: string,
data: Partial<DBTDocumentationColumn>,
): Promise<void> => {
const db = await IndexedDBHelper.getDb();
const transaction = db.transaction(["generations"], "readwrite");
const generationstore = transaction.objectStore("generations");
const operation = {
project,
model,
data,
timestamp: new Date().getTime(),
} as GenerationDBDataProps;
await generationstore.add(operation);
};
export const getGenerationsInModel = async (
project: string,
model: string,
): Promise<GenerationDBDataProps[]> => {
const db = await IndexedDBHelper.getDb();
const transaction = db.transaction(["generations"], "readwrite");
const generationstore = transaction.objectStore("generations");
const projectIndex = generationstore.index("projectIndex");
const range = IDBKeyRange.only(project);
const generations: GenerationDBDataProps[] = [];
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {
const cursor = await projectIndex.openCursor(range);
function iterateCursor() {
if (cursor) {
if (cursor.value.model === model) {
const generation = cursor.value;
if (
differenceInHours(new Date(), new Date(generation.timestamp)) < 24
) {
generations.push(generation);
}
}
cursor
.continue()
.then(iterateCursor)
.catch((err) =>
panelLogger.error("error while iterating cursor", err),
);
} else {
resolve(generations);
}
}
iterateCursor();
transaction.oncomplete = () => {
resolve(generations);
};
transaction.onerror = () => {
reject("Error retrieving operations");
};
});
};
export const mergeCurrentAndIncomingDocumentationColumns = (
current: DBTDocumentation["columns"] | undefined,
incoming: DBTDocumentation["columns"],
): DBTDocumentation["columns"] => {
return incoming.map((column) => {
const existingColumn = current?.find((c) => column.name === c.name);
return {
name: column.name ?? "",
type: column.type,
description: existingColumn?.description ?? "",
generated: existingColumn?.generated ?? false,
source: existingColumn !== undefined ? Source.YAML : Source.DATABASE,
};
});
};
export const isStateDirty = (state: DocumentationStateProps): boolean => {
if (!state.currentDocsData && !state.currentDocsTests) return false;
if (!state.incomingDocsData) return false;
if (!state.incomingDocsData.docs && !state.incomingDocsData.tests)
return false;
if (
state.currentDocsData?.description !==
state.incomingDocsData.docs?.description
) {
return true;
}
for (const column of state.currentDocsData?.columns ?? []) {
const incomingColumn = state.incomingDocsData.docs?.columns?.find(
(c) => c.name === column.name,
);
if (column.description !== incomingColumn?.description) {
return true;
}
}
if (state.currentDocsTests?.length !== state.incomingDocsData.tests?.length) {
return true;
}
for (const test of state.currentDocsTests ?? []) {
const incomingTest = state.incomingDocsData.tests?.find(
(t) => t.key === test.key,
);
if (!incomingTest) {
return true;
}
if (test.test_metadata?.name === DbtGenericTests.ACCEPTED_VALUES) {
if (
(
test.test_metadata?.kwargs as TestMetadataAcceptedValuesKwArgs
).values?.join(",") !==
(
incomingTest.test_metadata?.kwargs as TestMetadataAcceptedValuesKwArgs
).values?.join(",")
) {
return true;
}
}
if (test.test_metadata?.name === DbtGenericTests.RELATIONSHIPS) {
const currentTestsArgs = test.test_metadata
?.kwargs as TestMetadataRelationshipsKwArgs;
const incomingTestsArgs = incomingTest.test_metadata
?.kwargs as TestMetadataRelationshipsKwArgs;
if (
currentTestsArgs.to !== incomingTestsArgs.to ||
currentTestsArgs.field !== incomingTestsArgs.field
) {
return true;
}
}
}
return false;
};
export const isArrayEqual = (a: string[], b: string[]): boolean => {
return a.length === b.length && a.every((v, i) => v === b[i]);
};