Lines
96.75%
209 / 216
Methods
93.10%
27 / 29
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| run | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 1 | ||
| fetchArchiveData | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 1 | ||
| processArchiveRows | 88.88% | 16 / 18 | 0.00% | 0 / 1 | 7.07 | ||
| determineHourAndType | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 4 | ||
| initializeStatsIfNeeded | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 4 | ||
| saveStatistics | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 3 | ||
| ensureStatisticsRow | 100.00% | 18 / 18 | 100.00% | 1 / 1 | 2 | ||
| updateStatisticsValues | 100.00% | 19 / 19 | 100.00% | 1 / 1 | 3 | ||
| addHourUpdateColumns | 100.00% | 17 / 17 | 100.00% | 1 / 1 | 3 | ||
| hourSuffixForStatistic | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsbackend\Base] __construct | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] init | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] getWriter | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsbackend\Base] getReader | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 3 | ||
| [BO\Zmsbackend\Base] fetchPreparedStatement | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 3 | ||
| [BO\Zmsbackend\Base] startExecute | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] pdoExceptionHandler | 100.00% | 13 / 13 | 100.00% | 1 / 1 | 4 | ||
| [BO\Zmsbackend\Base] fetchStatement | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] fetchOne | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsbackend\Base] fetchRow | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] fetchValue | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] fetchAll | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] fetchHandle | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] fetchList | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 2 | ||
| [BO\Zmsbackend\Base] writeItem | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] deleteItem | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] perform | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] fetchAffected | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 1 | ||
| [BO\Zmsbackend\Base] readResolvedReferences | n/a | 0 / 0 | n/a | 0 / 0 | 1 | ||
| [BO\Zmsbackend\Base] hashStringValue | 50.00% | 5 / 10 | 0.00% | 0 / 1 | 8.12 | ||
| 14 | class CalculateDailyWaitingStatisticByCron extends \BO\Zmsbackend\Base | |
| 15 | { | |
| 16 | public function run(DateTimeImmutable $day, bool $commit = false): void | |
| 17 | { | |
| 18 | \App::$log->info('CalculateDailyWaitingStatisticByCron started', ['date' => $day->format('Y-m-d')]); | |
| 19 | ||
| 20 | $archiveRows = $this->fetchArchiveData($day); | |
| 21 | $statsByScopeDate = $this->processArchiveRows($archiveRows); | |
| 22 | $this->saveStatistics($statsByScopeDate, $commit); | |
| 23 | ||
| 24 | \App::$log->info('CalculateDailyWaitingStatisticByCron finished', ['date' => $day->format('Y-m-d')]); | |
| 25 | } | |
| 26 | ||
| 27 | private function fetchArchiveData(DateTimeImmutable $day): array | |
| 28 | { | |
| 29 | $sql = " | |
| 30 | SELECT | |
| 31 | StandortID, | |
| 32 | Datum, | |
| 33 | `Timestamp`, | |
| 34 | mitTermin, | |
| 35 | waiting_time, | |
| 36 | way_time | |
| 37 | FROM buergerarchiv | |
| 38 | WHERE Datum = :theDay | |
| 39 | AND StandortID > 0 | |
| 40 | "; | |
| 41 | return $this->getReader()->fetchAll($sql, [ | |
| 42 | 'theDay' => $day->format('Y-m-d'), | |
| 43 | ]); | |
| 44 | } | |
| 45 | ||
| 46 | private function processArchiveRows(array $archiveRows): array | |
| 47 | { | |
| 48 | $statsByScopeDate = []; | |
| 49 | ||
| 50 | foreach ($archiveRows as $row) { | |
| 51 | // Same skip as the former buerger path: no waiting_time => skip count, wait, and way | |
| 52 | if (empty($row['waiting_time'])) { | |
| 53 | continue; | |
| 54 | } | |
| 55 | ||
| 56 | $scopeId = (int) $row['StandortID']; | |
| 57 | if ($scopeId <= 0) { | |
| 58 | continue; | |
| 59 | } | |
| 60 | ||
| 61 | [$hour, $type] = $this->determineHourAndType($row); | |
| 62 | if ($hour < 0 || $hour > 23) { | |
| 63 | continue; | |
| 64 | } | |
| 65 | ||
| 66 | $waitMins = (float) $row['waiting_time']; | |
| 67 | $wayMins = is_numeric($row['way_time']) ? (float) $row['way_time'] : 0.0; | |
| 68 | ||
| 69 | $dateStr = $row['Datum']; | |
| 70 | $this->initializeStatsIfNeeded($statsByScopeDate, $scopeId, $dateStr); | |
| 71 | ||
| 72 | $statsByScopeDate[$scopeId][$dateStr][$hour][$type]['count'] += 1; | |
| 73 | $statsByScopeDate[$scopeId][$dateStr][$hour][$type]['sumWait'] += $waitMins; | |
| 74 | $statsByScopeDate[$scopeId][$dateStr][$hour][$type]['sumWay'] += $wayMins; | |
| 75 | } | |
| 76 | ||
| 77 | return $statsByScopeDate; | |
| 78 | } | |
| 79 | ||
| 80 | private function determineHourAndType(array $archiveRecord): array | |
| 81 | { | |
| 82 | $type = ((int) $archiveRecord['mitTermin'] === 1) ? 'termin' : 'spontan'; | |
| 83 | $parts = explode(':', (string) $archiveRecord['Timestamp']); | |
| 84 | $hour = isset($parts[0]) && $parts[0] !== '' ? (int) $parts[0] : -1; | |
| 85 | ||
| 86 | return [$hour, $type]; | |
| 87 | } | |
| 88 | ||
| 89 | private function initializeStatsIfNeeded(array &$statsByScopeDate, int $scopeId, string $dateStr): void | |
| 90 | { | |
| 91 | if (!isset($statsByScopeDate[$scopeId])) { | |
| 92 | $statsByScopeDate[$scopeId] = []; | |
| 93 | } | |
| 94 | ||
| 95 | if (!isset($statsByScopeDate[$scopeId][$dateStr])) { | |
| 96 | $statsByScopeDate[$scopeId][$dateStr] = []; | |
| 97 | foreach (range(0, 23) as $h) { | |
| 98 | $statsByScopeDate[$scopeId][$dateStr][$h] = [ | |
| 99 | 'spontan' => ['count' => 0, 'sumWait' => 0.0, 'sumWay' => 0.0], | |
| 100 | 'termin' => ['count' => 0, 'sumWait' => 0.0, 'sumWay' => 0.0], | |
| 101 | ]; | |
| 102 | } | |
| 103 | } | |
| 104 | } | |
| 105 | ||
| 106 | private function saveStatistics(array $statsByScopeDate, bool $commit): void | |
| 107 | { | |
| 108 | foreach ($statsByScopeDate as $scopeId => $dateArray) { | |
| 109 | foreach ($dateArray as $dateStr => $hoursData) { | |
| 110 | $this->updateStatisticsValues((int) $scopeId, $dateStr, $hoursData, $commit); | |
| 111 | } | |
| 112 | } | |
| 113 | } | |
| 114 | ||
| 115 | private function ensureStatisticsRow(int $scopeId, string $dateStr): int | |
| 116 | { | |
| 117 | $existingId = $this->fetchValue( | |
| 118 | 'SELECT wartenrstatistikid | |
| 119 | FROM wartenrstatistik | |
| 120 | WHERE standortid = :sid | |
| 121 | AND datum = :d | |
| 122 | ORDER BY wartenrstatistikid ASC | |
| 123 | LIMIT 1', | |
| 124 | [ | |
| 125 | 'sid' => $scopeId, | |
| 126 | 'd' => $dateStr, | |
| 127 | ] | |
| 128 | ); | |
| 129 | ||
| 130 | if ($existingId) { | |
| 131 | return (int) $existingId; | |
| 132 | } | |
| 133 | ||
| 134 | $this->perform( | |
| 135 | 'INSERT INTO wartenrstatistik (standortid, datum) VALUES (:sid, :d)', | |
| 136 | [ | |
| 137 | 'sid' => $scopeId, | |
| 138 | 'd' => $dateStr, | |
| 139 | ] | |
| 140 | ); | |
| 141 | ||
| 142 | return (int) $this->getWriter()->lastInsertId(); | |
| 143 | } | |
| 144 | ||
| 145 | private function updateStatisticsValues(int $scopeId, string $dateStr, array $hoursData, bool $commit): void | |
| 146 | { | |
| 147 | $updateParams = []; | |
| 148 | $updateCols = []; | |
| 149 | ||
| 150 | foreach (range(0, 23) as $hour) { | |
| 151 | $this->addHourUpdateColumns($updateCols, $updateParams, $hour, $hoursData, 'spontan'); | |
| 152 | $this->addHourUpdateColumns($updateCols, $updateParams, $hour, $hoursData, 'termin'); | |
| 153 | } | |
| 154 | ||
| 155 | if (!$commit) { | |
| 156 | \App::$log->info('[DRY RUN] update scope statistics', [ | |
| 157 | 'scopeId' => $scopeId, | |
| 158 | 'date' => $dateStr, | |
| 159 | ]); | |
| 160 | return; | |
| 161 | } | |
| 162 | ||
| 163 | $rowId = $this->ensureStatisticsRow($scopeId, $dateStr); | |
| 164 | $updateParams['id'] = $rowId; | |
| 165 | ||
| 166 | $sqlUpdate = sprintf( | |
| 167 | 'UPDATE wartenrstatistik | |
| 168 | SET %s | |
| 169 | WHERE wartenrstatistikid = :id | |
| 170 | LIMIT 1', | |
| 171 | implode(', ', $updateCols) | |
| 172 | ); | |
| 173 | ||
| 174 | $this->perform($sqlUpdate, $updateParams); | |
| 175 | } | |
| 176 | ||
| 177 | private function addHourUpdateColumns( | |
| 178 | array &$updateCols, | |
| 179 | array &$updateParams, | |
| 180 | int $hour, | |
| 181 | array $hoursData, | |
| 182 | string $type | |
| 183 | ): void { | |
| 184 | $hourSuffix = $this->hourSuffixForStatistic($type); | |
| 185 | $colWaitCount = sprintf('hour_%02d_waiting_count_%s', $hour, $hourSuffix); | |
| 186 | $colWaitTime = sprintf('hour_%02d_waiting_time_%s', $hour, $hourSuffix); | |
| 187 | $colWayTime = sprintf('hour_%02d_way_time_%s', $hour, $hourSuffix); | |
| 188 | ||
| 189 | $count = $hoursData[$hour][$type]['count']; | |
| 190 | $avgWait = ($count > 0) | |
| 191 | ? round($hoursData[$hour][$type]['sumWait'] / $count, 2) | |
| 192 | : 0.0; | |
| 193 | $avgWay = ($count > 0) | |
| 194 | ? round($hoursData[$hour][$type]['sumWay'] / $count, 2) | |
| 195 | : 0.0; | |
| 196 | ||
| 197 | $updateCols[] = "`$colWaitCount` = :$colWaitCount"; | |
| 198 | $updateCols[] = "`$colWaitTime` = :$colWaitTime"; | |
| 199 | $updateCols[] = "`$colWayTime` = :$colWayTime"; | |
| 200 | ||
| 201 | $updateParams[$colWaitCount] = $count; | |
| 202 | $updateParams[$colWaitTime] = $avgWait; | |
| 203 | $updateParams[$colWayTime] = $avgWay; | |
| 204 | } | |
| 205 | ||
| 206 | private function hourSuffixForStatistic(string $type): string | |
| 207 | { | |
| 208 | return $type === 'termin' ? 'appointment' : 'spontaneous'; | |
| 209 | } | |
| 210 | } |
Inherited from BO\Zmsbackend\Base
| 38 | public function __construct(?Connection\Pdo $writeConnection = null, ?Connection\Pdo $readConnection = null) | |
| 39 | { | |
| 40 | $this->writeDb = $writeConnection; | |
| 41 | $this->readDb = $readConnection; | |
| 42 | } |
| 44 | public static function init(?Connection\Pdo $writeConnection = null, ?Connection\Pdo $readConnection = null): static | |
| 45 | { | |
| 46 | $instance = new static($writeConnection, $readConnection); | |
| 47 | return $instance; | |
| 48 | } |
| 50 | public function getWriter(): Connection\Pdo | |
| 51 | { | |
| 52 | if (null === $this->writeDb) { | |
| 53 | $this->writeDb = Connection\Select::getWriteConnection(); | |
| 54 | $this->readDb = $this->writeDb; | |
| 55 | } | |
| 56 | return $this->writeDb; | |
| 57 | } |
| 59 | public function getReader(): Connection\Pdo | |
| 60 | { | |
| 61 | if (null !== $this->writeDb) { | |
| 62 | // if readDB gets a reset, still use writeDB | |
| 63 | return $this->writeDb; | |
| 64 | } | |
| 65 | if (null === $this->readDb) { | |
| 66 | $this->readDb = Connection\Select::getReadConnection(); | |
| 67 | } | |
| 68 | return $this->readDb; | |
| 69 | } |
| 71 | public function fetchPreparedStatement(Query\Base|string $query) | |
| 72 | { | |
| 73 | $sql = "$query"; | |
| 74 | $reader = $this->getReader(); | |
| 75 | if (spl_object_hash($reader) != static::$preparedConnectionId) { | |
| 76 | // do not use prepared statements on a different connection | |
| 77 | static::$preparedCache = []; | |
| 78 | static::$preparedConnectionId = spl_object_hash($reader); | |
| 79 | } | |
| 80 | if (!isset(static::$preparedCache[$sql])) { | |
| 81 | $prepared = $this->getReader()->prepare($sql); | |
| 82 | static::$preparedCache[$sql] = $prepared; | |
| 83 | } | |
| 84 | return static::$preparedCache[$sql]; | |
| 85 | } |
| 87 | public function startExecute($statement, array $parameters) | |
| 88 | { | |
| 89 | $statement = static::pdoExceptionHandler(function () use ($statement, $parameters) { | |
| 90 | $statement->execute($parameters); | |
| 91 | return $statement; | |
| 92 | }); | |
| 93 | return $statement; | |
| 94 | } |
| 96 | protected static function pdoExceptionHandler(\Closure $pdoFunction, $parameters = []) | |
| 97 | { | |
| 98 | try { | |
| 99 | $statement = $pdoFunction($parameters); | |
| 100 | } catch (\PDOException $pdoException) { | |
| 101 | if (stripos($pdoException->getMessage(), 'Lock wait timeout') !== false) { | |
| 102 | throw new \BO\Zmsbackend\Exception\Pdo\LockTimeout(); | |
| 103 | } | |
| 104 | //@codeCoverageIgnoreStart | |
| 105 | if (stripos($pdoException->getMessage(), 'Deadlock found') !== false) { | |
| 106 | throw new \BO\Zmsbackend\Exception\Pdo\DeadLockFound(); | |
| 107 | } | |
| 108 | //@codeCoverageIgnoreEnd | |
| 109 | $message = "SQL: " | |
| 110 | . " Err: " | |
| 111 | . $pdoException->getMessage() | |
| 112 | //. " || Statement: " | |
| 113 | //.$statement->queryString | |
| 114 | //." || Parameters=". var_export($parameters, true) | |
| 115 | ; | |
| 116 | throw new \BO\Zmsbackend\Exception\Pdo\PDOFailed($message, 0, $pdoException); | |
| 117 | } | |
| 118 | return $statement; | |
| 119 | } |
| 121 | public function fetchStatement(Query\Base $query) | |
| 122 | { | |
| 123 | $parameters = $query->getParameters(); | |
| 124 | $statement = $this->startExecute($this->fetchPreparedStatement($query), $parameters); | |
| 125 | return $statement; | |
| 126 | } |
| 133 | public function fetchOne(Query\Base $query, \BO\Zmsentities\Schema\Entity $entity) | |
| 134 | { | |
| 135 | $statement = $this->fetchStatement($query); | |
| 136 | $data = $statement->fetch(\PDO::FETCH_ASSOC); | |
| 137 | if ($data) { | |
| 138 | $entity->exchangeArray($query->postProcessJoins($data)); | |
| 139 | $entity->setResolveLevel($query->getResolveLevel()); | |
| 140 | } | |
| 141 | $statement->closeCursor(); | |
| 142 | return $entity; | |
| 143 | } |
| 145 | public function fetchRow(Query\Base|string $query, array|null $parameters = null) | |
| 146 | { | |
| 147 | return static::pdoExceptionHandler(function () use ($query, $parameters) { | |
| 148 | $prepared = $this->fetchPreparedStatement($query); | |
| 149 | $prepared->execute($parameters); | |
| 150 | $row = $prepared->fetch(\PDO::FETCH_ASSOC); | |
| 151 | $prepared->closeCursor(); | |
| 152 | return $row; | |
| 153 | }); | |
| 154 | } |
| 156 | public function fetchValue(Query\Base|string $query, array|null $parameters = null) | |
| 157 | { | |
| 158 | return static::pdoExceptionHandler(function () use ($query, $parameters) { | |
| 159 | $prepared = $this->fetchPreparedStatement($query); | |
| 160 | $prepared->execute($parameters); | |
| 161 | $value = $prepared->fetchColumn(); | |
| 162 | $prepared->closeCursor(); | |
| 163 | return $value; | |
| 164 | }); | |
| 165 | } |
| 167 | public function fetchAll(Query\Base|string $query, array|null $parameters = null) | |
| 168 | { | |
| 169 | return static::pdoExceptionHandler(function () use ($query, $parameters) { | |
| 170 | $prepared = $this->fetchPreparedStatement($query); | |
| 171 | $prepared->execute($parameters); | |
| 172 | $list = $prepared->fetchAll(\PDO::FETCH_ASSOC); | |
| 173 | $prepared->closeCursor(); | |
| 174 | return $list; | |
| 175 | }); | |
| 176 | } |
| 178 | public function fetchHandle(Query\Base|string $query, array|null $parameters = null) | |
| 179 | { | |
| 180 | return static::pdoExceptionHandler(function () use ($query, $parameters) { | |
| 181 | $prepared = $this->fetchPreparedStatement($query); | |
| 182 | $prepared->execute($parameters); | |
| 183 | return $prepared; | |
| 184 | }); | |
| 185 | } |
| 187 | public function fetchList(Query\Base $query, \BO\Zmsentities\Schema\Entity $entity, array|\ArrayAccess $resultList = []) | |
| 188 | { | |
| 189 | $statement = $this->fetchStatement($query); | |
| 190 | while ($data = $statement->fetch(\PDO::FETCH_ASSOC)) { | |
| 191 | $dataEntity = clone $entity; | |
| 192 | $dataEntity->exchangeArray($query->postProcessJoins($data)); | |
| 193 | $dataEntity->setResolveLevel($query->getResolveLevel()); | |
| 194 | $resultList[] = $dataEntity; | |
| 195 | } | |
| 196 | $statement->closeCursor(); | |
| 197 | return $resultList; | |
| 198 | } |
| 206 | public function writeItem(Query\Base $query) | |
| 207 | { | |
| 208 | return static::pdoExceptionHandler(function () use ($query) { | |
| 209 | $this->getWriter(); //Switch to writer for write/delete | |
| 210 | $statement = $this->fetchPreparedStatement($query); | |
| 211 | $status = $statement->execute($query->getParameters()); | |
| 212 | $statement->closeCursor(); | |
| 213 | return $status; | |
| 214 | }); | |
| 215 | } |
| 221 | public function deleteItem(Query\Base $query) | |
| 222 | { | |
| 223 | return $this->writeItem($query); | |
| 224 | } |
| 226 | public function perform(string $sql, array|null $parameters = null) | |
| 227 | { | |
| 228 | return static::pdoExceptionHandler(function () use ($sql, $parameters) { | |
| 229 | $this->getWriter(); //Switch to writer for perform | |
| 230 | $prepared = $this->fetchPreparedStatement($sql); | |
| 231 | $status = $prepared->execute($parameters); | |
| 232 | $prepared->closeCursor(); | |
| 233 | return $status; | |
| 234 | }); | |
| 235 | } |
| 237 | public function fetchAffected(string $sql, array|null $parameters = null) | |
| 238 | { | |
| 239 | return static::pdoExceptionHandler(function () use ($sql, $parameters) { | |
| 240 | $this->getWriter(); //Switch to writer for perform | |
| 241 | $prepared = $this->fetchPreparedStatement($sql); | |
| 242 | $prepared->execute($parameters); | |
| 243 | $count = $prepared->rowCount(); | |
| 244 | $prepared->closeCursor(); | |
| 245 | return $count; | |
| 246 | }); | |
| 247 | } |
| 256 | public function readResolvedReferences(\BO\Zmsentities\Schema\Entity $entity, int $resolveReferences) | |
| 257 | { | |
| 258 | return $entity; | |
| 259 | } |
| 268 | public function hashStringValue(string $value, ?callable $callable = null): string | |
| 269 | { | |
| 270 | if ($callable === null) { | |
| 271 | $callable = 'sha1'; | |
| 272 | } | |
| 273 | ||
| 274 | if (is_callable($callable)) { | |
| 275 | $hash = $callable($value); | |
| 276 | ||
| 277 | if (is_string($callable)) { | |
| 278 | return $callable . ':' . $hash; | |
| 279 | } | |
| 280 | if ($callable instanceof \Closure) { | |
| 281 | return 'closure:' . $hash; | |
| 282 | } | |
| 283 | // else | |
| 284 | return 'custom:' . $hash; | |
| 285 | } | |
| 286 | ||
| 287 | throw new \InvalidArgumentException('hashStringValue() should be called with a callable as second parameter.'); | |
| 288 | } |