datediff function for FQL, or other way to easily get event duration? - facebook-fql

I want to return the event durations from this single facebook FQL query. Is there something like the SQL datediff function that I can use?
SELECT eid, name, start_time, end_time FROM event WHERE eid IN ( SELECT eid FROM event_member WHERE uid = me() ) ORDER BY start_time ASC

Best way to do this is definitely to post process as CBroe mentioned.
Here is an incomplete way to do this in FQL
SELECT start_time, end_time, strtotime(lower(end_time)) - strtotime(lower(start_time)) FROM event WHERE eid IN ( SELECT eid FROM event_member WHERE uid = me() ) AND strlen(end_time) > 0
And a sample response
{
"data": [
{
"start_time": "2015-03-14T21:00:00-0400",
"end_time": "2015-03-15T03:00:00-0400",
"anon": 21600
},
{
"start_time": "2015-02-16T21:00:00-0400",
"end_time": "2015-02-17T02:00:00-0400",
"anon": 18000
},
{
"start_time": "2015-01-30T22:00:00-0400",
"end_time": "2015-01-31T08:00:00-0400",
"anon": 36000
},
{
"start_time": "2015-01-24T18:00:00-0400",
"end_time": "2015-01-25T00:00:00-0400",
"anon": 21600
},
{
"start_time": "2015-01-09T22:00:00-0400",
"end_time": "2015-01-10T04:00:00-0400",
"anon": 21600
},
{
"start_time": "2015-01-02T22:00:00-0400",
"end_time": "2015-01-03T04:00:00-0400",
"anon": 21600
}
]
}
The anon field is returned in seconds so
21600 = 6 hours
18000 = 5 hours
36000 = 10 hours
Which correctly match the difference in start and end times above.

Related

Jsonb array of objects update

So this is my jsonb array of objects. Column is called bids in my db.
bids column
[
{
"id": "1",
"size": "5.5Y",
"price": 180
},
{
"id": "f0d1d36a-f6af-409e-968e-54c1dc104566",
"size": "6.5Y",
"price": 22
}
]
I want to update price property by the ID of an element for ex. "f0d1d36a-f6af-409e-968e-54c1dc104566", so the price would change from 22 to 150 IN ROW WHICH CONTAINS ELEMENT WITH DESIRED ID IN THE COLUMN.
How can I do that?
create table json_update (id integer, json_fld jsonb);
insert into json_update values (1, '[
{
"id": "1",
"size": "5.5Y",
"price": 180
},
{
"id": "f0d1d36a-f6af-409e-968e-54c1dc104566",
"size": "6.5Y",
"price": 22
}
]'
)
;
UPDATE
json_update
SET
json_fld = jsonb_set(json_fld, ARRAY[(idx)::text, 'price'::text], '150'::jsonb)
FROM (
SELECT
(row_number() OVER (ORDER BY t.a ->> 'id') - 1) AS idx,
t.a
FROM (
SELECT
jsonb_array_elements(json_fld)
FROM
json_update) AS t (a)) AS i
WHERE
i.a ->> 'id' = 'f0d1d36a-f6af-409e-968e-54c1dc104566';
select * from json_update ;
id | json_fld
----+---------------------------------------------------------------------------------------------------------------------------
1 | [{"id": "1", "size": "5.5Y", "price": 180}, {"id": "f0d1d36a-f6af-409e-968e-54c1dc104566", "size": "6.5Y", "price": 150}]

Postgresql - Reducing rows to one if a condition is met

I have a table that returns a series of objects. This is a small section of an example result (in JSON):
Query:
SELECT id, starttime, endtime, duration, type FROM things
Result:
{
"id": 3,
"starttime": "2016-09-15T03:27:09",
"endtime": "2016-09-15T03:31:43",
"duration": 274,
"type": "bad"
},
{
"id": 2,
"starttime": "2016-09-15T03:26:48",
"endtime": "2016-09-15T03:27:09",
"duration": 20,
"status": "good"
},
{
"id": 1,
"starttime": "2016-09-15T03:19:46",
"endtime": "2016-09-15T03:26:48",
"duration": 422,
"status": "bad"
},
I am trying to exclude anything less than 30 seconds - this is simple enough. However I also need to combine the top two together - their durations combined, the starttime with id 1's starttime and endttime as id 3's endtime. So this:
{
"starttime": "2016-09-15T03:19:46",
"endtime": "2016-09-15T03:31:43",
"duration": 696,
"status": "bad"
},
Is this possible in Postgres/SQL? I think I could figure something out in Java/C# but would rather do it in the query.
You can use ROW_NUMBER() :
SELECT MAX(CASE WHEN s.rnk = 1 THEN s.starttime END) as starttime,
MAX(CASE WHEN s.rnk = 2 THEN s.endtime END) as endtime,
SUM(s.duration) as dur,
--You didn't say which status you want
FROM (
SELECT t.*,
ROW_NUMBER() OVER(ORDER BY t.id) as rnk
FROM things t
WHERE t.duration >= 30) s
WHERE s.rnk < 3

could not fetch info from comment table

Facebook just notified me about "July 2013 Breaking Changes". There is one related to my app.
Deprecating 'comments' field from 'stream' FQL table
We are deprecating the 'comments' field from 'stream' FQL table. Please select the 'comment_info' column to fetch the 'can_comment' and 'comment_count' fields (formerly called 'can_post' and 'count'), and use the comment table directly to retrieve the list of comments.
In our app we are using query like this :
SELECT post_id, message, likes.count, comments.count, comments.comment_list FROM stream
WHERE source_id=me() AND actor_id=me() LIMIT 500
and receive response like this :
{
"data": [
{
**"post_id": "100002510712421_451594341600919",**
"message": "text",
"likes": {
"count": 1
},
"comments": {
"count": 2,
"comment_list": [
{
"fromid": 100001228089363,
"time": 1367073822,
"text": "text",
"text_tags": [
],
"id": "100002510712421_451594341600919_1181882",
"likes": 0,
"user_likes": false,
"post_fbid": 452073094886377
},
{
"fromid": 100000020227797,
"time": 1367173047,
"text": "text",
"text_tags": [
],
"id": "100002510712421_451594341600919_1183852",
"likes": 0,
"user_likes": false,
"post_fbid": 452790081481345
}
]
}
},
....
ok now try to fetch info from 'comment' table using given post_id
SELECT id, text, time, fromid FROM comment WHERE
post_id='100002510712421_451594341600919'
{
"data": [
]
}
The response is empty
Can anybody come up with solution? We really need it!
your query looks just fine, are you sure that this post_id exists and you have permission to read it?
SELECT id, text, time, fromid FROM comment WHERE
post_id='125909647492772_502974003098530'
returns
{
"data": [
{
"id": "502974003098530_78616446",
"text": "saugeil!",
"time": 1364309881,
"fromid": 526559276
}
]
}
I'veI run into the same issue.
Surprisingly, I was able to fix it by using only last part of the post_id, i.e.: '451594341600919' instead of '100002510712421_451594341600919'. But it works only sometimes and in your case doesn't work at all.

Facebook Graph API fql query for like count vs direct object lookup

Why is it that these two queries return different like counts?
1) fql?q... query returns 0 likes
https://graph.facebook.com/fql?q=SELECT+url%2C+normalized_url%2C+share_count%2C+like_count%2C+comment_count%2C+total_count%2C+commentsbox_count%2C+comments_fbid%2C+click_count+FROM+link_stat+WHERE+url%3D%27http%3A%2F%2Fwww.facebook.com%2Fpages%2FLine-And-Circle%2F49561759728%27
returns:
{
"data": [
{
"url": "http://www.facebook.com/pages/Line-And-Circle/49561759728",
"normalized_url": "http://www.facebook.com/pages/Line-And-Circle/49561759728",
"share_count": 0,
"like_count": 0,
"comment_count": 0,
"total_count": 0,
"commentsbox_count": 0,
"comments_fbid": null,
"click_count": 0
}
]
}
2) Direct object query returns 436 likes
https://graph.facebook.com/49561759728
returns:
{
"name": "Line & Circle",
"is_published": true,
"website": "http://lineandcirclemusic.tumblr.com/",
"description": "follow \u0040LineAndCircle",
"about": "Echo Park, Los Angeles via the Midwest, USA. http://lineandcirclemusic.tumblr.com/",
"genre": "Indie/Alternative",
"hometown": "Los Angeles, California",
"current_location": "Los Angeles, California",
"record_label": "White Iris",
"press_contact": "lineandcirclemusic\u0040gmail.com",
"influences": "Richard Neutra, Erik Satie, Lord Byron, Richard Yates, Grace Kelly, Phil Hartman, Bobby Briggs, Denard Robinson, etc.",
"band_interests": "Ice cream, ice fishing, etc.",
"category": "Musician/band",
"id": "49561759728",
"link": "https://www.facebook.com/pages/Line-Circle/49561759728",
"likes": 436,
"cover": {
"cover_id": "10151094140979729",
"source": "http://sphotos-a.xx.fbcdn.net/hphotos-ash4/s720x720/391388_10151094140979729_1992918206_n.jpg",
"offset_y": 31
}
}
In other instances, like for username "newfoundglory", the fql query returns something close to the like count, but it is still different than the direct query.
Why is it that these two queries return different results?
The first graph call returns 0 likes, as expected, because that table is used to track the likes and comments for external urls, not internal pages on facebook.
To get that data, you should be using the page table and query for fan_count -
select page_id, name, page_url, fan_count from page where name = 'Line & Circle'
This returns the expected result -
"data": [
{
"page_id": 49561759728,
"name": "Line & Circle",
"page_url": "https://www.facebook.com/pages/Line-Circle/49561759728",
"fan_count": 436
}
]
graph.facebook.com/49561759728?fields=likes
{
"likes": 637,
"id": "49561759728"
}
i use this link for generate https://developers.facebook.com/tools/explorer/?method=GET&path=agenciadac%3Ffields%3Dlikes

"Timezone less events" migration for Facebook

Facebook recently added timezone less events (https://developers.facebook.com/roadmap/#timezone-less-events) to its Developer roadmap which says
"Since this migration was originally created, we have added a timezone field to events which indicates the name of the timezone (as defined here) where the event is expected to happen. FYI, developers reading time in ISO 8601 should be supporting the full standard when reading event times. Most events return local times (no GMT offset), but in the future events likely will return other formats (namely date-only and precise)."
It works for dates in ISO 8601 format but if I get dates in epoch format I always get +7 hrs difference.
e.g.
https://graph.facebook.com/369000383135224 returns
{
"id": "369000383135224",
"owner": {
"name": "Horst Uwe Peter",
"id": "1117563687"
},
"name": "Event in Dublin time 10:25",
"start_time": "2012-05-04T10:25:00",
"end_time": "2012-05-04T11:25:00",
"timezone": "Europe/Dublin",
"location": "Dublin, Ireland",
"venue": {
"id": "110769888951990"
},
"privacy": "FRIENDS",
"updated_time": "2012-05-04T09:27:29+0000",
"type": "event"
}
and
http://graph.facebook.com/369000383135224?date_format=U returns
{
"id": "369000383135224",
"owner": {
"name": "Horst Uwe Peter",
"id": "1117563687"
},
"name": "Event in Dublin time 10:25",
"start_time": 1336152300, <== Fri, 04 May 2012 17:25:00 GMT
"end_time": 1336155900, <== Fri, 04 May 2012 18:25:00 GMT
"timezone": "Europe/Dublin",
"location": "Dublin, Ireland",
"venue": {
"id": "110769888951990"
},
"privacy": "FRIENDS",
"updated_time": 1336123649,
"type": "event"
}
and with FQL using GRAPH end point
graph.facebook.com/fql?q=SELECT eid, name, description, location, venue, start_time, end_time, update_time, creator, privacy FROM event WHERE eid = 369000383135224
{
"data": [
{
"eid": 369000383135224,
"name": "Event in Dublin time 10:25",
"description": "",
"location": "Dublin, Ireland",
"venue": {
"id": 110769888951990
},
"start_time": 1336152300, <== Fri, 04 May 2012 18:25:00 GMT
"end_time": 1336155900, <== Fri, 04 May 2012 18:25:00 GMT
"update_time": 1336123649,
"creator": 1117563687,
"privacy": "FRIENDS"
}
]
}
does that mean migration works only for ISO 8601 formatted dates? and has no affect on FQL or epoch date format?
My events on a page I administer have never returned a timezone.
What I have found is that event times entered in the frontend dialog are treated as local times in "America/Los_Angeles" (complete with the US Daylight Savings Time changes, so you'll see +6 in winter and +7 in summer) and are then converted to "UTC" for storage in the database.
For display I use the following php function to show the correct times and note on the page that the times are local to the event's location:
function fb_event_time_convert($fb_time) {
$origin_dtz = new DateTimeZone('UTC');
$remote_dtz = new DateTimeZone('America/Los_Angeles');
$fb_time_str = '#' . $fb_time;
$origin_dt = new DateTime($fb_time_str, $origin_dtz);
$remote_dt = new DateTime($fb_time_str, $remote_dtz);
$offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt);
return $fb_time - $offset;
}