Skip to content

Instantly share code, notes, and snippets.

@gnchrv
Last active May 29, 2019 13:26
Show Gist options
  • Save gnchrv/d2154aa297db14861173cc6200d2917a to your computer and use it in GitHub Desktop.
Save gnchrv/d2154aa297db14861173cc6200d2917a to your computer and use it in GitHub Desktop.
Построение графика движения рабочих по календарному плану (output-1.csv и output-2.csv — результаты для первой и второй смен, соответственно)
const jsonexport = require('jsonexport')
const CSV = require('csvtojson')
const fs = require('fs')
// Путь к файлу
const file = __dirname + '/input.csv'
// Настраиваем csv-парсер
const csv = CSV({
delimiter: ';',
trim: true,
colParser: {
start: 'number',
end: 'number',
duration: 'number',
members: 'number',
shifts: 'number',
}
})
function compute(complexes, forShifts = 1) {
// Определяем начало и конец всех работ
const min = complexes.reduce((min, c) => c.start < min ? c.start : min, Infinity)
const max = complexes.reduce((max, c) => c.end > max ? c.end : max, -Infinity)
// Определяем максимально возможное количество смен
const shifts = complexes.reduce((max, c) => c.shifts > max ? c.shifts : max, 0)
// Создаем заготовку для периодов
const periods = []
// Проходимся по каждому дню
for (let day = min, members = 0; day <= max; day++) {
// Сохраняем количество людей за предыдущий день
const membersBefore = members
// Проверяем, изменилось ли количество людей в каком-либо комплексе
for (let complex of complexes) {
// Определяем число смен: меньшее из указанного и применяемого в комплексе
const shifts = Math.min(forShifts, complex.shifts)
if (day !== complex.start && day !== complex.end) continue
if (day === complex.start) members += complex.members * shifts
if (day === complex.end) members -= complex.members * shifts
}
// Если число людей не изменилось, переходим на следующией день
if (membersBefore === members) continue
// Если изменилось, завершаем предыдущий период и создаем новый
if (periods.length) {
const lastPeriod = periods.slice(-1)[0]
lastPeriod.end = day
lastPeriod.duration = lastPeriod.end - lastPeriod.start
}
periods.push({ members, start: day })
}
return periods
}
function run(complexes, output, shifts) {
// Заготовки для периодов и максимальных значений рабочих
const max = []
const results = []
for (let shift = 1; shift <= shifts; shift++) {
// Вычисляем периоды
const periods = compute(complexes, shift)
results.push(periods)
// Определяем максимальное количество рабочих
const maxMembers = periods.reduce((max, p) => p.members > max ? p.members : max, 0)
max.push(maxMembers)
// Записываем результат
const filename = __dirname + `/${output}-${shift}.csv`
const exportOpts = { rowDelimiter: ';' }
jsonexport(periods, exportOpts, (e, csv) => fs.writeFileSync(filename, csv))
}
return { results, max }
}
// Парсим файл
csv.fromFile(file)
.then(complexes => run(complexes, 'output', 2))
.then(res => console.dir(res, { depth: null }))
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
complex;start;end;duration;members;shifts
_1;0;78;78;5;2
_2;10;87;77;5;2
_1;20;181;161;25;2
_2;41;202;161;25;2
_3;62;223;161;25;2
_4;83;243;160;25;2
_;234;244;10;25;2
_1;244;556;312;16;2
_2;254;565;311;16;2
_3;264;575;311;16;2
_4;274;585;311;16;2
_1;372;591;219;6;2
_2;379;598;219;6;2
_3;386;604;218;6;2
_4;393;611;218;6;2
_;611;801;190;8;2
_;774;804;30;24;2
_;779;804;25;9;1
_;779;1120;341;25;2
_;853;1126;273;25;2
_;931;1130;199;24;2
_;978;1133;155;25;2
_;982;1171;189;24;2
_;987;1179;192;25;2
_;1160;1179;19;24;2
_;1160;1208;48;4;1
_;1162;1328;166;24;2
_;1167;1349;182;25;2
_;804;884;80;14;1
_;804;872;68;25;1
_;-90;0;90;25;2
_;993;1349;356;24;2
_;-90;1349;1439;25;2
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
members;start;end;duration
50;-90;0;90
30;0;10;10
35;10;20;10
60;20;41;21
85;41;62;21
110;62;78;16
105;78;83;5
130;83;87;4
125;87;181;94
100;181;202;21
75;202;223;21
50;223;234;11
75;234;243;9
50;243;244;1
41;244;254;10
57;254;264;10
73;264;274;10
89;274;372;98
95;372;379;7
101;379;386;7
107;386;393;7
113;393;556;163
97;556;565;9
81;565;575;10
65;575;585;10
49;585;591;6
43;591;598;7
37;598;604;6
31;604;611;7
33;611;774;163
57;774;779;5
91;779;801;22
83;801;804;3
89;804;853;49
114;853;872;19
89;872;884;12
75;884;931;47
99;931;978;47
124;978;982;4
148;982;987;5
173;987;993;6
197;993;1120;127
172;1120;1126;6
147;1126;1130;4
123;1130;1133;3
98;1133;1160;27
126;1160;1162;2
150;1162;1167;5
175;1167;1171;4
151;1171;1179;8
102;1179;1208;29
98;1208;1328;120
74;1328;1349;21
0;1349;;
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
members;start;end;duration
100;-90;0;90
60;0;10;10
70;10;20;10
120;20;41;21
170;41;62;21
220;62;78;16
210;78;83;5
260;83;87;4
250;87;181;94
200;181;202;21
150;202;223;21
100;223;234;11
150;234;243;9
100;243;244;1
82;244;254;10
114;254;264;10
146;264;274;10
178;274;372;98
190;372;379;7
202;379;386;7
214;386;393;7
226;393;556;163
194;556;565;9
162;565;575;10
130;575;585;10
98;585;591;6
86;591;598;7
74;598;604;6
62;604;611;7
66;611;774;163
114;774;779;5
173;779;801;22
157;801;804;3
139;804;853;49
189;853;872;19
164;872;884;12
150;884;931;47
198;931;978;47
248;978;982;4
296;982;987;5
346;987;993;6
394;993;1120;127
344;1120;1126;6
294;1126;1130;4
246;1130;1133;3
196;1133;1160;27
248;1160;1162;2
296;1162;1167;5
346;1167;1171;4
298;1171;1179;8
200;1179;1208;29
196;1208;1328;120
148;1328;1349;21
0;1349;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment