1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
workflowdurationstattable.php
См. документацию.
1<?php
2
4
12
30{
31 private const DURATION_ROWS_LIMIT = 20;
32 private const AVERAGE_DURATION_DEVIATION_PERCENT = 16;
33 private const AVERAGE_DURATIONS_CACHE_TTL = 86400; // 60 * 60 * 24
34 private const THREE_DAYS_IN_SECONDS = 259200; // 3600 * 24 *3
35
36 private static array $cutDurationStatQueue = [];
37
38 public static function getTableName()
39 {
40 return 'b_bp_workflow_duration_stat';
41 }
42
43 public static function getMap()
44 {
45 return [
46 (new IntegerField('ID'))
47 ->configureAutocomplete(true)
48 ->configurePrimary(true)
49 ,
50 (new StringField('WORKFLOW_ID'))
51 ->configureRequired(true)
52 ->configureSize(32)
53 ->addValidator(new LengthValidator(1, 32))
54 ,
55 (new IntegerField('TEMPLATE_ID'))
56 ->configureRequired(true)
57 ,
58 (new IntegerField('DURATION'))
59 ->configureRequired(true)
60 ,
61 ];
62 }
63
64 public static function getAverageDurationByTemplateId(int $templateId): ?int
65 {
66 $averageDuration = null;
67
68 if ($templateId > 0)
69 {
70 $query =
71 static::query()
72 ->setSelect(['ID', 'DURATION'])
73 ->where('TEMPLATE_ID', $templateId)
74 ->addOrder('ID', 'DESC')
75 ->setLimit(self::DURATION_ROWS_LIMIT)
76 // ->setCacheTtl(static::AVERAGE_DURATIONS_CACHE_TTL)
77 ;
78 $duration = $query->exec()->fetchCollection()->getDurationList();
79 $total = count($duration);
80
81 if ($total > 0)
82 {
83 $deviationCount = (int)(($total * self::AVERAGE_DURATION_DEVIATION_PERCENT) / 200);
84 $length = $total - 2 * $deviationCount;
85
86 $slicedDuration = $duration;
87 if ($deviationCount !== 0)
88 {
89 sort($duration, SORT_NUMERIC);
90 $slicedDuration = array_slice($duration, $deviationCount, $length);
91 }
92
93 $averageDuration = (int)(array_sum($slicedDuration) / $length);
94 }
95 }
96
97 return $averageDuration;
98 }
99
100 public static function getWorkflowEfficiency(int $currentDuration, ?int $averageDuration): string
101 {
102 if (null === $averageDuration)
103 {
104 return 'first';
105 }
106
107 if ($currentDuration < $averageDuration)
108 {
109 return 'fast';
110 }
111
112 if ($currentDuration < ($averageDuration + self::THREE_DAYS_IN_SECONDS))
113 {
114 return 'slow';
115 }
116
117 return 'stopped';
118 }
119
120 public static function getOutdatedIds(int $templateId): array
121 {
122 $ids = [];
123 if ($templateId > 0)
124 {
125 $query =
126 static::query()
127 ->addSelect('ID')
128 ->where('TEMPLATE_ID', $templateId)
129 ->addOrder('ID', 'DESC')
130 ->setOffset(static::DURATION_ROWS_LIMIT)
131 ->setLimit(100)
132 ;
133 $queryResult = $query->exec();
134 $ids = $queryResult->fetchCollection()->getIdList();
135 }
136
137 return $ids;
138 }
139
140 public static function deleteAllByTemplateId(int $templateId)
141 {
142 $connection = Application::getConnection();
143 $sqlHelper = $connection->getSqlHelper();
144
145 $tableName = $sqlHelper->forSql(static::getTableName());
146
147 $connection->queryExecute("DELETE FROM {$tableName} WHERE TEMPLATE_ID = {$templateId}");
148 }
149
150 public static function onAfterAdd(Event $event)
151 {
152 parent::onAfterAdd($event);
153
154 $fields = $event->getParameter('fields');
155 $templateId = $fields['TEMPLATE_ID'] ?? 0;
156
157 self::$cutDurationStatQueue[$templateId] = true;
158 static $isAddedBackgroundJod = false;
159 if (!$isAddedBackgroundJod)
160 {
161 Main\Application::getInstance()->addBackgroundJob(
162 [static::class, 'doBackgroundDurationStatCut'],
163 [],
164 Main\Application::JOB_PRIORITY_LOW - 10,
165 );
166 $isAddedBackgroundJod = true;
167 }
168 }
169
170 public static function doBackgroundDurationStatCut()
171 {
172 $connection = Application::getConnection();
173 $tableName = $connection->getSqlHelper()->forSql(static::getTableName());
174
175 $templateIds = array_keys(self::$cutDurationStatQueue);
176 self::$cutDurationStatQueue = [];
177
178 foreach ($templateIds as $templateId)
179 {
180 $ids = static::getOutdatedIds((int)$templateId);
181 if ($ids)
182 {
183 $connection->query(
184 sprintf(
185 "DELETE FROM {$tableName} WHERE ID IN (%s)",
186 implode(',', $ids)
187 ),
188 );
189 }
190 }
191 }
192
193}
$connection
Определения actionsdefinitions.php:38
static getWorkflowEfficiency(int $currentDuration, ?int $averageDuration)
Определения workflowdurationstattable.php:100
static getInstance()
Определения application.php:98
Определения event.php:5
$templateId
Определения component_props2.php:51
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$query
Определения get_search.php:11
$event
Определения prolog_after.php:141
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$fields
Определения yandex_run.php:501