-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrons.js
211 lines (174 loc) · 5.73 KB
/
crons.js
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
203
204
205
206
207
208
209
210
211
const float = require("./classes/apis/Float");
const blocksKit = require("./classes/utilities/BlockKitBuilder");
const cron = require("node-cron");
const confluence = require("./classes/apis/Confluence");
const pinecone = require("./classes/apis/Pinecone");
const openai = require("./classes/apis/OpenAI");
/**
* This cron job sends each member that has a Float schedule, a message
* at 16:00 on weekdays to check if they have reported their hours to the
* project manager.
*/
const collectFeedback = async () => {
const result = await app.client.users.list({
token: process.env.SLACK_BOT_TOKEN,
});
if (!result.ok || !result.members) {
throw new Error("Failed to fetch Slack members.");
}
const slackMembers = result.members;
const floatAccounts = await float.getPeople();
for (const member of slackMembers) {
const { id } = member;
const email = member.profile.email;
if (process.env.NODE_ENV === "development") {
if (email !== process.env.SLACK_EMAIL_DEVELOPER) continue;
}
const floatAccount =
process.env.NODE_ENV === "development"
? floatAccounts.find(
(acc) => acc.email === process.env.FLOAT_EMAIL_DEVELOPER
)
: floatAccounts.find((acc) => acc.email === email);
if (!floatAccount) {
continue;
}
const todaysTasks = await float.fetchTasksByPersonId(
floatAccount.people_id
);
if (!todaysTasks) {
continue;
}
const blocks = [];
blocks.push(
blocksKit.addSection({
text: `:wave: Ahoy <@${id}>, laten we je planning voor vandaag doornemen. Hou het kort; focus op bijzonderheden.`,
}),
blocksKit.addContext({
text: "Deze terugkoppeling vervangt *niet* de terugkoppeling in Trello en HubSpot.",
})
);
blocks.push(blocksKit.addWhitespace());
blocks.push(blocksKit.addDivider());
for (const task of todaysTasks) {
const { task_id: taskId, project_id: projectId, hours } = task;
const project = await float.getProjectById(projectId);
const { name: projectName, project_manager: projectManagerId } = project;
const floatTask = await float.getTaskById(taskId);
const { name: taskName } = floatTask;
const projectManager = await float.getAccountById(projectManagerId);
const { name: projectManagerName } = projectManager;
blocks.push(
blocksKit.addSection({
text: `Je stond vandaag ${hours} uur ingepland voor *${projectName}*.`,
})
);
if (taskName) {
blocks.push(
blocksKit.addContext({
text: `*Taak*: ${taskName}`,
})
);
}
blocks.push(
blocksKit.addDispatchInput({
blockId: `feedback_${projectId}`,
actionId: `feedback_${projectId}`,
label: `Terugkoppeling naar ${projectManagerName}`,
placeholder: "Hoe is het gegaan?",
})
);
blocks.push(blocksKit.addWhitespace());
blocks.push(blocksKit.addDivider());
}
await app.client.chat.postMessage({
token: process.env.SLACK_BOT_TOKEN,
channel: id,
text: "Dagelijkse terugkoppeling voor " + new Date().toDateString(),
blocks,
});
}
};
/**
* This cron job syncs our Confluence data to Pinecone for better
* Compass responses, every weekday at 00:00.
*/
const syncConfluenceToPinecone = async () => {
let pages = [];
let spaces = await confluence.getSpaces();
const spacesToImport = ["KB", "D2"];
// const spacesToImport = ["DEV", "KB", "D2"];
const index = await pinecone.getIndex("kompas-index");
if (!spaces.results) {
return;
}
for (const key of spacesToImport) {
// spaces are objects with the key property, find the object with the key dev
const devSpace = spaces.results.find((space) => space.key === key);
if (!devSpace) {
continue;
}
const devSpacePages = await confluence.getPagesBySpace({
spaceId: devSpace.id,
status: "current",
depth: "all",
limit: 10,
});
let pagesProcessed = 0;
for (let page of devSpacePages) {
const pageContent = await confluence.getPage(page.id);
if (/standup|stand-up|breakout|stand up/i.test(pageContent.title)) {
continue;
}
// Remove entire <ac:*> tags along with their content
let content = pageContent.body.storage.value.replace(
/<ac:[^>]*>.*?<\/ac:[^>]*>/gs,
""
);
// Remove standard HTML tags
content = content.replace(/<(?!\/?(p|h[1-6]|ul|ol|li)\b)[^>]+>/g, "");
// Trim any excess whitespace
content = content.trim();
const embedding = await openai.createEmbedding({ text: content });
if (embedding) {
const data = {
id: pageContent.id,
values: embedding,
metadata: {
title: pageContent.title,
content,
},
};
pages.push(data);
} else {
console.log(`Page ${pageContent.title} has no data to embed`);
continue;
}
console.log(`Page ${pageContent.title} processed.`);
if (50 === pagesProcessed) {
console.log(`Inserting ${pagesProcessed} pages into Pinecone...`);
await index.upsert(pages);
pagesProcessed = 0;
pages = [];
}
pagesProcessed++;
}
console.log(`Inserting ${pagesProcessed} pages into Pinecone...`);
await index.upsert(pages);
}
console.log("All pages inserted into Pinecone");
};
function registerCrons(app) {
if (process.env.NODE_ENV === "development") {
// collectFeedback();
} else if (process.env.NODE_ENV === "production") {
cron.schedule("0 16 * * 1-5", collectFeedback);
}
}
module.exports = {
registerCrons,
cronJobs: {
collectFeedback,
syncConfluenceToPinecone,
},
};