| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367 |
- <?php
- namespace app\commands;
- use app\models\Descarga;
- use yii\console\Controller;
- use yii\console\ExitCode;
- use yii\db\Query;
- use yii\helpers\ArrayHelper;
- class SyncController extends Controller {
- public $limite = 5;
- public $pagina = 1;
- public $ciudad = null;
- public $estacion = null;
- public $fi = null;
- public $ff = null;
- public $pc = null;
- public function options($actionID) {
- return [
- 'pagina',
- 'limite',
- 'ciudad',
- 'estacion',
- 'fi',
- 'ff',
- 'pc'
- ];
- }
- public function actionIndex() {
- if($this->pagina < 1) {
- $this->pagina = 1;
- }
- $ciudades = (new Query())
- ->select(["idCiudad"])
- ->distinct()
- ->from("box")
- ->innerJoin("CiudadEstacion", "box.id = {{CiudadEstacion}}.[[idCiudad]]")
- ->innerJoin("Estacion", "{{Estacion}}.id = {{CiudadEstacion}}.[[idEstacion]]")
- ->andWhere([
- "{{CiudadEstacion}}.monitoreable" => true,
- "box.activo" => true,
- ])
- ->andWhere("[[idGrupo]] is not null")
- ->limit($this->limite)
- ->offset($this->limite * ($this->pagina - 1))
- ->orderBy(["idCiudad" => SORT_ASC])
- ->column();
- return $this->descarga($ciudades);
- }
- public function descarga($ciudadesIds = []) {
- $estados = [
- 'AGUASCALIENTES' => 'AGU',
- 'BAJA CALIFORNIA' => 'BCN',
- 'BAJA CALIFORNIA SUR' => 'BCS',
- 'CAMPECHE' => 'CAM',
- 'CHIAPAS' => 'CHP',
- 'CHIHUAHUA' => 'CHH',
- 'CIUDAD DE MÉXICO' => 'CMX',
- 'COAHUILA' => 'COA',
- 'COLIMA' => 'COL',
- 'DURANGO' => 'DUR',
- 'GUANAJUATO' => 'GUA',
- 'GUERRERO' => 'GRO',
- 'HIDALGO' => 'HID',
- 'JALISCO' => 'JAL',
- 'MEXICO' => 'MEX',
- 'MICHOACAN' => 'MIC',
- 'MORELOS' => 'MOR',
- 'NAYARIT' => 'NAY',
- 'NUEVO LEON' => 'NLE',
- 'OAXACA' => 'OAX',
- 'PUEBLA' => 'PUE',
- 'QUERETARO' => 'QUE',
- 'QUINTANA ROO' => 'ROO',
- 'SAN LUIS POTOSI' => 'SLP',
- 'SINALOA' => 'SIN',
- 'SONORA' => 'SON',
- 'TABASCO' => 'TAB',
- 'TAMAULIPAS' => 'TAM',
- 'TLAXCALA' => 'TLA',
- 'VERACRUZ' => 'VER',
- 'YUCATAN' => 'YUC',
- 'ZACATECAS' => 'ZAC'
- ];
- $limite = 500;
- $order = SORT_ASC;
- $orderUltimo = SORT_DESC; // Siempre tiene que ser el contrario de order
- $ciudadEstacion = (new Query())
- ->select(["idCiudad", "idEstacion", "clave"])
- ->from("box")
- ->innerJoin("CiudadEstacion", "box.id = {{CiudadEstacion}}.[[idCiudad]]")
- ->innerJoin("Estacion", "{{Estacion}}.id = {{CiudadEstacion}}.[[idEstacion]]")
- ->andWhere([
- "monitoreable" => true,
- "box.activo" => true,
- "idCiudad" => $ciudadesIds
- ])
- ->andWhere("[[idGrupo]] is not null")
- ->all();
- $idCiudades = ArrayHelper::getColumn($ciudadEstacion, "idCiudad");
- $ciudades = (new Query())
- ->select(["id", "nombre", "idEstado", "timezone", "tipo"])
- ->from("box")
- ->where(["id" => $idCiudades])
- ->indexBy("id")
- ->all();
- $idEstaciones = ArrayHelper::getColumn($ciudadEstacion, "idEstacion");
- $estaciones = (new Query())
- ->select(["id", "clave", "siglas", "frecuencia", "descripcion"])
- ->from("Estacion")
- ->andWhere(["id" => $idEstaciones])
- ->indexBy("clave")
- ->all();
- while(true) {
- foreach($ciudadEstacion as $ce) {
- $inicio = time();
- $archivos = (new Query())
- ->select("hash, box, station, filename, [[timestamp]] at time zone box.timezone as timestamp")
- ->from("file")
- ->innerJoin("box", "box.id = file.box")
- ->andWhere([">=", "[[timestamp]] at time zone box.timezone", '2021-12-01 00:00:00'])
- // ->andWhere(["<=", "[[timestamp]] at time zone box.timezone", '2021-10-01 00:00:00'])
- ->andWhere([
- "box" => $ce["idCiudad"],
- "station" => $ce["clave"]
- ])
- ->limit($limite)
- ->orderBy(["timestamp" => $order]);
- $ultimo = (new Query())
- ->select("fecha")
- ->from("Descarga")
- ->andWhere([
- "ciudad" => $ce["idCiudad"],
- "estacion" => $ce["clave"]
- ])
- ->andWhere([">=", "fecha", '2021-12-01 00:00:00'])
- ->orderBy(["fecha" => $orderUltimo])
- ->limit(1);
- $this->stdout("{Ultimo query: {$ultimo->createCommand()->getRawSql()}\n");
- $ultimo = $ultimo->scalar();
- $this->stdout("Ultimo: {$ultimo}\n");
- if($ultimo) {
- if($order === SORT_DESC) {
- $archivos->andWhere(["<=", "timestamp", $ultimo]);
- } else {
- $archivos->andWhere([">=", "timestamp", $ultimo]);
- }
- }
- $this->stdout("Archivos query: {$archivos->createCommand()->getRawSql()}\n");
- /*
- $sql = $archivos->createCommand()->getRawSql();
- $this->stdout("{$sql}\n");
- // $this->stdout("ultimo: {$ultimo}\n");
- // */
-
- $count = 0;
- foreach($archivos->each() as $archivo) {
- $modelo = new Descarga();
- $modelo->hash = $archivo["hash"];
- $modelo->estacion = $archivo["station"];
- $modelo->archivo = $archivo["filename"];
- $modelo->ciudad = $archivo["box"];
- $modelo->pc = null;
- $modelo->fecha = $archivo["timestamp"];
- $modelo->descargado = false;
- $modelo->nombre = basename($archivo["filename"]);
- $ciudad = $ciudades[$archivo["box"]];
- $estacion = $estaciones[$archivo["station"]];
- $fecha = \DateTime::createFromFormat("Y-m-d H:i:s", $archivo["timestamp"]);
- $siglas = explode("-", $estacion["siglas"]);
- $tipo = "";
- $senal = $siglas[0];
- if(isset($siglas[1])) {
- $tipo = $siglas[1];
- }
- if($tipo === "") {
- if($ciudad["tipo"] === "tv") {
- $tipo = "TDT";
- } elseif ($ciudad["tipo"] === "radio") {
- $tipo = "FM";
- } else {
- $tipo = "AM";
- }
- }
- $nombreCiudad = $ciudad["idEstado"];
- if($tipo === "AM") {
- $desc = explode(",", $estacion["descripcion"]);
- $nombreCiudad = $estacion["descripcion"];
- if(isset($desc[1])) {
- $nombreCiudad = $desc[1];
- }
- }
- if(isset($estados[$nombreCiudad])) {
- $nombreCiudad = $estados[$nombreCiudad];
- }
- $nombreCiudad = str_replace(" ", "_", trim($nombreCiudad));
- $y = $fecha->format("Y");
- $m = $fecha->format("m");
- $d = $fecha->format("d");
- $modelo->ruta = "{$y}/{$tipo}/{$nombreCiudad}/{$senal}/{$m}/{$d}";
- try {
- if(!$modelo->save()) {
- $errores = json_encode($modelo->getFirstErrors());
- $this->stdout("Error al guardar {$modelo->hash} {$errores}\n");
- }
- } catch(\Exception $e) {
- $this->stdout("Error al guardar {$modelo->hash}\n");
- }
- $count++;
- }
- // */
- $seg = time() - $inicio;
- $this->stdout("[{$ce["idCiudad"]}][{$ce["clave"]}] {$count} registros insertados en {$seg} segundos\n");
- }
- $this->stdout("Durmiendo 10 segundos...\n");
- sleep(10);
- }
- return ExitCode::OK;
- }
- public function actionPorEstacion() {
- $estados = [
- 'AGUASCALIENTES' => 'AGU',
- 'BAJA CALIFORNIA' => 'BCN',
- 'BAJA CALIFORNIA SUR' => 'BCS',
- 'CAMPECHE' => 'CAM',
- 'CHIAPAS' => 'CHP',
- 'CHIHUAHUA' => 'CHH',
- 'CIUDAD DE MÉXICO' => 'CMX',
- 'COAHUILA' => 'COA',
- 'COLIMA' => 'COL',
- 'DURANGO' => 'DUR',
- 'GUANAJUATO' => 'GUA',
- 'GUERRERO' => 'GRO',
- 'HIDALGO' => 'HID',
- 'JALISCO' => 'JAL',
- 'MEXICO' => 'MEX',
- 'MICHOACAN' => 'MIC',
- 'MORELOS' => 'MOR',
- 'NAYARIT' => 'NAY',
- 'NUEVO LEON' => 'NLE',
- 'OAXACA' => 'OAX',
- 'PUEBLA' => 'PUE',
- 'QUERETARO' => 'QUE',
- 'QUINTANA ROO' => 'ROO',
- 'SAN LUIS POTOSI' => 'SLP',
- 'SINALOA' => 'SIN',
- 'SONORA' => 'SON',
- 'TABASCO' => 'TAB',
- 'TAMAULIPAS' => 'TAM',
- 'TLAXCALA' => 'TLA',
- 'VERACRUZ' => 'VER',
- 'YUCATAN' => 'YUC',
- 'ZACATECAS' => 'ZAC'
- ];
- $order = SORT_ASC;
- $_estaciones = explode(",", $this->estacion);
- $ciudades = (new Query())
- ->select(["id", "nombre", "idEstado", "timezone", "tipo"])
- ->from("box")
- ->where(["id" => $this->ciudad])
- ->indexBy("id")
- ->all();
- $estaciones = (new Query())
- ->select(["id", "clave", "siglas", "frecuencia", "descripcion"])
- ->from("Estacion")
- ->andWhere(["clave" => $_estaciones])
- ->indexBy("clave")
- ->all();
- $inicio = time();
- $archivos = (new Query())
- ->select("hash, box, station, filename, [[timestamp]] at time zone box.timezone as timestamp")
- ->from("file")
- ->innerJoin("box", "box.id = file.box")
- ->andWhere([">=", "[[timestamp]] at time zone box.timezone", $this->fi])
- ->andWhere(["<=", "[[timestamp]] at time zone box.timezone", $this->ff])
- ->andWhere([
- "box" => $this->ciudad,
- "station" => $_estaciones
- ])
- ->orderBy(["timestamp" => $order]);
- $count = 0;
- foreach($archivos->each() as $archivo) {
- $modelo = new Descarga();
- $modelo->hash = $archivo["hash"];
- $modelo->estacion = $archivo["station"];
- $modelo->archivo = $archivo["filename"];
- $modelo->ciudad = $archivo["box"];
- $modelo->pc = null;
- if($this->pc !== null) {
- $modelo->pc = $this->pc;
- }
- $modelo->fecha = $archivo["timestamp"];
- $modelo->descargado = false;
- $modelo->nombre = basename($archivo["filename"]);
- $ciudad = $ciudades[$archivo["box"]];
- $estacion = $estaciones[$archivo["station"]];
- $fecha = \DateTime::createFromFormat("Y-m-d H:i:s", $archivo["timestamp"]);
- $siglas = explode("-", $estacion["siglas"]);
- $tipo = "";
- $senal = $siglas[0];
- if(isset($siglas[1])) {
- $tipo = $siglas[1];
- }
- if($tipo === "") {
- if($ciudad["tipo"] === "tv") {
- $tipo = "TDT";
- } elseif ($ciudad["tipo"] === "radio") {
- $tipo = "FM";
- } else {
- $tipo = "AM";
- }
- }
- $nombreCiudad = $ciudad["idEstado"];
- if($tipo === "AM") {
- $desc = explode(",", $estacion["descripcion"]);
- $nombreCiudad = $estacion["descripcion"];
- if(isset($desc[1])) {
- $nombreCiudad = $desc[1];
- }
- }
- if(isset($estados[$nombreCiudad])) {
- $nombreCiudad = $estados[$nombreCiudad];
- }
- $nombreCiudad = str_replace(" ", "_", trim($nombreCiudad));
- $y = $fecha->format("Y");
- $m = $fecha->format("m");
- $d = $fecha->format("d");
- $modelo->ruta = "{$y}/{$tipo}/{$nombreCiudad}/{$senal}/{$m}/{$d}";
- try {
- if(!$modelo->save()) {
- $errores = json_encode($modelo->getFirstErrors());
- $this->stdout("Error al guardar {$modelo->hash} {$errores}\n");
- }
- $this->stdout("{$modelo->hash} {$modelo->estacion} {$modelo->archivo}\n");
- } catch(\Exception $e) {
- $this->stdout("Error al guardar {$modelo->hash}\n");
- }
- $count++;
- }
- $vuelta = $inicio - time();
- $this->stdout("Vuelta: {$vuelta}\n");
- return ExitCode::OK;
- }
- }
|