/*****************************************************************************/
/*                                                                           */
/*                             exampleQueries.txt                            */
/*                                                                           */
/* (c) 2005 by the Board of Trustees of the Leland Stanford, Jr., University */
/*                            All Rights Reserved                            */
/*       Produced by Jacek Becla for Stanford University under contract      */
/*              DE-AC02-76SF00515 with the Department of Energy              */
/*****************************************************************************/

// $Id: exampleQueries.txt 22437 2008-03-04 14:35:16Z rdm $



###################################################
############### show current status ###############
################################################### 


## current number of unique users:
SELECT COUNT(DISTINCT userId) 
FROM   rtOpenedSessions;


## current number of unique opened files
SELECT COUNT(DISTINCT pathId)
FROM   rtOpenedFiles;

## current number of non-unique opened files
SELECT COUNT(*)
FROM   rtOpenedFiles;


## current number of unique opened sessions
SELECT COUNT(DISTINCT CONCAT(pId, clientHId))
FROM   rtOpenedSessions;


## change of number of jobs [in #jobs and in %]
SELECT jobs, jobs_p FROM rtChanges;

## or much more complicated way:
 # 1st query
SELECT @lastMin := min, 
       @lastValue := noJobs
FROM   rtHour
WHERE  min = (SELECT MAX(min) FROM rtHour);
 # 2nd query: returns change in number of jobs
SELECT @lastValue-noJobs AS dif
FROM   rtHour
WHERE  min = SUBTIME(@lastMin, "00:01:00");
 # 3rd query: returns change in %
## change of number of jobs [in %]
SELECT 100*(@lastValue-noJobs)/@lastValue as difPerc
FROM rtHour
WHERE min = SUBTIME(@lastMin, "00:01:00");



## select day's range of noJobs
select MIN(noJobs), MAX(noJobs) from rtDay;



## current number of non-unique opened sessions
SELECT COUNT(*)
FROM   rtOpenedSessions;


## current number of unique active client hosts
SELECT COUNT(DISTINCT clientHId) as noClients
FROM   rtOpenedSessions;


## current number of unique active server hosts
SELECT COUNT(DISTINCT serverHId) as noServers
FROM   rtOpenedSessions;


###################################################
############ for plotting, time window ############
################################################### 

# all "per hour", to get "per day" replace 
# '%Y-%m-%d %H' with '%Y-%m-%d'


## unique closed sessions per hour
SELECT DATE_FORMAT(disconnectT, '%Y-%m-%d %H') AS time, 
       COUNT(*)                                AS noJobs
FROM   rtClosedSessions
       GROUP BY TIMEDIFF(NOW(), DATE_FORMAT(disconnectT, '%Y-%m-%d %H'))
       ORDER BY disconnectT;



## unique users per hour



## non-unique closed files per hour
SELECT DATE_FORMAT(closeT, '%Y-%m-%d %H') AS time,
       COUNT(*)                           AS noClosedFiles
FROM   rtClosedFiles
       GROUP BY TIMEDIFF(NOW(),DATE_FORMAT(closeT, '%Y-%m-%d %H'))
       ORDER BY hour;




## volume read per hour



## volume written per hour


## total duration of all the jobs per hour



###################################################
################# data for tables #################
###################################################


## hottest users, sorted by "now" (nJobs or nFiles)
###################################################
CREATE TEMPORARY TABLE tn (u INT, f INT, j INT);
INSERT INTO tn
       SELECT userId,
              COUNT(DISTINCT pathId)                  AS nFiles,
              COUNT(DISTINCT CONCAT(pId, clientHId) ) AS nJobs
       FROM   rtOpenedSessions os, rtOpenedFiles of
       WHERE  os.id = of.sessionId
              GROUP BY userId
              ORDER BY nFiles DESC # <-- sorting by nFiles
             #ORDER BY nJobs  DESC # <-- sorting by nJobs
              LIMIT 5;             # <-- number of rows




## hottest users, sorted by "past" (mbRead or nFiles)
#####################################################
CREATE TEMPORARY TABLE tp1 (u INT, r BIGINT, f INT);
INSERT INTO tp1
       SELECT userId,
              SUM(bytesR)/(1024*1024) AS mbRead,
              COUNT(DISTINCT pathId)  AS nFiles
       FROM   rtClosedSessions cs, rtClosedFiles cf
       WHERE  cs.id = cf.sessionId AND
              closeT > DATE_SUB(NOW(), INTERVAL 1 HOUR) # <-- interval
              GROUP BY userId
              ORDER BY mbRead DESC # <-- sorting by mbRead
             #ORDER BY nJobs  DESC # <-- sorting by nJobs
              LIMIT 5;             # <-- number of rows

CREATE TEMPORARY TABLE tp2 (u INT, j INT);
INSERT INTO tp2
       SELECT u,
              COUNT(DISTINCT CONCAT(pId, clientHId) ) AS nJobs
       FROM   tp1, rtClosedSessions cs
       WHERE  tp1.u = cs.userId AND
              disconnectT > DATE_SUB(NOW(), INTERVAL 1 HOUR) # <-- interval
              GROUP BY u;

CREATE TEMPORARY TABLE tn (u INT, f INT, j INT);
INSERT INTO tn
       SELECT userId,
              COUNT(DISTINCT pathId)                  AS nFiles,
              COUNT(DISTINCT CONCAT(pId, clientHId) ) AS nJobs
       FROM   tp1, rtOpenedSessions os, rtOpenedFiles of
       WHERE  tp1.u = os.userId AND
              os.id = of.sessionId
              GROUP BY userId;
SELECT userName, 
       IFNULL(tn.f,  0) AS nFilesNow, 
       IFNULL(tn.j,  0) AS nJobsNow,
       IFNULL(tp1.r, 0) AS mbRead,
       IFNULL(tp1.f, 0) AS nFiles,
       IFNULL(tp2.j, 0) AS nJobs
FROM   tp1, users
LEFT OUTER JOIN tn  ON (tp1.u = tn.u)
LEFT OUTER JOIN tp2 ON (tp1.u = tp2.u)
WHERE  tp1.u = users.id
       ORDER BY mbRead DESC; # <-- sorting by mbRead
      #ORDER BY nFiles DESC; # <-- sorting by nFiles

DROP TABLE tn;
DROP TABLE tp1;
DROP TABLE tp2;




## hottest users, sorted by "past" (nJobs)
##########################################
CREATE TEMPORARY TABLE tp2 (u INT, j INT);
INSERT INTO tp2
       SELECT userId,
              COUNT(DISTINCT CONCAT(pId, clientHId) ) AS nJobs
       FROM   rtClosedSessions cs
       WHERE  disconnectT > DATE_SUB(NOW(), INTERVAL 1 HOUR) # <-- interval
              GROUP BY userId
              ORDER BY nJobs DESC
              LIMIT 5;             # <-- number of rows to display

CREATE TEMPORARY TABLE tp1 (u INT, r BIGINT, f INT);
INSERT INTO tp1
       SELECT userId,
              SUM(bytesR)/(1024*1024) AS mbRead,
              COUNT(DISTINCT pathId)  AS nFiles
       FROM   tp2, rtClosedSessions cs, rtClosedFiles cf
       WHERE  tp2.u = userId       AND
              cs.id = cf.sessionId AND
              closeT > DATE_SUB(NOW(), INTERVAL 1 HOUR) # <-- interval
              GROUP BY userId;

CREATE TEMPORARY TABLE tn (u INT, f INT, j INT);
INSERT INTO tn
       SELECT userId,
              COUNT(DISTINCT pathId)                  AS nFiles,
              COUNT(DISTINCT CONCAT(pId, clientHId) ) AS nJobs
       FROM   tp2, rtOpenedSessions os, rtOpenedFiles of
       WHERE  tp2.u = os.userId AND
              os.id = of.sessionId
              GROUP BY userId;

SELECT userName, 
       IFNULL(tn.f,  0) AS nFilesNow, 
       IFNULL(tn.j,  0) AS nJobsNow,
       IFNULL(tp1.r, 0) AS mbRead,
       IFNULL(tp1.f, 0) AS nFiles,
       IFNULL(tp2.j, 0) AS nJobs
FROM   tp2, users
LEFT OUTER JOIN tn  ON (tp2.u = tn.u)
LEFT OUTER JOIN tp1 ON (tp2.u = tp1.u)
WHERE  tp2.u = users.id
       ORDER BY nJobs DESC;

DROP TABLE tn;
DROP TABLE tp1;
DROP TABLE tp2;





## hottests skims, sorted by "now"
##################################
CREATE TEMPORARY TABLE tn (s INT, f INT, j INT, u INT);
INSERT INTO tn
       SELECT paths.skimId,
              COUNT(DISTINCT of.id)     AS nFilesNow,
              COUNT(DISTINCT os.id)     AS nJobsNow,
              COUNT(DISTINCT os.userId) AS nUsersNow 
       FROM   paths, rtOpenedSessions os, rtOpenedFiles of
       WHERE  paths.id = of.pathId AND
              of.sessionId = os.id AND
              skimId > 0
              GROUP BY skimId
              ORDER BY nJobsNow DESC # <-- sorting nJobsNow/nFilesNow/nUsersNow
              LIMIT 5;               # <-- number of rows

CREATE TEMPORARY TABLE tp1 (s INT, r BIGINT, f INT);
INSERT INTO tp1
       SELECT paths.skimId,
              SUM(bytesR)/(1024*1024) AS mbRead,
              COUNT(DISTINCT cf.id)   AS nFiles
       FROM   tn, paths, rtClosedFiles cf
       WHERE  tn.s = paths.skimId  AND
              paths.id = cf.pathId AND
              closeT > DATE_SUB(NOW(), INTERVAL 1 HOUR) # <-- interval
              GROUP BY skimId;

CREATE TEMPORARY TABLE tp2 (s INT, j INT, u INT);
INSERT INTO tp2
       SELECT tn.s,
              COUNT(DISTINCT cs.id)     AS nJobs,
              COUNT(DISTINCT cs.userId) AS nUsers 
       FROM   tn, paths, rtClosedSessions cs, rtClosedFiles cf
       WHERE  tn.s = paths.skimId  AND
              paths.id = cf.pathId AND
              cf.sessionId = cs.id AND
              disconnectT > DATE_SUB(NOW(), INTERVAL 1 HOUR) # <-- interval
              GROUP BY skimId;

SELECT skimNames.name   AS skimName,
       IFNULL(tn.f,  0) AS nFilesNow,
       IFNULL(tn.j,  0) AS nJobsNow,
       IFNULL(tn.u,  0) AS nUsersNow,
       IFNULL(tp1.r, 0) AS mbRead,
       IFNULL(tp1.f, 0) AS nFiles,
       IFNULL(tp2.j, 0) AS nJobs,
       IFNULL(tp2.u, 0) AS nUsers
FROM   tn, skimNames
LEFT OUTER JOIN tp1 ON (tn.s = tp1.s)
LEFT OUTER JOIN tp2 ON (tn.s = tp2.s)
WHERE  tn.s = skimNames.id
       ORDER BY tn.j DESC; # <-- sorting

DROP TABLE tn;
DROP TABLE tp1;
DROP TABLE tp2;




## hottests skims, sorted by "past" (mbRead or nFiles)
######################################################
CREATE TEMPORARY TABLE tp1 (s INT, r BIGINT, f INT);
INSERT INTO tp1
       SELECT paths.skimId,
              SUM(bytesR)/(1024*1024)   AS mbRead,
              COUNT(DISTINCT cf.id)     AS nFiles
       FROM   paths, rtClosedFiles cf
       WHERE  paths.id = cf.pathId AND
              skimId > 0           AND
              closeT > DATE_SUB(NOW(), INTERVAL 1 HOUR) # <-- interval
              GROUP BY skimId
              ORDER BY mbRead DESC # <-- sorting by mbRead
             #ORDER BY nFiles DESC # <-- sorting by nFiles
              LIMIT 5;             # <-- number of rows to display

CREATE TEMPORARY TABLE tp2 (s INT, j INT, u INT);
INSERT INTO tp2
       SELECT paths.skimId,
              COUNT(DISTINCT cs.id)     AS nJobsNow,
              COUNT(DISTINCT cs.userId) AS nUsersNow
       FROM   tp1, paths, rtClosedSessions cs, rtClosedFiles cf
       WHERE  tp1.s = paths.skimId AND
              paths.id = cf.pathId AND
              cf.sessionId = cs.id
              GROUP BY skimId;

CREATE TEMPORARY TABLE tn (s INT, j INT, f INT, u INT);
INSERT INTO tn
       SELECT paths.skimId,
              COUNT(DISTINCT os.id)     AS nJobsNow,
              COUNT(DISTINCT of.id)     AS nFilesNow,
              COUNT(DISTINCT os.userId) AS nUsersNow 
       FROM   tp1, paths, skimNames, rtOpenedSessions os, rtOpenedFiles of
       WHERE  tp1.s = paths.skimId AND
              paths.id = of.pathId AND
              of.sessionId = os.id
              GROUP BY skimId;

SELECT skimNames.name   AS skimName,
       IFNULL(tn.f,  0) AS nFilesNow,
       IFNULL(tn.j,  0) AS nJobsNow,
       IFNULL(tn.u,  0) AS nUsersNow,
       IFNULL(tp1.r, 0) AS mbRead,
       IFNULL(tp1.f, 0) AS nFiles,
       IFNULL(tp2.j, 0) AS nJobs,
       IFNULL(tp2.u, 0) AS nUsers
FROM   tp1, skimNames
LEFT OUTER JOIN tn  ON (tp1.s = tn.s)
LEFT OUTER JOIN tp2 ON (tp1.s = tp2.s)
WHERE  tp1.s = skimNames.id
       ORDER BY mbRead DESC; # <-- sorting by mbRead
      #ORDER BY nFiles DESC; # <-- sorting by nFiles

DROP TABLE tn;
DROP TABLE tp1;
DROP TABLE tp2;





## hottests files, sorted by "now"
CREATE TEMPORARY TABLE tn (p INT, j INT, u INT);
INSERT INTO tn
       SELECT of.pathId,
              COUNT(DISTINCT os.id)     AS nJobsNow,
              COUNT(DISTINCT os.userId) AS nUsersNow 
       FROM   rtOpenedSessions os, rtOpenedFiles of
       WHERE  of.sessionId = os.id
              GROUP BY of.pathId
              ORDER BY nJobsNow DESC
              LIMIT 5;    #<-- number of rows

CREATE TEMPORARY TABLE tp (p INT, r BIGINT, j INT, u INT);
INSERT INTO tp
       SELECT cf.pathId,
              SUM(bytesR)/(1024*1024)   AS mbRead,
              COUNT(DISTINCT cs.id)     AS nJobs,
              COUNT(DISTINCT cs.userId) AS nUsers 
       FROM   tn, rtClosedSessions cs, rtClosedFiles cf
       WHERE  tn.p = cf.pathId AND
              cf.sessionId = cs.id AND
              disconnectT > DATE_SUB(NOW(), INTERVAL 1 HOUR) AND #<-- interval
              closeT      > DATE_SUB(NOW(), INTERVAL 1 HOUR)     #<-- interval
              GROUP BY cf.pathId;

SELECT paths.path      AS pathName,
       IFNULL(tn.j, 0) AS nJobsNow,
       IFNULL(tn.u, 0) AS nUsersNow,
       IFNULL(tp.r, 0) AS mbRead,
       IFNULL(tp.j, 0) AS nJobs,
       IFNULL(tp.u, 0) AS nUsers
FROM   tn, paths
LEFT OUTER JOIN tp ON (tn.p = tp.p)
WHERE  tn.p = paths.id
       ORDER BY tn.j DESC; # <-- sorting
DROP TABLE tn;
DROP TABLE tp;








## hottests files, sorted by "past"
CREATE TEMPORARY TABLE tp (p INT, r BIGINT, j INT, u INT);
INSERT INTO tp
       SELECT cf.pathId,
              SUM(bytesR)/(1024*1024)   AS mbRead,
              COUNT(DISTINCT cs.id)     AS nJobs,
              COUNT(DISTINCT cs.userId) AS nUsers 
       FROM   rtClosedSessions cs, rtClosedFiles cf
       WHERE  cf.sessionId = cs.id AND
              disconnectT > DATE_SUB(NOW(), INTERVAL 1 HOUR) AND # <-- interval
              closeT      > DATE_SUB(NOW(), INTERVAL 1 HOUR)     # <-- interval
              GROUP BY cf.pathId
              ORDER BY mbRead DESC # <-- sorting
              LIMIT 5;             # <-- number of rows to display

CREATE TEMPORARY TABLE tn (p INT, j INT, u INT);
INSERT INTO tn
       SELECT of.pathId,
              COUNT(DISTINCT os.id)     AS nJobsNow,
              COUNT(DISTINCT os.userId) AS nUsersNow 
       FROM   tp, rtOpenedSessions os, rtOpenedFiles of
       WHERE  tp.p = of.pathId AND
              of.sessionId = os.id
              GROUP BY of.pathId;

SELECT paths.path      AS pathName,
       IFNULL(tn.j, 0) AS nJobsNow,
       IFNULL(tn.u, 0) AS nUsersNow,
       IFNULL(tp.r, 0) AS mbRead,
       IFNULL(tp.j, 0) AS nJobs,
       IFNULL(tp.u, 0) AS nUsers
FROM   tp, paths
LEFT OUTER JOIN tn ON (tn.p = tp.p)
WHERE  tp.p = paths.id
       ORDER BY tp.r DESC; # <-- sorting
DROP TABLE tn;
DROP TABLE tp;















## top 10: number of jobs and number of uniqueFiles per user (now)
SELECT userName,
       COUNT(DISTINCT CONCAT(pId, clientHId) ) AS nJobs,
       COUNT(DISTINCT pathId)                  AS nFiles
FROM   rtOpenedSessions, users, rtOpenedFiles 
WHERE  userId = users.id AND
       rtOpenedSessions.id = rtOpenedFiles.sessionId
       GROUP BY userId
       ORDER BY nJobs DESC
       LIMIT 10;


## top 10: number of jobs per user
SELECT userName, 
       COUNT(DISTINCT CONCAT(pId, clientHId) ) AS nJobs,
FROM   rtOpenedSessions, users
WHERE  userId = users.id 
       GROUP BY userId 
       ORDER BY nJobs DESC
       LIMIT 10;


## top 10: number of files per user
SELECT userName, 
       COUNT(DISTINCT pathId) AS nFiles 
FROM   rtOpenedSessions, users, rtOpenedFiles 
WHERE  userId = users.id AND 
       rtOpenedSessions.id = rtOpenedFiles.sessionId 
       GROUP BY userId 
       ORDER BY nFiles DESC
       LIMIT 10;


##############################################







## top 5 skims (max no files)
SELECT skimNames.name AS skimName, 
       COUNT(*)       AS noFiles 
from   paths, skimNames 
WHERE  skimNames.id = skimId 
       GROUP BY skimId
       ORDER BY noFiles DESC
       LIMIT 5;

## top 5 skims (max no sessions)
SELECT skimNames.name        AS skimName, 
       COUNT(DISTINCT os.id) AS noJobs 
from   paths, skimNames, rtOpenedSessions os, rtOpenedFiles of
WHERE  skimNames.id = paths.skimId AND
       paths.id = of.pathId        AND
       of.sessionId = os.id
       GROUP BY skimId
       ORDER BY noJobs DESC
       LIMIT 5;

## top 5 skims (max no users)
SELECT skimNames.name            AS skimName, 
       COUNT(DISTINCT os.userId) AS noUsers 
from   paths, skimNames, rtOpenedSessions os, rtOpenedFiles of
WHERE  skimNames.id = paths.skimId AND
       paths.id = of.pathId        AND
       of.sessionId = os.id
       GROUP BY skimId
       ORDER BY noUsers DESC
       LIMIT 5;


## top 5 skims (max nofiles, no sessions, no users, sorted by max no files)
SELECT skimNames.name        AS skimName, 
       COUNT(DISTINCT os.id) AS noJobs,
       COUNT(DISTINCT of.id) AS noFiles,
       COUNT(DISTINCT os.userId) AS noUsers 
FROM   paths, skimNames, rtOpenedSessions os, rtOpenedFiles of
WHERE  skimNames.id = paths.skimId AND
       paths.id = of.pathId        AND
       of.sessionId = os.id
       GROUP BY skimId
       ORDER BY noFiles DESC
       LIMIT 5;


## hottest files:

SELECT paths.path                   AS filePath,
       COUNT(DISTINCT of.sessionId) AS noJobs,
       COUNT(DISTINCT os.userId)    AS noUsers
FROM   paths, rtOpenedFiles of, rtOpenedSessions os
WHERE  paths.id = of.pathId AND
       of.sessionId = os.id
       GROUP BY of.id
       ORDER BY noJobs DESC
       LIMIT 5;

      
## per data type (SP,PR, SPskims, PRskims)
SELECT fileTypes.name            AS type,
       COUNT(DISTINCT os.id)     AS noJobs,
       COUNT(DISTINCT of.id)     AS noFiles,
       COUNT(DISTINCT os.userId) AS noUsers
FROM   paths, fileTypes, rtOpenedSessions os, rtOpenedFiles of
WHERE  fileTypes.id = paths.skimId AND
       paths.id = of.pathId        AND
       of.sessionId = os.id
       GROUP BY skimId
       ORDER BY noFiles DESC;


###################################################
################# data for buttons ################
################################################### 

## show all currently active users
SELECT DISTINCT userName
FROM   users, rtOpenedSessions
WHERE  userId = users.id
ORDER BY userName;



## show all server machines with at least one opened session
SELECT DISTINCT hostName 
FROM   hosts, rtOpenedSessions 
WHERE  serverHId = hosts.id
       ORDER BY hostName;


## show all currently active client machines
SELECT DISTINCT hostName 
FROM   hosts, rtOpenedSessions 
WHERE  clientHId = hosts.id
       ORDER BY hostName;



## show all skims 
???


## show all currently opened files 
SELECT DISTINCT path 
FROM   paths, rtOpenedFiles 
WHERE  pathId = paths.id
       ORDER BY path;



###################################################
########## detailed info about one user ###########
################################################### 

#select user id, then use it in all queries:
SELECT id FROM users WHERE userName = 'blablabla';


current status
==============

## number of opened files
SELECT COUNT(DISTINCT pathId)
FROM   rtOpenedSessions os, rtOpenedFiles of, users
WHERE  of.sessionId = os.id AND
       os.userId = users.id AND
       userName = 'blabla';


## number of opened sessions
SELECT COUNT(*) 
FROM   rtOpenedSessions os, users 
WHERE  os.userId = users.id AND
       userName = 'bbrskim';


SELECT DISTINCT hosts.hostName AS clientName,
       pId
FROM   rtOpenedSessions os, users, hosts
WHERE  os.userId = users.id AND
       userName = 'bbrskim' AND
       os.clientHId = hosts.id
       ORDER BY clientName;


## number of client hosts 
SELECT COUNT(DISTINCT clientHId) 
FROM   rtOpenedSessions os, users 
WHERE  os.userId = users.id AND
       userName = 'bbrskim';



## list opened files
SELECT DISTINCT path 
FROM   rtOpenedSessions os, rtOpenedFiles of, users, paths
WHERE  paths.id = of.pathId AND
       of.sessionId = os.id AND
       os.userId = users.id AND
       userName = 'rahmi';


## list running jobs
SELECT DISTINCT hosts.hostName AS clientHost, 
       pId
FROM   rtOpenedSessions os, users, hosts
WHERE  os.userId = users.id AND
       userName = 'blabla'  AND
       os.clientHId = hosts.id
       ORDER BY clientHost;


## list used client hosts
SELECT DISTINCT hostName
FROM   rtOpenedSessions os, users, hosts
WHERE  os.userId = users.id AND
       hosts.id = os.clientHId AND
       userName = 'bbrskim';




time window:
============

## number of accessed (closed) files in the past hour
SELECT @t1+@t2
FROM   (SELECT @t1:=COUNT(*) 
        FROM   users, rtClosedFiles cf, rtOpenedSessions os
        WHERE  users.id = os.userId AND
               os.id = cf.sessionId) AS X1,
       (SELECT @t2:=COUNT(*) 
        FROM   users, rtClosedFiles cf, rtClosedSessions cs
        WHERE  users.id = cs.userId AND
               cs.id = cf.sessionId) AS X2;

     


## number of closed sessions


## total duration of all jobs


## peak number of simulteneous jobs


## volume read


## volume written




###################################################
########## detailed info about one server #########
################################################### 


[to be filled]

###################################################
##### detailed info about one client machine ######
################################################### 


[to be filled]


###################################################
########### detailed info about one skim ##########
################################################### 


[to be filled]


###################################################
########### detailed info about one file ##########
################################################### 


[to be filled]
