Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Veracity integration and Yodie+Crees bugfixing #11

Open
wants to merge 9 commits into
base: sheffield-service-integration
Choose a base branch
from
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,12 @@ ACTIONABILITY_REQUEST_COST=<cost per request>
ACTIONABILITY_QUOTA_RESET=<reset delay>
ACTIONABILITY_API_USERNAME=<username>
ACTIONABILITY_API_PASSWORD=<password>



VERACITY_API_URL=<full_url_of_actionability_api>
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops change to <full_url_of_veracity_api> for clarity

VERACITY_QUOTA=<remaining quota>
VERACITY_REQUEST_COST=<cost per request>
VERACITY_QUOTA_RESET=<reset delay>
VERACITY_API_USERNAME=<username>
VERACITY_API_PASSWORD=<password>
27 changes: 27 additions & 0 deletions app/Http/Controllers/VeracityController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Http\Controllers;

use App\Jobs\RunVeracityService;
use Illuminate\Http\Request;

use Log;

class VeracityController extends Controller
{

public function annotate(Request $request)
{
$post = $request->all();
dispatch(new RunVeracityService($post));
return;
}

public function all(Request $request)
{
$post = $request->all();
dispatch(new RunVeracityService($post, 'all'));

return;
}
}
2 changes: 1 addition & 1 deletion app/Jobs/RunProxyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function runService($post)
$source_field_key = $post['source_field_key'];
$response = $this->requestProcessing(array_get($post, $source_field_key));

$post = $this->format_as_post($post, $response);
$post = $this->format_as_post($post, $response, array_get($post, $source_field_key));

dispatch(new UpdateUshahidiPost($post));
}
Expand Down
94 changes: 94 additions & 0 deletions app/Jobs/RunVeracityService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace App\Jobs;

use GuzzleHttp\Client;
use GuzzleHttp\Psr7;
use GrahamCampbell\Throttle\Facades\Throttle;
use Log;

class RunVeracityService extends RunProxyService
{
/**
* Service Type
* eventRelated, eventType, infoType
*/
protected $request_type;

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$remaining = config('options.veracity.request_per_time_block');
$time = config('options.veracity.quota_reset');

$minutes = $time / 60;

$throttler = Throttle::get([
'ip' => 'ushahidi',
'route' => 'veracity',
], $remaining, $minutes);

if (!$throttler->check()) {
$this->release($time);
return;
}
$this->runService($this->post);
}

public function requestProcessing($text)
{
try {
$client = new Client();
$url = config('options.veracity.api.url');
$response = $client->request('POST', $url,
[
'headers' => [
'Accept' => 'application/json',
'Content-type' => 'text/plain',
],
'auth' => [
config('options.veracity.api.username'),
config('options.veracity.api.password')
],
'body' => json_encode([$text])
]
);
return $response;
} catch (RequestException $e) {
if ($e->hasResponse()) {
Log::error(Psr7\str($e->getResponse()));
}
}
}

public function format_as_post($post, $response)
{
$tags = [];
$json = json_decode($response->getBody(), true);
switch($json['rumour_label']) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eh so this is really gross but since we don't receive the names we want to use for categories, I had to map the
rumor_label : true => Rumor
rumor_label : false => Not a rumor
rumor_label : 'unverified' => 'Unverified'
I think maybe a cleaner approach might be to "stringify" the rumour_label to ensure we can just do a map and return ir ['false' => "Not a rumor" , 'true' => "Rumor" , 'unverified' => "Unverified"]

case 'unverified':
$label = 'Unverified';
break;
case false:
$label ='Not a rumor';
break;
default:
$label = 'Rumor';
break;
}
$tags = array_merge($tags, ['value' => $label, 'confidence_score' => $json['confidence'] * 100]);
// At the moment it is important to only set fields that you intend to change
// as Post updates are async it is possible to overwrite other user's data
// by performing a full update with the complete object recevied
return [
'id' => $post['id'],
'tags' => $tags,
'values' => [],
'webhook_uuid' => $post['webhook_uuid']
];
}
}
6 changes: 3 additions & 3 deletions app/Jobs/RunYodieService.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function replaceTextWithMD($indices, $link, $text)
);
}

public function format_as_post($post, $response)
public function format_as_post($post, $response, $source_field=null)
{
$json = json_decode($response->getBody());
$yodie_post_field = config('options.ushahidi.survey_destination_field');
Expand Down Expand Up @@ -112,10 +112,10 @@ public function format_as_post($post, $response)

foreach($replacement_strings as $replace => $value)
{
$text = str_replace($replace, $value, $text);
$source_field = str_replace($replace, $value, $source_field);
}

$yodie_text = $text;
$yodie_text = $source_field;

$destination_field_key = $post['destination_field_key'];

Expand Down
9 changes: 9 additions & 0 deletions config/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,14 @@
],
'request_per_time_block' => env('ACTIONABILITY_QUOTA') / env('ACTIONABILITY_REQUEST_COST'),
'quota_reset' => env('ACTIONABILITY_QUOTA_RESET')
],
'veracity' => [
'api' => [
'url' => env('VERACITY_API_URL'),
'username' => env('VERACITY_API_USERNAME'),
'password' => env('VERACITY_API_PASSWORD')
],
'request_per_time_block' => env('VERACITY_QUOTA') / env('VERACITY_REQUEST_COST'),
'quota_reset' => env('VERACITY_QUOTA_RESET')
]
];
3 changes: 2 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
$app->post('/crees/eventType', ['middleware' => 'UshahidiRequestValidator', 'uses' => 'CreesController@eventType']);
$app->post('/crees/infoType', ['middleware' => 'UshahidiRequestValidator', 'uses' => 'CreesController@infoType']);

$app->post('/actionability/annotate', ['middleware' => 'UshahidiRequestValidator', 'uses' => 'ActionabilityController@annotate']);
$app->post('/actionability/annotate', ['middleware' => 'UshahidiRequestValidator', 'uses' => 'ActionabilityController@annotate']);
$app->post('/veracity/annotate', ['middleware' => 'UshahidiRequestValidator', 'uses' => 'VeracityController@annotate']);