varias alteracoes
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Contracts\Console\Kernel;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
require __DIR__.'/../vendor/autoload.php';
|
||||
$app = require __DIR__.'/../bootstrap/app.php';
|
||||
$app->make(Kernel::class)->bootstrap();
|
||||
if (! $app->environment('local', 'testing') || DB::connection()->getDriverName() !== 'pgsql') {
|
||||
throw new RuntimeException('Local/testing PostgreSQL required.');
|
||||
}
|
||||
$path = storage_path('app/private/territory-workbook-candidates.json');
|
||||
$data = json_decode(file_get_contents($path), true, 512, JSON_THROW_ON_ERROR);
|
||||
$expected = '964a187ad32ea553b9e112cd159caa90b900050c42b7329594a75bcd798a96ff';
|
||||
if ($data['checksum'] !== $expected || hash_file('sha256', 'C:/Mac/Home/Documents/eBAU/cartografia_moz.xlsx') !== $expected) {
|
||||
throw new RuntimeException('Authorised source checksum mismatch.');
|
||||
}
|
||||
$created = DB::transaction(function () use ($data, $expected) {
|
||||
DB::select('SELECT pg_advisory_xact_lock(280926, 150000)');
|
||||
$created = 0;
|
||||
foreach ($data['nodes'] as $node) {
|
||||
$existing = DB::table('geo_locations')->where('id', $node['id'])->first();
|
||||
if ($existing) {
|
||||
foreach (['name', 'parent_id', 'location_type', 'code'] as $field) {
|
||||
if ($node[$field] !== $existing->$field) {
|
||||
throw new RuntimeException('Existing territory differs: '.$node['id']);
|
||||
}
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
if ($node['parent_id'] && ! DB::table('geo_locations')->where('id', $node['parent_id'])->exists()) {
|
||||
throw new RuntimeException('Missing parent.');
|
||||
}
|
||||
DB::table('geo_locations')->insert([...$node, 'source_system' => 'cartografia-workbook', 'external_id' => $expected.':'.$node['id'], 'data_classification' => 'internal']);
|
||||
$created++;
|
||||
}
|
||||
if ($created) {
|
||||
DB::table('audit_events')->insert(['source_system' => 'cartografia-workbook', 'entity_type' => 'geo_locations', 'action' => 'owner_authorised_reference_import',
|
||||
'after_data' => json_encode(['checksum' => $expected, 'created' => $created, 'rejected_rows' => $data['rejected_rows'],
|
||||
'approval_basis' => 'workspace_user_instructions', 'codes' => 'internal_identifiers_not_official_ine', 'classification' => 'internal'], JSON_THROW_ON_ERROR)]);
|
||||
}
|
||||
|
||||
return $created;
|
||||
});
|
||||
echo json_encode(['created' => $created, 'rejected_rows' => count($data['rejected_rows']), 'scope' => 'internal_management_dropdowns'], JSON_THROW_ON_ERROR).PHP_EOL;
|
||||
@@ -0,0 +1,29 @@
|
||||
// Synthetic QA only; tile traffic is blocked to avoid automated OSM downloads.
|
||||
const target = await (await fetch('http://127.0.0.1:9224/json/new?about:blank', { method: 'PUT' })).json();
|
||||
const ws = new WebSocket(target.webSocketDebuggerUrl);
|
||||
await new Promise(resolve => ws.addEventListener('open', resolve, { once: true }));
|
||||
let sequence = 0;
|
||||
const pending = new Map();
|
||||
const errors = [];
|
||||
ws.onmessage = event => { const message = JSON.parse(event.data); if (message.method === 'Runtime.exceptionThrown') errors.push(message.params.exceptionDetails.text); if (message.id) { const callback = pending.get(message.id); pending.delete(message.id); message.error ? callback.reject(message.error) : callback.resolve(message.result); } };
|
||||
const send = (method, params = {}) => new Promise((resolve, reject) => { const id = ++sequence; pending.set(id, { resolve, reject }); ws.send(JSON.stringify({ id, method, params })); });
|
||||
const evaluate = async expression => { const response = await send('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true }); if (response.exceptionDetails) throw Error(JSON.stringify(response.exceptionDetails)); return response.result?.value; };
|
||||
const wait = async expression => { for (let i = 0; i < 150; i++) { if (await evaluate(expression)) return; await new Promise(resolve => setTimeout(resolve, 100)); } throw Error('Timed out: '+expression); };
|
||||
try {
|
||||
await send('Runtime.enable'); await send('Page.enable'); await send('Network.enable');
|
||||
await send('Network.setBlockedURLs', { urls: ['*://tile.openstreetmap.org/*'] });
|
||||
await send('Page.navigate', { url: 'http://127.0.0.1:18027/oportunidades' });
|
||||
await wait(`document.querySelectorAll('.leaflet-marker-icon').length===1`);
|
||||
await evaluate(`document.querySelector('.leaflet-marker-icon').click()`);
|
||||
await wait(`document.querySelector('.leaflet-popup-content')?.textContent.includes('Synthetic map marker')`);
|
||||
if (!await evaluate(`document.querySelector('.leaflet-popup-content a')?.getAttribute('href')==='/oportunidades/synthetic-map-browser'`)) throw Error('Missing detail link');
|
||||
await evaluate(`document.querySelector('.leaflet-control-zoom-in').click()`);
|
||||
const layouts = [];
|
||||
for (const width of [390, 1440]) { await send('Emulation.setDeviceMetricsOverride', { width, height: 900, deviceScaleFactor: 1, mobile: width < 600 }); layouts.push({ width, noOverflow: await evaluate('document.documentElement.scrollWidth<=innerWidth+1') }); }
|
||||
await evaluate(`window.__mapSentinel=1;document.querySelector('main form input').focus()`);
|
||||
await send('Input.insertText', { text: 'NoMatchingSyntheticInvestment' });
|
||||
await wait(`location.search.includes('NoMatchingSyntheticInvestment') && document.querySelectorAll('.leaflet-marker-icon').length===0`);
|
||||
if (!await evaluate(`window.__mapSentinel===1`)) throw Error('Full page reload detected');
|
||||
if (errors.length || layouts.some(l=>!l.noOverflow)) throw Error('Browser validation failed');
|
||||
console.log(JSON.stringify({ marker: true, popup: true, detail_link: true, zoom_control: true, automatic_filter: true, no_full_reload: true, layouts, javascript_errors: errors.length, tiles_blocked_for_test: true }));
|
||||
} finally { ws.close(); await fetch(`http://127.0.0.1:9224/json/close/${target.id}`); }
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
use App\Services\InvestmentCatalogue;
|
||||
use Illuminate\Contracts\Console\Kernel;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
require __DIR__.'/../vendor/autoload.php';
|
||||
$app = require __DIR__.'/../bootstrap/app.php';
|
||||
$app->make(Kernel::class)->bootstrap();
|
||||
if (! $app->environment('local', 'testing') || DB::connection()->getDriverName() !== 'pgsql') {
|
||||
throw new RuntimeException('Local/testing PostgreSQL required.');
|
||||
}
|
||||
$report = DB::transaction(function () {
|
||||
DB::statement('SET TRANSACTION ISOLATION LEVEL REPEATABLE READ, READ ONLY');
|
||||
$map = app(InvestmentCatalogue::class)->map([]);
|
||||
|
||||
return ['generated_at' => date(DATE_ATOM), 'published_opportunities' => $map['matched_count'],
|
||||
'georeferenced' => $map['georeferenced_count'], 'complete' => $map['complete'], 'truncated' => $map['truncated'],
|
||||
'unlocated' => $map['unlocated'], 'geo_geometries' => DB::table('geo_geometries')->whereNull('deleted_at')->count(),
|
||||
'zone_geometries' => DB::table('investment_zone_geometries')->count()];
|
||||
});
|
||||
echo json_encode($report, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR).PHP_EOL;
|
||||
@@ -0,0 +1,69 @@
|
||||
"""B:G contain codes. Designacao names only the deepest populated level."""
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import uuid
|
||||
import xml.etree.ElementTree as ET
|
||||
import zipfile
|
||||
|
||||
source = Path('C:/Mac/Home/Documents/eBAU/cartografia_moz.xlsx')
|
||||
checksum = hashlib.sha256(source.read_bytes()).hexdigest()
|
||||
if checksum != '964a187ad32ea553b9e112cd159caa90b900050c42b7329594a75bcd798a96ff':
|
||||
raise ValueError('Authorised workbook changed')
|
||||
ns = {'s': 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'}
|
||||
country = str(uuid.uuid5(uuid.NAMESPACE_URL, 'investmoz:workbook:MZ'))
|
||||
nodes = {(): dict(id=country, parent_id=None, name='Moçambique', location_type='country', code='508')}
|
||||
definitions = {}
|
||||
conflicts = set()
|
||||
rejected = []
|
||||
ignored = 0
|
||||
with zipfile.ZipFile(source) as archive:
|
||||
strings = [''.join(si.itertext()) for si in ET.fromstring(archive.read('xl/sharedStrings.xml')).findall('s:si', ns)]
|
||||
sheet = ET.fromstring(archive.read('xl/worksheets/sheet1.xml'))
|
||||
for row in sheet.findall('s:sheetData/s:row', ns)[1:]:
|
||||
cells = {}
|
||||
invalid = False
|
||||
for cell in row:
|
||||
column = ''.join(c for c in cell.attrib['r'] if c.isalpha())
|
||||
value = cell.findtext('s:v', '', ns)
|
||||
if cell.attrib.get('t') == 's':
|
||||
value = strings[int(value)]
|
||||
elif cell.attrib.get('t') == 'inlineStr':
|
||||
value = ''.join(cell.find('s:is', ns).itertext())
|
||||
if cell.find('s:f', ns) is not None or cell.attrib.get('t') == 'e':
|
||||
invalid = True
|
||||
cells[column] = ' '.join(value.split())
|
||||
if cells.get('F') or cells.get('G'):
|
||||
ignored += 1
|
||||
continue
|
||||
segments = [cells.get(column, '') for column in 'BCDE']
|
||||
while segments and not segments[-1]:
|
||||
segments.pop()
|
||||
if invalid or not segments or '' in segments or not cells.get('H'):
|
||||
rejected.append({'row': row.attrib['r'], 'reason': 'missing_code_path_or_designation'})
|
||||
continue
|
||||
key = tuple(segments)
|
||||
name = cells['H']
|
||||
if key in conflicts:
|
||||
rejected.append({'row': row.attrib['r'], 'reason': 'conflicting_designation'})
|
||||
continue
|
||||
if key in definitions and definitions[key]['name'] != name:
|
||||
rejected.extend([{'row': definitions[key]['row'], 'reason': 'conflicting_designation'}, {'row': row.attrib['r'], 'reason': 'conflicting_designation'}])
|
||||
conflicts.add(key)
|
||||
del definitions[key]
|
||||
continue
|
||||
definitions[key] = {'name': name, 'row': row.attrib['r']}
|
||||
for key, definition in sorted(definitions.items(), key=lambda item: (len(item[0]), item[0])):
|
||||
if key[:-1] not in nodes:
|
||||
rejected.append({'row': definition['row'], 'reason': 'missing_parent_definition'})
|
||||
continue
|
||||
parent = nodes[key[:-1]]['id']
|
||||
kind = ['province', 'district', 'admin_post', 'locality'][len(key)-1]
|
||||
identifier = str(uuid.uuid5(uuid.NAMESPACE_URL, parent + ':' + kind + ':' + key[-1].casefold()))
|
||||
nodes[key] = dict(id=identifier, parent_id=parent, name=definition['name'], location_type=kind, code='XLSX-' + identifier)
|
||||
output = Path('storage/app/private/territory-workbook-candidates.json')
|
||||
previous = output.with_name('territory-workbook-candidates-before-designation-fix.json')
|
||||
if output.exists() and not previous.exists():
|
||||
previous.write_bytes(output.read_bytes())
|
||||
output.write_text(json.dumps(dict(checksum=checksum, mapping_version=2, nodes=list(nodes.values()), rejected_rows=rejected, ignored_sub_locality_rows=ignored), ensure_ascii=False), encoding='utf-8')
|
||||
print(json.dumps(dict(counts={kind: sum(n['location_type'] == kind for n in nodes.values()) for kind in ['country','province','district','admin_post','locality']}, rejected_rows=len(rejected), ignored_sub_locality_rows=ignored)))
|
||||
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Contracts\Console\Kernel;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
require __DIR__.'/../vendor/autoload.php';
|
||||
$app = require __DIR__.'/../bootstrap/app.php';
|
||||
$app->make(Kernel::class)->bootstrap();
|
||||
if (! $app->environment('local') || DB::connection()->getDriverName() !== 'pgsql') {
|
||||
throw new RuntimeException('Local PostgreSQL only.');
|
||||
}
|
||||
$checksum = '964a187ad32ea553b9e112cd159caa90b900050c42b7329594a75bcd798a96ff';
|
||||
if (hash_file('sha256', 'C:/Mac/Home/Documents/eBAU/cartografia_moz.xlsx') !== $checksum) {
|
||||
throw new RuntimeException('Source changed.');
|
||||
}
|
||||
$data = json_decode(file_get_contents(storage_path('app/private/territory-workbook-candidates.json')), true, 512, JSON_THROW_ON_ERROR);
|
||||
$result = DB::transaction(function () use ($data, $checksum) {
|
||||
DB::select('SELECT pg_advisory_xact_lock(280926, 150000)');
|
||||
$ids = [];
|
||||
foreach ($data['nodes'] as $node) {
|
||||
$record = DB::table('geo_locations')->where('id', $node['id'])->where('source_system', 'cartografia-workbook')->where('external_id', $checksum.':'.$node['id'])->whereNull('deleted_at')->first();
|
||||
if (! $record || ! in_array($record->data_classification, ['internal', 'public'], true)) {
|
||||
throw new RuntimeException('Reference unavailable or reclassified.');
|
||||
}
|
||||
foreach (['name', 'parent_id', 'location_type', 'code'] as $field) {
|
||||
if ($node[$field] !== $record->$field) {
|
||||
throw new RuntimeException('Reference changed since import.');
|
||||
}
|
||||
}
|
||||
$ids[] = $node['id'];
|
||||
}
|
||||
$published = DB::table('geo_locations')->whereIn('id', $ids)->where('data_classification', 'internal')->update(['data_classification' => 'public']);
|
||||
$matched = $unmatched = [];
|
||||
foreach (['opportunities', 'investment_zones'] as $table) {
|
||||
foreach (DB::table($table)->whereNull('deleted_at')->whereNull('primary_territory_id')->whereNotNull('province')->get() as $record) {
|
||||
$normalise = fn ($value) => mb_strtolower(Str::ascii(trim((string) $value)));
|
||||
$province = DB::table('geo_locations')->whereIn('id', $ids)->where('location_type', 'province')->get()->filter(fn ($node) => $normalise($node->name) === $normalise($record->province))->values();
|
||||
if ($province->count() !== 1) {
|
||||
$unmatched[] = ['table' => $table, 'id' => $record->id, 'reason' => 'province_not_unique'];
|
||||
|
||||
continue;
|
||||
}
|
||||
$location = $province->first();
|
||||
if (trim((string) $record->district) !== '') {
|
||||
$district = DB::table('geo_locations')->whereIn('id', $ids)->where('location_type', 'district')->where('parent_id', $location->id)->get()->filter(fn ($node) => $normalise($node->name) === $normalise($record->district))->values();
|
||||
if ($district->count() !== 1) {
|
||||
$unmatched[] = ['table' => $table, 'id' => $record->id, 'reason' => 'district_not_unique'];
|
||||
|
||||
continue;
|
||||
}
|
||||
$location = $district->first();
|
||||
}
|
||||
DB::table($table)->where('id', $record->id)->update(['primary_territory_id' => $location->id, 'territory_version' => $record->territory_version + 1]);
|
||||
$matched[] = ['table' => $table, 'id' => $record->id, 'territory_id' => $location->id];
|
||||
}
|
||||
}
|
||||
$result = ['published_reference_names' => $published, 'matched' => $matched, 'unmatched' => $unmatched,
|
||||
'basis' => 'owner_authorised_public_dropdowns', 'checksum' => $checksum, 'official_territory_publication' => false];
|
||||
if ($published || $matched !== []) {
|
||||
DB::table('audit_events')->insert(['source_system' => 'territory-reference-publication', 'entity_type' => 'geo_locations', 'action' => 'reference_names_enabled', 'after_data' => json_encode($result, JSON_THROW_ON_ERROR)]);
|
||||
}
|
||||
|
||||
return $result;
|
||||
});
|
||||
file_put_contents(storage_path('app/private/territory-publication-'.date('Ymd-His').'.json'), json_encode($result, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR));
|
||||
echo json_encode(['reference_names_enabled' => $result['published_reference_names'], 'exact_associations' => count($result['matched']), 'unmatched' => count($result['unmatched'])], JSON_THROW_ON_ERROR).PHP_EOL;
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Contracts\Console\Kernel;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
require __DIR__.'/../vendor/autoload.php';
|
||||
$app = require __DIR__.'/../bootstrap/app.php';
|
||||
$app->make(Kernel::class)->bootstrap();
|
||||
if (! $app->environment('local') || DB::connection()->getDriverName() !== 'pgsql') {
|
||||
throw new RuntimeException('This recorded instruction applies to the local PostgreSQL installation only.');
|
||||
}
|
||||
|
||||
// Specific owner instruction received in this workspace; never impersonate an application user.
|
||||
$directory = storage_path('app/private/geo-ine-review-20260928-v2');
|
||||
$expected = [
|
||||
'original.pdf' => '5c69b6f189a1d3f5307e0cf7dd8224554705158a682637d3105afad615db26e2',
|
||||
'layout.txt' => '783a7886ed5a27b8f9d0063a787928e9a4b09723c28a260c019c2de215a670a3',
|
||||
];
|
||||
foreach ($expected as $file => $hash) {
|
||||
if (! is_file($directory.'/'.$file) || hash_file('sha256', $directory.'/'.$file) !== $hash) {
|
||||
throw new RuntimeException('Source differs from the reviewed package: '.$file);
|
||||
}
|
||||
}
|
||||
$receipt = [
|
||||
'schema_version' => 1,
|
||||
'decision_date' => '2026-09-28',
|
||||
'decision' => 'accepted_by_owner',
|
||||
'validation' => 'declared_validated_by_owner',
|
||||
'authenticity' => 'declared_authenticated_by_owner',
|
||||
'approval_basis' => 'explicit_workspace_user_instruction',
|
||||
'instruction' => 'aplicar os resultados e aceite os dados como validados e autenticados',
|
||||
'source_checksums' => $expected,
|
||||
'scope' => 'INE PDF and extraction package geo-ine-review-20260928-v2',
|
||||
'independent_authentication_performed' => false,
|
||||
'technical_mapping_status' => 'unresolved_column_alignment',
|
||||
'territories_imported_by_this_decision' => 0,
|
||||
];
|
||||
$json = json_encode($receipt, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR).PHP_EOL;
|
||||
$hash = hash('sha256', $json);
|
||||
$path = $directory.'/owner-acceptance-'.$hash.'.json';
|
||||
if (is_file($path)) {
|
||||
if (file_get_contents($path) !== $json) {
|
||||
throw new RuntimeException('Existing acceptance receipt differs.');
|
||||
}
|
||||
} else {
|
||||
// Exclusive creation preserves any existing evidence.
|
||||
$stream = fopen($path, 'x');
|
||||
if ($stream === false) {
|
||||
throw new RuntimeException('Could not create acceptance receipt.');
|
||||
}
|
||||
try {
|
||||
if (fwrite($stream, $json) !== strlen($json)) {
|
||||
throw new RuntimeException('Incomplete acceptance receipt.');
|
||||
}
|
||||
} finally {
|
||||
fclose($stream);
|
||||
}
|
||||
}
|
||||
$created = DB::transaction(function () use ($receipt, $hash): bool {
|
||||
DB::select('SELECT pg_advisory_xact_lock(280926, 150001)');
|
||||
if (DB::table('audit_events')->where('source_system', 'territorial-owner-acceptance')->where('external_id', $hash)->exists()) {
|
||||
return false;
|
||||
}
|
||||
DB::table('audit_events')->insert([
|
||||
'source_system' => 'territorial-owner-acceptance', 'external_id' => $hash,
|
||||
'entity_type' => 'territorial_source_package', 'action' => 'owner_accepted',
|
||||
'after_data' => json_encode($receipt, JSON_THROW_ON_ERROR),
|
||||
]);
|
||||
|
||||
return true;
|
||||
});
|
||||
echo json_encode(['status' => 'accepted_by_owner', 'audit_created' => $created,
|
||||
'receipt' => $path, 'territories_imported' => 0], JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR).PHP_EOL;
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Contracts\Console\Kernel;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
require __DIR__.'/../vendor/autoload.php';
|
||||
$app = require __DIR__.'/../bootstrap/app.php';
|
||||
$app->make(Kernel::class)->bootstrap();
|
||||
if (! $app->environment('local') || DB::connection()->getDriverName() !== 'pgsql') {
|
||||
throw new RuntimeException('Local PostgreSQL only.');
|
||||
}
|
||||
$root = storage_path('app/private/');
|
||||
$before = json_decode(file_get_contents($root.'territory-workbook-candidates-before-designation-fix.json'), true, 512, JSON_THROW_ON_ERROR);
|
||||
$after = json_decode(file_get_contents($root.'territory-workbook-candidates.json'), true, 512, JSON_THROW_ON_ERROR);
|
||||
if (($after['mapping_version'] ?? 0) !== 2 || $before['checksum'] !== $after['checksum'] || hash_file('sha256', 'C:/Mac/Home/Documents/eBAU/cartografia_moz.xlsx') !== $after['checksum']) {
|
||||
throw new RuntimeException('Source or mapping mismatch.');
|
||||
}
|
||||
$old = array_column($before['nodes'], null, 'id');
|
||||
$new = array_column($after['nodes'], null, 'id');
|
||||
$result = DB::transaction(function () use ($old, $new, $after) {
|
||||
DB::select('SELECT pg_advisory_xact_lock(280926, 150000)');
|
||||
$renamed = $quarantined = 0;
|
||||
foreach ($old as $id => $prior) {
|
||||
$record = DB::table('geo_locations')->where('id', $id)->where('source_system', 'cartografia-workbook')->where('external_id', $after['checksum'].':'.$id)->lockForUpdate()->first();
|
||||
if (! $record || $record->parent_id !== $prior['parent_id'] || $record->code !== $prior['code'] || $record->location_type !== $prior['location_type']) {
|
||||
throw new RuntimeException('Existing reference changed: '.$id);
|
||||
}
|
||||
$desired = $new[$id] ?? null;
|
||||
if (! $desired) {
|
||||
if ($record->deleted_at === null) {
|
||||
DB::table('geo_locations')->where('id', $id)->update(['data_classification' => 'internal', 'deleted_at' => now(), 'version_no' => $record->version_no + 1]);
|
||||
$quarantined++;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
if ($record->name === $desired['name']) {
|
||||
continue;
|
||||
}
|
||||
if ($record->name !== $prior['name'] || $record->deleted_at !== null) {
|
||||
throw new RuntimeException('Manual edit must not be overwritten: '.$id);
|
||||
}
|
||||
DB::table('geo_locations')->where('id', $id)->update(['name' => $desired['name'], 'version_no' => $record->version_no + 1]);
|
||||
$renamed++;
|
||||
}
|
||||
foreach ($new as $id => $node) {
|
||||
if (! isset($old[$id])) {
|
||||
throw new RuntimeException('Unexpected identity change: '.$id);
|
||||
}
|
||||
}
|
||||
$result = ['renamed' => $renamed, 'quarantined' => $quarantined, 'mapping_version' => 2, 'checksum' => $after['checksum']];
|
||||
if ($renamed || $quarantined) {
|
||||
DB::table('audit_events')->insert(['source_system' => 'cartografia-workbook', 'entity_type' => 'geo_locations', 'action' => 'designation_mapping_corrected', 'after_data' => json_encode($result, JSON_THROW_ON_ERROR)]);
|
||||
}
|
||||
|
||||
return $result;
|
||||
});
|
||||
echo json_encode($result, JSON_THROW_ON_ERROR).PHP_EOL;
|
||||
@@ -0,0 +1,79 @@
|
||||
"""Preserve an INE PDF and prepare a review package; never guess table joins.
|
||||
|
||||
The 2012 INE PDF contains independently wrapped columns. A line-based merge
|
||||
would associate valid codes with the wrong district. This tool deliberately
|
||||
exports the extracted lines for review instead of manufacturing such joins.
|
||||
"""
|
||||
import argparse
|
||||
import csv
|
||||
import hashlib
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
|
||||
def prepare(source, destination, executable, source_url, source_version):
|
||||
source = Path(source).resolve(strict=True)
|
||||
destination = Path(destination).resolve()
|
||||
original = source.read_bytes()
|
||||
if len(original) > 20 * 1024 * 1024 or not original.startswith(b"%PDF-"):
|
||||
raise ValueError("Expected a PDF of at most 20 MiB")
|
||||
# Never overwrite an earlier review or its preserved original.
|
||||
destination.mkdir(parents=True, exist_ok=False)
|
||||
(destination / "original.pdf").write_bytes(original)
|
||||
version = subprocess.run([executable, "-v"], capture_output=True, timeout=10, check=False)
|
||||
# Xpdf 4.00 exits 99 after printing its version; Poppler exits zero.
|
||||
if version.returncode not in (0, 99) or b"pdftotext version" not in version.stdout + version.stderr:
|
||||
raise RuntimeError("Could not identify the pdftotext executable")
|
||||
subprocess.run(
|
||||
[executable, "-enc", "UTF-8", "-layout", str(destination / "original.pdf"), str(destination / "layout.txt")],
|
||||
capture_output=True, timeout=60, check=True,
|
||||
)
|
||||
extracted = (destination / "layout.txt").read_text(encoding="utf-8")
|
||||
counts = {}
|
||||
records = 0
|
||||
with (destination / "review.csv").open("w", encoding="utf-8", newline="") as stream:
|
||||
writer = csv.writer(stream, delimiter=";")
|
||||
writer.writerow(["page", "line", "extracted_text", "issue"])
|
||||
for page, contents in enumerate(extracted.split("\f"), 1):
|
||||
for line, text in enumerate(contents.splitlines(), 1):
|
||||
if not text.strip():
|
||||
continue
|
||||
records += 1
|
||||
# Preserve text exactly. A code occurrence is not a joined territory.
|
||||
code = re.match(r"^\s*([0-9]{6})\s", text)
|
||||
if code:
|
||||
prefix = code.group(1)[:2]
|
||||
counts[prefix] = counts.get(prefix, 0) + 1
|
||||
writer.writerow([page, line, text, "manual_column_alignment_required"])
|
||||
report = {
|
||||
"schema_version": 1,
|
||||
"source_url": source_url,
|
||||
"source_version_declared": source_version,
|
||||
"sha256": hashlib.sha256(original).hexdigest(),
|
||||
"extraction_sha256": hashlib.sha256((destination / "layout.txt").read_bytes()).hexdigest(),
|
||||
"extractor": (version.stdout + version.stderr).decode("utf-8", errors="replace").strip(),
|
||||
"status": "requires_steward_review",
|
||||
"license_status": "unverified",
|
||||
"nonempty_extracted_lines": records,
|
||||
"lines_requiring_review": records,
|
||||
"automatically_accepted_territories": 0,
|
||||
"observed_admin_post_code_lines_by_prefix": dict(sorted(counts.items())),
|
||||
"source_totals_comparison": "pending_independent_visual_review",
|
||||
"notice": "Code occurrences are not validated totals. No administrative relation or locality was inferred."
|
||||
}
|
||||
(destination / "report.json").write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
|
||||
return report
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("source")
|
||||
parser.add_argument("destination")
|
||||
parser.add_argument("--pdftotext", required=True)
|
||||
parser.add_argument("--source-url", required=True)
|
||||
parser.add_argument("--source-version", required=True)
|
||||
args = parser.parse_args()
|
||||
report = prepare(args.source, args.destination, args.pdftotext, args.source_url, args.source_version)
|
||||
print(json.dumps({"status": report["status"], "sha256": report["sha256"], "review_lines": report["lines_requiring_review"]}))
|
||||
@@ -0,0 +1,39 @@
|
||||
// Synthetic QA server only; never writes to the application database.
|
||||
const target = await (await fetch('http://127.0.0.1:9224/json/new?about:blank', { method: 'PUT' })).json();
|
||||
const ws = new WebSocket(target.webSocketDebuggerUrl);
|
||||
await new Promise(resolve => ws.addEventListener('open', resolve, { once: true }));
|
||||
let sequence = 0;
|
||||
const pending = new Map();
|
||||
const errors = [];
|
||||
ws.onmessage = event => { const message = JSON.parse(event.data); if (message.method === 'Runtime.exceptionThrown') errors.push(message.params.exceptionDetails.text); if (message.id) { const callback = pending.get(message.id); pending.delete(message.id); message.error ? callback.reject(message.error) : callback.resolve(message.result); } };
|
||||
const send = (method, params = {}) => new Promise((resolve, reject) => { const id = ++sequence; pending.set(id, { resolve, reject }); ws.send(JSON.stringify({ id, method, params })); });
|
||||
const evaluate = async expression => { const result = await send('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true }); if (result.exceptionDetails) throw Error(JSON.stringify(result.exceptionDetails)); return result.result?.value; };
|
||||
const wait = async expression => { for (let n = 0; n < 100; n++) { if (await evaluate(expression)) return; await new Promise(resolve => setTimeout(resolve, 100)); } throw Error('Timed out: ' + expression); };
|
||||
const select = async (index, value) => evaluate(`(()=>{const element=document.querySelectorAll('select')[${index}];Object.getOwnPropertyDescriptor(HTMLSelectElement.prototype,'value').set.call(element,${JSON.stringify(value)});element.dispatchEvent(new Event('change',{bubbles:true}));})()`);
|
||||
try {
|
||||
await send('Runtime.enable'); await send('Page.enable');
|
||||
await send('Page.navigate', { url: 'http://127.0.0.1:18027/login' });
|
||||
await wait(`document.readyState==='complete' && (location.pathname!='/login' || !!document.querySelector('input[type="password"]'))`);
|
||||
if (await evaluate(`location.pathname==='/login'`)) {
|
||||
for (const [selector, text] of [['input[type="email"]', 'mne-browser@example.test'], ['input[type="password"]', 'SyntheticMneOnly-2026!']]) { await evaluate(`document.querySelector(${JSON.stringify(selector)}).focus()`); await send('Input.insertText', { text }); }
|
||||
await evaluate(`document.querySelector('button[type="submit"]').click()`); await wait(`!location.pathname.startsWith('/login')`);
|
||||
}
|
||||
await send('Page.navigate', { url: 'http://127.0.0.1:18027/management/territory-associations' });
|
||||
await wait(`document.querySelectorAll('select').length===2`);
|
||||
await select(0, 'clusters');
|
||||
await wait(`document.querySelectorAll('select')[1].options.length>1`);
|
||||
await select(1, '20000000-0000-4000-8000-000000000001');
|
||||
await wait(`document.querySelectorAll('select').length===6`);
|
||||
for (let level = 0; level < 4; level++) {
|
||||
await wait(`!document.querySelectorAll('select')[${level+2}].disabled && document.querySelectorAll('select')[${level+2}].options.length>1`);
|
||||
await select(level+2, `10000000-0000-4000-8000-${String(level+2).padStart(12, '0')}`);
|
||||
}
|
||||
await evaluate(`[...document.querySelectorAll('button')].find(b=>b.textContent==='Guardar localização').click()`);
|
||||
await wait(`document.body.innerText.includes('Localização associada e guardada.')`);
|
||||
await select(2, '');
|
||||
await wait(`[...document.querySelectorAll('section[aria-label="Localização do investimento"] select')].slice(1).every(s=>s.value===''&&s.disabled)`);
|
||||
const widths = [];
|
||||
for (const width of [390, 1440]) { await send('Emulation.setDeviceMetricsOverride', { width, height: 900, deviceScaleFactor: 1, mobile: width < 600 }); widths.push({ width, noOverflow: await evaluate('document.documentElement.scrollWidth<=innerWidth+1') }); }
|
||||
if (errors.length || widths.some(w => !w.noOverflow)) throw Error('Browser validation failed');
|
||||
console.log(JSON.stringify({ cascade: true, save: true, reset_descendants: true, widths, javascript_errors: errors.length }));
|
||||
} finally { ws.close(); await fetch(`http://127.0.0.1:9224/json/close/${target.id}`); }
|
||||
@@ -0,0 +1,36 @@
|
||||
// Read-only checks against the local public portal and real reference names.
|
||||
const base = 'http://127.0.0.1:8000';
|
||||
const provinces = await (await fetch(`${base}/api/v1/investment/territory-options?type=province`)).json();
|
||||
if (provinces.data.length !== 11 || provinces.data.some(p => /^\d+$/.test(p.name))) throw Error('Province designations invalid');
|
||||
const province = provinces.data.find(p => p.name === 'Zambezia');
|
||||
if (!province) throw Error('Expected source designation missing');
|
||||
const result = await (await fetch(`${base}/api/v1/investment/search?province_id=${province.id}`)).json();
|
||||
if (result.total < 1) throw Error('Existing opportunities not linked to province');
|
||||
const target = await (await fetch('http://127.0.0.1:9224/json/new?about:blank', { method: 'PUT' })).json();
|
||||
const ws = new WebSocket(target.webSocketDebuggerUrl);
|
||||
await new Promise(resolve => ws.addEventListener('open', resolve, { once: true }));
|
||||
let sequence = 0;
|
||||
const pending = new Map();
|
||||
const errors = [];
|
||||
ws.onmessage = event => { const message = JSON.parse(event.data); if (message.method === 'Network.requestWillBeSent' && message.params.request.url.includes('/oportunidades?')) console.log(message.params.request.url); if (message.method === 'Runtime.exceptionThrown') errors.push(message.params.exceptionDetails.text); if (message.id) { const callback = pending.get(message.id); pending.delete(message.id); message.error ? callback.reject(message.error) : callback.resolve(message.result); } };
|
||||
const send = (method, params = {}) => new Promise((resolve, reject) => { const id = ++sequence; pending.set(id, { resolve, reject }); ws.send(JSON.stringify({ id, method, params })); });
|
||||
const evaluate = async expression => { const response = await send('Runtime.evaluate', { expression, returnByValue: true, awaitPromise: true }); if (response.exceptionDetails) throw Error(JSON.stringify(response.exceptionDetails)); return response.result?.value; };
|
||||
const wait = async expression => { for (let i = 0; i < 100; i++) { if (await evaluate(expression)) return; await new Promise(resolve => setTimeout(resolve, 150)); } throw Error('Timed out: '+expression); };
|
||||
const element = label => `document.getElementById([...document.querySelectorAll('label')].find(l=>l.textContent===${JSON.stringify(label)})?.htmlFor)`;
|
||||
try {
|
||||
await send('Runtime.enable'); await send('Page.enable'); await send('Network.enable');
|
||||
await send('Page.navigate', { url: `${base}/oportunidades` });
|
||||
await wait(`${element('Província')}?.options.length===12`);
|
||||
await evaluate(`(()=>{const e=${element('Província')};Object.getOwnPropertyDescriptor(HTMLSelectElement.prototype,'value').set.call(e,${JSON.stringify(province.id)});e.dispatchEvent(new Event('change',{bubbles:true}));})()`);
|
||||
await wait(`${element('Distrito')}?.options.length>1`);
|
||||
const districtNames = await evaluate(`[...${element('Distrito')}.options].slice(1).map(o=>o.textContent)`);
|
||||
if (districtNames.some(n => /^\d+$/.test(n))) throw Error('District designations invalid');
|
||||
await evaluate(`[...document.querySelectorAll('button')].find(b=>b.textContent.trim()==='Aplicar filtros').click()`);
|
||||
await wait(`location.search.includes('province_id=') && ${element('Província')}?.value===${JSON.stringify(province.id)}`);
|
||||
await send('Page.reload');
|
||||
await wait(`${element('Província')}?.value===${JSON.stringify(province.id)} && ${element('Distrito')}?.options.length>1`);
|
||||
const layouts = [];
|
||||
for (const width of [390, 1440]) { await send('Emulation.setDeviceMetricsOverride', { width, height: 900, deviceScaleFactor: 1, mobile: width < 600 }); layouts.push({ width, noOverflow: await evaluate('document.documentElement.scrollWidth<=innerWidth+1') }); }
|
||||
if (errors.length || layouts.some(l=>!l.noOverflow)) throw Error('Browser check failed');
|
||||
console.log(JSON.stringify({ named_provinces: 11, named_district_options: districtNames.length, matching_opportunities: result.total, persisted_url_filter: true, layouts, javascript_errors: errors.length }));
|
||||
} catch (error) { console.log(JSON.stringify(await evaluate(`({url:location.href,alerts:[...document.querySelectorAll('[role="alert"]')].map(e=>e.textContent),selected:[...document.querySelectorAll('select')].map(e=>e.value)})`))); throw error; } finally { ws.close(); await fetch(`http://127.0.0.1:9224/json/close/${target.id}`); }
|
||||
Reference in New Issue
Block a user