forked from ubiquity/ubiquibot-logging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
139 lines (117 loc) · 4.19 KB
/
index.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
import { containsValidJson, createGitHubCommentURL, generateRandomId, getLevelString } from "./helpers/utils";
import { Logs } from "./types/log";
import { Database } from "../types/supabase";
import { createClient } from "@supabase/supabase-js";
import { SUPABASE_URL, SUPABASE_KEY } from "./constants/index";
const filterSelect = document.getElementById("filter") as unknown as HTMLSelectElement;
const clearButton = document.getElementById("clear") as HTMLButtonElement;
const logBody = document.getElementById("log-body") as HTMLDivElement;
const jsonModal = document.getElementById("json-modal") as HTMLDivElement;
const closeModalButton = document.getElementById("close-modal") as HTMLButtonElement;
const jsonContent = document.getElementById("json-content") as HTMLDivElement;
let isLoading = false;
const openJsonModal = (validJson: string) => {
jsonContent.textContent = validJson;
jsonModal.style.display = "flex";
};
const updateLogTable = () => {
const selectedFilter = filterSelect.value;
const filteredLogs = selectedFilter === "all" ? logs : logs.filter((log) => getLevelString(log.level) === selectedFilter);
logBody.innerHTML = "";
filteredLogs.forEach((log) => {
const classId = generateRandomId(10);
const commentUrl = createGitHubCommentURL(log.org_name, log.repo_name, log.issue_number, log.comment_id);
const row = document.createElement("tr");
const [validJson, match, beforeText] = containsValidJson(log.log_message);
row.innerHTML = `
${
validJson
? `<td class="log-cell">${beforeText} - <button id="button_${classId}">Show JSON</button></td>`
: `<td class="log-cell">${log.log_message}</td>`
}
<td>${getLevelString(log.level)}</td>
<td>${log.timestamp}</td>
<td><a href="${commentUrl}">Comment - ${log.comment_id}</a></td>
`;
logBody.appendChild(row);
const logCell = document.getElementsByClassName("log-cell");
if (validJson) {
// show modal button for valid json row
const showMoreButton = document.getElementById(`button_${classId}`) as HTMLButtonElement;
showMoreButton.addEventListener("click", () => {
if (validJson) {
openJsonModal(JSON.stringify(JSON.parse(match), null, 2)); // properly formatted json
}
});
}
});
};
let logs: Logs[] = [];
const supabaseClient = createClient<Database>(SUPABASE_URL, SUPABASE_KEY);
const fetchData = async () => {
isLoading = true;
if (logs.length > 0) {
const firstAvailableTimestamp = logs.at(logs.length-1)?.timestamp;
const { data, error } = await supabaseClient
.from("logs")
.select()
.lt("timestamp", firstAvailableTimestamp)
.order("timestamp", { ascending: false })
.limit(25);
if (data && data.length > 0) {
logs.push(...data);
updateLogTable();
} else console.log(error);
} else {
const { data, error } = await supabaseClient.from("logs").select().order("timestamp", { ascending: false }).limit(30);
if (data && data.length > 0) {
logs.push(...data);
updateLogTable();
} else console.log(error);
}
isLoading = false;
};
const handleScroll = async () => {
const bottom = document.documentElement.scrollHeight - document.documentElement.scrollTop === document.documentElement.clientHeight;
if (!bottom || isLoading) {
return;
}
await fetchData();
};
window.addEventListener("scroll", handleScroll);
supabaseClient
.channel("table-db-changes")
.on(
"postgres_changes",
{
event: "INSERT",
schema: "public",
table: "logs",
},
(payload) => handlePayload(payload)
)
.subscribe();
const handlePayload = (logEntry: any) => {
if (logEntry?.eventType !== "INSERT") return;
logs.unshift(logEntry.new);
updateLogTable();
};
filterSelect.addEventListener("change", () => {
updateLogTable();
});
clearButton.addEventListener("click", () => {
logs = [];
updateLogTable();
});
closeModalButton.addEventListener("click", () => {
jsonModal.style.display = "none";
});
window.addEventListener("click", (event) => {
if (event.target === jsonModal) {
jsonModal.style.display = "none";
}
isLoading = !isLoading;
});
// Initial update
fetchData();
updateLogTable();