计算数据库中没有新行的百分比

时间:2014-06-10 08:21:14

标签: php mysql

我已经创建了一个脚本,以查看我们管理的PC上是否有任何活动(我已经在C#中创建了一个每隔5分钟检查一次活动的程序)。数据库如下所示:

服务器

readip, time(datetime), user, computeruse (int, 0=no 1=yes), ID

我已经有一个cronjob运行每天后创建一定比例的计算机使用。但是,如果客户的计算机已关闭,则不会被计算在内。

有没有办法检查在一段时间内是否没有新的行添加到数据库中?

我的代码是:

'$result2 = mysql_query("SELECT * FROM server WHERE serverip >='$ipfrom' AND    serverip<='$ipto' AND DATE(time) = CURDATE() GROUP BY serverip ORDER BY time");
while($row2 = mysql_fetch_array($result2)) {
    $serverip = $row2['serverip'];

    $queryfornumrowsno = mysql_query("SELECT * From server WHERE serverip = '$serverip'") or die(mysql_error());
    $numrowstotal = mysql_num_rows($queryfornumrowsno) . ' ';

    $queryfornumrowsyes = mysql_query("SELECT * FROM server WHERE serverip = '$serverip' AND computeruse = 'Yes'");
    $numrowsyes = mysql_num_rows($queryfornumrowsyes);

    $percentage = $numrowsyes * 100 / $numrowstotal;
    $percentage = round($percentage);
    $percentagetotal[] = $percentage;



    $datenow = $row2['time'];


}
$totaalPercentage = 0;
foreach( $percentagetotal as $totaal )
{
    $totaalPercentage += $totaal;
}

$percentagecount = count($percentagetotal);


if($percentagecount == "0"){
    $totaal = "0";
}
else{
    $totaal =  $totaalPercentage / $percentagecount;
}
echo $datenow . ' @ ' . $totaal . '% van ' . $lokaal . '<br />'; 
if($datenow){


}

抱歉我的英语不好,不是我的母语。

解决方案:

include 'connect.php';
$result = mysql_query("SELECT * FROM iprange");
while($row = mysql_fetch_array($result)) {
$ipfrom = $row['iprangefrom'];
$lokaal = $row['lokaal'];
$ipto = $row['iprangeto'];
$ipfrom = ip2long($ipfrom);
$ipto = ip2long($ipto);
$listpc = 0;
$result = mysql_query("SELECT * FROM server WHERE serverip >='$ipfrom' AND            serverip<='$ipto' AND DATE(time) = CURDATE() AND computeruse = 'Yes'");
$countpc = mysql_query("SELECT * FROM server WHERE serverip >='$ipfrom' AND serverip<='$ipto' AND DATE(time) = CURDATE() GROUP BY serverip ORDER BY time") or die(mysql_error());
$countpcrows = mysql_num_rows($countpc);



while($row = mysql_fetch_array($result)) {

    $listpc = $listpc + 1;

    }

echo $countpcrows . ' ' . $listpc . '<br />';
$percentage = $listpc / 84 * 100 / $countpcrows;
    //84 = 5 minutes.
echo round($percentage) . '%';
}

1 个答案:

答案 0 :(得分:0)

要获取过去10分钟内没有活动的服务器列表: -

SELECT a.serverip
FROM
(
    SELECT DISTINCT serverip 
    FROM server 
    WHERE serverip >='$ipfrom' 
    AND   serverip<='$ipto' 
) a
LEFT OUTER JOIN
(
    SELECT DISTINCT serverip 
    FROM server 
    WHERE serverip >='$ipfrom' 
    AND   serverip<='$ipto' 
    AND time > DATE_ADD(CURDATE(), INTERVAL -10 MINUTE) 
) b
ON a.serverip = b.serverip
WHERE b.serverip IS NULL

第一个子查询获取您感兴趣的范围内的服务器列表。第二个查询获取过去10分钟内具有任何活动的服务器。 LEFT JOIN将获得2的组合,包括那些在过去10分钟内没有活动的服务器。然后,WHERE子句将排除除最近10分钟内有任何活动的那些以外的所有内容。

基于此,您应该能够计算百分比。您是希望百分比作为所有服务器的数量,还是仅仅是您感兴趣的范围内的服务器数量?

假设您感兴趣的范围内所有服务器的百分比: -

SELECT (COUNT(DISTINCT a.serverip) / c.all_server) * 100 AS percentage_not_used
FROM server a
CROSS JOIN
(
    SELECT COUNT(*) AS all_server
    FROM server
    WHERE serverip >='$ipfrom' 
    AND   serverip <='$ipto' 
) c
LEFT OUTER JOIN
(
    SELECT DISTINCT serverip 
    FROM server 
    WHERE serverip >='$ipfrom' 
    AND   serverip<='$ipto' 
    AND time > DATE_ADD(CURDATE(), INTERVAL -10 MINUTE) 
) b
ON a.serverip = b.serverip
WHERE a.serverip >='$ipfrom' 
AND   a.serverip <='$ipto' 
AND   b.serverip IS NULL

编辑 - 计算过去24小时内每台服务器使用的次数百分比(假设24小时内可以使用的最大值为84次): -

SELECT a.serverip, (COUNT(b.serverip) / 84) * 100 AS percentage_not_used
FROM server a
LEFT OUTER JOIN server b
ON a.serverip = b.serverip
AND b.serverip >='$ipfrom' 
AND b.serverip<='$ipto' 
AND b.time > DATE_ADD(CURDATE(), INTERVAL -24 HOUR) 
GROUP BY a.serverip

加入服务器表对自己。表的第一次出现用于获取serverip(无论是否在过去24小时内使用),而第二次用于获取在过去24小时内使用它的次数(计数将忽略空值)。