Skip to content

Commit

Permalink
Merge pull request #649 from OpenLitterMap/staging
Browse files Browse the repository at this point in the history
merge master into staging
  • Loading branch information
xlcrr authored Sep 24, 2024
2 parents ad802c5 + 41a4e86 commit 36dd89c
Show file tree
Hide file tree
Showing 747 changed files with 281,904 additions and 35,621 deletions.
99 changes: 99 additions & 0 deletions .env.testing
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
APP_NAME=OpenLitterMap
APP_ENV=testing
APP_KEY=base64:wtfMWBVJMehE9KRxZqwTmY+G0vPoCoPOwtloNZmueLM=
APP_DEBUG=true
APP_URL=http://olm.test
APP_ROOT_DIR=/home/vagrant/Code/olm

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=olm
DB_USERNAME=homestead
DB_PASSWORD=secret

# For Testing
DB_FOREIGN_KEYS=false

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

LOCATE_API_KEY=

MIX_GOOGLE_RECAPTCHA_KEY=
MIX_GOOGLE_RECAPTCHA_SECRET=

BROADCAST_DRIVER=pusher
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=redis
QUEUE_CONNECTION=redis

MAIL_DRIVER=smtp
MAILGUN_DOMAIN=
MAILGUN_SECRET=
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=
MAIL_PASSWORD=
MAIL_ENCRYPTION=tls

STRIPE_KEY=
STRIPE_SECRET=
STRIPE_MODEL=App\User
MIX_STRIPE_KEY=
MIX_STRIPE_PUBLIC_KEY=

CASHIER_MODEL=App\User
CASHIER_CURRENCY=eur

AWS_KEY=homestead
AWS_SECRET=secretkey
AWS_BUCKET=olm-public
AWS_REGION=us-east-1
AWS_ENDPOINT=http://192.168.56.4:9600

x500_AWS_KEY=homestead
x500_AWS_SECRET=secretkey
x500_AWS_REGION=us-east-1
x500_AWS_BUCKET=olm-public-bbox
x500_AWS_ENDPOINT=http://192.168.56.4:9600

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=eu
LARAVEL_WEBSOCKETS_PORT=6002

WEBSOCKET_BROADCAST_HOST=192.168.56.4

LOCATION_API_KEY=

BACKUP_NAME=OpenLitterMap_Backup
BACKUP_ARCHIVE_PASSWORD=
DROPBOX_TOKEN=

SLACK_WEBHOOK_URL=

ROOT_KEY=
OWNER_PKH=

# consumer keys
TWITTER_API_CONSUMER_KEY=
TWITTER_API_CONSUMER_SECRET=

# tokens
TWITTER_API_BEARER_TOKEN=

# access token + secret
TWITTER_API_ACCESS_TOKEN=
TWITTER_API_ACCESS_SECRET=

# new
TWITTER_API_CLIENT_ID=
TWITTER_API_CLIENT_SECRET=

ENTROPY=
10 changes: 5 additions & 5 deletions .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ jobs:
steps:
- uses: shivammathur/setup-php@v2
with:
php-version: '7.3'
php-version: '8.2'
extensions: mbstring, dom, fileinfo, mysql
# coverage: xdebug #optional
- uses: actions/checkout@v3
# TODO: Remove this line when we can safely upgrade all our packages
# to versions that support Node 16.
with:
node-version: 18.20.3
- uses: actions/setup-node@v3
with:
node-version: 16
node-version: 18.20.3
- name: Start mysql service
run: sudo service mysql start
- name: Setup minio
Expand Down Expand Up @@ -95,7 +95,7 @@ jobs:
- name: Install NPM assets
run: npm install --silent --force
- name: Compile NPM assets
run: npm run dev --silent
run: npm run build --silent
- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/phpunit
env:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/storage/*.key
/vendor
.env
.env.testing
.env.backup
.phpunit.result.cache
Homestead.json
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@
namespace App\Actions\Locations;

use App\Models\Photo;

use Illuminate\Support\Facades\Redis;

class UpdateLeaderboardsForLocationAction
{
/** @var UpdateLeaderboardsXpAction */
protected $updateXpAction;
protected UpdateLeaderboardsXpAction $updateXpAction;

/**
* @param UpdateLeaderboardsXpAction $updateXpAction
Expand Down
21 changes: 17 additions & 4 deletions app/Actions/Locations/UpdateLeaderboardsXpAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,24 @@ public function run (
$day = now()->day;

// Update the Users total score in the Global Leaderboard
Redis::zincrby("xp.users", $incrXp, $userId);
$this->addXp("xp.users", $incrXp, $userId);

// Update the Users total score for each time-stamped Leaderboard
Redis::zincrby("leaderboard:users:$year:$month:$day", $incrXp, $userId);
Redis::zincrby("leaderboard:users:$year:$month", $incrXp, $userId);
Redis::zincrby("leaderboard:users:$year", $incrXp, $userId);
$this->addXp("leaderboard:users:$year:$month:$day", $incrXp, $userId);
$this->addXp("leaderboard:users:$year:$month", $incrXp, $userId);
$this->addXp("leaderboard:users:$year", $incrXp, $userId);
}

protected function addXp ($key, $xp, $userId): void {
if ($xp <= 0) {
$currentScore = Redis::zscore($key, $userId) ?? 0;

$newScore = max(0, $currentScore + $xp);

Redis::zadd($key, $newScore, $userId);
} else {

Redis::zincrby($key, $xp, $userId);
}
}
}
6 changes: 4 additions & 2 deletions app/Actions/Locations/UpdateTotalPhotosForLocationAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ class UpdateTotalPhotosForLocationAction
* @param string $stateId State
* @param string $cityId City
* @param int $increaseBy can be negative, value will be subtracted, but not below 0
* @return void
*/
public function run(string $countryId, string $stateId, string $cityId, int $increaseBy = 1)
public function run (string $countryId, string $stateId, string $cityId, int $increaseBy = 1): void
{
$this->updateValue("country:$countryId", $increaseBy);

Expand All @@ -33,8 +34,9 @@ public function run(string $countryId, string $stateId, string $cityId, int $inc
*
* @param $hashName
* @param $value
* @return void
*/
protected function updateValue($hashName, $value)
protected function updateValue ($hashName, $value): void
{
// Separate if conditions to skip redis check when $value is positive
if ($value < 0) {
Expand Down
11 changes: 5 additions & 6 deletions app/Actions/Photos/DeletePhotoAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,11 @@ public function run (Photo $photo)
*/
protected function deletePhoto (string $filename, string $disk) :void
{
$path = str_replace(
rtrim(Storage::disk($disk)->url('/'), '/'),
'',
$filename
);
// Remove storage prefix if found. Only needed for local testing.
$filename = ltrim($filename, '/storage/');

Storage::disk($disk)->delete($path);
if (Storage::disk($disk)->exists($filename)) {
Storage::disk($disk)->delete($filename);
}
}
}
5 changes: 3 additions & 2 deletions app/Actions/Photos/MakeImageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ class MakeImageAction
* @param bool $resize
*
* @return array
* @throws Exception
*/
public function run(UploadedFile $file, bool $resize = false): array
public function run (UploadedFile $file, bool $resize = false): array
{
$imageAndExifData = $this->getImageAndExifData($file);

Expand All @@ -39,7 +40,7 @@ public function run(UploadedFile $file, bool $resize = false): array
* @return array
* @throws Exception
*/
protected function getImageAndExifData(UploadedFile $file): array
protected function getImageAndExifData (UploadedFile $file): array
{
$extension = $file->getClientOriginalExtension();

Expand Down
4 changes: 3 additions & 1 deletion app/Console/Commands/Clusters/GenerateClusters.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ protected function generateFeatures (int $year = null): void
{
$this->info('Generating features...');

$photos = Photo::query()->select('lat', 'lon');
$photos = Photo::query()
->select('verified', 'lat', 'lon')
->where('verified', 2);

if ($year) {
$photos->whereYear('datetime', $year);
Expand Down
147 changes: 147 additions & 0 deletions app/Console/Commands/Photos/GenerateData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

namespace App\Console\Commands\Photos;

use App\Models\Photo;
use App\Models\User\User;
use App\Models\Location\City;
use App\Models\Location\State;
use App\Models\Location\Country;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use App\Actions\Photos\AddTagsToPhotoAction;
use Illuminate\Support\Facades\Redis;

class GenerateData extends Command
{
protected $signature = 'olm:generate-data {photos=1500}';

protected $description = 'Generates data in Cork, Ireland. Default = 1500 photos';

private AddTagsToPhotoAction $addTagsToPhotoAction;

public function __construct (AddTagsToPhotoAction $addTagsToPhotoAction)
{
parent::__construct();

$this->addTagsToPhotoAction = $addTagsToPhotoAction;
}

public function handle (): void
{
$userId = User::first()->id;

$country = Country::firstOrCreate([
'country' => 'Ireland',
'shortcode' => 'ie',
'created_by' => $userId,
]);

State::firstOrCreate([
'state' => 'County Cork',
'country_id' => $country->id,
'created_by' => $userId
]);

$city = City::firstOrCreate([
'city' => 'Cork',
'country_id' => $country->id,
'created_by' => $userId
]);

$photosToGen = $this->argument('photos');

$this->generatePhotos($photosToGen, $country, $city);
}

protected function generatePhotos ($photosToGen, $country, $city): array
{
$this->line('Generating photos...');

$bar = $this->output->createProgressBar($photosToGen);
$bar->setFormat('debug');
$bar->start();

$photos = [];
$litterJson = $this->getLitterJson();
$categories = ['smoking', 'food', 'coffee', 'alcohol', 'softdrinks', 'sanitary', 'other', 'brands'];

// Get max 10 users
$users = User::inRandomOrder()->limit(10)->get();

for ($i = 0; $i < $photosToGen; $i++)
{
$lat = rand(51.85391800 * 100000000, 51.92249800 * 100000000) / 100000000;
$lon = rand(-8.53209200 * 100000000, -8.36823900 * 100000000) / 100000000;

$tags = [];

foreach ($categories as $category)
{
if (isset($litterJson[$category]))
{
$litterTypes = array_keys($litterJson[$category]);

// Ensure that we only select as many items as are available
$availableItems = count($litterTypes);
$numberToSelect = min(rand(1, 5), $availableItems);

// Select 1-5 random types of litter from the category
$selectedLitter = collect($litterTypes)->random($numberToSelect);

// Populate tags array with randomly selected litter types and quantities
foreach ($selectedLitter as $litterKey) {
$tags[$category][$litterKey] = rand(1, 5);
}
}
}

// Pick 1 random user
$user = $users->random();

$createdAt = now()->subWeek()->startOfWeek()->addHour();

$photo = Photo::create([
'user_id' => $user->id,
'country_id' => $country->id,
'city_id' => $city->id,
'lat' => $lat,
'lon' => $lon,
'model' => "iPhone 5",
'filename' => "dummy.png",
'datetime' => $createdAt,
'verified' => 2,
'verification' => 1,
'remaining' => 1,
'geohash' => \GeoHash::encode($lat, $lon),
'created_at' => $createdAt,
'updated_at' => $createdAt,
]);

$litterTotals = $this->addTagsToPhotoAction->run($photo, $tags);

$year = $createdAt->year;
$month = $createdAt->month;
$day = $createdAt->day;
$key = "leaderboard:users:$year:$month:$day";
Redis::zadd($key, $litterTotals['all'], $user->id);

$photo->save();

$bar->advance();
}

$bar->finish();

return $photos;
}

protected function getLitterJson (): array
{
$path = resource_path('js/langs/en/litter.json');

$contents = File::get($path);

return json_decode($contents, true);
}
}
Loading

0 comments on commit 36dd89c

Please sign in to comment.