liquid_feedback_frontend
view lib/ontomap/ontomap.lua @ 1372:635c6b7d2d0a
Work around layout issue
| author | bsw | 
|---|---|
| date | Wed Aug 08 17:15:48 2018 +0200 (2018-08-08) | 
| parents | 32cc544d5a5b | 
| children | 056bccb61eee | 
 line source
     1 function _G.ontomap_get_instances(event)
     2   if true then return {} end 
     3   local url = config.ontomap.base_url .. "instances/Issue?geometries=true&descriptions=true"
     4   print("OnToMap>>")
     6   local output, err, status = extos.pfilter(doc, "curl", "-X", "GET", "-H", "Content-Type: application/json", "--cert", config.ontomap.client_cert_file, "-d", "@-", url)
     7   print(output)
     9   local data = json.import(output)
    11   if not data then
    12     return {}
    13   end
    15   if data.type ~= "FeatureCollection" or not data.features then
    16     return {}
    17   end
    19   local instances = {}
    20   for i, feature in ipairs(data.features) do
    21     if feature.geometry then
    22       table.insert(instances, {
    23         application = feature.applicationName,
    24         title = feature.hasName,
    25         description = slot.use_temporary(function()
    26           ui.link{ external = feature.properties.external_url, text = feature.properties.hasName or "" }
    27           ui.container{ content = feature.hasDescription }
    28         end),
    29         lon = feature.geometry.coordinates[1],
    30         lat = feature.geometry.coordinates[2],
    31         label = "IMC" .. feature.properties.hasID,
    32         type = "Improve My City"
    33       })
    34       print(feature.applicationName, feature.properties.hasName, feature.properties.hasDescription)
    35     end
    36   end
    37   return instances
    39 end
    42 local function new_log_event(event, actor, activity_type)
    43   local e = json.object{
    44     activity_type = activity_type,
    45     activity_objects = json.array(),
    46     references = json.array(),
    47     visibility_details = json.array(),
    48     details = json.object()
    49   }
    50   e.actor = actor
    51   e.timestamp = math.floor(event.occurrence_epoch * 1000)
    52   return e
    53 end
    55 local function new_activity_object(attr)
    56   return json.object{
    57     type = "Feature",
    58     geometry = attr.geometry or json.null,
    59     properties = json.object{
    60       hasType = attr.type,
    61       external_url = attr.url
    62     }
    63   }
    64 end
    66 local function new_reference_object(url, application)
    67   return json.object{
    68     application = application or config.ontomap.application_ident,
    69     external_url = url
    70   }
    71 end
    73 local function log_to_ontomap(log_events)
    74   if json.type(log_events) == "object" then
    75     log_events = json.array{ log_events }
    76   end
    77   for i, log_event in ipairs(log_events) do
    78     if #(log_event.activity_objects) == 0 then
    79       log_event.activity_objects = nil
    80     end
    81     if #(log_event.references) == 0 then
    82       log_event.references = nil
    83     end
    84     if #(log_event.visibility_details) == 0 then
    85       log_event.visibility_details = nil
    86     end
    87     if not (next(log_event.details)) then -- TODO
    88       log_event.details = nil
    89     end
    90   end
    92   local doc = json.export(json.object{
    93     event_list = log_events
    94   }, "  ")
    96   local url = config.ontomap.base_url .. "logger/events"
    97   print("OnToMap<<")
    98   print(doc)
   100   local output, err, status = extos.pfilter(doc, "curl", "-X", "POST", "-H", "Content-Type: application/json", "--cert", config.ontomap.client_cert_file, "-d", "@-", url)
   102   print("---------")
   103   print(output)
   104   print("---------")
   106   if err then
   107     -- TODO log error
   108   end
   109 end
   111 local function url_for(relation, id)
   112   return config.absolute_base_url .. relation .. "/show/" .. id .. ".html"
   113 end
   116 local function unit_updated(event, event_type)
   117   local log_event = new_log_event(event, 0, event_type == "created" and "object_created" or "object_updated")
   118   table.insert(log_event.activity_objects, new_activity_object{
   119     type = "unit",
   120     url = url_for("unit", event.unit_id)
   121   })
   122   log_event.details.active = event.unit.active
   123   log_event.details.name = event.unit.name
   124   log_event.details.description = event.unit.description
   125   log_event.details.external_reference = event.unit.external_reference
   126   log_event.details.region = event.unit.region
   127   log_to_ontomap(log_event)
   128 end
   130 local function area_updated(event, event_type)
   131   local log_event = new_log_event(event, 0, event_type == "created" and "object_created" or "object_updated")
   132   table.insert(log_event.activity_objects, new_activity_object{
   133     type = "area",
   134     url = url_for("area", event.area_id)
   135   })
   136   table.insert(log_event.references, new_reference_object(
   137     url_for("unit", event.area.unit_id)
   138   ))
   139   log_event.details.active = event.area.active
   140   log_event.details.name = event.area.name
   141   log_event.details.description = event.area.description
   142   log_event.details.external_reference = event.area.external_reference
   143   log_event.details.region = event.area.region
   144   log_to_ontomap(log_event)
   145 end
   147 local function policy_updated(event, event_type)
   148   local log_event = new_log_event(event, 0, event_type == "created" and "object_created" or "object_updated")
   149   table.insert(log_event.activity_objects, new_activity_object{
   150     type = "policy",
   151     url = url_for("policy", event.policy_id)
   152   })
   153   log_event.details.active = event.policy.active
   154   log_event.details.name = event.policy.name
   155   log_event.details.description = event.policy.description
   156   log_to_ontomap(log_event)
   157 end
   159 local mapper = {
   161   unit_created = function(event)
   162     unit_updated(event, "created") 
   163   end,
   165   unit_updated = function(event)
   166     unit_updated(event, "updated") 
   167   end,
   169   area_created = function(event)
   170     area_updated(event, "created")
   171   end,
   173   area_updated = function(event)
   174     area_updated(event, "updated")
   175   end,
   177   policy_created = function(event)
   178     policy_updated(event, "created")
   179   end,
   181   policy_updated = function(event)
   182     policy_updated(event, "updated")
   183   end,
   185   issue_state_changed = function(event)
   186     local log_event = new_log_event(event, 0, "issue_status_updated")
   187     table.insert(log_event.references, new_reference_object(
   188       url_for("issue", event.issue_id)
   189     ))
   190     log_event.details.new_issue_state = event.state
   191     log_to_ontomap(log_event)
   192   end,
   194   initiative_created_in_new_issue = function(event)
   195     local log_events = json.array()
   197     local log_event = new_log_event(event, 0, "object_created")
   198     table.insert(log_event.activity_objects, new_activity_object{
   199       type = "issue",
   200       url = url_for("issue", event.issue_id)
   201     })
   202     table.insert(log_event.references, new_reference_object(
   203       url_for("policy", event.issue.policy_id)
   204     ))
   205     log_event.details.new_issue_state = event.state
   206     table.insert(log_events, log_event)
   208     local log_event = new_log_event(event, event.member_id, "object_created")
   209     table.insert(log_event.activity_objects, new_activity_object{
   210       type = "initiative",
   211       url = url_for("initiative", event.initiative_id),
   212       geometry = event.initiative.location
   213     })
   214     table.insert(log_event.references, new_reference_object(
   215       url_for("issue", event.issue_id)
   216     ))
   217     log_event.details.name = event.initiative.name
   218     table.insert(log_events, log_event)
   220     log_to_ontomap(log_events)
   221   end,
   223   initiative_created_in_existing_issue = function(event)
   224     local log_event = new_log_event(event, event.member_id, "object_created")
   225     table.insert(log_event.activity_objects, new_activity_object{
   226       type = "initiative",
   227       url = url_for("initiative", event.initiative_id),
   228       geometry = event.initiative.location
   229     })
   230     table.insert(log_event.references, new_reference_object(
   231       url_for("issue", event.issue_id)
   232     ))
   233     log_event.details.name = event.initiative.name
   234     log_to_ontomap(log_event)
   235   end,
   237   initiative_revoked = function(event)
   238     -- TODO -> which activity?
   239   end,
   241   new_draft_created = function(event)
   242     local log_event = new_log_event(event, event.member_id, "object_updated")
   243     table.insert(log_event.activity_objects, new_activity_object{
   244       type = "initiative",
   245       url = url_for("initiative", event.issue_id)
   246     })
   247     table.insert(log_event.references, new_reference_object(
   248       url_for("issue", event.issue_id)
   249     ))
   250     log_event.details.name = event.initiative.name
   251     log_event.details.location = event.initiative.current_draft.location
   252     log_to_ontomap(log_event)
   253   end,
   255   interest = function(event)
   256     local activity_type = event.boolean_value and "interest_added" or "interest_removed"
   257     local log_event = new_log_event(event, event.member_id, activity_type)
   258     table.insert(log_event.references, new_reference_object(
   259       url_for("issue", event.issue_id)
   260     ))
   261     log_to_ontomap(log_event)
   262   end,
   264   initiator = function(event)
   265     local activity_type = event.boolean_value and "initiator_added" or "initiator_removed"
   266     local log_event = new_log_event(event, event.member_id, activity_type)
   267     table.insert(log_event.references, new_reference_object(
   268       url_for("initiative", event.initiative_id)
   269     ))
   270     log_to_ontomap(log_event)
   271   end,
   273   support = function(event)
   274     local activity_type = event.boolean_value and "support_added" or "support_removed"
   275     local log_event = new_log_event(event, event.member_id, activity_type)
   276     table.insert(log_event.references, new_reference_object(
   277       url_for("initiative", event.initiative_id)
   278     ))
   279     log_event.details.draft_id = event.draft_id
   280     log_to_ontomap(log_event)
   281   end,
   283   support_updated = function(event)
   284     local log_event = new_log_event(event, event.member_id, "support_updated")
   285     table.insert(log_event.references, new_reference_object(
   286       url_for("initiative", event.initiative_id)
   287     ))
   288     log_event.details.draft_id = event.draft_id
   289     log_to_ontomap(log_event)
   290   end,
   292   suggestion_created = function(event)
   293     local log_event = new_log_event(event, event.member_id, "object_created")
   294     table.insert(log_event.activity_objects, new_activity_object{
   295       type = "suggestion",
   296       url = url_for("suggestion", event.suggestion_id)
   297     })
   298     table.insert(log_event.references, new_reference_object(
   299       url_for("initiative", event.initiative_id)
   300     ))
   301     log_to_ontomap(log_event)
   302   end,
   304   suggestion_removed = function(event)
   305     local log_event = new_log_event(event, 0, "object_removed")
   306     table.insert(log_event.activity_objects, new_activity_object{
   307       type = "suggestion",
   308       url = url_for("suggestion", event.suggestion_id)
   309     })
   310     table.insert(log_event.references, new_reference_object(
   311       url_for("initiative", event.initiative_id)
   312     ))
   313     log_to_ontomap(log_event)
   314   end,
   316   suggestion_rated = function(event)
   317     local log_event = new_log_event(event, event.member_id, "suggestion_rated")
   318     table.insert(log_event.references, new_reference_object(
   319       url_for("suggestion", event.suggestion_id)
   320     ))
   321     log_event.details.degree = event.numeric_value
   322     log_event.details.fulfilled = event.boolean_value or json.null
   323     log_to_ontomap(log_event)
   324   end,
   326   delegation = function(event)
   327     -- TODO
   328   end,
   330   member_activated = function(event)
   331     local log_event = new_log_event(event, event.member_id, "account_registered")
   332     log_to_ontomap(log_event)
   333   end,
   335   member_removed = function(event)
   336     -- TODO -> which activity to log?
   337   end,
   339   member_active = function(event)
   340     -- TODO -> which activity to log?
   341   end,
   343   member_name_updated = function(event)
   344     local log_event = new_log_event(event, event.member_id, "screen_name_changed")
   345     log_event.details.screen_name = event.text_value
   346     log_to_ontomap(log_event)
   347   end,
   349   member_profile_updated = function(event)
   350     local log_event = new_log_event(event, event.member_id, "profile_updated")
   351     log_to_ontomap(log_event)
   352   end,
   354   member_image_updated = function(event)
   355     local log_event = new_log_event(event, event.member_id, "avatar_changed")
   356     log_to_ontomap(log_event)
   357   end,
   359   contact = function(event)
   360     local activity_type = event.boolean_value and "contact_published" or "contact_unpublished"
   361     local log_event = new_log_event(event, event.member_id, activity_type)
   362     log_event.details.other_member_id = event.other_member_id
   363     log_to_ontomap(log_event)
   364   end
   366 }
   368 function _G.ontomap_log_event(event)
   370   if mapper[event.event] then
   371     local e = Event:new_selector()
   372       :add_where{ "id = ?", event.id }
   373       :add_field("extract(epoch from occurrence)", "occurrence_epoch")
   374       :optional_object_mode()
   375       :exec()
   376     if e then
   377       mapper[event.event](e)
   378     end
   379   end
   382 end
