-- integrating scores, create geoip tables and jam shadows of those and scores

-----------------
-- geoipblocks --
-----------------

CREATE TABLE geoipblocks
(
  beginip BIGINT NOT NULL,
  endip BIGINT NOT NULL,
  locid INTEGER NOT NULL
);

--------------
-- geoipisp --
--------------

CREATE TABLE geoipisp
(
  beginip BIGINT NOT NULL,
  endip BIGINT NOT NULL,
  company CHARACTER VARYING(50) NOT NULL
);

CREATE INDEX geoipisp_company_ndx ON geoipisp (company);

--------------------
-- geoiplocations --
--------------------

CREATE TABLE geoiplocations
(
  locid INTEGER PRIMARY KEY,
  countrycode CHARACTER VARYING(2),
  region CHARACTER VARYING(2),
  city CHARACTER VARYING(255),
  postalcode CHARACTER VARYING(8),
  latitude DOUBLE PRECISION NOT NULL,
  longitude DOUBLE PRECISION NOT NULL,
  metrocode INTEGER,
  areacode CHARACTER(3)
);

----------------
-- jamcompany --
----------------

CREATE TABLE jamcompany
(
  coid SERIAL PRIMARY KEY,
  company CHARACTER VARYING(50) NOT NULL
);

CREATE UNIQUE INDEX jamcompany_company_ndx ON jamcompany (company);

------------
-- jamisp --
------------

CREATE TABLE jamisp
(
  beginip BIGINT NOT NULL,
  endip BIGINT NOT NULL,
  coid INTEGER NOT NULL
);

CREATE INDEX jamisp_coid_ndx ON jamisp (coid);

------------
-- scores --
------------

CREATE TABLE scores
(
  alocidispid BIGINT NOT NULL,
  anodeid CHARACTER VARYING(64) NOT NULL,
  aaddr BIGINT NOT NULL,
  blocidispid BIGINT NOT NULL,
  bnodeid CHARACTER VARYING(64) NOT NULL,
  baddr BIGINT NOT NULL,
  score INTEGER NOT NULL,
  scorer INTEGER NOT NULL,
  score_dt TIMESTAMP NOT NULL DEFAULT current_timestamp
);

CREATE INDEX scores_alocidispid_blocidispid_score_dt_ndx ON scores (alocidispid, blocidispid, score_dt);
CREATE INDEX scores_blocidispid_alocidispid_score_dt_ndx ON scores (blocidispid, alocidispid, score_dt);

delete from GeoIPLocations;
insert into GeoIPLocations (locId, countryCode, region, city, postalCode, latitude, longitude, metroCode, areaCode) values
  (17192,'US','TX','Austin','78749',30.2076,-97.8587,635,'512');

delete from GeoIPBlocks;
insert into GeoIPBlocks (beginIp, endIp, locId) values
  (0,4294967295,17192);

delete from GeoIPISP;
insert into GeoIPISP values
  (0,4294967295,'Intergalactic Boogie Corp');

DELETE FROM jamcompany;
ALTER SEQUENCE jamcompany_coid_seq RESTART WITH 1;
INSERT INTO jamcompany (company) SELECT DISTINCT company FROM geoipisp ORDER BY company;

DELETE FROM jamisp;
INSERT INTO jamisp (beginip, endip, coid) SELECT x.beginip, x.endip, y.coid FROM geoipisp x, jamcompany y WHERE x.company = y.company;

--ALTER TABLE geoiplocations DROP COLUMN geog;
--ALTER TABLE geoiplocations ADD COLUMN geog geography(point, 4326);
--UPDATE geoiplocations SET geog = ST_SetSRID(ST_MakePoint(longitude, latitude), 4326)::geography;
--CREATE INDEX geoiplocations_geog_gix ON geoiplocations USING GIST (geog);

--ALTER TABLE geoipblocks DROP COLUMN geom;
--ALTER TABLE geoipblocks ADD COLUMN geom geometry(polygon);
--UPDATE geoipblocks SET geom = ST_MakeEnvelope(beginip, -1, endip, 1);
--CREATE INDEX geoipblocks_geom_gix ON geoipblocks USING GIST (geom);

--ALTER TABLE jamisp DROP COLUMN geom;
--ALTER TABLE jamisp ADD COLUMN geom geometry(polygon);
--UPDATE jamisp SET geom = ST_MakeEnvelope(beginip, -1, endip, 1);
--CREATE INDEX jamisp_geom_gix ON jamisp USING GIST (geom);

--DROP VIEW current_scores;
CREATE VIEW current_scores AS SELECT * FROM scores s WHERE score_dt = (SELECT max(score_dt) FROM scores s0 WHERE s0.alocidispid = s.alocidispid AND s0.blocidispid = s.blocidispid);

--DROP FUNCTION get_work (mylocidispid BIGINT);
CREATE FUNCTION get_work (mylocidispid BIGINT) RETURNS TABLE (client_id VARCHAR(64)) ROWS 5 VOLATILE AS $$
BEGIN
	CREATE TEMPORARY TABLE foo (locidispid BIGINT, locid INT);
	INSERT INTO foo SELECT DISTINCT locidispid, locidispid/1000000 FROM connections;
	DELETE FROM foo WHERE locidispid IN (SELECT DISTINCT blocidispid FROM current_scores WHERE alocidispid = mylocidispid AND (current_timestamp - score_dt) < interval '24 hours');
	DELETE FROM foo WHERE locid NOT IN (SELECT locid FROM geoiplocations WHERE geog && st_buffer((SELECT geog from geoiplocations WHERE locid = mylocidispid/1000000), 806000));
	CREATE TEMPORARY TABLE bar (client_id VARCHAR(64), locidispid BIGINT, r DOUBLE PRECISION);
	INSERT INTO bar SELECT l.client_id, l.locidispid, random() FROM connections l, foo f WHERE l.locidispid = f.locidispid;
	DROP TABLE foo;
	DELETE FROM bar b WHERE r != (SELECT max(r) FROM bar b0 WHERE b0.locidispid = b.locidispid);
	RETURN QUERY SELECT b.client_id FROM bar b ORDER BY r LIMIT 5;
	DROP TABLE bar;
	RETURN;
END;
$$ LANGUAGE plpgsql;

