bitrix_task_control/src/reports/effectiveness/reportBuilder.ts

104 lines
4.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { formatDate } from '../../core/dates';
import { MetricsStorage } from '../../core/metrics/storage';
const DAYS = ['вс', 'пн', 'вт', 'ср', 'чт', 'пт', 'сб'];
export function formatRuDate(date: Date): string {
const formatted = formatDate(date);
return `${DAYS[date.getDay()]}, ${formatted.slice(0, 5)}`;
}
export function formatDateRange(startDate: Date, endDate: Date): string[] {
const dates: string[] = [];
const current = new Date(startDate);
while (current <= endDate) {
dates.push(formatRuDate(current));
current.setDate(current.getDate() + 1);
}
return dates;
}
export class MetricsReport {
sheet: GoogleAppsScript.Spreadsheet.Sheet;
storage: MetricsStorage;
metrics: Array<{ title: string; field: string }>;
columns: Record<string, number>;
constructor(reportSheet = 'Отчет') {
this.sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(reportSheet);
this.storage = new MetricsStorage();
this.metrics = [
{ title: 'Общее количество завершенных задач', field: 'completed' },
{ title: 'Количество завершенных задач, требующих приемки', field: 'reviewCompleted' },
{ title: 'Количество принятых задач, требующих приемки', field: 'reviewAccepted' },
{ title: 'Эффективность по требующим приемки', field: 'reviewPercent' },
{ title: 'Суммарно эффективно затраченное время за вчера', field: 'spentHours' },
{ title: 'Количество просроченных и потом перенсенных задач на сегодня', field: 'overdue' },
{ title: 'Количество новых задач самому себе за вчера', field: 'newTasks' },
{ title: 'Количество задач в работе со сроком на сегодня', field: 'todayTasks' },
{ title: 'Суммарное время, которое сотрудник потратит на выполнение этих задач в часах', field: 'todayHours' },
{ title: 'Согласовано ли общее время и количество задач на сегодня руководителем', field: 'approved' },
{ title: 'Общее количество задач в работе', field: 'totalTasks' },
{ title: 'Оценка эффективности сотрудника от руководителя', field: 'efficiency' }
];
this.columns = {
date: 0, executor: 1, efficiency: 2, completed: 3, returned: 4,
completedPercent: 5, reviewCompleted: 6, reviewAccepted: 7, reviewPercent: 8,
spentHours: 9, overdue: 10, newTasks: 11, todayTasks: 12, approved: 13,
todayHours: 14, totalTasks: 15
};
}
build(fromDate: Date, toDate: Date): void {
const rows = this.storage.getRange(fromDate, toDate);
const executors: string[] = [];
const allDates = formatDateRange(fromDate, toDate);
const map: Record<string, any> = {};
rows.forEach(row => {
const executor = row[this.columns.executor];
const date = formatRuDate(row[this.columns.date]);
if (!map[executor]) {
map[executor] = {};
executors.push(executor);
}
if (!map[executor][date]) map[executor][date] = {};
this.metrics.forEach(metric => {
map[executor][date][metric.field] = row[this.columns[metric.field]];
});
});
const result: any[][] = [['Сотрудник', 'Критерий', ...allDates, 'Среднее']];
executors.forEach(executor => {
this.metrics.forEach(metric => {
const line: any[] = [executor, metric.title];
let sum = 0;
let count = 0;
allDates.forEach(date => {
let value = map[executor][date] ? map[executor][date][metric.field] : '';
if (metric.field === 'completedPercent' || metric.field === 'reviewPercent') {
value = Math.round(value * 10) / 10;
line.push(value ? `${value}%` : '');
} else {
line.push(value);
}
if (value && typeof value === 'number') {
sum += value;
count++;
}
});
line.push(count ? Math.round(sum / count * 10) / 10 : '');
result.push(line);
});
result.push(Array(result[0].length).fill(''));
});
this.sheet.clearContents();
this.sheet.getRange(1, 1, result.length, result[0].length).setValues(result);
}
}