bsw/jbe@1309: function _G.ontomap_get_instances(event) bsw/jbe@1309: if true then return {} end bsw/jbe@1309: local url = config.ontomap.base_url .. "instances/Issue?geometries=true&descriptions=true" bsw/jbe@1309: print("OnToMap>>") bsw/jbe@1309: bsw/jbe@1309: local output, err, status = extos.pfilter(doc, "curl", "-X", "GET", "-H", "Content-Type: application/json", "--cert", config.ontomap.client_cert_file, "-d", "@-", url) bsw/jbe@1309: print(output) bsw/jbe@1309: bsw/jbe@1309: local data = json.import(output) bsw/jbe@1309: bsw/jbe@1309: if not data then bsw/jbe@1309: return {} bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: if data.type ~= "FeatureCollection" or not data.features then bsw/jbe@1309: return {} bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: local instances = {} bsw/jbe@1309: for i, feature in ipairs(data.features) do bsw/jbe@1309: if feature.geometry then bsw/jbe@1309: table.insert(instances, { bsw/jbe@1309: application = feature.applicationName, bsw/jbe@1309: title = feature.hasName, bsw/jbe@1309: description = slot.use_temporary(function() bsw/jbe@1309: ui.link{ external = feature.properties.external_url, text = feature.properties.hasName or "" } bsw/jbe@1309: ui.container{ content = feature.hasDescription } bsw/jbe@1309: end), bsw/jbe@1309: lon = feature.geometry.coordinates[1], bsw/jbe@1309: lat = feature.geometry.coordinates[2], bsw/jbe@1309: label = "IMC" .. feature.properties.hasID, bsw/jbe@1309: type = "Improve My City" bsw/jbe@1309: }) bsw/jbe@1309: print(feature.applicationName, feature.properties.hasName, feature.properties.hasDescription) bsw/jbe@1309: end bsw/jbe@1309: end bsw/jbe@1309: return instances bsw/jbe@1309: bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: bsw/jbe@1309: local function new_log_event(event, actor, activity_type) bsw/jbe@1309: local e = json.object{ bsw/jbe@1309: activity_type = activity_type, bsw/jbe@1309: activity_objects = json.array(), bsw/jbe@1309: references = json.array(), bsw/jbe@1309: visibility_details = json.array(), bsw/jbe@1309: details = json.object() bsw/jbe@1309: } bsw/jbe@1309: e.actor = actor bsw/jbe@1309: e.timestamp = math.floor(event.occurrence_epoch * 1000) bsw/jbe@1309: return e bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: local function new_activity_object(attr) bsw/jbe@1309: return json.object{ bsw/jbe@1309: type = "Feature", bsw/jbe@1309: geometry = attr.geometry or json.null, bsw/jbe@1309: properties = json.object{ bsw/jbe@1309: hasType = attr.type, bsw/jbe@1309: external_url = attr.url bsw/jbe@1309: } bsw/jbe@1309: } bsw/jbe@1309: end bsw/jbe@1309: bsw@1519: local function new_reference_object(url, application, type) bsw/jbe@1309: return json.object{ bsw/jbe@1309: application = application or config.ontomap.application_ident, bsw@1519: external_url = url, bsw@1519: type = type bsw/jbe@1309: } bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: local function log_to_ontomap(log_events) bsw/jbe@1309: if json.type(log_events) == "object" then bsw/jbe@1309: log_events = json.array{ log_events } bsw/jbe@1309: end bsw/jbe@1309: for i, log_event in ipairs(log_events) do bsw/jbe@1309: if #(log_event.activity_objects) == 0 then bsw/jbe@1309: log_event.activity_objects = nil bsw/jbe@1309: end bsw/jbe@1309: if #(log_event.references) == 0 then bsw/jbe@1309: log_event.references = nil bsw/jbe@1309: end bsw/jbe@1309: if #(log_event.visibility_details) == 0 then bsw/jbe@1309: log_event.visibility_details = nil bsw/jbe@1309: end bsw/jbe@1309: if not (next(log_event.details)) then -- TODO bsw/jbe@1309: log_event.details = nil bsw/jbe@1309: end bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: local doc = json.export(json.object{ bsw/jbe@1309: event_list = log_events bsw/jbe@1309: }, " ") bsw/jbe@1309: bsw/jbe@1309: local url = config.ontomap.base_url .. "logger/events" bsw/jbe@1309: print("OnToMap<<") bsw/jbe@1309: print(doc) bsw/jbe@1309: bsw/jbe@1309: local output, err, status = extos.pfilter(doc, "curl", "-X", "POST", "-H", "Content-Type: application/json", "--cert", config.ontomap.client_cert_file, "-d", "@-", url) bsw/jbe@1309: bsw/jbe@1309: print("---------") bsw/jbe@1309: print(output) bsw/jbe@1309: print("---------") bsw/jbe@1309: bsw/jbe@1309: if err then bsw/jbe@1309: -- TODO log error bsw/jbe@1309: end bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: local function url_for(relation, id) bsw/jbe@1309: return config.absolute_base_url .. relation .. "/show/" .. id .. ".html" bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: bsw/jbe@1309: local function unit_updated(event, event_type) bsw/jbe@1309: local log_event = new_log_event(event, 0, event_type == "created" and "object_created" or "object_updated") bsw/jbe@1309: table.insert(log_event.activity_objects, new_activity_object{ bsw/jbe@1309: type = "unit", bsw/jbe@1309: url = url_for("unit", event.unit_id) bsw/jbe@1309: }) bsw/jbe@1309: log_event.details.active = event.unit.active bsw/jbe@1309: log_event.details.name = event.unit.name bsw/jbe@1309: log_event.details.description = event.unit.description bsw/jbe@1309: log_event.details.external_reference = event.unit.external_reference bsw/jbe@1309: log_event.details.region = event.unit.region bsw/jbe@1309: log_to_ontomap(log_event) bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: local function area_updated(event, event_type) bsw/jbe@1309: local log_event = new_log_event(event, 0, event_type == "created" and "object_created" or "object_updated") bsw/jbe@1309: table.insert(log_event.activity_objects, new_activity_object{ bsw/jbe@1309: type = "area", bsw/jbe@1309: url = url_for("area", event.area_id) bsw/jbe@1309: }) bsw/jbe@1309: table.insert(log_event.references, new_reference_object( bsw/jbe@1309: url_for("unit", event.area.unit_id) bsw/jbe@1309: )) bsw/jbe@1309: log_event.details.active = event.area.active bsw/jbe@1309: log_event.details.name = event.area.name bsw/jbe@1309: log_event.details.description = event.area.description bsw/jbe@1309: log_event.details.external_reference = event.area.external_reference bsw/jbe@1309: log_event.details.region = event.area.region bsw/jbe@1309: log_to_ontomap(log_event) bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: local function policy_updated(event, event_type) bsw/jbe@1309: local log_event = new_log_event(event, 0, event_type == "created" and "object_created" or "object_updated") bsw/jbe@1309: table.insert(log_event.activity_objects, new_activity_object{ bsw/jbe@1309: type = "policy", bsw/jbe@1309: url = url_for("policy", event.policy_id) bsw/jbe@1309: }) bsw/jbe@1309: log_event.details.active = event.policy.active bsw/jbe@1309: log_event.details.name = event.policy.name bsw/jbe@1309: log_event.details.description = event.policy.description bsw/jbe@1309: log_to_ontomap(log_event) bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: local mapper = { bsw/jbe@1309: bsw/jbe@1309: unit_created = function(event) bsw/jbe@1309: unit_updated(event, "created") bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: unit_updated = function(event) bsw/jbe@1309: unit_updated(event, "updated") bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: area_created = function(event) bsw/jbe@1309: area_updated(event, "created") bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: area_updated = function(event) bsw/jbe@1309: area_updated(event, "updated") bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: policy_created = function(event) bsw/jbe@1309: policy_updated(event, "created") bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: policy_updated = function(event) bsw/jbe@1309: policy_updated(event, "updated") bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: issue_state_changed = function(event) bsw/jbe@1309: local log_event = new_log_event(event, 0, "issue_status_updated") bsw/jbe@1309: table.insert(log_event.references, new_reference_object( bsw/jbe@1309: url_for("issue", event.issue_id) bsw/jbe@1309: )) bsw/jbe@1309: log_event.details.new_issue_state = event.state bsw/jbe@1309: log_to_ontomap(log_event) bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: initiative_created_in_new_issue = function(event) bsw/jbe@1309: local log_events = json.array() bsw/jbe@1309: bsw/jbe@1309: local log_event = new_log_event(event, 0, "object_created") bsw/jbe@1309: table.insert(log_event.activity_objects, new_activity_object{ bsw/jbe@1309: type = "issue", bsw/jbe@1309: url = url_for("issue", event.issue_id) bsw/jbe@1309: }) bsw/jbe@1309: table.insert(log_event.references, new_reference_object( bsw/jbe@1309: url_for("policy", event.issue.policy_id) bsw/jbe@1309: )) bsw/jbe@1309: log_event.details.new_issue_state = event.state bsw/jbe@1309: table.insert(log_events, log_event) bsw@1519: bsw/jbe@1309: local log_event = new_log_event(event, event.member_id, "object_created") bsw@1519: bsw@1519: local location = event.initiative.location bsw@1527: if location and location.marker_link then bsw@1519: local marker_link = location.marker_link bsw@1519: location.marker_link = nil bsw@1519: table.insert(log_event.references, new_reference_object( bsw@1519: marker_link, config.firstlife.application_ident, "BELONGS_TO" bsw@1519: )) bsw@1519: end bsw@1519: bsw@1519: local activity_object = new_activity_object{ bsw/jbe@1309: type = "initiative", bsw/jbe@1309: url = url_for("initiative", event.initiative_id), bsw@1519: geometry = location bsw@1519: } bsw@1519: activity_object.properties.name = event.initiative.name bsw@1519: table.insert(log_event.activity_objects, activity_object) bsw/jbe@1309: table.insert(log_event.references, new_reference_object( bsw/jbe@1309: url_for("issue", event.issue_id) bsw/jbe@1309: )) bsw/jbe@1309: table.insert(log_events, log_event) bsw/jbe@1309: bsw/jbe@1309: log_to_ontomap(log_events) bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: initiative_created_in_existing_issue = function(event) bsw/jbe@1309: local log_event = new_log_event(event, event.member_id, "object_created") bsw@1519: local location = event.initiative.location bsw@1519: if location and location.marker_link then bsw@1519: local marker_link = location.marker_link bsw@1519: location.marker_link = nil bsw@1519: table.insert(log_event.references, new_reference_object( bsw@1519: marker_link, config.firstlife.application_ident, "BELONGS_TO" bsw@1519: )) bsw@1519: end bsw@1519: local activity_object = new_activity_object{ bsw/jbe@1309: type = "initiative", bsw/jbe@1309: url = url_for("initiative", event.initiative_id), bsw@1519: geometry = location bsw@1519: } bsw@1519: activity_object.properties.name = event.initiative.name bsw@1519: table.insert(log_event.activity_objects, activity_object) bsw/jbe@1309: table.insert(log_event.references, new_reference_object( bsw/jbe@1309: url_for("issue", event.issue_id) bsw/jbe@1309: )) bsw/jbe@1309: log_to_ontomap(log_event) bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: initiative_revoked = function(event) bsw/jbe@1309: -- TODO -> which activity? bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: new_draft_created = function(event) bsw/jbe@1309: local log_event = new_log_event(event, event.member_id, "object_updated") bsw/jbe@1309: table.insert(log_event.activity_objects, new_activity_object{ bsw/jbe@1309: type = "initiative", bsw/jbe@1309: url = url_for("initiative", event.issue_id) bsw/jbe@1309: }) bsw/jbe@1309: table.insert(log_event.references, new_reference_object( bsw/jbe@1309: url_for("issue", event.issue_id) bsw/jbe@1309: )) bsw/jbe@1309: log_event.details.name = event.initiative.name bsw/jbe@1309: log_event.details.location = event.initiative.current_draft.location bsw/jbe@1309: log_to_ontomap(log_event) bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: interest = function(event) bsw/jbe@1309: local activity_type = event.boolean_value and "interest_added" or "interest_removed" bsw/jbe@1309: local log_event = new_log_event(event, event.member_id, activity_type) bsw/jbe@1309: table.insert(log_event.references, new_reference_object( bsw/jbe@1309: url_for("issue", event.issue_id) bsw/jbe@1309: )) bsw/jbe@1309: log_to_ontomap(log_event) bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: initiator = function(event) bsw/jbe@1309: local activity_type = event.boolean_value and "initiator_added" or "initiator_removed" bsw/jbe@1309: local log_event = new_log_event(event, event.member_id, activity_type) bsw/jbe@1309: table.insert(log_event.references, new_reference_object( bsw/jbe@1309: url_for("initiative", event.initiative_id) bsw/jbe@1309: )) bsw/jbe@1309: log_to_ontomap(log_event) bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: support = function(event) bsw/jbe@1309: local activity_type = event.boolean_value and "support_added" or "support_removed" bsw/jbe@1309: local log_event = new_log_event(event, event.member_id, activity_type) bsw/jbe@1309: table.insert(log_event.references, new_reference_object( bsw/jbe@1309: url_for("initiative", event.initiative_id) bsw/jbe@1309: )) bsw/jbe@1309: log_event.details.draft_id = event.draft_id bsw/jbe@1309: log_to_ontomap(log_event) bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: support_updated = function(event) bsw/jbe@1309: local log_event = new_log_event(event, event.member_id, "support_updated") bsw/jbe@1309: table.insert(log_event.references, new_reference_object( bsw/jbe@1309: url_for("initiative", event.initiative_id) bsw/jbe@1309: )) bsw/jbe@1309: log_event.details.draft_id = event.draft_id bsw/jbe@1309: log_to_ontomap(log_event) bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: suggestion_created = function(event) bsw/jbe@1309: local log_event = new_log_event(event, event.member_id, "object_created") bsw/jbe@1309: table.insert(log_event.activity_objects, new_activity_object{ bsw/jbe@1309: type = "suggestion", bsw/jbe@1309: url = url_for("suggestion", event.suggestion_id) bsw/jbe@1309: }) bsw/jbe@1309: table.insert(log_event.references, new_reference_object( bsw/jbe@1309: url_for("initiative", event.initiative_id) bsw/jbe@1309: )) bsw/jbe@1309: log_to_ontomap(log_event) bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: suggestion_removed = function(event) bsw/jbe@1309: local log_event = new_log_event(event, 0, "object_removed") bsw/jbe@1309: table.insert(log_event.activity_objects, new_activity_object{ bsw/jbe@1309: type = "suggestion", bsw/jbe@1309: url = url_for("suggestion", event.suggestion_id) bsw/jbe@1309: }) bsw/jbe@1309: table.insert(log_event.references, new_reference_object( bsw/jbe@1309: url_for("initiative", event.initiative_id) bsw/jbe@1309: )) bsw/jbe@1309: log_to_ontomap(log_event) bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: suggestion_rated = function(event) bsw/jbe@1309: local log_event = new_log_event(event, event.member_id, "suggestion_rated") bsw/jbe@1309: table.insert(log_event.references, new_reference_object( bsw/jbe@1309: url_for("suggestion", event.suggestion_id) bsw/jbe@1309: )) bsw/jbe@1309: log_event.details.degree = event.numeric_value bsw/jbe@1309: log_event.details.fulfilled = event.boolean_value or json.null bsw/jbe@1309: log_to_ontomap(log_event) bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: delegation = function(event) bsw/jbe@1309: -- TODO bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: member_activated = function(event) bsw/jbe@1309: local log_event = new_log_event(event, event.member_id, "account_registered") bsw/jbe@1309: log_to_ontomap(log_event) bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: member_removed = function(event) bsw/jbe@1309: -- TODO -> which activity to log? bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: member_active = function(event) bsw/jbe@1309: -- TODO -> which activity to log? bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: member_name_updated = function(event) bsw/jbe@1309: local log_event = new_log_event(event, event.member_id, "screen_name_changed") bsw/jbe@1309: log_event.details.screen_name = event.text_value bsw/jbe@1309: log_to_ontomap(log_event) bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: member_profile_updated = function(event) bsw/jbe@1309: local log_event = new_log_event(event, event.member_id, "profile_updated") bsw/jbe@1309: log_to_ontomap(log_event) bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: member_image_updated = function(event) bsw/jbe@1309: local log_event = new_log_event(event, event.member_id, "avatar_changed") bsw/jbe@1309: log_to_ontomap(log_event) bsw/jbe@1309: end, bsw/jbe@1309: bsw/jbe@1309: contact = function(event) bsw/jbe@1309: local activity_type = event.boolean_value and "contact_published" or "contact_unpublished" bsw/jbe@1309: local log_event = new_log_event(event, event.member_id, activity_type) bsw/jbe@1309: log_event.details.other_member_id = event.other_member_id bsw/jbe@1309: log_to_ontomap(log_event) bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: } bsw/jbe@1309: bsw/jbe@1309: function _G.ontomap_log_event(event) bsw/jbe@1309: bsw/jbe@1309: if mapper[event.event] then bsw/jbe@1309: local e = Event:new_selector() bsw/jbe@1309: :add_where{ "id = ?", event.id } bsw/jbe@1309: :add_field("extract(epoch from occurrence)", "occurrence_epoch") bsw/jbe@1309: :optional_object_mode() bsw/jbe@1309: :exec() bsw/jbe@1309: if e then bsw/jbe@1309: mapper[event.event](e) bsw/jbe@1309: end bsw/jbe@1309: end bsw/jbe@1309: bsw/jbe@1309: bsw/jbe@1309: end