Example Dgraph recurse sum query - recursion

New Dgraph user wondering if anyone can provide me with an example recursive count and sum query to help get me going.
The data looks like this (there are more predicates, but left out for simplicity):
{
"uid" : <0x1>,
"url" : "example.com",
"link" : [
{
"uid" : <0x2>,
"url" : "example2.com",
"link" : [
{
"uid" : <0x4>,
"url" : "example4.com",
"link" : [
{
"uid" : <0x6>,
"url" : "example6.com",
"link" : [
{
etc...
}
]
}
]
},
{
"uid" : <0x5>,
"url" : "example5.com",
}
]
},
{
"uid" : <0x2>,
"url" : "example2.com",
"link" : [
{
etc ....
}
},
]
}
Just a home page with n-links which each have n-links and the depth, obviously, can vary. Just hoping for a good example of how to count all the links for each url and sum them up. I will add different filters to the query at some point, but just wanting to see a basic query to help get me going. Thanks.

Related

Rules scheduler for expire role and sending reminder email fire all at the same time

I have 3 rules which make up my "membership".
Assign role when product X gets purchased (This one works!)
Send a reminder email after 2 minutes.
Remove role after 4 minutes.
Please note the 2 and 4 minutes are only there for testing purposes, they will become 350 and 365 days.
While both 2. or 3. work, I have two issues:
Rules n2 and n3 fire at the same time, so at 2 mins mark not only the email is sent, but also the role is removed.
Let's say user MARK purchase product X, and a minute after, JOE does the same. After 2 minutes that MARK purchased the product X, MARK gets the reminder email, his role is removed but also JOE gets the reminder email, and his role is removed.
It seems as soon something triggers, all do.
As you can see from the exports, I made one rule trigger from x minutes of the commerce order completed, and the other from the role assigned, thinking that relating to 2 different events, I would solve the issue. It didn't work.
Here are the exports of my rules:
Rule "Send a reminder" component:
{ "rules_sends_email_to_pipps_reminder_1_week_prior_expiry" : {
"LABEL" : "Sends Email to PIPPS Reminder 1 week prior expiry",
"PLUGIN" : "rule set",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"USES VARIABLES" : { "user_argument" : { "label" : "User {argument}", "type" : "user" } },
"RULES" : [
{ "RULE" : {
"DO" : [
{ "mail" : {
"to" : "[commerce-order:mail]",
"subject" : "Heads up!",
"message" : "Yo! Heads up!",
"from" : "pipps#siteemail.com",
"language" : [ "site:current-cart-order:state" ]
}
}
],
"LABEL" : "Send email reminder to PIPPS"
}
}
]
}
}
Rule "Send a reminder" trigger:
{ "rules_send_reminder_email_to_pipps_role_trigger_rule_" : {
"LABEL" : "Send reminder email to PIPPS Role {trigger rule}",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "commerce_order", "rules", "rules_scheduler", "commerce_checkout" ],
"ON" : { "commerce_checkout_complete" : [] },
"IF" : [
{ "commerce_order_contains_product" : {
"commerce_order" : [ "commerce_order" ],
"product_id" : "PIPPS",
"operator" : "=",
"value" : "1"
}
}
],
"DO" : [
{ "schedule" : {
"component" : "rules_sends_email_to_pipps_reminder_1_week_prior_expiry",
"date" : {
"select" : "site:current-cart-order:created",
"date_offset" : { "value" : 120 }
},
"identifier" : "Reminder email to [account:uid]",
"param_user_argument" : [ "commerce-order:owner" ]
}
}
]
}
}
Rule "Role Expire" component:
{ "rules_expire_pipps_role_rule_set_" : {
"LABEL" : "Expire PIPPS Role {rule_set}",
"PLUGIN" : "rule set",
"OWNER" : "rules",
"REQUIRES" : [ "rules" ],
"USES VARIABLES" : { "user_argument" : { "label" : "User {argument}", "type" : "user" } },
"RULES" : [
{ "RULE" : {
"DO" : [
{ "user_remove_role" : {
"account" : [ "user_argument" ],
"roles" : { "value" : { "13" : "13" } }
}
}
],
"LABEL" : "Expire action {rule}"
}
}
]
}
}
Rule "Expire Role" trigger:
{ "rules_remove_pipps_role_trigger_rule_" : {
"LABEL" : "Remove PIPPS Role {trigger rule}",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "rules", "rules_scheduler" ],
"ON" : { "user_update" : [] },
"IF" : [
{ "user_has_role" : { "account" : [ "account" ], "roles" : { "value" : { "13" : "13" } } } }
],
"DO" : [
{ "schedule" : {
"component" : "rules_expire_pipps_role_rule_set_",
"date" : {
"select" : "site:current-cart-order:created",
"date_offset" : { "value" : 240 }
},
"identifier" : "Remove User role [account:uid]",
"param_user_argument" : [ "account" ]
}
}
]
}
}
Any suggestion is very appreciated! Thanks.
Your Rules (and Rules Components), seem to "work as implemented" ... You 'issue' appears to be that you should be more patient during your testing ... Read on for more details ...
Understanding what is happening
After a rule is scheduled (using the Rules Scheduler submodule), it will only actually execute next time cron runs.
Example:
schedule a 1st Rules Component to execute at timestamp X1.
if the next time that cron runs is at Y mins after timestamp X1, then the execution of this 1st Rules Component will only happen then (= at Y mins after timestamp X1).
schedule a 2nd Rules Component to execute at timestamp X2.
if the next time that cron runs is at Y mins after timestamp X2, then the execution of this 2nd Rules Component will only happen then (= at Y mins after timestamp X2).
Because of what is described in the scenario of your question, it seems that timestamp X2 is happening BEFORE Y mins after timestamp X1. That would also explain why BOTH Rules Components actually execute (more or less) at the same time.
Remedy to make it work as you really want
Assuming that your cron jobs are running every hour, a straight forward (minor) fix to your Rules Components that is scheduled after 4 minutes, would be to schedule them after (e.g) 2 hours instead. So that you are sure that both Rules Component are not actually executed during the very same cron job.

Loading mapbox with Firebase database

I'm trying to learn Firebase and Mapbox and wanted to integrate the two. Firebase stores some of my data in the following format:
{
"messages" : {
"-KUE2EwfvbI48Azw01Hv" : {
"geometry" : {
"coordinates" : [ 28.6618976, 77.22739580000007 ],
"type" : "Point"
},
"properties" : {
"description" : "xyz",
"hashtag" : "#xyz",
"imageUrl" : "xyz.jpg",
"name" : "Xyz Xyz",
"photoUrl" : "xyz.jpg",
"title" : "XYZ"
},
"type" : "Issue"
},
"-KUD2EwfvbI48Azw01Hv" : {
"geometry" : {
"coordinates" : [ 12.9715987, 77.59456269999998 ],
"type" : "Point"
},
"properties" : {
"description" : "xyz",
"hashtag" : "#xyz",
"imageUrl" : "xyz.jpg",
"name" : "Xyz Xyz",
"photoUrl" : "xyz.jpg",
"title" : "XYZ"
},
"type" : "Issue"
}
}
}
Is there a way to load the data and plot it into Mapbox? The examples require a GeoJSON file hosted somewhere that can be used to plot them. How can we use the Firebase database to plot on the Mapbox in realtime?
Sorry if my question is ambiguous. I'm willing to provide more information if needed :D
Thanks!
You can load the data, but you first have to convert it to a valid GeoJSON object.
Here is a JSFiddle using the data you provided:
https://jsfiddle.net/mkrv9uuy/
var firebaseGeojsonFeatures = [];
for (var key in firebaseData.messages) {
var f = firebaseData.messages[key];
f.type = "Feature";
firebaseGeojsonFeatures.push(f);
}

Using Usergrid how do I get related entities nested in a single json and not only the link to them

When I query /mycollections?ql=Select * where name='dfsdfsdfsdfsdfsdf' I get
{
"action" : "get",
"application" : "859e6180-de8a-11e4-9360-f1aabbc15f58",
"params" : {
"ql" : [ "Select * where name='dfsdfsdfsdfsdfsdf'" ]
},
"path" : "/mycollections",
"uri" : "http://localhost:8080/myorg/myapp/mycollections",
"entities" : [ {
"uuid" : "2ff8961a-dea8-11e4-996b-63ce373ace35",
"type" : "mycollection",
"name" : "dfsdfsdfsdfsdfsdf",
"created" : 1428577466865,
"modified" : 1428577466865,
"metadata" : {
"path" : "/mycollections/2ff8961a-dea8-11e4-996b-63ce373ace35",
"connections" : {
"relations" : "/mycollections/2ff8961a-dea8-11e4-996b-63ce373ace35/relations"
}
}
} ],
"timestamp" : 1428589309204,
"duration" : 53,
"organization" : "myorg",
"applicationName" : "myapp",
"count" : 1
}
Now if I query /mycollections/2ff8961a-dea8-11e4-996b-63ce373ace35/relations I get the second entity
{
"action" : "get",
"application" : "859e6180-de8a-11e4-9360-f1aabbc15f58",
"params" : { },
"path" : "/mycollections/2ff8961a-dea8-11e4-996b-63ce373ace35/relations",
"uri" : "http://localhost:8080/myorg/myapp/mycollections/2ff8961a-dea8-11e4-996b-63ce373ace35/relations",
"entities" : [ {
"uuid" : "56a1185a-dec1-11e4-9ac0-e9343f86b604",
"type" : "secondcollection",
"name" : "coucou",
"created" : 1428588269141,
"modified" : 1428588269141,
"metadata" : {
"connecting" : {
"relations" : "/mycollections/2ff8961a-dea8-11e4-996b-63ce373ace35/relations/56a1185a-dec1-11e4-9ac0-e9343f86b604/connecting/relations"
},
"path" : "/mycollections/2ff8961a-dea8-11e4-996b-63ce373ace35/relations/56a1185a-dec1-11e4-9ac0-e9343f86b604"
}
} ],
"timestamp" : 1428589668542,
"duration" : 51,
"organization" : "myorg",
"applicationName" : "myapp"
}
What I want is that instead of providing me the path of the related entity Usergrid directly nest it in the first JSON answer so that I only need to make a single http request instead of two.
You cannot. Usergrid is not designed in that way. You need to write an extra wrapper rest endpoint to simulate one response.
Not sure what DB you are using. If you are using document db like mongo then you can write a node.js scripts to do this manipulation. Apigee has volvo.js check is it possible to do scripting.

problems on elasticsearch with parent child documents

We work with two types of documents on elastic search (ES): items and slots, where items are parents of slot documents.
We define the index with the following command:
curl -XPOST 'localhost:9200/items' -d #itemsdef.json
where itemsdef.json has the following definition
{
"mappings" : {
"item" : {
"properties" : {
"id" : {"type" : "long" },
"name" : {
"type" : "string",
"_analyzer" : "textIndexAnalyzer"
},
"location" : {"type" : "geo_point" },
}
}
},
"settings" : {
"analysis" : {
"analyzer" : {
"activityIndexAnalyzer" : {
"alias" : ["activityQueryAnalyzer"],
"type" : "custom",
"tokenizer" : "whitespace",
"filter" : ["trim", "lowercase", "asciifolding", "spanish_stop", "spanish_synonym"]
},
"textIndexAnalyzer" : {
"type" : "custom",
"tokenizer" : "whitespace",
"filter" : ["word_delimiter_impl", "trim", "lowercase", "asciifolding", "spanish_stop", "spanish_synonym"]
},
"textQueryAnalyzer" : {
"type" : "custom",
"tokenizer" : "whitespace",
"filter" : ["trim", "lowercase", "asciifolding", "spanish_stop"]
}
},
"filter" : {
"spanish_stop" : {
"type" : "stop",
"ignore_case" : true,
"enable_position_increments" : true,
"stopwords_path" : "analysis/spanish-stopwords.txt"
},
"spanish_synonym" : {
"type" : "synonym",
"synonyms_path" : "analysis/spanish-synonyms.txt"
},
"word_delimiter_impl" : {
"type" : "word_delimiter",
"generate_word_parts" : true,
"generate_number_parts" : true,
"catenate_words" : true,
"catenate_numbers" : true,
"split_on_case_change" : false
}
}
}
}
}
Then we add the child document definition using the following command:
curl -XPOST 'localhost:9200/items/slot/_mapping' -d #slotsdef.json
Where slotsdef.json has the following definition:
{
"slot" : {
"_parent" : {"type" : "item"},
"_routing" : {
"required" : true,
"path" : "parent_id"
},
"properties": {
"id" : { "type" : "long" },
"parent_id" : { "type" : "long" },
"activity" : {
"type" : "string",
"_analyzer" : "activityIndexAnalyzer"
},
"day" : { "type" : "integer" },
"start" : { "type" : "integer" },
"end" : { "type" : "integer" }
}
}
}
Finally we perform a bulk index with the following command:
curl -XPOST 'localhost:9200/items/_bulk' --data-binary #testbulk.json
Where testbulk.json holds the following data:
{"index":{"_type": "item", "_id":35}}
{"location":[40.4,-3.6],"id":35,"name":"A Name"}
{"index":{"_type":"slot","_id":126,"_parent":35}}
{"id":126,"start":1330,"day":1,"end":1730,"activity":"An Activity","parent_id":35}
We see through ES Head plugin that definitions seem to be ok. We test the analyzers to check that they have been loaded and they work. Both documents appear listed in ES Head browser view. But if we try to retrieve the child item using the API, ES responds that it does not exist:
$ curl -XGET 'localhost:9200/items/slot/126'
{"_index":"items","_type":"slot","_id":"126","exists":false}
When we import 50 documents, all parent documents can be retrieved through API, but only SOME of the requests for child elements get a successful response.
My guess is that it may have something to do with how docs are stored across shards and the routing...which certainly is not clear to me how it works.
Any clue on how to be able to retrieve individual child documents? ES Head shows they have been stored but HTTP GETs to localhost:9200/items/slot/XXX respond randomly with "exists":false.
The child documents are using parent's id for routing. So, in order to retrieve child documents you need to specify parent id in the routing parameter on your query:
curl "localhost:9200/items/slot/126?routing=35"
If parent id is not available, you will have to search for the child documents:
curl "localhost:9200/items/slot/_search?q=id:126"
or switch to an index with a single shard.

Freebase MQL query for topic summary and image?

I'm trying to write an MQL query to be executed using Freebase API's. I would like to retrieve the topic summary and the image for the topic.
I have been able to work out the below query which will get me the images associated with the Bill Gates topic.
MQL:
[
{
"/common/topic/image" : [
{
"id" : null
}
],
"name" : "bill gates",
"type" : "/people/person"
}
]
Results:
[
{
"/common/topic/image" : [
{
"id" : "/guid/9202a8c04000641f8000000004fb4c01"
},
{
"id" : "/wikipedia/images/commons_id/4486276"
}
],
"name" : "Bill Gates",
"type" : "/people/person"
}
]
For those that may have not run into MQL in the past but are interested in playing around with it. Check out the Freebase MQL Query Editor.
billg profile page http://i.friendfeed.com/c31a22d9e439eb67b0feeb4ffd64c3b5ed9a8e16
UPDATE
Query that I ended up using:
[
{
"/common/topic/image" : [
{
"id" : null
}
],
"article" : [
{
"content" : null
}
],
"name" : "bill gates",
"type" : "/common/topic"
}
]
These results can be combined with narphorium's answer to retrieve the actual data:
[
{
"/common/topic/image" : [
{
"id" : "/guid/9202a8c04000641f8000000004fb4c01"
},
{
"id" : "/wikipedia/images/commons_id/4486276"
}
],
"article" : [
{
"content" : null
},
{
"content" : "/guid/9202a8c04000641f800000000903535d"
}
],
"name" : "Bill Gates",
"type" : "/common/topic"
}
]
The images and topic summaries are stored separately in the content store and are accessible via another web service API.
For example, Bill Gates' image can be accessed like this:
http://www.freebase.com/api/trans/raw/guid/9202a8c04000641f8000000004fb4c01
Similarly, the GUID for the topic summary can be found by replacing /common/topic/image with /common/topic/article in your query. The results can be accessed again like this:
http://www.freebase.com/api/trans/raw/guid/9202a8c04000641f8000000008bfed35
You can read more about the content store here.
The new image service provided by freebase can now be used to get the images using the freebase ids, e.g., for Bill Gates following is the image URL:
https://usercontent.googleapis.com/freebase/v1/image/en/bill_gates
More about this service can be found at: http://wiki.freebase.com/wiki/Image_Service

Resources