<%@language = "javascript" %>
<% /*
    $Revision$
    $Id$

    File:           fac_scan_aiai.asp
    Description:    FACILITOR remote support
                    API om AiAi data op te halen en (datum van) logfiles
                    Wordt regelmatig (10m) aangeroepen vanaf de FACWS001
    Context:        Je moet ingelogd zijn als FACFAC bij een bepaalde klant
                    maar eigenlijk maakt het niet uit bij welke: de hele
                    AiAi folder wordt gescanned
*/
var JSON_Result = true;
var result = { svnrevision: parseInt("$Revision$".replace(/\D/g,'')), started: new Date() };
%>
<!-- #include file="../Shared/common.inc"-->
<%

user.checkAutorisation("WEB_FACFAC");

var fso = Server.CreateObject("Scripting.FileSystemObject")

// Vrije ruimte
try
{
    var drvPath = Server.MapPath("../../");
    result.drive = fso.GetDriveName(drvPath);
    var d = fso.GetDrive(result.drive);

    result.webdrive = {
            drive: fso.GetDriveName(drvPath),
            total: d.TotalSize,
            free: d.AvailableSpace
    }
    var d = fso.GetDrive("C:"); // Eigenlijk: te bepalen maar ik heb het nooit anders gezien
    result.windowsdrive = {
            drive: "C:",
            total: d.TotalSize,
            free: d.AvailableSpace
    }
}
catch (e)
{
    /* could not read */
}

// AiAi's
var f = fso.GetFolder(Server.MapPath("../../temp/"));
var fname = "500_error";

var aiaifolder = Server.MapPath("../../temp/_AiAi_error");
result.aiaifiles = [];
if (fso.FolderExists(aiaifolder))
{
    var f = fso.GetFolder(aiaifolder);
    var fc = new Enumerator(f.files);

    var dd = new Date();
    result.todayname = "log_" + toISODateString(dd) + ".html"

    for (; !fc.atEnd(); fc.moveNext())
    {
        var f = fc.item();
        var filedata = { name: f.Name, date: new Date(f.DateLastModified), size: f.Size };
        // Merk op dat we echt 168 uur doen. Dat kan over 8 dagen verspreid zijn.
        if (filedata.date < new Date().getTime() - 7*24*60*60*1000) // alleen laatste week
            continue;
        filedata.url = HTTP.urlzelf() + "/appl/fac/fac_Streamfile.asp?logger=3&dt=" + filedata.date;
        if (f.Name == result.todayname)
            filedata.today = true;
        result.aiaifiles.push(filedata);
    }

    // nieuwste vooraan
    result.aiaifiles.sort(function (a, b) { return a.date < b.date? 1: -1 });
}

var dblog = Server.MapPath("../../temp/log_database.html");
if (fso.FileExists(dblog))
{
    var f = fso.GetFile(dblog);
    result.dblog = { name: f.Name,
                     date: new Date(f.DateLastModified),
                     size: f.Size,
                     url: HTTP.urlzelf() + "/appl/fac/fac_Streamfile.asp?logger=6"
                   };
}

// Ook de reguliere logfiles
var logfolder = Server.MapPath("../../temp");
result.logfiles = [];
var tempfolder = fso.GetFolder(logfolder);
var fc = new Enumerator(tempfolder.SubFolders);

for (; !fc.atEnd(); fc.moveNext())
{
    var folder = fc.item();
    if (folder.Name.length != 4)
        continue;

    if (fso.FileExists(folder.Path + "/log_" + folder.Name + ".html"))
    {
        var f = fso.getFile(folder.Path + "/log_" + folder.Name + ".html");
        var filedata = {
                         cust: folder.Name,
                         name: f.Name,
                         date: new Date(f.DateLastModified),
                         size: f.Size
                        };
        result.logfiles.push(filedata);
    }
}
function tmsort(c1, c2)
{
    if (c1.date > c2.date)
        return -1;
    else
        return 1;
}
result.logfiles.sort(tmsort); // nieuwste bovenaan wil je het vaakst

// Putorders datum zodat we op prod kunnen constateren dat hij niet draait
var sql = "select fac_version_schema,"
        + "       fac_version_cust,"
        + "       fac_version_putorders_date,"
        + "       fac_version_notify_date,"
        + "       fac_version_scheduler_date,"
        + "       fac_version_tasker_date"
        + " from fac_version";
var oRs = Oracle.Execute(sql);
result.scanner = { schema : oRs("fac_version_schema").Value,
                   custid: oRs("fac_version_cust").Value
                 }
result.putorders_date = oRs("fac_version_putorders_date").Value!=null?new Date(oRs("fac_version_putorders_date").Value):null;
result.notify_date = oRs("fac_version_notify_date").Value!=null?new Date(oRs("fac_version_notify_date").Value):null;
result.scheduler_date = oRs("fac_version_scheduler_date").Value!=null?new Date(oRs("fac_version_scheduler_date").Value):null;
result.tasker_date = oRs("fac_version_tasker_date").Value!=null?new Date(oRs("fac_version_tasker_date").Value):null;
oRs.Close();
var sql = "SELECT user, created FROM user_users";
var oRs = Oracle.Execute(sql);
result.scanner.dbcreated = new Date(oRs("created").Value);
result.scanner.dbuser = oRs("user").Value;
oRs.Close();

// Busy taken
var sql = "SELECT fac_cust_customerid, "
        + "       fac_task_code, "
        + "       fac_task_nextrun, "
        + "       fac_task_lastrun, "
        + "       fac_task_flags"
        + "  FROM fac_cust fc, fac_task ft "
        + " WHERE ft.fac_cust_key = fc.fac_cust_key  "
        + "   AND BITAND (fac_task_flags, 2) = 2 " // busy
        + "   AND fac_task_nextrun IS NOT NULL"
        + " ORDER BY fac_cust_customerid, fac_task_code"
var oRs = Oracle.Execute(sql);
result.tasker = {};
while (!oRs.EOF)
{
    var data = { "fac_task_code": oRs("fac_task_code").Value,
                 "fac_task_nextrun": new Date(oRs("fac_task_nextrun").Value),
                 "fac_task_lastrun": new Date(oRs("fac_task_lastrun").Value),
                 "fac_task_flags": oRs("fac_task_flags").Value
               }
    result.tasker[oRs("fac_cust_customerid").Value] = data;
    oRs.MoveNext;
}
oRs.Close();


result.applicationpoolstart = new Date(Application("SET_INSTANCE_TIME"));

var sql = "SELECT SYSDATE FROM DUAL";
var oRs = Oracle.Execute(sql);
result.sysdate = new Date(oRs(0).Value);
oRs.Close();
result.finished = new Date();

result.scanduration = result.finished.getTime() - result.started.getTime();
Response.Write(JSON.stringify(result));
Response.End;
%>
<% ASPPAGE_END(); %>
