Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
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
35 private static array $cutDurationStatQueue = [];
36
37 public static function getTableName()
38 {
39 return 'b_bp_workflow_duration_stat';
40 }
41
42 public static function getMap()
43 {
44 return [
45 (new IntegerField('ID'))
46 ->configureAutocomplete(true)
47 ->configurePrimary(true)
48 ,
49 (new StringField('WORKFLOW_ID'))
50 ->configureRequired(true)
51 ->configureSize(32)
52 ->addValidator(new LengthValidator(1, 32))
53 ,
54 (new IntegerField('TEMPLATE_ID'))
55 ->configureRequired(true)
56 ,
57 (new IntegerField('DURATION'))
58 ->configureRequired(true)
59 ,
60 ];
61 }
62
63 public static function getAverageDurationByTemplateId(int $templateId): ?int
64 {
65 $averageDuration = null;
66
67 if ($templateId > 0)
68 {
69 $query =
70 static::query()
71 ->setSelect(['ID', 'DURATION'])
72 ->where('TEMPLATE_ID', $templateId)
73 ->addOrder('ID', 'DESC')
74 ->setLimit(self::DURATION_ROWS_LIMIT)
75 // ->setCacheTtl(static::AVERAGE_DURATIONS_CACHE_TTL)
76 ;
77 $duration = $query->exec()->fetchCollection()->getDurationList();
78 $total = count($duration);
79
80 if ($total > 0)
81 {
82 $deviationCount = (int)(($total * self::AVERAGE_DURATION_DEVIATION_PERCENT) / 200);
83 $length = $total - 2 * $deviationCount;
84
85 $slicedDuration = $duration;
86 if ($deviationCount !== 0)
87 {
88 sort($duration, SORT_NUMERIC);
89 $slicedDuration = array_slice($duration, $deviationCount, $length);
90 }
91
92 $averageDuration = (int)(array_sum($slicedDuration) / $length);
93 }
94 }
95
96 return $averageDuration;
97 }
98
99 public static function getOutdatedIds(int $templateId): array
100 {
101 $ids = [];
102 if ($templateId > 0)
103 {
104 $query =
105 static::query()
106 ->addSelect('ID')
107 ->where('TEMPLATE_ID', $templateId)
108 ->addOrder('ID', 'DESC')
109 ->setOffset(static::DURATION_ROWS_LIMIT)
110 ->setLimit(100)
111 ;
112 $queryResult = $query->exec();
113 $ids = $queryResult->fetchCollection()->getIdList();
114 }
115
116 return $ids;
117 }
118
119 public static function deleteAllByTemplateId(int $templateId)
120 {
121 $connection = Application::getConnection();
122 $sqlHelper = $connection->getSqlHelper();
123
124 $tableName = $sqlHelper->forSql(static::getTableName());
125
126 $connection->queryExecute("DELETE FROM {$tableName} WHERE TEMPLATE_ID = {$templateId}");
127 }
128
129 public static function onAfterAdd(Event $event)
130 {
131 parent::onAfterAdd($event);
132
133 $fields = $event->getParameter('fields');
134 $templateId = $fields['TEMPLATE_ID'] ?? 0;
135
136 self::$cutDurationStatQueue[$templateId] = true;
137 static $isAddedBackgroundJod = false;
138 if (!$isAddedBackgroundJod)
139 {
140 Main\Application::getInstance()->addBackgroundJob(
141 [static::class, 'doBackgroundDurationStatCut'],
142 [],
144 );
145 $isAddedBackgroundJod = true;
146 }
147 }
148
149 public static function doBackgroundDurationStatCut()
150 {
151 $connection = Application::getConnection();
152 $tableName = $connection->getSqlHelper()->forSql(static::getTableName());
153
154 $templateIds = array_keys(self::$cutDurationStatQueue);
155 self::$cutDurationStatQueue = [];
156
157 foreach ($templateIds as $templateId)
158 {
159 $ids = static::getOutdatedIds((int)$templateId);
160 if ($ids)
161 {
162 $connection->query(
163 sprintf(
164 "DELETE FROM {$tableName} WHERE ID IN (%s)",
165 implode(',', $ids)
166 ),
167 );
168 }
169 }
170 }
171
172}
static getConnection($name="")
getParameter($key)
Definition event.php:80