forked from kuutsav/leetcode-compensation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
312 lines (278 loc) · 10.7 KB
/
script.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Min Data points for box plot
const minDataPoints = 3;
// Set of roles to display in the box plot
const validRoles = new Set([
'SDE I',
'SDE II',
'SDE III',
"Staff SDE",
"Data Scientist",
"Data Engineer",
"Associate Software Engineer",
"Analyst",
]);
const offersPerPage = 10;
// Utility function to capitalize the first letter of a string
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
function statsStr(data) {
const nRecs = data.length;
startDate = data[0].creation_date;
endDate = data[nRecs - 1].creation_date;
return `
Based on ${nRecs} recs parsed between ${startDate} and ${endDate}
(only includes posts that were parsed successfully and had non negative votes)
`;
}
function formatSalaryInINR(lpa) {
// Convert LPA to total rupees
const totalRupees = Math.ceil(lpa * 100000);
let rupeesStr = totalRupees.toString();
let lastThree = rupeesStr.substring(rupeesStr.length - 3);
const otherNumbers = rupeesStr.substring(0, rupeesStr.length - 3);
if (otherNumbers != '') {
lastThree = ',' + lastThree;
}
let formattedSalary = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
return `₹${formattedSalary}`;
}
function extractValues(data, key) {
return data.map(item => item[key]);
}
function calculateFrequencies(values) {
return values.reduce((acc, value) => {
const bin = Math.floor(value / 10);
acc[bin] = (acc[bin] || 0) + 1;
return acc;
}, {});
}
function prepareChartData(frequencies) {
return Object.entries(frequencies)
.sort(([a], [b]) => a - b)
.map(([bin, count]) => ({
name: `${bin * 10}-${bin * 10 + 9}`,
y: count
}));
}
function initializeHistogramChart(chartData, baseOrTotal) {
Highcharts.chart('salaryBarPlot', {
chart: { type: 'column' },
title: { text: '' },
xAxis: {
type: 'category',
title: { text: `${capitalize(baseOrTotal)} Compensation (₹ LPA)` },
labels: { rotation: 0 }
},
yAxis: { title: { text: '' } },
legend: { enabled: false },
plotOptions: {
series: {
borderWidth: 0,
dataLabels: { enabled: true, format: '{point.y}' }
}
},
series: [{ name: 'Total', data: chartData, color: '#55b17f' }]
});
}
function mostOfferCompanies(jsonData) {
const companyCounts = countCompanies(jsonData);
let [categories, counts] = sortAndSliceData(companyCounts);
initializeBarChart(categories, counts);
}
function countCompanies(data) {
return data.reduce((acc, { company }) => {
acc[company] = (acc[company] || 0) + 1;
return acc;
}, {});
}
function sortAndSliceData(companyCounts) {
const sortedData = Object.entries(companyCounts)
.sort(([, a], [, b]) => b - a)
.slice(0, 10);
const categories = sortedData.map(([company]) => company);
const counts = sortedData.map(([, count]) => count);
return [categories, counts];
}
function initializeBarChart(categories, counts) {
Highcharts.chart('companyBarPlot', {
chart: { type: 'bar' },
title: { text: '' },
xAxis: {
categories: categories,
title: { text: null }
},
yAxis: {
min: 0,
title: { text: '# Offers', align: 'high' },
labels: { overflow: 'justify' }
},
tooltip: { valueSuffix: ' occurrences' },
plotOptions: { bar: { dataLabels: { enabled: true } } },
legend: { enabled: false },
series: [{ name: 'Offers', data: counts, color: '#55b17f' }]
});
}
function plotHistogram(jsonData, baseOrTotal) {
const totalValues = extractValues(jsonData, baseOrTotal);
const totalFrequencies = calculateFrequencies(totalValues);
const chartData = prepareChartData(totalFrequencies);
initializeHistogramChart(chartData, baseOrTotal);
}
// Helper function to calculate quantiles
function quantile(arr, q) {
const sorted = arr.slice().sort((a, b) => a - b);
const pos = (sorted.length - 1) * q;
const base = Math.floor(pos);
const rest = pos - base;
return sorted[base + 1] !== undefined ? sorted[base] + rest * (sorted[base + 1] - sorted[base]) : sorted[base];
}
// Function to group salary values by role or company
function groupSalariesBy(jsonData, groupBy, valueKey) {
return jsonData.reduce((acc, item) => {
const key = item[groupBy];
const value = item[valueKey];
if (!acc[key]) acc[key] = [];
acc[key].push(value);
return acc;
}, {});
}
// Function to calculate the five-number summary for each group
function calculateBoxPlotData(salariesByGroup, validItems, minDataPoints = 3) {
return Object.keys(salariesByGroup)
.filter(key => salariesByGroup[key].length >= minDataPoints && (validItems.size === 0 || validItems.has(key)))
.map(key => {
const values = salariesByGroup[key];
return {
name: key,
data: [[Math.min(...values), quantile(values, 0.25), quantile(values, 0.5), quantile(values, 0.75), Math.max(...values)]]
};
})
.sort((a, b) => b.data[0][2] - a.data[0][2]) // Sort by median value
.slice(0, 20); // Keep only the top 20
}
// Function to initialize the Highcharts chart for box plot
function initializeBoxPlotChart(docId, boxPlotData, baseOrTotal, roleOrCompany) {
Highcharts.chart(docId, {
chart: { type: 'boxplot' },
title: { text: '' },
legend: { enabled: false },
xAxis: {
categories: boxPlotData.map(item => item.name),
title: { text: '' },
labels: { rotation: -90 }
},
yAxis: {
title: { text: `${capitalize(baseOrTotal)} Compensation (₹ LPA)` }
},
series: [{
name: 'Salaries',
data: boxPlotData.map(item => item.data[0]),
tooltip: { headerFormat: `<em>${capitalize(roleOrCompany)}: {point.key}</em><br/>` },
color: '#55b17f'
}]
});
}
function plotBoxPlot(jsonData, baseOrTotal, docId, roleOrCompany, validItems) {
const salariesByGroup = groupSalariesBy(jsonData, roleOrCompany, baseOrTotal);
const boxPlotData = calculateBoxPlotData(salariesByGroup, validItems);
initializeBoxPlotChart(docId, boxPlotData, baseOrTotal, roleOrCompany);
}
document.addEventListener('DOMContentLoaded', async function () {
let currentPage = 1;
let offers = [];
// Fetch your JSONL data converted to JSON array
async function fetchOffers() {
const response = await fetch('data/parsed_comps.json');
const data = await response.json();
offers = data;
displayOffers(currentPage);
}
await fetchOffers();
let statsInfo = statsStr(offers);
document.getElementById('statsStr').textContent = statsInfo;
plotHistogram(offers, 'total');
mostOfferCompanies(offers);
plotBoxPlot(offers, 'total', 'companyBoxPlot', 'company', new Set([]));
plotBoxPlot(offers, 'total', 'roleBoxPlot', 'mapped_role', validRoles);
function displayOffers(page) {
const startIndex = (page - 1) * offersPerPage;
const endIndex = startIndex + offersPerPage;
const paginatedOffers = offers.slice(startIndex, endIndex);
const table = document.createElement('table');
table.classList.add('table');
const emptyRow = table.insertRow();
emptyRow.innerHTML = `
<th style="width: 5%"></th><th style="width: 15%"></th>
<th style="width: 30%"></th><th style="width: 25%"></th>
<th style="width: 5%"></th><th style="width: 20%"></th>
`;
const headerRow = table.insertRow();
headerRow.style.border = 'none';
const indexHeader = headerRow.insertCell();
indexHeader.innerHTML = '<b style="font-size: 13px;">#</b>';
const idHeader = headerRow.insertCell();
idHeader.innerHTML = '<b style="font-size: 13px;">ID</b>';
const companyHeader = headerRow.insertCell();
companyHeader.innerHTML = `
<b style="font-size: 13px;">Company<br>
<span class="text-secondary">Location | Date</span></b>
`;
const roleHeader = headerRow.insertCell();
roleHeader.innerHTML = '<b style="font-size: 13px;">Role</b>';
const yoeHeader = headerRow.insertCell();
yoeHeader.innerHTML = '<b style="font-size: 13px;">Yoe</b>';
const salaryHeader = headerRow.insertCell();
salaryHeader.innerHTML = `
<p class="text-end" style="margin-bottom: 0px;">
<b style="font-size: 13px;">Total<br>
<span class="text-secondary">Base</span></b></p>
`;
paginatedOffers.forEach((offer, index) => {
const row = table.insertRow();
const indexCell = row.insertCell();
indexCell.innerHTML = `<p>${startIndex + index + 1}</p>`;
const idCell = row.insertCell();
idCell.innerHTML = `
<p><abbr title="attribute">
<a class="link-secondary" href="https://leetcode.com/discuss/compensation/${offer.id}">
${offer.id}
</a></abbr></p>
`;
const companyCell = row.insertCell();
companyCell.innerHTML = `
<b style="font-size: 13px;">${offer.company}</b>
<br><span class="text-secondary">
${offer.location} | ${offer.creation_date}
</span>`;
const roleCell = row.insertCell();
roleCell.innerHTML = `
<b style="font-size: 13px;">${offer.mapped_role}</b>
<br><span class="text-secondary">${offer.role}</span>`;
const yoeCell = row.insertCell();
yoeCell.textContent = offer.yoe;
const salaryCell = row.insertCell();
salaryCell.innerHTML = `
<p class="text-end" style="margin-bottom: 0px;">
<b style="font-size: 13px;">${formatSalaryInINR(offer.total)}</b>
<br><span class="text-secondary" style="font-size: 13px;">
${formatSalaryInINR(offer.base)}</span></p>
`;
});
const container = document.getElementById('offersTable');
container.innerHTML = '';
container.appendChild(table);
}
document.getElementById('prevPage').addEventListener('click', () => {
if (currentPage > 1) {
currentPage--;
displayOffers(currentPage);
}
});
document.getElementById('nextPage').addEventListener('click', () => {
if ((currentPage * offersPerPage) < offers.length) {
currentPage++;
displayOffers(currentPage);
}
});
});