-- file: create.sql
-- toughlog

-- Drop section
drop table httpLog;
drop table Status;
--drop table Cookie;
--drop table CookieSet;
-- drop sequence httpLog_httpLogID_seq;
drop sequence httpLog_SessionID_seq;

create table Status (
	StatusID	INT4 UNIQUE primary key,
	Status	TEXT
);

COMMENT ON TABLE Status IS 'HTTP status code as defined in RFC 2616.
Like Error 404: Not found.';

create sequence httpLog_SessionID_seq;

create table httpLog(
	httpLogID	BIGserial NOT NULL primary key,
	hCreated	timestamp with time zone DEFAULT now(),
	ReturnCode	int4 NOT NULL default 0, -- Return code (status) when adding new record
	FeedHostID	int4 NOT NULL,-- REFERENCES FeedHost, -- Hostname where program runs i.e '07.rack.isp.com'
	SessionID	int4,
	ServerNameID	int4 NOT NULL REFERENCES HostName(HostNameID), -- %v example.com
	HostID		int4,-- REFERENCES HostReq, -- %{Host}i www.example.com - could be NULL for HTTP/1.0
	RemoteIPID	int4 NOT NULL, -- %a Remote client IP address 12.34.56.78
	AuthUserID	int4,-- REFERENCES AuthUsers, -- %u joe
	hitDateTime     timestamp with time zone, -- %{%F %T %z}t
	MethodID	int4,-- REFERENCES ReqMethods -- %m GET HEAD SEARCH
	PathID		int4, -- %U The URL path requested, not including any query string: /index.php
	QueryID		int4, -- %q The query string (prepended with a ? if a query string exists, otherwise an empty string) (?q=hello+world)
	ProtocolID	int4, -- %H The request protocol (HTTP/1.1 or HTTP/1.0)
	FileServedID	int4, -- %f Full path filename: /home/example.com/index.html
	StatusID	int4,-- REFERENCES Status NOT NULL, -- %>s Status & Error codes (like 404) rfc2616
	TransSize       int4,-- NOT NULL, -- %B Number of bytes send without header
	TimeToServe	int4,--interval,-- NOT NULL, -- %T Time to serve http request
	TimeSpent	interval,-- NOT NULL, -- Time spent between this and previous hit
	Connection	char(1) default '?', -- %c Connection status when response was completed. (X,+,-)
	RefererID	int4,-- REFERENCES Referer, -- %{Referer}i http://www.google.com/?q=linux+games
	UserAgentID	int4,--REFERENCES UserAgents, -- %{User-Agent}i
	UserAgentType	int4, -- Human, Search-robot, Virus, Email-harvest, Screen-saver
	AcceptLanguageID	int4,--REFREENCES AccLanguages, -- %{Accept-Language}i da,en;q=0.7
	AcceptID		int4,--REFERENCES AcceptMimes, -- %{Accept}i (text/xml,application/xml,application/xhtml+xml)
	AcceptEncodingID	int4,--REFERENCES AccEncodings, -- %{Accept-Encoding}i (gzip,deflate)
	AcceptCharsetID	int4,--REFERENCES AccCharsets, -- %{Accept-Charset}i
	ViaID		int4,--REFERENCES Vias, -- %{Via}i Gateway or web proxy
	CookieID	int4,--REFERENCES Cookies, -- %{Cookie}i kun een cookie
	SetCookieID	int4,--REFERENCES Cookies, -- %{Set-Cookie}o
	UserTrackID	int4,--REFERENCES UserTracks, -- %{cookie}n
	ContentTypeID	int4,  -- %{Content-Type}o text/html
	CountryID	int4,-- REFERENCES Country -- dk us uk de
	CountryIdent	int4 -- Method used for identify country: reverse-host, cidr, country in AccLanguage
	-- CONSTRAINT (hitDateTime)
);
create index httpLog_hitID on httpLog(httpLogID);
--create index httpLog_hitDatetime_ServerNameID on httpLog(hitDatetime,ServerNameID);
--create index httpLog_hitDatetime_RemoteIPID on httpLog(RemoteIPID,hitDatetime); -- 59 sek
--create index httpLog_hitDatetime_PathID on httpLog(RemoteIPID,PathID); -- 33 sek
create index httpLog_RemoteIPID_PathID_hitDateTime on httpLog(RemoteIPID,PathID,hitDateTime); -- 34 sek
create index httpLog_UserTrackID_hitDateTime on httpLog(UserTrackID,hitDateTime);
create index httpLog_SessionID on httpLog(SessionID);
create index httpLog_RemoteIPID on httpLog(RemoteIPID);
create index httplog_hitdatetime on httplog(hitdatetime);
create index httplog_usertrackid on httplog(usertrackid);

-- Sample: PHPSESSID=3dd41093a80998b65a5b6321b8d8bff6; Test=42
--create table Cookie (
--	CookieID SERIAL primary key,
--	CookieKey TEXT, -- PHPSESSID
--	CookieValue TEXT, -- 3dd41093a80998b65a5b6321b8d8bff6
--);

--create table CookieSet (
--	httpLogID	INT4 REFERENCES httpLog,
--	CookieID INT4 REFERENCES Cookie,
--	CookieIn BOOL -- Either incoming or outgoing cookie
--);

--drop sequence IP_IPID_seq;
--drop table IP;
--create table IP(
--	IPID		serial primary key,
--	IPText		text,	-- (123.45.67.89)
--	IPname		text,	-- (dial-ip192.inet)
--	IPdomain	text,	-- (tele)
--	IPcountry	text	-- (dk)
--);
--create index IP_IPID on IP(IPID);
--create index IP_IPText on IP(IPText);

--create table Site(
--	SiteID	serial,
--	SiteName	text not null, -- example.com
--	OwnerName	text, -- Probably empty
--	KeepLogLive	int4, -- number of days to keep live log, like 30 days
--	KeepLogAgre	int4, -- number of days to keep agregated logs, like 365 days
--);

--create table GeneralAdmin(
--	AllowIP	text, -- Space delimited list of IP who can change
--	email	text,	-- E-mail address of contact person
--	WebContact	text, -- Website for contact, like www.isp.com/contact.html
--	WebHelp	text, -- Help for end users, like www.isp.com/loghelp.html
--);

--create table users (
--	UserID	SERIAL,
--	UName	text,
--	UEmail	text,
--	TimeZoneMin	int4,  -- Copenhagen == +60
--	Sites	int4 REFERENCES xref_site, -- Site user can view
--	password	text,
--	CurrentSessID	text,
--	CurrentLoginTime	timestamp without time zone,
--);

COPY status (statusid, status) FROM stdin;
0	undefined HTTP code
100	Continue
101	Switching Protocols
200	OK
201	Created
202	Accepted
203	Non-Authoritative Information
204	No Content
205	Reset Content
206	Partial Content
300	Multiple Choices
301	Moved Permanently
302	Found
303	See Other
304	Not Modified
305	Use Proxy
307	Temporary Redirect
400	Bad Request
401	Unauthorized
402	Payment Required
403	Forbidden
404	Not Found
405	Method Not Allowed
406	Not Acceptable
407	Proxy Authentication Required
408	Request Time-out
409	Conflict
410	Gone
411	Length Required
412	Precondition Failed
413	Request Entity Too Large
414	Request-URI Too Large
415	Unsupported Media Type
416	Requested range not satisfiable
417	Expectation Failed
500	Internal Server Error
501	Not Implemented
502	Bad Gateway
503	Service Unavailable
504	Gateway Time-out
505	HTTP Version not supported
\.

