liquid_feedback_frontend
diff lib/ontomap/ontomap.lua @ 1309:32cc544d5a5b
Cumulative patch for upcoming frontend version 4
author | bsw/jbe |
---|---|
date | Sun Jul 15 14:07:29 2018 +0200 (2018-07-15) |
parents | |
children | 056bccb61eee |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/lib/ontomap/ontomap.lua Sun Jul 15 14:07:29 2018 +0200 1.3 @@ -0,0 +1,382 @@ 1.4 +function _G.ontomap_get_instances(event) 1.5 + if true then return {} end 1.6 + local url = config.ontomap.base_url .. "instances/Issue?geometries=true&descriptions=true" 1.7 + print("OnToMap>>") 1.8 + 1.9 + local output, err, status = extos.pfilter(doc, "curl", "-X", "GET", "-H", "Content-Type: application/json", "--cert", config.ontomap.client_cert_file, "-d", "@-", url) 1.10 + print(output) 1.11 + 1.12 + local data = json.import(output) 1.13 + 1.14 + if not data then 1.15 + return {} 1.16 + end 1.17 + 1.18 + if data.type ~= "FeatureCollection" or not data.features then 1.19 + return {} 1.20 + end 1.21 + 1.22 + local instances = {} 1.23 + for i, feature in ipairs(data.features) do 1.24 + if feature.geometry then 1.25 + table.insert(instances, { 1.26 + application = feature.applicationName, 1.27 + title = feature.hasName, 1.28 + description = slot.use_temporary(function() 1.29 + ui.link{ external = feature.properties.external_url, text = feature.properties.hasName or "" } 1.30 + ui.container{ content = feature.hasDescription } 1.31 + end), 1.32 + lon = feature.geometry.coordinates[1], 1.33 + lat = feature.geometry.coordinates[2], 1.34 + label = "IMC" .. feature.properties.hasID, 1.35 + type = "Improve My City" 1.36 + }) 1.37 + print(feature.applicationName, feature.properties.hasName, feature.properties.hasDescription) 1.38 + end 1.39 + end 1.40 + return instances 1.41 + 1.42 +end 1.43 + 1.44 + 1.45 +local function new_log_event(event, actor, activity_type) 1.46 + local e = json.object{ 1.47 + activity_type = activity_type, 1.48 + activity_objects = json.array(), 1.49 + references = json.array(), 1.50 + visibility_details = json.array(), 1.51 + details = json.object() 1.52 + } 1.53 + e.actor = actor 1.54 + e.timestamp = math.floor(event.occurrence_epoch * 1000) 1.55 + return e 1.56 +end 1.57 + 1.58 +local function new_activity_object(attr) 1.59 + return json.object{ 1.60 + type = "Feature", 1.61 + geometry = attr.geometry or json.null, 1.62 + properties = json.object{ 1.63 + hasType = attr.type, 1.64 + external_url = attr.url 1.65 + } 1.66 + } 1.67 +end 1.68 + 1.69 +local function new_reference_object(url, application) 1.70 + return json.object{ 1.71 + application = application or config.ontomap.application_ident, 1.72 + external_url = url 1.73 + } 1.74 +end 1.75 + 1.76 +local function log_to_ontomap(log_events) 1.77 + if json.type(log_events) == "object" then 1.78 + log_events = json.array{ log_events } 1.79 + end 1.80 + for i, log_event in ipairs(log_events) do 1.81 + if #(log_event.activity_objects) == 0 then 1.82 + log_event.activity_objects = nil 1.83 + end 1.84 + if #(log_event.references) == 0 then 1.85 + log_event.references = nil 1.86 + end 1.87 + if #(log_event.visibility_details) == 0 then 1.88 + log_event.visibility_details = nil 1.89 + end 1.90 + if not (next(log_event.details)) then -- TODO 1.91 + log_event.details = nil 1.92 + end 1.93 + end 1.94 + 1.95 + local doc = json.export(json.object{ 1.96 + event_list = log_events 1.97 + }, " ") 1.98 + 1.99 + local url = config.ontomap.base_url .. "logger/events" 1.100 + print("OnToMap<<") 1.101 + print(doc) 1.102 + 1.103 + local output, err, status = extos.pfilter(doc, "curl", "-X", "POST", "-H", "Content-Type: application/json", "--cert", config.ontomap.client_cert_file, "-d", "@-", url) 1.104 + 1.105 + print("---------") 1.106 + print(output) 1.107 + print("---------") 1.108 + 1.109 + if err then 1.110 + -- TODO log error 1.111 + end 1.112 +end 1.113 + 1.114 +local function url_for(relation, id) 1.115 + return config.absolute_base_url .. relation .. "/show/" .. id .. ".html" 1.116 +end 1.117 + 1.118 + 1.119 +local function unit_updated(event, event_type) 1.120 + local log_event = new_log_event(event, 0, event_type == "created" and "object_created" or "object_updated") 1.121 + table.insert(log_event.activity_objects, new_activity_object{ 1.122 + type = "unit", 1.123 + url = url_for("unit", event.unit_id) 1.124 + }) 1.125 + log_event.details.active = event.unit.active 1.126 + log_event.details.name = event.unit.name 1.127 + log_event.details.description = event.unit.description 1.128 + log_event.details.external_reference = event.unit.external_reference 1.129 + log_event.details.region = event.unit.region 1.130 + log_to_ontomap(log_event) 1.131 +end 1.132 + 1.133 +local function area_updated(event, event_type) 1.134 + local log_event = new_log_event(event, 0, event_type == "created" and "object_created" or "object_updated") 1.135 + table.insert(log_event.activity_objects, new_activity_object{ 1.136 + type = "area", 1.137 + url = url_for("area", event.area_id) 1.138 + }) 1.139 + table.insert(log_event.references, new_reference_object( 1.140 + url_for("unit", event.area.unit_id) 1.141 + )) 1.142 + log_event.details.active = event.area.active 1.143 + log_event.details.name = event.area.name 1.144 + log_event.details.description = event.area.description 1.145 + log_event.details.external_reference = event.area.external_reference 1.146 + log_event.details.region = event.area.region 1.147 + log_to_ontomap(log_event) 1.148 +end 1.149 + 1.150 +local function policy_updated(event, event_type) 1.151 + local log_event = new_log_event(event, 0, event_type == "created" and "object_created" or "object_updated") 1.152 + table.insert(log_event.activity_objects, new_activity_object{ 1.153 + type = "policy", 1.154 + url = url_for("policy", event.policy_id) 1.155 + }) 1.156 + log_event.details.active = event.policy.active 1.157 + log_event.details.name = event.policy.name 1.158 + log_event.details.description = event.policy.description 1.159 + log_to_ontomap(log_event) 1.160 +end 1.161 + 1.162 +local mapper = { 1.163 + 1.164 + unit_created = function(event) 1.165 + unit_updated(event, "created") 1.166 + end, 1.167 + 1.168 + unit_updated = function(event) 1.169 + unit_updated(event, "updated") 1.170 + end, 1.171 + 1.172 + area_created = function(event) 1.173 + area_updated(event, "created") 1.174 + end, 1.175 + 1.176 + area_updated = function(event) 1.177 + area_updated(event, "updated") 1.178 + end, 1.179 + 1.180 + policy_created = function(event) 1.181 + policy_updated(event, "created") 1.182 + end, 1.183 + 1.184 + policy_updated = function(event) 1.185 + policy_updated(event, "updated") 1.186 + end, 1.187 + 1.188 + issue_state_changed = function(event) 1.189 + local log_event = new_log_event(event, 0, "issue_status_updated") 1.190 + table.insert(log_event.references, new_reference_object( 1.191 + url_for("issue", event.issue_id) 1.192 + )) 1.193 + log_event.details.new_issue_state = event.state 1.194 + log_to_ontomap(log_event) 1.195 + end, 1.196 + 1.197 + initiative_created_in_new_issue = function(event) 1.198 + local log_events = json.array() 1.199 + 1.200 + local log_event = new_log_event(event, 0, "object_created") 1.201 + table.insert(log_event.activity_objects, new_activity_object{ 1.202 + type = "issue", 1.203 + url = url_for("issue", event.issue_id) 1.204 + }) 1.205 + table.insert(log_event.references, new_reference_object( 1.206 + url_for("policy", event.issue.policy_id) 1.207 + )) 1.208 + log_event.details.new_issue_state = event.state 1.209 + table.insert(log_events, log_event) 1.210 + 1.211 + local log_event = new_log_event(event, event.member_id, "object_created") 1.212 + table.insert(log_event.activity_objects, new_activity_object{ 1.213 + type = "initiative", 1.214 + url = url_for("initiative", event.initiative_id), 1.215 + geometry = event.initiative.location 1.216 + }) 1.217 + table.insert(log_event.references, new_reference_object( 1.218 + url_for("issue", event.issue_id) 1.219 + )) 1.220 + log_event.details.name = event.initiative.name 1.221 + table.insert(log_events, log_event) 1.222 + 1.223 + log_to_ontomap(log_events) 1.224 + end, 1.225 + 1.226 + initiative_created_in_existing_issue = function(event) 1.227 + local log_event = new_log_event(event, event.member_id, "object_created") 1.228 + table.insert(log_event.activity_objects, new_activity_object{ 1.229 + type = "initiative", 1.230 + url = url_for("initiative", event.initiative_id), 1.231 + geometry = event.initiative.location 1.232 + }) 1.233 + table.insert(log_event.references, new_reference_object( 1.234 + url_for("issue", event.issue_id) 1.235 + )) 1.236 + log_event.details.name = event.initiative.name 1.237 + log_to_ontomap(log_event) 1.238 + end, 1.239 + 1.240 + initiative_revoked = function(event) 1.241 + -- TODO -> which activity? 1.242 + end, 1.243 + 1.244 + new_draft_created = function(event) 1.245 + local log_event = new_log_event(event, event.member_id, "object_updated") 1.246 + table.insert(log_event.activity_objects, new_activity_object{ 1.247 + type = "initiative", 1.248 + url = url_for("initiative", event.issue_id) 1.249 + }) 1.250 + table.insert(log_event.references, new_reference_object( 1.251 + url_for("issue", event.issue_id) 1.252 + )) 1.253 + log_event.details.name = event.initiative.name 1.254 + log_event.details.location = event.initiative.current_draft.location 1.255 + log_to_ontomap(log_event) 1.256 + end, 1.257 + 1.258 + interest = function(event) 1.259 + local activity_type = event.boolean_value and "interest_added" or "interest_removed" 1.260 + local log_event = new_log_event(event, event.member_id, activity_type) 1.261 + table.insert(log_event.references, new_reference_object( 1.262 + url_for("issue", event.issue_id) 1.263 + )) 1.264 + log_to_ontomap(log_event) 1.265 + end, 1.266 + 1.267 + initiator = function(event) 1.268 + local activity_type = event.boolean_value and "initiator_added" or "initiator_removed" 1.269 + local log_event = new_log_event(event, event.member_id, activity_type) 1.270 + table.insert(log_event.references, new_reference_object( 1.271 + url_for("initiative", event.initiative_id) 1.272 + )) 1.273 + log_to_ontomap(log_event) 1.274 + end, 1.275 + 1.276 + support = function(event) 1.277 + local activity_type = event.boolean_value and "support_added" or "support_removed" 1.278 + local log_event = new_log_event(event, event.member_id, activity_type) 1.279 + table.insert(log_event.references, new_reference_object( 1.280 + url_for("initiative", event.initiative_id) 1.281 + )) 1.282 + log_event.details.draft_id = event.draft_id 1.283 + log_to_ontomap(log_event) 1.284 + end, 1.285 + 1.286 + support_updated = function(event) 1.287 + local log_event = new_log_event(event, event.member_id, "support_updated") 1.288 + table.insert(log_event.references, new_reference_object( 1.289 + url_for("initiative", event.initiative_id) 1.290 + )) 1.291 + log_event.details.draft_id = event.draft_id 1.292 + log_to_ontomap(log_event) 1.293 + end, 1.294 + 1.295 + suggestion_created = function(event) 1.296 + local log_event = new_log_event(event, event.member_id, "object_created") 1.297 + table.insert(log_event.activity_objects, new_activity_object{ 1.298 + type = "suggestion", 1.299 + url = url_for("suggestion", event.suggestion_id) 1.300 + }) 1.301 + table.insert(log_event.references, new_reference_object( 1.302 + url_for("initiative", event.initiative_id) 1.303 + )) 1.304 + log_to_ontomap(log_event) 1.305 + end, 1.306 + 1.307 + suggestion_removed = function(event) 1.308 + local log_event = new_log_event(event, 0, "object_removed") 1.309 + table.insert(log_event.activity_objects, new_activity_object{ 1.310 + type = "suggestion", 1.311 + url = url_for("suggestion", event.suggestion_id) 1.312 + }) 1.313 + table.insert(log_event.references, new_reference_object( 1.314 + url_for("initiative", event.initiative_id) 1.315 + )) 1.316 + log_to_ontomap(log_event) 1.317 + end, 1.318 + 1.319 + suggestion_rated = function(event) 1.320 + local log_event = new_log_event(event, event.member_id, "suggestion_rated") 1.321 + table.insert(log_event.references, new_reference_object( 1.322 + url_for("suggestion", event.suggestion_id) 1.323 + )) 1.324 + log_event.details.degree = event.numeric_value 1.325 + log_event.details.fulfilled = event.boolean_value or json.null 1.326 + log_to_ontomap(log_event) 1.327 + end, 1.328 + 1.329 + delegation = function(event) 1.330 + -- TODO 1.331 + end, 1.332 + 1.333 + member_activated = function(event) 1.334 + local log_event = new_log_event(event, event.member_id, "account_registered") 1.335 + log_to_ontomap(log_event) 1.336 + end, 1.337 + 1.338 + member_removed = function(event) 1.339 + -- TODO -> which activity to log? 1.340 + end, 1.341 + 1.342 + member_active = function(event) 1.343 + -- TODO -> which activity to log? 1.344 + end, 1.345 + 1.346 + member_name_updated = function(event) 1.347 + local log_event = new_log_event(event, event.member_id, "screen_name_changed") 1.348 + log_event.details.screen_name = event.text_value 1.349 + log_to_ontomap(log_event) 1.350 + end, 1.351 + 1.352 + member_profile_updated = function(event) 1.353 + local log_event = new_log_event(event, event.member_id, "profile_updated") 1.354 + log_to_ontomap(log_event) 1.355 + end, 1.356 + 1.357 + member_image_updated = function(event) 1.358 + local log_event = new_log_event(event, event.member_id, "avatar_changed") 1.359 + log_to_ontomap(log_event) 1.360 + end, 1.361 + 1.362 + contact = function(event) 1.363 + local activity_type = event.boolean_value and "contact_published" or "contact_unpublished" 1.364 + local log_event = new_log_event(event, event.member_id, activity_type) 1.365 + log_event.details.other_member_id = event.other_member_id 1.366 + log_to_ontomap(log_event) 1.367 + end 1.368 + 1.369 +} 1.370 + 1.371 +function _G.ontomap_log_event(event) 1.372 + 1.373 + if mapper[event.event] then 1.374 + local e = Event:new_selector() 1.375 + :add_where{ "id = ?", event.id } 1.376 + :add_field("extract(epoch from occurrence)", "occurrence_epoch") 1.377 + :optional_object_mode() 1.378 + :exec() 1.379 + if e then 1.380 + mapper[event.event](e) 1.381 + end 1.382 + end 1.383 + 1.384 + 1.385 +end