同步流量池数据
This commit is contained in:
parent
bcfcca3ec8
commit
a90b0f11d4
90
app/Domains/Virtual/Commands/Sync/FlowPoolMonthSync.php
Normal file
90
app/Domains/Virtual/Commands/Sync/FlowPoolMonthSync.php
Normal file
@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Virtual\Commands\Sync;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use App\Models\Virtual\FlowPool;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Exceptions\NotExistException;
|
||||
use App\Models\Virtual\FlowPoolMonth;
|
||||
use App\Models\Real\FlowPool as RealFlowPool;
|
||||
use App\Domains\Virtual\Repositories\PackageRepository;
|
||||
use App\Domains\Virtual\Services\FlowPoolService;
|
||||
|
||||
class FlowPoolMonthSync extends Command
|
||||
{
|
||||
protected $name = 'virtual:sync-flow-pool-month';
|
||||
|
||||
protected $description = '同步VD流量池每月数据';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$this->line($this->description);
|
||||
$realFlowPools = RealFlowPool::select(['id', 'sn'])->get()->keyBy('sn');
|
||||
$flowPools = FlowPool::withTrashed()->select(['id', 'real_pool_ids', 'name'])->get();
|
||||
$flowPoolsKeyByName = $flowPools->keyBy('name');
|
||||
$packages = app(PackageRepository::class)->withTrashed()->get()->keyBy('sn');
|
||||
|
||||
$old_stats = DB::connection('vd_old')->table('ckb_data_pool_statis')->orderBy('dpmb_id')->get()->collect()->toArray();
|
||||
|
||||
$array = [];
|
||||
|
||||
foreach ($old_stats as $stat) {
|
||||
$this->line($stat['dpmb_id'] . '-' . $stat['year_month'] . '-' . $stat['td_pool_sn']);
|
||||
|
||||
$pool_id = 0;
|
||||
|
||||
if (strpos($stat['td_pool_sn'], 'vd-') === 0) {
|
||||
$pool_id = $flowPoolsKeyByName[str_replace('vd-', '', $stat['td_pool_sn'])]['id'];
|
||||
} else {
|
||||
$real_pool_id = $realFlowPools[$stat['td_pool_sn']]['id'];
|
||||
|
||||
if (!$real_pool_id) {
|
||||
throw new NotExistException('未找到RD流量池 #:' . $stat['td_pool_sn']);
|
||||
}
|
||||
|
||||
foreach ($flowPools as $key => $value) {
|
||||
if (in_array($real_pool_id, $value['real_pool_ids'])) {
|
||||
$pool_id = $value['id'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!$pool_id) {
|
||||
throw new NotExistException('未找到对应VD流量池 #:' . $stat['td_pool_sn']);
|
||||
}
|
||||
|
||||
$items = DB::connection('vd_old')->table('ckb_data_pool_statis_item')
|
||||
->select(['sim', 'vd_package_sn', 'flows_used'])
|
||||
->where('pool_statis_id', $stat['dpmb_id'])->get()->collect()->toArray();
|
||||
|
||||
$items = array_map(function ($item) use ($packages, $stat, $pool_id) {
|
||||
return [
|
||||
'month' => $stat['year_month'],
|
||||
'sim' => intval($item['sim']),
|
||||
'package_id' => $packages[$item['vd_package_sn']]['id'],
|
||||
'pool_id' => $pool_id,
|
||||
'kilobyte' => ceil($item['flows_used'] * 1024),
|
||||
];
|
||||
}, $items);
|
||||
|
||||
$array = array_merge($array, $items);
|
||||
}
|
||||
|
||||
DB::transaction(function () use ($array) {
|
||||
$this->line('插入数据,条数 #:' . count($array));
|
||||
|
||||
foreach (array_groupBy($array, 'month') as $month => $group) {
|
||||
$table = FlowPoolService::checkTable($month);
|
||||
|
||||
foreach (array_chunk($group, 1000) as $values) {
|
||||
echo '.';
|
||||
app(FlowPoolMonth::class)->setTable($table)->upsert($values, ['sim', 'month']);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
$this->line($this->description . ' End');
|
||||
}
|
||||
}
|
@ -6,11 +6,12 @@ use Carbon\Carbon;
|
||||
use Illuminate\Console\Command;
|
||||
use App\Models\Virtual\FlowPool;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Models\Virtual\FlowPoolSetting;
|
||||
use App\Models\Real\FlowPool as RealFlowPool;
|
||||
use App\Domains\Virtual\Services\CommonService;
|
||||
use App\Domains\Virtual\Repositories\CompanyRepository;
|
||||
use App\Domains\Virtual\Repositories\PackageRepository;
|
||||
use App\Domains\Virtual\Repositories\FlowPoolRepository;
|
||||
use App\Models\Virtual\FlowPoolSetting;
|
||||
use App\Domains\Virtual\Repositories\FlowPoolSettingRepository;
|
||||
|
||||
class FlowPoolSync extends Command
|
||||
@ -27,6 +28,7 @@ class FlowPoolSync extends Command
|
||||
$companies = app(CompanyRepository::class)->withTrashed()->get()->keyBy('sn');
|
||||
$packages = app(PackageRepository::class)->withTrashed()->get()->keyBy('sn');
|
||||
$flowPools = FlowPool::withTrashed()->select(['id', 'sn'])->get()->keyBy('sn');
|
||||
$realFlowPools = RealFlowPool::select(['id', 'sn'])->get()->keyBy('sn');
|
||||
|
||||
$oldFlowPools = DB::connection('vd_old')->table('ckb_data_pool')->select([
|
||||
'dp_sn',
|
||||
@ -35,7 +37,7 @@ class FlowPoolSync extends Command
|
||||
'dp_share_type',
|
||||
'dp_vd_company_id',
|
||||
'dp_vd_packages',
|
||||
'dp_td_data_pool_id',
|
||||
'dp_td_data_pool_sn',
|
||||
'dp_del',
|
||||
'dp_create_time',
|
||||
])->get()->collect()->toArray();
|
||||
@ -116,6 +118,15 @@ class FlowPoolSync extends Command
|
||||
$settingArray[] = $settingData;
|
||||
}
|
||||
|
||||
$real_pool_ids = [];
|
||||
|
||||
foreach ( json_decode($value['dp_td_data_pool_sn'], true) as $pool_sn) {
|
||||
$pool_sn = explode('---', $pool_sn)[0];
|
||||
if(isset($realFlowPools[$pool_sn])){
|
||||
array_push($real_pool_ids, $realFlowPools[$pool_sn]['id']);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$data = [
|
||||
'id' => $id,
|
||||
@ -125,7 +136,7 @@ class FlowPoolSync extends Command
|
||||
'carrier_operator' => self::$carrier_operator[$value['dp_carrieroperator']],
|
||||
'shared' => self::$shared[$value['dp_share_type']],
|
||||
'package_ids' => json_encode($flowPoolPackages),
|
||||
'real_pool_ids' => json_encode(array_map('intval', json_decode($value['dp_td_data_pool_id'], true))),
|
||||
'real_pool_ids' => json_encode($real_pool_ids),
|
||||
'start_at' => date('Y-m-1 00:00:00', $value['dp_create_time']),
|
||||
'remark' => '',
|
||||
'created_at' => date('Y-m-d H:i:s', $value['dp_create_time']),
|
||||
|
@ -27,6 +27,7 @@ class VirtualServiceProvider extends ServiceProvider
|
||||
\App\Domains\Virtual\Commands\Sync\PackageSync::class,
|
||||
\App\Domains\Virtual\Commands\Sync\ProductSync::class,
|
||||
\App\Domains\Virtual\Commands\Sync\FlowPoolSync::class,
|
||||
\App\Domains\Virtual\Commands\Sync\FlowPoolMonthSync::class,
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -93,7 +93,7 @@ class FlowPoolService extends Service
|
||||
foreach ($item['package_ids'] as $key => $value) {
|
||||
if (isset($array[$value])) {
|
||||
array_push($array[$value], $item['company_id']);
|
||||
}else{
|
||||
} else {
|
||||
$array[$value] =[$item['company_id']];
|
||||
}
|
||||
}
|
||||
@ -531,13 +531,7 @@ class FlowPoolService extends Service
|
||||
}
|
||||
}
|
||||
|
||||
$table = app(FlowPoolMonth::class)->getTable() . '_' . $month->format('Ym');
|
||||
|
||||
if (!Schema::hasTable($table)) {
|
||||
Schema::table(app(FlowPoolMonth::class)->getTable(), function ($blueprint) use ($table, $month) {
|
||||
$blueprint->addPartition($table, 'list', [$month->format('Ym')]);
|
||||
});
|
||||
}
|
||||
$table = self::checkTable($month->format('Ym'));
|
||||
|
||||
$flowPoolData = $conditions;
|
||||
$flowPoolData['month'] = $month->format('Ym');
|
||||
@ -549,7 +543,7 @@ class FlowPoolService extends Service
|
||||
app(FlowPoolMonth::class)->setTable($table)->where('pool_id', $pool_id)->delete();
|
||||
|
||||
foreach (array_chunk($data, 10000) as $chunk) {
|
||||
app(FlowPoolMonth::class)->setTable($table)->insert($chunk);
|
||||
app(FlowPoolMonth::class)->setTable($table)->upsert($chunk, ['sim', 'month']);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
throw $e;
|
||||
@ -737,4 +731,25 @@ class FlowPoolService extends Service
|
||||
{
|
||||
return human_filesize($int, 2, ['unit' => 'KB', 'min' => 'MB', 'max' => 'PB']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查当月分区表是否已创建
|
||||
*
|
||||
* @param string $month
|
||||
* @return string
|
||||
*/
|
||||
public static function checkTable(string $month)
|
||||
{
|
||||
$table = app(FlowPoolMonth::class)->getTable() . '_' . $month;
|
||||
if (!Schema::hasTable($table)) {
|
||||
Schema::table(app(FlowPoolMonth::class)->getTable(), function ($blueprint) use ($table, $month) {
|
||||
$blueprint->addPartition($table, 'list', [$month]);
|
||||
});
|
||||
|
||||
Schema::table($table, function ($blueprint) {
|
||||
$blueprint->unique(['sim', 'month']);
|
||||
});
|
||||
}
|
||||
return $table;
|
||||
}
|
||||
}
|
||||
|
@ -1,2 +0,0 @@
|
||||
<?php
|
||||
echo time();
|
Loading…
x
Reference in New Issue
Block a user