Skip to content

Commit

Permalink
Merge pull request #46 from urbanriskmap/dev
Browse files Browse the repository at this point in the history
Merge dev to prepare next release (v3.0.7)
  • Loading branch information
tomasholderness authored May 17, 2018
2 parents 13ce5a2 + 8cabf38 commit 9f010a5
Show file tree
Hide file tree
Showing 12 changed files with 2,788 additions and 2,757 deletions.
26 changes: 17 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
dist: trusty
language: node_js
node_js:
- "6.10.0"
sudo: false

addons:
postgresql: "9.6"
apt:
packages:
- postgresql-9.6-postgis-2.3
- "8.9.3"
sudo: true

branches:
only:
- master
- dev
- notifications

addons:
postgresql: "10"
apt:
packages:
- postgresql-10-postgis-2.4
- proj-bin
- postgresql-10-postgis-2.4-scripts

before_install:
- sudo cp /etc/postgresql/9.6/main/pg_hba.conf /etc/postgresql/10/main/pg_hba.conf
- sudo /etc/init.d/postgresql restart
- export PGPORT=5433

install:
- npm install
Expand All @@ -24,4 +32,4 @@ script:
after_success:
- if [[ $TRAVIS_BRANCH != 'master' ]]; then BRANCH='dev'; else BRANCH='master'; fi;
- export PGDATABASE=cognicity; npm run build
- git clone -b $BRANCH https://github.com/urbanriskmap/cognicity-server.git urbanriskmap/cognicity-server ; cd urbanriskmap/cognicity-server ; npm install ; npm test
- git clone -b $BRANCH https://github.com/urbanriskmap/cognicity-server.git urbanriskmap/cognicity-server ; cd urbanriskmap/cognicity-server ; npm install ; export PGPORT=5433 ; npm test
13 changes: 10 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
CHANGE LOG
==========

## v3.0.4
### v3.0.4
- added sensors schema
- dropped public schema from upgrade script
- added data for Mumbai and Bangalore
- added test support for multiple instances per deployment
- added tests for Surabaya and Bandung

## v3.0.5
### v3.0.5
- added Semarang extents to Indonesian data
- added test instance parameters for Semarang
- removed default parameters from database build script

## v3.0.6
### v3.0.6
- documented release procedure in README.md
- added RDS permissions script
- added table reports_points_log
- added district_id field to local_areas data in Indonesia
- added district_id field to tags property of reports

### v3.0.7
- updated Travis tests against PostgreSQL v10 with PostGIS v2.4
- updated Travis tests to use Node v8.9.3 (production)
- updated README.md minimum requirements
- added 'attributes' column to local_areas
- implemented new notification architecture
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ Flood affected area polygon data provided by emergency services via the REM inte
<dl>Broward County section grids courtesy of Broward County (http://gis.broward.org/GISData.htm)</dl>

### Dependencies
* [PostgreSQL](http://www.postgresql.org) version 9.5 or later, with
* [PostGIS](http://postgis.net) version 2.0 or later
* [PostgreSQL](http://www.postgresql.org) version 9.6 or later, with
* [PostGIS](http://postgis.net) version 2.3 or later

### Installation
* The PostgreSQL database server must be running with a UTF-8 character set.
Expand Down
5,410 changes: 2,705 additions & 2,705 deletions data/indonesia/cognicity/local_areas.data.sql

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cognicity-schema",
"version": "3.0.6",
"version": "3.0.7",
"description": "Cognicity Database Schema",
"engines": {
"node": ">=8.1.4"
Expand Down
4 changes: 2 additions & 2 deletions schema/cognicity/cognicity.functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CREATE OR REPLACE FUNCTION cognicity.define_report_region()
$BODY$
BEGIN
UPDATE cognicity.all_reports
SET tags = (SELECT (la.x->0)::jsonb || (code.x->0)::jsonb FROM (SELECT COALESCE(array_to_json(array_agg(row_to_json(a))),'[{"local_area_id":null, "district_id":null}]') as x FROM (SELECT pkey::varchar as local_area_id, district_id::varchar as district_id FROM cognicity.local_areas as j WHERE ST_Within(NEW.the_geom, j.the_geom)) as a) as la, (SELECT COALESCE(array_to_json(array_agg(row_to_json(a))),'[{"instance_region_code":null}]') as x FROM (SELECT code as instance_region_code FROM cognicity.instance_regions as i WHERE ST_Within(NEW.the_geom, i.the_geom)) as a) as code)
SET tags = (SELECT (la.x->0)::jsonb || (code.x->0)::jsonb FROM (SELECT COALESCE(array_to_json(array_agg(row_to_json(a))),'[{"local_area_id":null, "district_id":null}]') as x FROM (SELECT pkey::varchar as local_area_id, attributes->>'district_id' as district_id FROM cognicity.local_areas as j WHERE ST_Within(NEW.the_geom, j.the_geom)) as a) as la, (SELECT COALESCE(array_to_json(array_agg(row_to_json(a))),'[{"instance_region_code":null}]') as x FROM (SELECT code as instance_region_code FROM cognicity.instance_regions as i WHERE ST_Within(NEW.the_geom, i.the_geom)) as a) as code)
WHERE pkey = NEW.pkey;
RETURN NEW;
END;
Expand Down Expand Up @@ -113,7 +113,7 @@ CREATE FUNCTION cognicity.version()
RETURNS varchar
AS $BODY$
BEGIN
RETURN '3.0.6';
RETURN '3.0.7';
END
$BODY$
LANGUAGE plpgsql VOLATILE
Expand Down
2 changes: 1 addition & 1 deletion schema/cognicity/cognicity.schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ CREATE TABLE cognicity.local_areas(
parent_name character varying,
city_name character varying,
instance_region_code character varying NOT NULL,
district_id integer,
attributes json,
CONSTRAINT local_area_pkey PRIMARY KEY (pkey)
);

Expand Down
49 changes: 26 additions & 23 deletions schema/reports/grasp/grasp.functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ $BODY$
IF (TG_OP = 'UPDATE') THEN
UPDATE cognicity.all_reports SET image_url = NEW.image_url WHERE fkey = NEW.pkey AND source = 'grasp';
RETURN NEW;
ELSIF (TG_OP = 'INSERT') THEN
INSERT INTO cognicity.all_reports (fkey, created_at, text, source, disaster_type, lang, url, report_data, the_geom, image_url)
SELECT NEW.pkey, NEW.created_at, NEW.text, 'grasp', NEW.disaster_type, card.language, card.card_id, NEW.card_data, NEW.the_geom, NEW.image_url
FROM grasp.cards AS card WHERE card.card_id = NEW.card_id;
RETURN NEW;
END IF;
END;
$BODY$
Expand All @@ -23,25 +18,33 @@ ALTER FUNCTION grasp.update_all_reports_from_grasp()

-- Update all_reports table
CREATE TRIGGER trigger_update_all_reports_from_grasp
BEFORE INSERT OR UPDATE
BEFORE UPDATE
ON grasp.reports
FOR EACH ROW
EXECUTE PROCEDURE grasp.update_all_reports_from_grasp();
EXECUTE PROCEDURE grasp.update_all_reports_from_grasp();

--Notifications on grasp cards table updates
CREATE OR REPLACE FUNCTION grasp.notify_grasp_cards_trigger() RETURNS trigger AS $$
DECLARE
report_id BIGINT;
report_area VARCHAR;
BEGIN
SELECT into report_id, report_area c.PKEY, c.tags->>'instance_region_code' FROM cognicity.all_reports c, grasp.reports g WHERE c.fkey = g.pkey AND c.source = 'grasp' AND g.card_id = NEW.card_id;
PERFORM pg_notify('watchers', '{"' || TG_TABLE_NAME || '":{"pkey":"' || NEW.pkey || '", "username": "'|| NEW.username ||'", "network": "' || NEW.network || '", "language": "'|| NEW.language ||'", "report_id": "' || report_id ||'", "report_impl_area": "' || report_area || '"}}' );
RETURN new;
END;
$$ LANGUAGE plpgsql;
-- Push a grasp card report to all_reports. Requires card_id UUID.
CREATE OR REPLACE FUNCTION grasp.push_to_all_reports(varchar)
RETURNS varchar AS $$
DECLARE
-- internal variables
cardId ALIAS FOR $1; -- the report in grasp.reports
reportId BIGINT;
instanceRegionCode VARCHAR;
lang VARCHAR;
username VARCHAR;
network VARCHAR;
BEGIN
INSERT INTO cognicity.all_reports (fkey, created_at, text, source, disaster_type, lang, url, report_data, the_geom, image_url)
SELECT reports.pkey, reports.created_at, reports.text, 'grasp', reports.disaster_type, cards.language, cards.card_id, reports.card_data, reports.the_geom, reports.image_url
FROM grasp.cards AS cards, grasp.reports AS reports WHERE cards.card_id = cardId::uuid AND reports.card_id = cardId::uuid
RETURNING pkey INTO reportId;

CREATE TRIGGER watch_grasp_cards_trigger
AFTER UPDATE ON grasp.cards
FOR EACH ROW
WHEN (NEW.received = TRUE)
EXECUTE PROCEDURE grasp.notify_grasp_cards_trigger();
SELECT r.tags->>'instance_region_code', r.lang INTO instanceRegionCode, lang FROM cognicity.all_reports r WHERE r.pkey = reportId;
SELECT c.username, c.network INTO username, network FROM grasp.cards c WHERE card_id = cardId::uuid;

RETURN ('{"reportId":' || reportId || ', "instanceRegionCode":"'|| instanceRegionCode ||'", "language":"' || lang || '", "username":"' || username ||'", "network":"' || network ||'"}');
END;
$$ LANGUAGE plpgsql;
ALTER FUNCTION grasp.push_to_all_reports(varchar)
OWNER TO postgres;
13 changes: 7 additions & 6 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const pgp = require('pg-promise')({

const DATABASE = process.env['PGDATABASE'];
const COUNTRY = process.env['COUNTRY'];
const PORT = process.env['PGPORT'];
console.log('Testing against ' + DATABASE + ' for ' + COUNTRY);

import testInstanceRegions from './testInstanceRegions.js';
Expand All @@ -20,7 +21,7 @@ import testVersion from './testVersion';

let deployments = {
'indonesia': {
'connection': 'postgres://postgres@localhost:5432/' + DATABASE,
'connection': 'postgres://postgres@localhost:' + PORT + '/' + DATABASE,
'name': 'cognicity_indonesia',
'instances': [
{
Expand All @@ -38,7 +39,7 @@ let deployments = {
{
'name': 'Surabaya',
'test_instance_region_code': 'sby',
'test_local_area_id': 'null',
'test_local_area_id': null,
'test_report_lat': -7.410,
'test_report_lon': 112.771,
'test_report_text': 'report text',
Expand All @@ -50,7 +51,7 @@ let deployments = {
{
'name': 'Bandung',
'test_instance_region_code': 'bdg',
'test_local_area_id': 'null',
'test_local_area_id': null,
'test_report_lat': -6.930,
'test_report_lon': 107.650,
'test_report_text': 'report text',
Expand All @@ -62,7 +63,7 @@ let deployments = {
{
'name': 'Semarang',
'test_instance_region_code': 'srg',
'test_local_area_id': 'null',
'test_local_area_id': null,
'test_report_lat': -7.1141,
'test_report_lon': 110.3850,
'test_report_text': 'report text',
Expand All @@ -75,7 +76,7 @@ let deployments = {
},

'india': {
'connection': 'postgres://postgres@localhost:5432/' + DATABASE,
'connection': 'postgres://postgres@localhost:' + PORT + '/' + DATABASE,
'name': 'cognicity_india',
'instances': [
{
Expand Down Expand Up @@ -117,7 +118,7 @@ let deployments = {
],
},
'us': {
'connection': 'postgres://postgres@localhost:5432/' + DATABASE,
'connection': 'postgres://postgres@localhost:' + PORT + '/' + DATABASE,
'name': 'cognicity_us',
'instances': [
{
Expand Down
4 changes: 2 additions & 2 deletions test/testFloodgauge.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export default (db, instance) => {
test.value(data[0].observation_comment).is('comment');
test.value(data[0].tags.instance_region_code)
.is(instance.test_instance_region_code);
test.value(data[0].tags.local_area_id)
.is(instance.test_local_area_id);
// test.value(data[0].tags.local_area_id)
// .is(instance.test_local_area_id);
done();
})
.catch((error) => test.fail(error));
Expand Down
16 changes: 14 additions & 2 deletions test/testGRASP.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default (db, instance) => {
before('Insert dummy GRASP data', (done) => {
// Insert test data
let query = `INSERT INTO grasp.cards (username, network, language, received)
VALUES ('user', 'test network', 'en', True) RETURNING pkey, card_id`;
VALUES ('username', 'test network', 'en', True) RETURNING pkey, card_id`;

db.oneOrNone(query)
.then((data) => {
Expand All @@ -31,7 +31,19 @@ export default (db, instance) => {
db.oneOrNone(query, values)
.then((data) => {
reportFkey = data.pkey;
done();

let query = `SELECT * FROM grasp.push_to_all_reports($1) as notify`;

let values = [cardId];

db.oneOrNone(query, values)
.then((data) => {
let response = JSON.parse(data.notify);
test.value(response.language).is('en');
test.value(response.username).is('username');
test.value(response.network).is('test network');
done();
});
})
.catch((error) => console.log(error));
})
Expand Down

0 comments on commit 9f010a5

Please sign in to comment.