This commit is contained in:
Your Name
2026-04-19 20:50:02 +08:00
parent d469c4b93d
commit 53a1d7e4fd
12 changed files with 801 additions and 91 deletions

View File

@@ -0,0 +1,210 @@
<?php
declare(strict_types=1);
require dirname(__DIR__) . '/vendor/autoload.php';
use app\common\helper\DomainImportProbeRunHelper;
use app\model\DomainModel;
use think\App;
use think\facade\Db;
function printUsage(): void
{
echo "Usage:\n";
echo " php scripts/old_tmp_domain_daily_probe.php [--templates=1001,1002,1003,1004,1005] [--run-root=/abs/path] [--format=json|text]\n\n";
echo "Example:\n";
echo " php scripts/old_tmp_domain_daily_probe.php --format=text\n";
echo " php scripts/old_tmp_domain_daily_probe.php --templates=1002,1003 --format=json\n";
}
function ensureAppInitialized(): void
{
static $initialized = false;
if ($initialized) {
return;
}
(new App())->initialize();
$initialized = true;
}
function normalizeTemplateIds(string $raw): array
{
$items = array_filter(array_map('trim', explode(',', $raw)), static fn(string $v): bool => $v !== '');
$ids = [];
foreach ($items as $item) {
$ids[] = (int)$item;
}
$ids = array_values(array_unique(array_filter($ids, static fn(int $v): bool => $v > 0)));
sort($ids);
return $ids;
}
function loadHosts(array $templateIds): array
{
ensureAppInitialized();
$rows = Db::name('domain')
->whereIn('t_id', $templateIds)
->field(['d_domain', 't_id'])
->select()
->toArray();
$hosts = [];
$templateBuckets = [];
foreach ($rows as $row) {
if (!is_array($row)) {
continue;
}
$host = DomainModel::normalizeHost((string)($row['d_domain'] ?? ''));
$templateId = (int)($row['t_id'] ?? 0);
if ($host === '') {
continue;
}
$hosts[] = $host;
if (!isset($templateBuckets[$templateId])) {
$templateBuckets[$templateId] = [];
}
$templateBuckets[$templateId][] = $host;
}
$hosts = array_values(array_unique($hosts));
sort($hosts);
ksort($templateBuckets);
foreach ($templateBuckets as &$bucket) {
$bucket = array_values(array_unique($bucket));
sort($bucket);
}
return [
'hosts' => $hosts,
'template_buckets' => $templateBuckets,
];
}
function renderText(array $summary): string
{
$lines = [
'status: ' . (string)($summary['status'] ?? ''),
'processed_count: ' . (int)($summary['processed_count'] ?? 0),
'passed_count: ' . (int)($summary['passed_count'] ?? 0),
'failed_count: ' . (int)($summary['failed_count'] ?? 0),
'run_root: ' . (string)($summary['run_root'] ?? ''),
'summary_json_path: ' . (string)($summary['summary_json_path'] ?? ''),
'summary_html_path: ' . (string)($summary['summary_html_path'] ?? ''),
'template_ids: ' . implode(',', (array)($summary['meta']['template_ids'] ?? [])),
'hosts_count: ' . count((array)($summary['meta']['hosts'] ?? [])),
'failed_hosts:',
];
foreach ((array)($summary['items'] ?? []) as $item) {
if (!empty($item['all_passed'])) {
continue;
}
$lines[] = '- '
. (string)($item['host'] ?? '')
. ' | status=' . (string)($item['status'] ?? '')
. ' | failed_stage=' . (string)($item['failed_stage'] ?? '')
. ' | message=' . (string)($item['message'] ?? '');
}
return implode(PHP_EOL, $lines) . PHP_EOL;
}
$args = $argv;
array_shift($args);
$templateIds = [1001, 1002, 1003, 1004, 1005];
$runRoot = dirname(__DIR__) . '/storage/domain-import-runs';
$format = 'json';
foreach ($args as $arg) {
if (str_starts_with($arg, '--templates=')) {
$parsed = normalizeTemplateIds(substr($arg, strlen('--templates=')));
if (!empty($parsed)) {
$templateIds = $parsed;
}
continue;
}
if (str_starts_with($arg, '--run-root=')) {
$runRoot = rtrim(trim(substr($arg, strlen('--run-root='))), '/');
continue;
}
if (str_starts_with($arg, '--format=')) {
$format = strtolower(trim(substr($arg, strlen('--format='))));
continue;
}
if (in_array($arg, ['-h', '--help'], true)) {
printUsage();
exit(0);
}
}
if (!in_array($format, ['json', 'text'], true)) {
fwrite(STDERR, "Invalid format: {$format}\n");
printUsage();
exit(1);
}
try {
$loaded = loadHosts($templateIds);
$hosts = (array)($loaded['hosts'] ?? []);
$templateBuckets = (array)($loaded['template_buckets'] ?? []);
if (empty($hosts)) {
$empty = [
'status' => 'empty',
'processed_count' => 0,
'passed_count' => 0,
'failed_count' => 0,
'items' => [],
'meta' => [
'template_ids' => $templateIds,
'hosts' => [],
'template_buckets' => $templateBuckets,
],
'generated_at' => date(DATE_ATOM),
];
echo $format === 'text'
? renderText($empty)
: json_encode($empty, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
exit(0);
}
$probeRunRoot = DomainImportProbeRunHelper::createRunRoot($runRoot, 'old_tmp_daily_probe');
$summary = DomainImportProbeRunHelper::run($hosts, $probeRunRoot, [
'template_ids' => $templateIds,
'template_buckets' => $templateBuckets,
'hosts' => $hosts,
'scope' => 'old_tmp_daily_probe',
]);
$summary['run_root'] = $probeRunRoot;
echo $format === 'text'
? renderText($summary)
: json_encode($summary, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
} catch (Throwable $throwable) {
$error = [
'status' => 'script_failed',
'error_class' => get_class($throwable),
'message' => $throwable->getMessage(),
'file' => $throwable->getFile(),
'line' => $throwable->getLine(),
'trace' => $throwable->getTraceAsString(),
];
if ($format === 'text') {
echo 'status: script_failed' . PHP_EOL;
echo 'error_class: ' . $error['error_class'] . PHP_EOL;
echo 'message: ' . $error['message'] . PHP_EOL;
echo 'file: ' . $error['file'] . PHP_EOL;
echo 'line: ' . $error['line'] . PHP_EOL;
echo 'trace:' . PHP_EOL . $error['trace'] . PHP_EOL;
} else {
echo json_encode($error, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL;
}
exit(1);
}