drop function addhit(text,text,text,text,text,text,
  text,int4,int4,text,text,text,text,text,int4);

create function addhit(text,text,text,text,text,text,
 text,int4,int4,text,text,text,text,text,int4) returns int4 as '
  declare
    p_CustomerText	alias for $1; -- pi.dk
    p_IPText		alias for $2; -- 231.223.42.2
    p_AuthUser		alias for $3; -- -
    p_AuthName		alias for $4; -- -
    p_hitDatetime	alias for $5; -- 2002-01-21 14:54:33
    p_HostText		alias for $6; -- www.pi.dk
    p_URLText		alias for $7; -- /
    p_Status		alias for $8; -- 200
    p_Size		alias for $9; -- 485
    p_RefererHostText	alias for $10; -- www.sslug.dk
    p_RefererURLText	alias for $11; -- /sponsor.html
    p_BrowserText	alias for $12; -- Mozilla/5.0
    p_XferTime          alias for $13; -- 1
    p_MimeTypeText	alias for $14; -- text/html
    p_find_referer	alias for $15; -- 1

    v_CustomerID	int4; -- hit.CustomerID%rowtype;
    v_IPID		int4; -- hit.IPID%rowtype;
    v_HostID		int4;
    v_URLID		int4; -- hit.URLID%rowtype;
    v_RefererHostID	int4;
    v_RefererURLID	int4;
    v_BrowserID		int4;
    v_MimeTypeID	int4;
    v_SessionID		int4;

    referer		record;
    session_latest	record;
    v_latestTimeSpent	interval;
    v_return		int4;
  begin
    v_CustomerID	:= getCustomerID(p_CustomerText);
    v_IPID		:= getIPID(p_IPText);
    v_HostID		:= getHostID(p_HostText);
    v_URLID		:= getURLID(p_URLText);
    v_RefererHostID	:= getHostID(p_RefererHostText);
    v_RefererURLID	:= getURLID(p_RefererURLText);
    v_BrowserID		:= getBrowserID(p_BrowserText);
    v_MimeTypeID	:= getMimeTypeID(p_MimeTypeText);

    -- DEBUG
    insert into timing values(timeofday(), p_IPtext, ''got mimIDs'');

    -- Find what session this is in
    select hitID,SessionID into referer from hit
     where IPID		= v_IPID
       and CustomerID	= v_CustomerID
       and URLID	= v_RefererURLID
       and hitDateTime	<= p_hitDateTime::timestamp
       and hitDateTime  > (p_hitDateTime::timestamp 
			  - ''1 hour''::interval)
     order by HitDateTime desc limit 1;
    if found then
      -- Refered from a page less than 1 hour ago
      v_SessionID = referer.SessionID;
      if p_find_referer = ''1'' then
        -- This is an HTML-page: Calculate time spent
	-- Find the latest HTML-page in this session
	-- DEBUG
	insert into timing values(timeofday(), p_IPtext, ''find ses'');
	select hitID,SessionID,hitdatetime into session_latest
	  from hit H, MimeType M
	 where SessionID		= v_SessionID
	   and H.MimeTypeID = M.MimeTypeID
	   and MimeTypeText = ''text/html''
	 order by HitDateTime desc limit 1;
	if found then
	  v_latestTimeSpent	:= age(p_hitDateTime,
				   session_latest.hitDateTime);
          update hit set TimeSpent = v_latestTimeSpent
	   where hitid=session_latest.hitid and TimeSpent = null;
	else
	  -- This should not happen as there is a 
	  insert into debug values(p_IPtext||p_URLText);
	end if; -- if found 
      end if; -- if p_find_referer = ''1''
    else -- if found then
      -- No referer: New sessionID
      v_SessionID = nextval(''Hit_SessionID_seq'');
    end if; -- if found

    -- DEBUG
    insert into timing values(timeofday(), p_IPtext,''preinsert'');

    insert into hit(SessionID, CustomerID,IPID,AuthUser,AuthName,hitDatetime,
       HostID,URLID,Status,Size,RefererHostID,RefererURLID,BrowserID,
       XferTime, MimeTypeID)
    values(
      v_SessionID,
      v_CustomerID,
      v_IPID,
      p_AuthUser,
      p_AuthName,
      p_hitDatetime,
      v_HostID,
      v_URLID,
      p_Status,
      p_Size,
      v_RefererHostID,
      v_RefererURLID,
      v_BrowserID,
      p_XferTime,
      v_MimeTypeID
    );
    return v_return;      
  end;
' language 'PLpgSQL';

-- For debugging:
-- create table timing (tim timestamp, why text);
-- insert into timing values(timeofday(), ''got mimIDs''||p_IPtext);
-- select date_part('second',tim),why from timing order by tim desc limit 100

-- create table timing (tim timestamp, which text, why text);
-- insert into timing values(timeofday(), p_IPText, ''got mimIDs'');
-- select date_part('second',tim),which,why from timing order by tim desc limit 100;


