How to handle the result of a list of future with ReasonML? - future

I am trying to go over a list of items, cache the URL and then update the URL in the list before drawing it.
However I don't seems to be able to do so.
Any idea?
external cache: option(string) => Js.Promise.t({. "uri": string, "filePath": string }) = "get";
let items = result##data##data->Option.getWithDefault([||]);
let cachedUri = items
->Belt.Array.map(
item =>
cache(item##hosted_video_url)
->FutureJs.fromPromise(Js.String.make)
->Future.flatMapOk(cachedObj => (
{. "hosted_video_url": cachedObj##uri }
)
))
->Belt.Array.toList
->Future.all;

The idea is that you cannot exit a future, so you need to update the state inside the future thing rather than trying to get an equivalent of an asyncio.gather or something similar.
I changed:
setVerticalData(_ => {items-> Js.Array2.sortInPlaceWith((a, b) => {...})});
with
items
->List.fromArray;
->List.map(item =>
cache(item##hosted_video_url)
->FutureJs.fromPromise(Js.String.make)
)
->Future.all
->Future.map(cachedList => {
setVerticalData(_ => {items: cachedList})
})

Related

Logstash mutate filters

In my logs i am able to see database password.I want to remove the fields when ever it matches to one particular method.Below is the sample log format.
Date=2016-02-23:00:36:29:242, Level=INFO , class=abc_class, method=abc_method, line=266, name=dataSourceUser, message=returning Property, value=password
below is the sample logstashclient filter am using but still am able to see value field(password)
filter {
grok {
match => {"message"=>"Date=(?<LogTime>%{YEAR:Year}-%{MONTHNUM:Month}-%{MONTHDAY:Day}:%{TIME:Time}), (Level=)(?<SeverityLevel>[A-Z]*)(\s)?, %{GREEDYDATA:message}"}
break_on_match => false
}
date {
match => ["LogTime", "YYYY-MM-dd:HH:mm:ss:SSS"]
target => "#timestamp"
}
if[method=="abc_method"]{
mutate {
remove_field => ["%{Value}"]
}
}
ETA : when ever it matches to method=abc_method it should not pass value field,for other metods it should pass
Try:
remove_field => [ "value" ]
found the answer we can use gsub filter
eg :
gsub => [ "message", "value=%{Value}", "" ]

Count by group and subgroup

I want to generate some stats regarding some data I have in a model
I want to create stats according to an association and a status column.
i.e.
Model.group(:association_id).group(:status).count
to get an outcome like
[{ association_id1 => { status1 => X1, status2 => y1 } },
{ association_id2 => { status1 => x2, status2 => y2 } }...etc
Not really bothered whether it comes out in as an array or hash, just need the numbers to come out consistently.
Is there a 'rails' way to do this or a handy gem?
Ok. Worked out something a little better, though happy to take advice on how to clean this up.
group_counts = Model.group(["association_id","status"]).count
This returns something like:
=> {[nil, "status1"]=>2,
[ass_id1, "status1"]=>58,
[ass_id2, "status7"]=>1,
[ass_id2, "status3"]=>71 ...etc
Which, while it contains the data, is a pig to work with.
stats = group_counts.group_by{|k,v| k[0]}.map{|k,v| {k => v.map{|x| {x[0][1] => x[1] }}.inject(:merge) }}
Gives me something a little friendlier
=> [{
nil => {
"status1" => 10,
"status2" => 23},
"ass_id1" => {
"status1" => 7,
"status2" => 23
}...etc]
Hope that helps somebody.
This is pretty ugly, inefficient and there must be a better way to do it, but...
Model.pluck(:association_id).uniq.map{ |ass| {
name:(ass.blank? ? nil : Association.find(ass).name), data:
Model.where(association_id:ass).group(:status).count
} }
Gives what I need.
Obviously if you didn't need name the first term would be a little simpler.
i.e.
Model.pluck(:association_id).uniq.map{ |ass| {
id:ass, data:Model.where(association_id:ass).group(:status).count
} }

sfWidgetFormDoctrineChoice: bug, error or what?

I'm using sfWidgetFormDoctrineChoice to build select elements in a form. This is how I'm using:
// Fill maquinaemisorid
$this->widgetSchema['maquinaemisorid'] = new sfWidgetFormDoctrineChoice(array('model' => 'SdrivingMaquina', 'add_empty' => 'Seleccione una Máquina', 'table_method' => 'fillChoice'));
// Fill idoperador
$this->widgetSchema['idoperador'] = new sfWidgetFormDoctrineChoice(array('model' => 'SdrivingOperador', 'add_empty' => 'Seleccione un Operador', 'table_method' => 'fillChoice'));
// Fill idturno
$this->widgetSchema['idturno'] = new sfWidgetFormDoctrineChoice(array('model' => 'SdrivingTurno', 'add_empty' => 'Seleccione un Turno', 'table_method' => 'fillChoice'));
For the first widget all is fine and values are taken from DB as should be but for the second one and the third is not working. As you may notice I'm calling a method fillChoice in all widgets table_method. This is the code for the three:
SdrivingMaquinaTable.class.php
public function fillChoice() {
$id_empresa = sfContext::getInstance()->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();
return Doctrine_Core::getTable('SdrivingMaquina')->createQuery('a')->where('a.idempresa = ?', $id_empresa)->execute();
}
SdrivingOperadorTable.class.php
public function fillChoice() {
$id_empresa = sfContext::getInstance()->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();
return Doctrine_Core::getTable('SdrivingOperador')->createQuery('a')->where('a.idempresa = ?', $id_empresa)->execute();
}
SdrivingTurno.class.php
public function fillChoice() {
$id_empresa = sfContext::getInstance()->getUser()->getGuardUser()->getSfGuardUserProfile()->getIdempresa();
return Doctrine_Core::getTable('SdrivingTurno')->createQuery('a')->where('a.idempresa = ?', $id_empresa)->execute();
}
The method is the same always just changes the table used for each class. I check the generated queries and all are fine and if I run each on phpMyAdmin for example more than one value is fetched altough in idoperador and idturno just the latest is rendered. This is a very rare behavior and I can't find the error or mistake so I need some help or advice here.
As an adition I tried this in SdrivingRegistrosEmisoresForm.class.php (this is where the widgets are being generated):
$id_empresa = $this->current_user->getSfGuardUserProfile()->getIdempresa();
$choices_operador = Doctrine_Core::getTable('SdrivingOperador')->createQuery('a')->where('a.idempresa = ?', $id_empresa)->execute();
$this->widgetSchema['idoperador'] = new sfWidgetFormSelect(array('choices' => $choices_operador));
And this way works fine but the id for each item fetched isn't right meaning:
id item real_id
0 Item1 2
1 Item2 16
2 Item4 3
I talked about this in this topic but didn't get any answer, any help?
As stated in sfWidgetFormDoctrineChoice, if option *table_method* returns a collection like your methods, it renders choices with options *key_method* and method. By default , they are respectively getPrimaryKey() and *__toString()*.
Can we check you schema.yml and the __toString() ?
As for your last example the option choices of sfWidgetFormSelect should be an array an not a *Doctrine_Collection*

Using result of `JSON.decode` in Rails

So I have an action in my controller that does a get_response to an API:
def memeapi
require "net/http"
require "uri"
#meme = Meme.new(params[:meme])
url = "http://version1.api.memegenerator.net/Instance_Create?username=apigen&password=SECRET&languageCode=en&generatorID=#{#meme.memeid}&imageID=#{#meme.imgid}&text0=#{#meme.text0}&text1=#{#meme.text1}"
resp = Net::HTTP.get_response(URI.parse(url))
data = resp.body
# I want to convert it to the Rails data structure - a hash
result = ActiveSupport::JSON.decode(data)
end
Ok but now I want to get back the information, use it to create another object, but I cant even format the information I am getting, can anyone tell me what I am doing wrong, what am I missing?
I want to be able to access the information from the get_response...
Thank you.
This is the JSON structure
{"success":true,"result":{"generatorID":45,"displayName":"Insanity Wolf","urlName":"Insanity-Wolf","totalVotesScore":0,"imageUrl":"/cache/images/400x/0/0/20.jpg","instanceID":13226270,"text0":"push","text1":null,"instanceImageUrl":"/cache/instances/400x/12/12916/13226270.jpg","instanceUrl":"http://memegenerator.net/instance/13226270"}}
I dont want to save all the fields btw...
result will look something like this after JSON.decode in your code.
{
'success' => true,
'result' => {
"generatorID" => 45,
"displayName" => "Insanity Wolf",
"urlName" => "Insanity-Wolf",
"totalVotesScore" => 0,
"imageUrl" => "/cache/images/400x/0/0/20.jpg",
"instanceID" => 13226270,
"text0" => "push",
"text1" => nil,
"instanceImageUrl" => "/cache/instances/400x/12/12916/13226270.jpg",
"instanceUrl" => "http://memegenerator.net/instance/13226270"
}
}
You have a nested hash (hash as part of a hash) here, which you can access like this:
image_url = result['result']['imageUrl'] # => "/cache/images/400x/0/0/20.jpg"
What you actually want to do with this information, I cannot guess. Maybe you want to update the Meme object you created?
#meme.url = image_url

Get list people you are following on twitter

IS it possible to something like this:
http://api.twitter.com/1/statuses/followers/xxxxxx.json
but instead of list of people following you, list of people you are following?
Looks like this is what you need:
https://api.twitter.com/1.1/friends/ids.json?id=:screen_name_or_user_id
https://dev.twitter.com/docs/api/1.1/get/friends/ids
Once you have the list of ID's returned, you can look them up by passing them as a comma delimited list to another API call:
http://api.twitter.com/1.1/users/lookup.json?user_id=[comma delimited list goes here]
https://dev.twitter.com/docs/api/1.1/get/users/lookup
You could use this:
https://api.twitter.com/1/friends.json?screen_name=bitboxer
that way you don't have to do two calls for id and than for details.
The new 1.1 URL you need to call is https://api.twitter.com/1.1/friends/ids.json
Full documentation is at https://dev.twitter.com/docs/api/1.1/get/friends/ids.
require_once('tmhOAuth.php');
require_once('tmhUtilities.php');
$profile_username = "abcdefg"; //twitter username
$oauth_access_token = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; //Your access token
$oauth_access_token_secret = "YYYYYYYYYYYYYYYYYYYYYYYYYY"; //Your access token secret
$consumer_key = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; //Your key
$consumer_secret = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"; //Your secret key
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => $consumer_key,
'consumer_secret' => $consumer_secret,
'user_token' => $oauth_access_token,
'user_secret' => $oauth_access_token_secret,
'curl_ssl_verifypeer' => false
));
$code = $tmhOAuth->request(
'GET',
$tmhOAuth->url('1.1/friends/ids'),
array(
'screen_name' => $profile_username,
'count' => '10'
)
);
$response = $tmhOAuth->response['response'];
$following_ids = json_decode($response, true);
print_r($following_ids);

Resources