Skip to content

Commit

Permalink
Merge branch 'develop' into DOT-810_show_events_to_host
Browse files Browse the repository at this point in the history
  • Loading branch information
edwh committed Jun 28, 2021
2 parents 516af8a + db89219 commit 63ffef7
Show file tree
Hide file tree
Showing 449 changed files with 5,656 additions and 4,954 deletions.
8 changes: 7 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ SESSION_COOKIE=restarters_session
SESSION_DOMAIN=.restarters.test
SESSION_LIFETIME=10080
SESSION_TABLE=laravel_sessions
QUEUE_DRIVER=sync
QUEUE_CONNECTION=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
Expand Down Expand Up @@ -59,6 +59,9 @@ DISPLACEMENT_VALUE=0.5

FEATURE__WIKI_INTEGRATION=false
WIKI_URL=http://wiki.restarters.test
WIKI_DB=wiki_db
WIKI_USER=wikiuser
WIKI_PASSWORD=example
WIKI_APIUSER=wikiapi
WIKI_APIPASSWORD=w1k1ap1
WIKI_COOKIE_PREFIX=wiki_db
Expand All @@ -82,3 +85,6 @@ DRIP_ACCOUNT_ID=
DRIP_CAMPAIGN_ID=

CALENDAR_HASH=somehash

SENTRY_LARAVEL_DSN=https://50fd2fa440af4bb4a230f40ca8d8cf90@o879179.ingest.sentry.io/5831645
SENTRY_TRACES_SAMPLE_RATE=1
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ Culture, Media & Sport.

[![CircleCI](https://circleci.com/gh/TheRestartProject/restarters.net/tree/develop.svg?style=svg)](https://circleci.com/gh/TheRestartProject/restarters.net/?branch=develop)

[![Coverage Status](https://coveralls.io/repos/github/TheRestartProject/restarters.net/badge.svg?branch=develop)](https://coveralls.io/github/TheRestartProject/restarters.net?branch=develop)
[![Coverage Status](https://coveralls.io/repos/github/TheRestartProject/restarters.net/badge.svg?branch=master)](https://coveralls.io/github/TheRestartProject/restarters.net?branch=master)
2 changes: 1 addition & 1 deletion app/Barrier.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class Barrier extends Model

public function barriers()
{
return $this->belongsToMany('App\Device', 'devices_barriers', 'barrier_id', 'device_id');
return $this->belongsToMany(\App\Device::class, 'devices_barriers', 'barrier_id', 'device_id');
}
}
281 changes: 281 additions & 0 deletions app/BattcatOra.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
<?php

namespace App;

use DB;
use Illuminate\Database\Eloquent\Model;
// use Illuminate\Support\Facades\Schema;
// use Illuminate\Database\Schema\Blueprint;

class BattcatOra extends Model
{

protected $table = 'devices_faults_batteries_ora_opinions';
protected $dateFormat = 'Y-m-d H:i';
protected $dates = ['created_at', 'updated_at'];
protected $primaryKey = 'id';

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['id_ords', 'fault_type_id', 'user_id', 'ip_address', 'session_id'];

/**
* Fetch a single random computer device record that has less than 3
* existing opinions and a non-empty problem.
*
* Not the most efficient query
*
* @return array
*/
public function fetchFault($exclusions = [], $locale = NULL)
{
$result = [];
$records = DB::select("SELECT COUNT(*) as total FROM `devices_battcat_ora`");
if ($records[0]->total > count($exclusions)) {
// try once with locale, even if it is NULL
$sql = $this->_getSQL($exclusions, $locale);
$result = DB::select($sql);
if (!$result) {
// if no user-lang recs left, get one without locale
$sql = $this->_getSQL($exclusions);
$result = DB::select($sql);
}
}
return $result;
}

protected function _getSQL($exclusions = [], $locale = NULL)
{
$sql = "SELECT
d.`id_ords` as `id_ords`,
d.`data_provider` as `partner`,
d.`product_category` as `product_category`,
TRIM(d.`brand`) as `brand`,
d.`repair_status` as `repair_status`,
TRIM(d.`problem`) as `problem`,
d.`language` as `language`,
TRIM(d.`translation`) as `translation`,
COUNT(o.`id_ords`) as `opinions_count`
FROM `devices_battcat_ora` d
LEFT JOIN `devices_faults_batteries_ora_opinions` o ON o.`id_ords` = d.`id_ords`
WHERE 1 %s
GROUP BY d.`id_ords`
HAVING `opinions_count` < 3
ORDER BY rand()
LIMIT 1;
";
$and = '';
if (!empty($exclusions)) {
$ids = implode("','", $exclusions);
$and .= "\nAND d.`id_ords` NOT IN ('$ids')";
}
if (!is_null($locale)) {
$and .= "\nAND (d.`language` = '$locale')";
}
$sql = sprintf($sql, $and);
logger($sql);
return $sql;
}

/**
* Fetch all fault_type_ids for battcat
*
* @return array
*/
public function fetchFaultTypes($repair_status)
{
$sql = "SELECT * FROM `fault_types_batteries` WHERE `repair_status` = '$repair_status'";
logger($sql);
return DB::select($sql);
}

/**
*
*
* @return mixed
*/
public function fetchStatus()
{

$result = [];

$result['total_devices'] = DB::select("
SELECT COUNT(DISTINCT d.id_ords) AS total
FROM `devices_battcat_ora` d
");

$result['total_opinions_3'] = DB::select("
SELECT COUNT(DISTINCT o.id_ords) AS total
FROM devices_faults_batteries_ora_opinions o
WHERE (SELECT COUNT(o2.id_ords) FROM devices_faults_batteries_ora_opinions o2 WHERE o2.id_ords = o.id_ords GROUP BY o2.id_ords) = 3
");

$result['total_opinions_2'] = DB::select("
SELECT COUNT(DISTINCT o.id_ords) AS total
FROM devices_faults_batteries_ora_opinions o
WHERE (SELECT COUNT(o2.id_ords) FROM devices_faults_batteries_ora_opinions o2 WHERE o2.id_ords = o.id_ords GROUP BY o2.id_ords) = 2
");

$result['total_opinions_1'] = DB::select("
SELECT COUNT(DISTINCT o.id_ords) AS total
FROM devices_faults_batteries_ora_opinions o
WHERE (SELECT COUNT(o2.id_ords) FROM devices_faults_batteries_ora_opinions o2 WHERE o2.id_ords = o.id_ords GROUP BY o2.id_ords) = 1
");

$result['total_opinions_0'] = DB::select("
SELECT COUNT(d.id_ords) AS total
FROM devices_battcat_ora d
LEFT JOIN devices_faults_batteries_ora_opinions o ON o.id_ords = d.id_ords
WHERE o.id_ords IS NULL
");

/*
This is how to get progress with SQL but quicker code used below
*/
// $result['progress'] = DB::select("
// SELECT
// ROUND((r2.opinions/r2.batteries)*100) as percent
// FROM (
// SELECT
// COUNT(*) AS opinions,
// (SELECT COUNT(*) FROM devices_battcat_ora) as batteries
// FROM (
// SELECT
// o.id_ords,
// (SELECT o1.fault_type_id FROM devices_faults_batteries_ora_opinions o1 WHERE o1.id_ords = o.id_ords GROUP BY o1.fault_type_id ORDER BY COUNT(o1.fault_type_id) DESC LIMIT 1) AS winning_opinion_id,
// ROUND((SELECT COUNT(o3.fault_type_id) as top_crowd_opinion_count FROM devices_faults_batteries_ora_opinions o3 WHERE o3.id_ords = o.id_ords GROUP BY o3.fault_type_id ORDER BY top_crowd_opinion_count DESC LIMIT 1) /
// (SELECT COUNT(o4.fault_type_id) as all_votes FROM devices_faults_batteries_ora_opinions o4 WHERE o4.id_ords = o.id_ords) * 100) AS top_crowd_opinion_percentage,
// COUNT(o.fault_type_id) AS all_crowd_opinions_count
// FROM devices_faults_batteries_ora_opinions o
// GROUP BY o.id_ords
// HAVING
// (all_crowd_opinions_count > 1 AND top_crowd_opinion_percentage > 60)
// OR
// (all_crowd_opinions_count = 3 AND top_crowd_opinion_percentage < 60)
// ) AS r1
// ) AS r2
// ");

$result['total_recats'] = DB::select("
SELECT COUNT(*) AS total FROM (
SELECT
o.id_ords,
(SELECT o1.fault_type_id FROM devices_faults_batteries_ora_opinions o1 WHERE o1.id_ords = o.id_ords GROUP BY o1.fault_type_id ORDER BY COUNT(o1.fault_type_id) DESC LIMIT 1) AS winning_opinion_id,
ROUND((SELECT COUNT(o3.fault_type_id) as top_crowd_opinion_count FROM devices_faults_batteries_ora_opinions o3 WHERE o3.id_ords = o.id_ords GROUP BY o3.fault_type_id ORDER BY top_crowd_opinion_count DESC LIMIT 1) /
(SELECT COUNT(o4.fault_type_id) as all_votes FROM devices_faults_batteries_ora_opinions o4 WHERE o4.id_ords = o.id_ords) * 100) AS top_crowd_opinion_percentage,
COUNT(o.fault_type_id) AS all_crowd_opinions_count
FROM devices_faults_batteries_ora_opinions o
GROUP BY o.id_ords
HAVING
(all_crowd_opinions_count > 1 AND top_crowd_opinion_percentage > 60)
UNION
SELECT
a.id_ords,
ANY_VALUE(a.fault_type_id) AS winning_opinion_id,
100 AS top_crowd_opinion_percentage,
3 AS all_crowd_opinions_count
FROM devices_faults_batteries_ora_adjudicated a
) AS result
");

$result['list_recats'] = DB::select("
SELECT result.winning_opinion_id, fta.title as winning_opinion, COUNT(*) AS total FROM (
SELECT
o.id_ords,
(SELECT o1.fault_type_id FROM devices_faults_batteries_ora_opinions o1 WHERE o1.id_ords = o.id_ords GROUP BY o1.fault_type_id ORDER BY COUNT(o1.fault_type_id) DESC LIMIT 1) AS winning_opinion_id,
ROUND((SELECT COUNT(o3.fault_type_id) as top_crowd_opinion_count FROM devices_faults_batteries_ora_opinions o3 WHERE o3.id_ords = o.id_ords GROUP BY o3.fault_type_id ORDER BY top_crowd_opinion_count DESC LIMIT 1) /
(SELECT COUNT(o4.fault_type_id) as all_votes FROM devices_faults_batteries_ora_opinions o4 WHERE o4.id_ords = o.id_ords) * 100) AS top_crowd_opinion_percentage,
COUNT(o.fault_type_id) AS all_crowd_opinions_count
FROM devices_faults_batteries_ora_opinions o
GROUP BY o.id_ords
HAVING
(all_crowd_opinions_count > 1 AND top_crowd_opinion_percentage > 60)
UNION
SELECT
a.id_ords,
a.fault_type_id AS winning_opinion_id,
100 AS top_crowd_opinion_percentage,
3 AS all_crowd_opinions_count
FROM devices_faults_batteries_ora_adjudicated a
) AS result
LEFT JOIN fault_types_batteries fta ON fta.id = result.winning_opinion_id
GROUP BY winning_opinion_id
ORDER BY total DESC
");

$result['list_splits'] = DB::select("
SELECT
d.id_ords,
(SELECT o1.fault_type_id FROM devices_faults_batteries_ora_opinions o1 WHERE o1.id_ords = o.id_ords GROUP BY o1.fault_type_id ORDER BY COUNT(o1.fault_type_id) DESC LIMIT 1) AS winning_opinion_id,
ROUND((SELECT COUNT(o3.fault_type_id) as top_crowd_opinion_count FROM devices_faults_batteries_ora_opinions o3 WHERE o3.id_ords = o.id_ords GROUP BY o3.fault_type_id ORDER BY top_crowd_opinion_count DESC LIMIT 1) /
(SELECT COUNT(o4.fault_type_id) as all_votes FROM devices_faults_batteries_ora_opinions o4 WHERE o4.id_ords = o.id_ords) * 100) AS top_crowd_opinion_percentage,
COUNT(o.fault_type_id) AS all_crowd_opinions_count,
GROUP_CONCAT(ft.title ORDER BY ft.title) as opinions,
d.brand as brand,
d.problem as problem
FROM `devices_battcat_ora` d
LEFT OUTER JOIN devices_faults_batteries_ora_opinions o ON o.id_ords = d.id_ords
LEFT OUTER JOIN fault_types_batteries ft ON ft.id = o.fault_type_id
WHERE (SELECT a.id_ords FROM devices_faults_batteries_ora_adjudicated a WHERE a.id_ords = d.id_ords) IS NULL
GROUP BY d.id_ords
HAVING
(all_crowd_opinions_count = 3 AND top_crowd_opinion_percentage < 60)
");

$total_splits = count($result['list_splits']);
$result['total_splits'] = [json_decode(json_encode(['total' => $total_splits]), FALSE)];
$progress = round((($result['total_recats'][0]->total + $result['total_splits'][0]->total) / $result['total_devices'][0]->total) * 100);
$result['progress'] = [json_decode(json_encode(['total' => $progress]), FALSE)];
return $result;
}

/**
* Write the winning opinions to `devices_battcat_ora`.`fault_type`.
*
* @return mixed
*/
public function updateDevices()
{

DB::statement("CREATE TEMPORARY TABLE IF NOT EXISTS `devices_faults_batteries_ora_temporary` AS
SELECT *
FROM
(SELECT
r2.id_ords,
CASE
WHEN (r2.opinions<2) OR (r2.opinions=2 AND r2.opinions_distinct=2) THEN NULL
WHEN (r2.opinions_distinct=1 AND r2.opinions>1) THEN r2.faultnames
WHEN (r2.opinions_distinct=3) THEN ((SELECT a.fault_type_id FROM devices_faults_batteries_ora_adjudicated a WHERE a.id_ords = r2.id_ords))
ELSE (SELECT o.fault_type_id FROM devices_faults_batteries_ora_opinions o WHERE o.id_ords = r2.id_ords GROUP BY fault_type_id ORDER BY COUNT(*) DESC LIMIT 1)
END AS winning_opinion_id
FROM
(SELECT
r1.id_ords,
COUNT(r1.fault_type_id) as opinions,
COUNT(DISTINCT r1.fault_type_id) as opinions_distinct,
GROUP_CONCAT(DISTINCT r1.fault_type_id) as faultnames
FROM
(SELECT
o.id_ords,
o.fault_type_id
FROM devices_faults_batteries_ora_opinions o
) AS r1
GROUP BY r1.id_ords
) AS r2
) AS r3
WHERE r3.winning_opinion_id IS NOT NULL
");
DB::statement("ALTER TABLE `devices_faults_batteries_ora_temporary` ADD PRIMARY KEY(`id_ords`);");

$result = DB::update("UPDATE devices_battcat_ora d, devices_faults_batteries_ora_temporary t
SET d.fault_type_id = t.winning_opinion_id
WHERE d.id_ords = t.id_ords;");

DB::statement("DROP TEMPORARY TABLE IF EXISTS `devices_faults_batteries_ora_temporary`");

return $result;
}
}
2 changes: 1 addition & 1 deletion app/Cluster.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ class Cluster extends Model

public function categories()
{
return $this->hasMany('App\Category', 'cluster', 'idclusters');
return $this->hasMany(\App\Category::class, 'cluster', 'idclusters');
}
}
2 changes: 1 addition & 1 deletion app/Console/Commands/ImportRepairTogether.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function importGroups()
'longitude' => $longitude,
'country' => $country,
'free_text' => $free_text,
'shareable_code' => FixometerHelper::generateUniqueShareableCode('App\Group', 'shareable_code'),
'shareable_code' => FixometerHelper::generateUniqueShareableCode(\App\Group::class, 'shareable_code'),
//'network_id' => $repairTogetherNetworkId,
'external_id' => $external_id,
];
Expand Down
Loading

0 comments on commit 63ffef7

Please sign in to comment.