132 lines
3.4 KiB
PHP
132 lines
3.4 KiB
PHP
<?php // phpcs:ignoreFile
|
|
header('Content-Type: application/json');
|
|
header('Cache-Control: no-cache, must-revalidate');
|
|
|
|
$payload = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!is_array($payload)) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'Invalid request payload.'
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
$tags = $payload['tags'] ?? [];
|
|
|
|
if (!is_array($tags) || count($tags) === 0) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'At least one tag is required.'
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
$normalizedTags = [];
|
|
foreach ($tags as $tag) {
|
|
$trimmed = trim((string) $tag);
|
|
if ($trimmed === '') {
|
|
continue;
|
|
}
|
|
$normalizedTags[$trimmed] = true;
|
|
}
|
|
|
|
if (count($normalizedTags) === 0) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => 'No valid tag names provided.'
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
$servername = '192.168.0.13\\SQLEXPRESS';
|
|
$username = 'opce';
|
|
$password = 'opcelasuca';
|
|
$dbname = 'history';
|
|
|
|
try {
|
|
$pdo = new PDO(
|
|
"sqlsrv:Server=$servername;Database=$dbname",
|
|
$username,
|
|
$password,
|
|
[
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
|
|
]
|
|
);
|
|
|
|
$endTime = new DateTimeImmutable('now');
|
|
$startTime = $endTime->modify('-3 hours');
|
|
|
|
$series = [];
|
|
|
|
$query = $pdo->prepare(
|
|
'WITH ordered_samples AS (
|
|
SELECT TOP (7200)
|
|
a.TimeStamp,
|
|
a.Value
|
|
FROM dbo.archive AS a
|
|
INNER JOIN dbo.id_names AS n
|
|
ON CAST(n.idnumber AS INT) = a.ID
|
|
WHERE n.name = :tag_name
|
|
AND a.TimeStamp BETWEEN :start_time AND :end_time
|
|
ORDER BY a.TimeStamp DESC
|
|
)
|
|
SELECT TimeStamp, Value
|
|
FROM ordered_samples
|
|
ORDER BY TimeStamp ASC'
|
|
);
|
|
|
|
foreach (array_keys($normalizedTags) as $tagName) {
|
|
$query->execute([
|
|
':tag_name' => $tagName,
|
|
':start_time' => $startTime->format('Y-m-d H:i:s'),
|
|
':end_time' => $endTime->format('Y-m-d H:i:s')
|
|
]);
|
|
|
|
$rows = $query->fetchAll();
|
|
$points = [];
|
|
$values = [];
|
|
|
|
foreach ($rows as $row) {
|
|
$timestamp = $row['TimeStamp'] ?? null;
|
|
$value = isset($row['Value']) ? (float) $row['Value'] : null;
|
|
|
|
if ($timestamp === null || $value === null) {
|
|
continue;
|
|
}
|
|
|
|
$points[] = [
|
|
'timestamp' => $timestamp,
|
|
'value' => $value
|
|
];
|
|
$values[] = $value;
|
|
}
|
|
|
|
$series[] = [
|
|
'tag' => $tagName,
|
|
'points' => $points,
|
|
'stats' => [
|
|
'count' => count($values),
|
|
'min' => count($values) > 0 ? min($values) : null,
|
|
'max' => count($values) > 0 ? max($values) : null,
|
|
'latest' => count($values) > 0 ? end($values) : null
|
|
]
|
|
];
|
|
}
|
|
|
|
echo json_encode([
|
|
'success' => true,
|
|
'series' => $series,
|
|
'window' => [
|
|
'start' => $startTime->format(DateTimeInterface::ATOM),
|
|
'end' => $endTime->format(DateTimeInterface::ATOM)
|
|
]
|
|
]);
|
|
} catch (Throwable $exception) {
|
|
echo json_encode([
|
|
'success' => false,
|
|
'error' => $exception->getMessage()
|
|
]);
|
|
}
|