diff --git a/Dockerfile b/Dockerfile index 78c4a1d..70944ea 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,51 +1,27 @@ -FROM php:8.5-cli +FROM uspdev/uspdev-php-apache:8.4 -RUN sed -i 's|main|main non-free|' /etc/apt/sources.list.d/debian.sources && apt-get update && apt-get install -y \ - unixodbc \ - unixodbc-dev \ - freetds-bin \ - freetds-dev \ - libicu-dev \ - git \ - unzip \ - libzip-dev \ - libpng-dev \ - libonig-dev \ - libxml2-dev \ - libjpeg-dev \ - libfreetype6-dev \ - libssl-dev \ - curl +RUN sed -i 's|/var/www/html|/var/www/html/public|' \ + /etc/apache2/sites-available/000-default.conf -RUN apt-get clean && rm -rf /var/lib/apt/lists/* - -COPY --from=composer:latest /usr/bin/composer /usr/bin/composer - -# php libs (Core) -RUN docker-php-ext-install \ - intl \ - pdo_mysql \ - soap \ - zip \ - mbstring \ - bcmath \ - pdo_dblib +# bibliotecas para mongo +RUN apt-get update && apt-get install -y \ + libssl-dev \ + pkg-config # Driver do MongoDB (Obrigatório para CLI também) RUN pecl install mongodb && docker-php-ext-enable mongodb -# gd -RUN docker-php-ext-configure gd --with-freetype --with-jpeg && \ - docker-php-ext-install gd - -# php memory +# php memory ENV PHP_MEMORY_LIMIT=2048M RUN echo "memory_limit=${PHP_MEMORY_LIMIT}" > "${PHP_INI_DIR}/conf.d/memory.ini" -# Setup do diretório de trabalho -WORKDIR /app +USER www-data + +COPY --chown=www-data . . -COPY . . -RUN composer install +RUN composer install \ + --no-dev \ + --optimize-autoloader \ + --no-interaction -CMD ["tail", "-f", "/dev/null"] \ No newline at end of file +CMD ["apache2-foreground"] \ No newline at end of file diff --git a/bin/make-collection.php b/bin/make-collection.php new file mode 100644 index 0000000..3368b0d --- /dev/null +++ b/bin/make-collection.php @@ -0,0 +1,37 @@ +sync(); -*/ + // Roda todas collections -$runner = new SyncRunner(); -$runner->run(); +/* $runner = new SyncRunner(); +$runner->run(); */ diff --git a/docker-compose.yml b/docker-compose.yml index 4afc674..07c3b01 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,13 +1,16 @@ services: replicado2mongodb: build: . + image: replicado2mongodb container_name: replicado2mongodb + ports: + - "8000:80" depends_on: - mongodb networks: - replicado2mongodb-network volumes: - - ./:/app + - ./:/var/www/html environment: HOME: /tmp user: "${UID:-1000}:${GID:-1000}" @@ -25,7 +28,7 @@ services: - mongodb_data:/data/db networks: - replicado2mongodb-network - + mongo-express: image: mongo-express:latest container_name: replicado2mongodb_mongo_express diff --git a/resources/queries/listarDisciplinasDocentes.sql b/resources/queries/listarDisciplinasDocentes.sql new file mode 100644 index 0000000..de0fb68 --- /dev/null +++ b/resources/queries/listarDisciplinasDocentes.sql @@ -0,0 +1,11 @@ +SELECT DISTINCT + S.nomset, --AS NomeDepartamento, + V.tipmer, --AS MeritoDocente, + M.codpes, --AS NUSP, + V.nompes, --AS NomeDocente, + M.coddis, --AS Disciplina, + M.codtur --AS Turma +FROM MINISTRANTE M +INNER JOIN VINCULOPESSOAUSP V ON V.codpes = M.codpes +INNER JOIN SETOR S ON S.codset = V.codset +WHERE V.codfusclgund IN (__unidades__) \ No newline at end of file diff --git a/resources/queries/listarDocentes.sql b/resources/queries/listarDocentes.sql new file mode 100644 index 0000000..41744d7 --- /dev/null +++ b/resources/queries/listarDocentes.sql @@ -0,0 +1,7 @@ +SELECT DISTINCT +V.codpes, V.nompes, S.nomset, V.codset, S.nomabvset, V.tipmer, V.nomabvcla, V.nomabvfnc, V.sitatl, V.sitoco, V.dtafimvin, V.dtafimdctati +FROM VINCULOPESSOAUSP AS V +INNER JOIN SETOR AS S ON S.codset = V.codset +WHERE V.tipvin = 'SERVIDOR' + AND V.nomcaa = 'Docente' + AND V.codfusclgund IN (__unidades__) \ No newline at end of file diff --git a/resources/queries/listarEstagiarios.sql b/resources/queries/listarEstagiarios.sql new file mode 100644 index 0000000..f89a585 --- /dev/null +++ b/resources/queries/listarEstagiarios.sql @@ -0,0 +1,5 @@ +SELECT V.codpes, V.nompes, S.nomset, V.dtainivin, V.dtafimvin + FROM VINCULOPESSOAUSP V + INNER JOIN SETOR S ON V.codset = S.codset + WHERE V.tipvin = 'ESTAGIARIORH' + AND V.codfusclgund IN (__unidades__) \ No newline at end of file diff --git a/resources/queries/listarIntercambistasRecebidos.sql b/resources/queries/listarIntercambistasRecebidos.sql new file mode 100644 index 0000000..052eba7 --- /dev/null +++ b/resources/queries/listarIntercambistasRecebidos.sql @@ -0,0 +1,4 @@ +SELECT DISTINCT V.nompes, V.codpes, V.dtainivin, V.dtafimvin, V.tipvin + FROM VINCULOPESSOAUSP AS V + WHERE V.tipvin IN ('ALUNOICD', 'ALUNOCONVENIOINT') + AND V.codfusclgund IN (__unidades__) \ No newline at end of file diff --git a/resources/queries/listarPosGrad.sql b/resources/queries/listarPosGrad.sql new file mode 100644 index 0000000..84cbe27 --- /dev/null +++ b/resources/queries/listarPosGrad.sql @@ -0,0 +1,10 @@ +SELECT DISTINCT + V.codpes, --AS NUSP, + L.codema, --AS Email, + L.nompes, --AS Nome, + V.codare + FROM VINCULOPESSOAUSP AS V + INNER JOIN LOCALIZAPESSOA as L ON (V.codpes = L.codpes) + WHERE V.tipvin = 'ALUNOPOS' + AND V.sitatl = 'A' + AND V.codfusclgund IN (__unidades__) \ No newline at end of file diff --git a/src/Collections/disciplinasdocentesCollection.php b/src/Collections/disciplinasdocentesCollection.php new file mode 100644 index 0000000..53035c3 --- /dev/null +++ b/src/Collections/disciplinasdocentesCollection.php @@ -0,0 +1,59 @@ +getQuery('listarDisciplinasDocentes.sql', + [ + '__unidades__' => env('REPLICADO_CODUNDCLG') + ] + ); + $disciplinasdocentes = ReplicadoDB::fetchAll($query); + $qtd = count($disciplinasdocentes); + + // Pegar dados do replicado + $now = new UTCDateTime(); + foreach ($disciplinasdocentes as $registro) { + $bulk[] = [ + 'updateOne' => [ + ['nusp_docente' => $registro['codpes'], + 'disciplina' => $registro['coddis'], + 'turma' => $registro['codtur'] + ], + [ + '$set' => [ + 'departamento' => $registro['nomset'], + 'merito_docente' => $registro['tipmer'], + 'nusp_docente' => $registro['codpes'], + 'nome_docente' => $registro['nompes'], + 'disciplina' => $registro['coddis'], + 'turma' => $registro['codtur'], + 'updated_at_sync' => $now + ] + ], + ['upsert' => true] + ] + ]; + } + + $collection = MongoConnection::getCollection('disciplinasdocentes'); + if (!empty($bulk)) { + $collection->bulkWrite($bulk); + } + + // delete antigos + $collection->deleteMany([ + 'updated_at_sync' => ['$lt' => $now] + ]); + } +} \ No newline at end of file diff --git a/src/Collections/docentesCollection.php b/src/Collections/docentesCollection.php new file mode 100644 index 0000000..28ada47 --- /dev/null +++ b/src/Collections/docentesCollection.php @@ -0,0 +1,62 @@ +getQuery('listarDocentes.sql', + [ + '__unidades__' => env('REPLICADO_CODUNDCLG') + ] + ); + + $docentes = ReplicadoDB::fetchAll($query); + + // Pegar dados do replicado + $now = new UTCDateTime(); + foreach ($docentes as $registro) { + $bulk[] = [ + 'updateOne' => [ + ['codpes' => $registro['codpes']], + [ + '$set' => [ + 'codpes' => $registro['codpes'], + 'nome_docente' => $registro['nompes'], + 'nome_setor' => $registro['nomset'], + 'cod_setor' => $registro['codset'], + 'clg_setor' => $registro['nomabvset'], + 'merito' => $registro['tipmer'], + 'classe' => $registro['nomabvcla'], + 'funcao' => $registro['nomabvfnc'], + 'status' => $registro['sitatl'], + 'ultima_ocorrencia' => $registro['sitoco'], + 'fim_vinculo' => explode(' ',$registro['dtafimvin'] ?? '')[0], + 'fim_atividade' => explode(' ',$registro['dtafimdctati'] ?? '')[0], + 'updated_at_sync' => $now + ] + ], + ['upsert' => true] + ] + ]; + } + + $collection = MongoConnection::getCollection('docentes'); + if (!empty($bulk)) { + $collection->bulkWrite($bulk); + } + + // delete antigos + $collection->deleteMany([ + 'updated_at_sync' => ['$lt' => $now] + ]); + } +} \ No newline at end of file diff --git a/src/Collections/estagiariosCollection.php b/src/Collections/estagiariosCollection.php new file mode 100644 index 0000000..09b2c9e --- /dev/null +++ b/src/Collections/estagiariosCollection.php @@ -0,0 +1,54 @@ +getQuery('listarEstagiarios.sql', + [ + '__unidades__' => env('REPLICADO_CODUNDCLG') + ] + ); + $estagiarios = ReplicadoDB::fetchAll($query); + + // Pegar dados do replicado + $now = new UTCDateTime(); + foreach ($estagiarios as $registro) { + $bulk[] = [ + 'updateOne' => [ + ['codpes' => $registro['codpes']], + [ + '$set' => [ + 'codpes'=> $registro['codpes'], + 'nome' => $registro['nompes'], + 'setor' => $registro['nomset'], + 'inicio'=> explode(' ', $registro['dtainivin'])[0], + 'fim' => explode(' ', $registro['dtafimvin'])[0], + 'updated_at_sync' => $now + ] + ], + ['upsert' => true] + ] + ]; + } + + $collection = MongoConnection::getCollection('estagiarios'); + if (!empty($bulk)) { + $collection->bulkWrite($bulk); + } + + // delete antigos + $collection->deleteMany([ + 'updated_at_sync' => ['$lt' => $now] + ]); + } +} \ No newline at end of file diff --git a/src/Collections/intercambistasrecebidosCollection.php b/src/Collections/intercambistasrecebidosCollection.php new file mode 100644 index 0000000..301129c --- /dev/null +++ b/src/Collections/intercambistasrecebidosCollection.php @@ -0,0 +1,55 @@ +getQuery('listarIntercambistasRecebidos.sql', + [ + '__unidades__' => env('REPLICADO_CODUNDCLG') + ] + ); + + $intercambistasrecebidos = ReplicadoDB::fetchAll($query); + + // Pegar dados do replicado + $now = new UTCDateTime(); + foreach ($intercambistasrecebidos as $registro) { + $bulk[] = [ + 'updateOne' => [ + ['codpes' => $registro['codpes']], + [ + '$set' => [ + 'codpes' => $registro['codpes'], + 'nome' => $registro['nompes'], + 'tipo_vinculo' => $registro['tipvin'], + 'inicio' => explode(' ', $registro['dtainivin'] ?? '')[0], + 'fim' => explode(' ', $registro['dtafimvin'] ?? '')[0], + 'updated_at_sync' => $now + ] + ], + ['upsert' => true] + ] + ]; + } + + $collection = MongoConnection::getCollection('intercambistasrecebidos'); + if (!empty($bulk)) { + $collection->bulkWrite($bulk); + } + + // delete antigos + $collection->deleteMany([ + 'updated_at_sync' => ['$lt' => $now] + ]); + } +} \ No newline at end of file diff --git a/src/Collections/posgradCollection.php b/src/Collections/posgradCollection.php new file mode 100644 index 0000000..43b522e --- /dev/null +++ b/src/Collections/posgradCollection.php @@ -0,0 +1,54 @@ +getQuery('listarPosGrad.sql', + [ + '__unidades__' => env('REPLICADO_CODUNDCLG') + ] + ); + + $posgrad = ReplicadoDB::fetchAll($query); + + // Pegar dados do replicado + $now = new UTCDateTime(); + foreach ($posgrad as $registro) { + $bulk[] = [ + 'updateOne' => [ + ['codpes' => $registro['codpes']], + [ + '$set' => [ + 'codpes' => $registro['codpes'], + 'email' => $registro['codema'], + 'nome' => $registro['nompes'], + 'cod_area' => $registro['codare'], + 'updated_at_sync' => $now + ] + ], + ['upsert' => true] + ] + ]; + } + + $collection = MongoConnection::getCollection('posgrad'); + if (!empty($bulk)) { + $collection->bulkWrite($bulk); + } + + // delete antigos + $collection->deleteMany([ + 'updated_at_sync' => ['$lt' => $now] + ]); + } +} \ No newline at end of file diff --git a/src/Database/MongoConnection.php b/src/Database/MongoConnection.php index e8167fc..fb6b348 100644 --- a/src/Database/MongoConnection.php +++ b/src/Database/MongoConnection.php @@ -15,9 +15,12 @@ public static function getClient(): Client $port = env('REPLICADO2MONGODB_PORT', 27017); $user = env('REPLICADO2MONGODB_DB', 'root'); $pass = env('REPLICADO2MONGODB_PASS', 'replicado2mongodb'); + + $uriOptions = ['socketTimeoutMS' => 900000]; self::$client = new Client( - "mongodb://$user:$pass@$host:$port" + "mongodb://$user:$pass@$host:$port", + $uriOptions ); } diff --git a/stubs/collection.stub b/stubs/collection.stub new file mode 100644 index 0000000..455bba0 --- /dev/null +++ b/stubs/collection.stub @@ -0,0 +1,49 @@ +getQuery('listar{{ name_capitalize }}.sql'); + $query = str_replace('__unidades__', env('REPLICADO_CODUNDCLG'), $query); + + ${{ name_lower }} = ReplicadoDB::fetchAll($query); + + // Pegar dados do replicado + $now = new UTCDateTime(); + foreach (${{ name_lower }} as $registro) { + $bulk[] = [ + 'updateOne' => [ + ['codcur' => $registro['codcur']], + [ + '$set' => [ + 'codcur' => $registro['codcur'], + 'nomcur' => $registro['nomcur'], + 'updated_at_sync' => $now + ] + ], + ['upsert' => true] + ] + ]; + } + + $collection = MongoConnection::getCollection('{{ name_lower }}'); + if (!empty($bulk)) { + $collection->bulkWrite($bulk); + } + + // delete antigos + $collection->deleteMany([ + 'updated_at_sync' => ['$lt' => $now] + ]); + } +} \ No newline at end of file