Tuesday, June 26, 2012

How to find the maximum number of logged on users on a given day...

A very typical question that every client faces during their yearly Microsoft renewal is "How many user licenses are we actually using?"  There is no sense in paying for an extra 30 users that you don't need.

I wrote this simple job to find the maximum number of users logged in during a working day for the last 90 days.  You will most likely need to tweak the "addHours" functions for your working days.  I was having all sorts of problems with UTC and timezone offset, so I just added numbers until it was right.

This checks max users for every hour because my original task was to show user count by hour.  You can easily modify this to check every second or whatever if you need a much more detailed number.

static void JobFindMaxUsersLoggedInPerDay(Args _args)
    SysUserLog      sysUserLog;
    utcDateTime     utc = DateTimeUtil::addHours(DateTimeUtil::newDateTime(systemDateGet(), 0), 8);
    int             i;
    int             n;
    int             iUsers;

    utc = DateTimeUtil::newDateTime(systemDateGet(), 0);

    utc = DateTimeUtil::addDays(DateTimeUtil::addHours(utc, 13), -13);
//    utc = DateTimeUtil::applyTimeZoneOffset(utc, DateTimeUtil::getUserPreferredTimeZone());

    for (i=1; i<=90; i++)
        iUsers = 0;
        for (n=1; n<=11; n++)
            // Find the number of users logged in
            select count(recId) from sysUserLog
                where sysUserLog.createdDateTime < DateTimeUtil::addHours(utc, n)      &&
                      sysUserLog.LogoutDateTime  > DateTimeUtil::addHours(utc, n)      &&

            if (!iUsers || sysUserLog.RecId > iUsers)
                iUsers = sysUserLog.RecId;

        info(strfmt("%1, %2", DateTimeUtil::date(utc), iUsers));
        utc = DateTimeUtil::addDays(utc, -1);