SyncCompleteController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace app\commands;
  3. use app\models\Descarga;
  4. use yii\console\Controller;
  5. use yii\console\ExitCode;
  6. use yii\db\Expression;
  7. use yii\db\Query;
  8. use yii\helpers\ArrayHelper;
  9. class SyncCompleteController extends Controller {
  10. public function actionIndex() {
  11. $estados = [
  12. 'AGUASCALIENTES' => 'AGU',
  13. 'BAJA CALIFORNIA' => 'BCN',
  14. 'BAJA CALIFORNIA SUR' => 'BCS',
  15. 'CAMPECHE' => 'CAM',
  16. 'CHIAPAS' => 'CHP',
  17. 'CHIHUAHUA' => 'CHH',
  18. 'CIUDAD DE MÉXICO' => 'CMX',
  19. 'COAHUILA' => 'COA',
  20. 'COLIMA' => 'COL',
  21. 'DURANGO' => 'DUR',
  22. 'GUANAJUATO' => 'GUA',
  23. 'GUERRERO' => 'GRO',
  24. 'HIDALGO' => 'HID',
  25. 'JALISCO' => 'JAL',
  26. 'MEXICO' => 'MEX',
  27. 'MICHOACAN' => 'MIC',
  28. 'MORELOS' => 'MOR',
  29. 'NAYARIT' => 'NAY',
  30. 'NUEVO LEON' => 'NLE',
  31. 'OAXACA' => 'OAX',
  32. 'PUEBLA' => 'PUE',
  33. 'QUERETARO' => 'QUE',
  34. 'QUINTANA ROO' => 'ROO',
  35. 'SAN LUIS POTOSI' => 'SLP',
  36. 'SINALOA' => 'SIN',
  37. 'SONORA' => 'SON',
  38. 'TABASCO' => 'TAB',
  39. 'TAMAULIPAS' => 'TAM',
  40. 'TLAXCALA' => 'TLA',
  41. 'VERACRUZ' => 'VER',
  42. 'YUCATAN' => 'YUC',
  43. 'ZACATECAS' => 'ZAC'
  44. ];
  45. $order = SORT_ASC;
  46. $ciudadEstacion = (new Query())
  47. ->select(["idCiudad", "idEstacion"])
  48. ->from("box")
  49. ->innerJoin("CiudadEstacion", "box.id = {{CiudadEstacion}}.[[idCiudad]]")
  50. ->andWhere([
  51. "box.activo" => true
  52. ])
  53. ->andWhere("[[idGrupo]] is not null")
  54. ->all();
  55. $idCiudades = ArrayHelper::getColumn($ciudadEstacion, "idCiudad");
  56. $ciudades = (new Query())
  57. ->select(["id", "nombre", "idEstado", "timezone", "tipo"])
  58. ->from("box")
  59. ->where(["id" => $idCiudades])
  60. ->indexBy("id")
  61. ->all();
  62. $idEstaciones = ArrayHelper::getColumn($ciudadEstacion, "idEstacion");
  63. $estaciones = (new Query())
  64. ->select(["id", "clave", "siglas", "frecuencia", "descripcion"])
  65. ->from("Estacion")
  66. ->andWhere(["id" => $idEstaciones])
  67. ->indexBy("clave")
  68. ->all();
  69. while(true) {
  70. $inicio = time();
  71. $archivos = (new Query())
  72. ->select("hash, box, station, filename, [[timestamp]] at time zone box.timezone as timestamp")
  73. ->from("file")
  74. ->innerJoin("box", "box.id = file.box")
  75. ->andWhere([">=", "[[timestamp]] at time zone box.timezone", new Expression("now() - interval '2 day'")])
  76. // ->andWhere(["<=", "[[timestamp]] at time zone box.timezone", '2021-10-01 00:00:00'])
  77. ->orderBy(["timestamp" => $order]);
  78. // $this->stdout("{$archivos->createCommand()->getRawSql()}\n");
  79. $count = 0;
  80. foreach($archivos->each() as $archivo) {
  81. if(!isset($ciudades[$archivo["box"]])) {
  82. continue;
  83. }
  84. $modelo = new Descarga();
  85. $modelo->hash = $archivo["hash"];
  86. $modelo->estacion = $archivo["station"];
  87. $modelo->archivo = $archivo["filename"];
  88. $modelo->ciudad = $archivo["box"];
  89. $modelo->pc = null;
  90. $modelo->fecha = $archivo["timestamp"];
  91. $modelo->descargado = false;
  92. $modelo->nombre = basename($archivo["filename"]);
  93. $ciudad = $ciudades[$archivo["box"]];
  94. $estacion = $estaciones[$archivo["station"]];
  95. $fecha = \DateTime::createFromFormat("Y-m-d H:i:s", $archivo["timestamp"]);
  96. $siglas = explode("-", $estacion["siglas"]);
  97. $tipo = "";
  98. $senal = $siglas[0];
  99. if(isset($siglas[1])) {
  100. $tipo = $siglas[1];
  101. }
  102. if($tipo === "") {
  103. if($ciudad["tipo"] === "tv") {
  104. $tipo = "TDT";
  105. } elseif ($ciudad["tipo"] === "radio") {
  106. $tipo = "FM";
  107. } else {
  108. $tipo = "AM";
  109. }
  110. }
  111. $nombreCiudad = $ciudad["idEstado"];
  112. if($tipo === "AM") {
  113. $desc = explode(",", $estacion["descripcion"]);
  114. $nombreCiudad = $estacion["descripcion"];
  115. if(isset($desc[1])) {
  116. $nombreCiudad = $desc[1];
  117. }
  118. }
  119. if(isset($estados[$nombreCiudad])) {
  120. $nombreCiudad = $estados[$nombreCiudad];
  121. }
  122. $nombreCiudad = str_replace(" ", "_", trim($nombreCiudad));
  123. $y = $fecha->format("Y");
  124. $m = $fecha->format("m");
  125. $d = $fecha->format("d");
  126. $modelo->ruta = "{$y}/{$tipo}/{$nombreCiudad}/{$senal}/{$m}/{$d}";
  127. try {
  128. if(!$modelo->save()) {
  129. $errores = json_encode($modelo->getFirstErrors());
  130. $this->stdout("Error al guardar {$modelo->hash} {$errores}\n");
  131. }
  132. } catch(\Exception $e) {
  133. $this->stdout("Error al guardar {$modelo->hash} {$modelo->archivo}\n");
  134. }
  135. $count++;
  136. }
  137. $vuelta = $inicio - time();
  138. $this->stdout("Vuelta: {$vuelta}\n");
  139. }
  140. return ExitCode::OK;
  141. }
  142. }