Skip to content

Commit

Permalink
DP-177 Add Service Analytics
Browse files Browse the repository at this point in the history
- Create migration for unique_key table
- Implement functionality for generating unique key in first admin registering time
- Add phone number to data that sending to Zapier
- Implement functionality for sending created service data to updates
- Use guzzle async post request to post created service data to updates.df.com
- Add phone number to setup artisan command
- Change way to get server ip address
- Change endpoint for created services to env
- Add checking for / in url
- Rename unique_key to instance_id
- Move send service data to updates to separate class
- Change way to getting instance id
- Add checking if instance id exist, and generate if does not exist
- Move generating instance id logic to InstanceId model
- Add checking if instance id exists in generating instance id method
- Remove phone field from df:setup script
- Fix issue with generating instance id if it not exist
- Fix bug with sending first service if instance id does not exist
- Change private method to public
  • Loading branch information
oleksandrono committed Apr 1, 2020
1 parent e3f0f6a commit 8b84ecd
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 3 deletions.
34 changes: 34 additions & 0 deletions database/migrations/2020_03_02_121555_create_instance_id_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateInstanceIdTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{

Schema::create('instance_id', function (Blueprint $t) {
$t->increments('id');
$t->string('instance_id');
$t->timestamp('created_date')->nullable();
$t->timestamp('last_modified_date')->useCurrent();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('instance_id');
}
}
6 changes: 5 additions & 1 deletion src/Components/RegisterContact.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace DreamFactory\Core\Components;

use DreamFactory\Core\Models\InstanceId;
use DreamFactory\Core\Models\User;
use DreamFactory\Core\Utility\Curl;

Expand All @@ -12,7 +13,7 @@ class RegisterContact
const ENDPOINT = 'https://www.dreamfactory.com/in_product_v2/registration.php';

/**
* @param User $user
* @param User $user
* @param array $payload
*
* @return bool
Expand All @@ -36,6 +37,7 @@ public static function registerUser($user, array $payload = [])
if (empty($partner) && (false !== stripos(env('DB_DATABASE', ''), 'bitnami'))) {
$partner = 'Bitnami';
}

$payload = array_merge(
[
'email' => $user->email,
Expand All @@ -49,6 +51,8 @@ public static function registerUser($user, array $payload = [])
'product' => 'DreamFactory',
'version' => config('app.version', 'unknown'),
'host_os' => PHP_OS,
'instance_id' => InstanceId::getInstanceIdOrGenerate(),
'ip_address' => getHostByName(getHostName()),
],
$payload
);
Expand Down
59 changes: 59 additions & 0 deletions src/Components/UpdatesSender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace DreamFactory\Core\Components;

use DreamFactory\Core\Models\InstanceId;
use DreamFactory\Core\Utility\Session;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;


/**
* Class UpdatesSender
*
*/

class UpdatesSender {

/**
* @param $service_type
*
*/
public static function sendServiceData(string $service_type) {
$updates_endpoint = 'https://updates.dreamfactory.com/api/created-services';

if(Session::isAuthenticated()){
$client = new \GuzzleHttp\Client();
$client->postAsync($updates_endpoint, [
'json' => UpdatesSender::gatherServiceAnalyticData($service_type),
'timeout' => 2,
'connect_timeout' => 2
])
->then(
function (ResponseInterface $_ignore) {},
function (RequestException $ex) {
\Log::debug($ex->getMessage());
}
)
->wait();
}
}

/**
* @param $service_type
*
* @return array
*/
public static function gatherServiceAnalyticData(string $service_type) {
$instance_id = InstanceId::getInstanceIdOrGenerate();
$email = Session::user()->getAttribute('email');

$data = [];
$data['service_type'] = $service_type;
$data['instance_id'] = $instance_id;
$data['ip_address'] = getHostByName(getHostName());
$data['email'] = $email;

return $data;
}
}
35 changes: 35 additions & 0 deletions src/Models/InstanceId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace DreamFactory\Core\Models;

class InstanceId extends BaseSystemModel
{
protected $table = 'instance_id';

protected $fillable = ['instance_id'];

/**
*
* @return string
*/
public static function getInstanceIdOrGenerate() {
$model = InstanceId::first();
if(!$model) {
return self::generateInstanceId()->instance_id;
}
return $model['instance_id'];
}

/**
*
* @return InstanceId
*/
public static function generateInstanceId() {
$model = new InstanceId([
'instance_id' => uniqid(),
]);
$model->save();

return $model;
}
}
5 changes: 4 additions & 1 deletion src/Models/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace DreamFactory\Core\Models;

use DreamFactory\Core\Components\DsnToConnectionConfig;
use DreamFactory\Core\Components\UpdatesSender;
use DreamFactory\Core\Events\ServiceDeletedEvent;
use DreamFactory\Core\Events\ServiceModifiedEvent;
use DreamFactory\Core\Exceptions\BadRequestException;
Expand Down Expand Up @@ -92,6 +93,8 @@ function (Service $service) {
}
}

UpdatesSender::sendServiceData($service -> getAttribute('type'));

return true;
}
);
Expand Down Expand Up @@ -274,4 +277,4 @@ protected static function cleanResult($response, $fields)

return $response;
}
}
}
4 changes: 3 additions & 1 deletion src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,14 +628,16 @@ public static function createFirstAdmin(array &$data)
return false;
} else {
/** @type User $user */
$attributes = array_only($data, ['name', 'first_name', 'last_name', 'email', 'username']);
$attributes = array_only($data, ['name', 'first_name', 'last_name', 'email', 'username', 'phone']);
$attributes['is_active'] = 1;
$user = static::create($attributes);

$user->password = array_get($data, 'password');
$user->is_sys_admin = 1;
$user->save();

InstanceId::getInstanceIdOrGenerate();

// Register user
RegisterContact::registerUser($user);
// Reset admin_exists flag in cache.
Expand Down

0 comments on commit 8b84ecd

Please sign in to comment.