|
|
@@ -0,0 +1,360 @@
|
|
|
+<?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 function options($actionID) {
|
|
|
+ return [
|
|
|
+ 'pagina',
|
|
|
+ 'limite',
|
|
|
+ 'ciudad',
|
|
|
+ 'estacion',
|
|
|
+ 'fi',
|
|
|
+ 'ff'
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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;
|
|
|
+
|
|
|
+ $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" => $this->estacion])
|
|
|
+ ->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" => $this->estacion
|
|
|
+ ])
|
|
|
+ ->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;
|
|
|
+ $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;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|