<?php
require_once __DIR__ . '/../db.php';

$res = $conn->query("
    SELECT v.id, v.online,
           t.daily_earning today,
           y.daily_earning yesterday
    FROM vms v
    LEFT JOIN earnapp_history t
      ON t.vm_id=v.id AND t.earning_date=CURDATE()
    LEFT JOIN earnapp_history y
      ON y.vm_id=v.id AND y.earning_date=CURDATE()-INTERVAL 1 DAY
");

while ($r = $res->fetch_assoc()) {
    if (
        $r['online'] &&
        $r['today'] == 0 &&
        $r['yesterday'] > 0
    ) {
        $conn->query("
            INSERT IGNORE INTO alerts
            (vm_id, type, severity)
            VALUES
            ('{$r['id']}', 'ZERO_EARNING', 'WARNING')
        ");
    }
}