From 7a5391c5ace2f9bb57af79b349f1106873c2b24f Mon Sep 17 00:00:00 2001 From: Arthur de Moulins Date: Tue, 17 Oct 2023 16:14:03 +0200 Subject: [PATCH] WIP --- docker-compose.dev.yml | 4 -- docker-compose.yml | 5 -- expose/api/Dockerfile | 3 - expose/api/docker/cron/app-crontab | 1 - .../cron/scripts/matomo-sync-phraseanet.sh | 4 -- expose/api/docker/cron/start-cron.sh | 13 ---- expose/api/src/Matomo/PhraseanetClient.php | 70 ++++++++++++++----- expose/api/src/Storage/AssetManager.php | 4 ++ 8 files changed, 57 insertions(+), 47 deletions(-) delete mode 100644 expose/api/docker/cron/app-crontab delete mode 100755 expose/api/docker/cron/scripts/matomo-sync-phraseanet.sh delete mode 100755 expose/api/docker/cron/start-cron.sh diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index b312b17e6..136fb6ea8 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -59,10 +59,6 @@ services: - ./expose/api:/srv/app - ./expose/api/docker/supervisor:/etc/supervisor.d - expose-cron: - volumes: - - ./expose/api:/srv/app - uploader-api-php: environment: - XDEBUG_ENABLED diff --git a/docker-compose.yml b/docker-compose.yml index 4ba10a89d..14b0a3dec 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -533,11 +533,6 @@ services: extra_hosts: - zippy-svc.${PHRASEA_DOMAIN}:${PS_GATEWAY_IP} - expose-cron: - extends: - service: expose-worker - command: ['/srv/app/docker/cron/start-cron.sh'] - expose-api-nginx: profiles: - expose diff --git a/expose/api/Dockerfile b/expose/api/Dockerfile index c79648fbe..3e7b992b6 100644 --- a/expose/api/Dockerfile +++ b/expose/api/Dockerfile @@ -44,7 +44,4 @@ RUN apk add --no-cache \ COPY docker/supervisor/* /etc/supervisor.d/ -RUN touch /var/log/cron.log \ - && chmod 755 /srv/app/docker/cron/scripts/*.sh - CMD ["/bin/sh", "-c", "/usr/bin/supervisord -n"] diff --git a/expose/api/docker/cron/app-crontab b/expose/api/docker/cron/app-crontab deleted file mode 100644 index cc3be83d1..000000000 --- a/expose/api/docker/cron/app-crontab +++ /dev/null @@ -1 +0,0 @@ -30 4 * * * /srv/app/docker/cron/scripts/matomo-sync-phraseanet.sh 2>&1 diff --git a/expose/api/docker/cron/scripts/matomo-sync-phraseanet.sh b/expose/api/docker/cron/scripts/matomo-sync-phraseanet.sh deleted file mode 100755 index cd664828b..000000000 --- a/expose/api/docker/cron/scripts/matomo-sync-phraseanet.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/sh - -cd /srv/app \ - && bin/console app:matomo:sync-phraseanet diff --git a/expose/api/docker/cron/start-cron.sh b/expose/api/docker/cron/start-cron.sh deleted file mode 100755 index 70537f695..000000000 --- a/expose/api/docker/cron/start-cron.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/sh - -set -e - -/usr/bin/crontab /srv/app/docker/cron/app-crontab - -printenv | grep -v "no_proxy" >> /etc/environment - -for script in /srv/app/docker/cron/scripts/*.sh; do - "$script" -done - -/usr/sbin/crond -f -l 2 diff --git a/expose/api/src/Matomo/PhraseanetClient.php b/expose/api/src/Matomo/PhraseanetClient.php index e20c41064..5b685fafe 100644 --- a/expose/api/src/Matomo/PhraseanetClient.php +++ b/expose/api/src/Matomo/PhraseanetClient.php @@ -33,31 +33,67 @@ public function patchField(array $stat): void unset($stat['idsubdatatable']); try { - $res = $this->client->request('PATCH', sprintf('/api/v3/records/%s/%s/', $baseId, $recordId), [ + $data = $this->client->request('GET', sprintf('/api/v3/records/%s/%s/', $baseId, $recordId), [ 'headers' => [ 'Authorization' => 'OAuth '.$this->authToken, + 'Accept' => 'application/vnd.phraseanet.record-extended+json', ], - 'json' => [ - 'metadatas' => [ - [ - 'field_name' => 'MatomoMediaMetrics', - 'value' => \GuzzleHttp\json_encode($stat) - ] - ] - ] - ]); - $code = $res->getStatusCode(); - if (in_array($code, [200, 404], true)) { - return; - } - - throw new \Exception(sprintf('Got invalid HTTP response code %d: %s', $code, $res->getContent(false))); + ])->toArray(); } catch (HttpExceptionInterface $e) { - if (404 !== $e->getResponse()->getStatusCode()) { + if (404 === $e->getResponse()->getStatusCode()) { return; } throw $e; } + + $currentAttr = []; + foreach ($data['response']['metadata'] as $meta) { + if ('MatomoMediaMetrics' === $meta['name']) { + $currentAttr = json_decode($meta['value']['value'], true); + break; + } + } + + if (!shouldUpdate($currentAttr, $stat)) { + return; + } + + $res = $this->client->request('PATCH', sprintf('/api/v3/records/%s/%s/', $baseId, $recordId), [ + 'headers' => [ + 'Authorization' => 'OAuth '.$this->authToken, + ], + 'json' => [ + 'metadatas' => [ + [ + 'field_name' => 'MatomoMediaMetrics', + 'value' => \GuzzleHttp\json_encode($stat) + ] + ] + ] + ]); + + $code = $res->getStatusCode(); + if (200 !== $code) { + throw new \Exception(sprintf('Got invalid HTTP response code %d: %s', $code, $res->getContent(false))); + } + } +} + +function shouldUpdate(array $current, array $new): bool { + if (empty($current)) { + return true; } + + if (count($new) !== count($current)) { + return true; + } + + foreach ($new as $k => $v) { + if (!isset($current[$k]) || $current[$k] !== $v) { + return true; + } + } + + return false; } diff --git a/expose/api/src/Storage/AssetManager.php b/expose/api/src/Storage/AssetManager.php index ad8b894a2..1b8c6e5dc 100644 --- a/expose/api/src/Storage/AssetManager.php +++ b/expose/api/src/Storage/AssetManager.php @@ -48,6 +48,10 @@ public function createAsset( $asset->setSlug($options['slug']); } + if (isset($options['title'])) { + $asset->setTitle($options['title']); + } + if (isset($options['description'])) { $asset->setDescription($options['description']); }