Jelajahi Sumber

Actualizacion de reportes

Hugo 4 tahun lalu
induk
melakukan
154aa216e1

+ 188 - 0
commands/ReporteController.php

@@ -0,0 +1,188 @@
+<?php
+
+namespace app\commands;
+
+use yii\base\View;
+use yii\db\Query;
+
+class ReporteController extends \yii\console\Controller {
+
+  public $pc = null;
+  public $st = null;
+  public $fi = null;
+  public $ff = null;
+
+  public function options($actionID) {
+    return [
+      'pc',
+      'st',
+      'fi',
+      'ff'
+    ];
+  }
+
+  public function actionIndex() {
+    $query = (new Query())
+      ->select([
+        "{{Estacion}}.id",
+        "{{Estacion}}.clave",
+        "{{Estacion}}.siglas",
+        "{{Estacion}}.frecuencia",
+        "ciudad",
+        "pc",
+        "to_char(fecha, 'YYYY-MM-DD') as fecha",
+        "extract(epoch from to_char(fecha, 'YYYY-MM-DD')::DATE) as epoch",
+        "count(estacion) filter (where descargado = true) as descargados",
+        "count(estacion) filter (where descargado = false) as pendientes",
+        "count(estacion) as total"
+      ])
+      ->from("Descarga")
+      ->innerJoin("Estacion", "{{Estacion}}.clave = {{Descarga}}.estacion")
+      ->andWhere(['not', ['pc' => null]])
+      ->groupBy(["{{Estacion}}.id", "to_char(fecha, 'YYYY-MM-DD')", "ciudad", "pc"])
+      ->orderBy([
+        "pc" => SORT_ASC,
+        "clave" => SORT_ASC,
+        "fecha" => SORT_ASC,
+      ]);
+    
+    if($this->fi !== null) {
+      $query->andWhere([">=", "fecha", "{$this->fi} 00:00:00"]);
+    }
+  
+    if($this->ff !== null) {
+      $query->andWhere(["<=", "fecha", "{$this->ff} 23:59:59"]);
+    }
+
+    if($this->pc !== null) {
+      $aux = explode(",", $this->pc);
+      $pcs = [];
+      foreach($aux as $p) {
+        $v = trim($p);
+        if(!empty($v)) {
+          $pcs[] = $v;
+        }
+      }
+      $query->andWhere(["pc" => $pcs]);
+    }
+
+    if($this->st !== null) {
+      $aux = explode(",", $this->st);
+      $sts = [];
+      foreach($aux as $p) {
+        $v = trim($p);
+        if(!empty($v)) {
+          $sts[] = $v;
+        }
+      }
+      $query->andWhere(["estacion" => $sts]);
+    }
+
+    $aux = $query->all();
+    $primer = null;
+    $ultimo = null;
+    if(!empty($aux)) {
+      $tz = new \DateTimeZone("America/Mexico_City");
+      $primer = \DateTime::createFromFormat("Y-m-d", $aux[0]['fecha'], $tz);
+      $ultimo = \DateTime::createFromFormat("Y-m-d", $aux[count($aux) - 1]['fecha'], $tz);
+      $primer->setTime(0, 0, 0);
+      $ultimo->setTime(0, 0, 0);
+    }
+    $descargas = [];
+    $estaciones = [];
+    foreach($aux as $d) {
+      if(!isset($estaciones[$d['pc']]) || (isset($estaciones[$d['pc']]) && !isset($estaciones[$d['pc']][$d['clave']]))) {
+        $estaciones[$d['pc']][$d['clave']] = [
+          "clave" => $d['clave'],
+          "siglas" => $d['siglas'],
+          "ciudad" => $d['ciudad'],
+          "frecuencia" => $d['frecuencia']
+        ];
+      }
+      $color = "darkgreen";
+      if($d["descargados"] >= 70 && $d["descargados"] < 90) {
+        $color = "darkgray";
+      } elseif($d["descargados"] < 70) {
+        $color = "red";
+      }
+      $descargas[$d['pc']][$d['clave']][$d['fecha']] = [
+        "descargados" => $d['descargados'],
+        "pendientes" => $d['pendientes'],
+        "total" => $d['total'],
+        "color" => $color
+      ];
+    }
+
+    
+    $view = new View();
+
+    $content = $view->render('/reporte/index', [
+      "estaciones" => $estaciones,
+      "descargas" => $descargas,
+      "primer" => $primer,
+      "ultimo" => $ultimo
+    ]);
+
+    /*
+    $content = '<table border="1" width="100%" id="tabla">
+      <tr>
+        <th>pc</th>
+        <th>siglas</th>
+        <th>clave</th>
+        <th>frecuencia</th>
+        <th colspan="30">
+          <!--table>
+            <tr id="dias"></tr>
+          </table -->
+        </th>
+      </tr>
+    ';
+    foreach ($estaciones as $pc => $est) {
+      foreach($est as $clave => $info) {
+        $content .= '<tr>
+          <td>' . $pc . '</td>
+          <td>' . $info['siglas'] . '</td>
+          <td>' . $info['clave'] . '</td>
+          <td>' . $info['frecuencia'] . '</td>';
+        $dias = [];
+        $fecha = $primer;
+        while($fecha <= $ultimo) {
+          $dias[] = $fecha->format("Y-m-d");
+          $fecha->add(new \DateInterval("P1D"));
+          $this->stdout("Fecha: {$fecha->format("Y-m-d")}\n");
+        }
+        $content .= '<td>';
+        foreach($dias as $dia) {
+          if(isset($descargas[$pc][$clave][$dia])) {
+            $content .= '<table>
+              <tr>
+                <td>' . $dia . '</td>
+                <td>' . $descargas[$pc][$clave][$dia]['descargados'] . '</td>
+                <td>' . $descargas[$pc][$clave][$dia]['pendientes'] . '</td>
+                <td>' . $descargas[$pc][$clave][$dia]['total'] . '</td>
+              </tr>
+            </table>';
+          } else {
+            $content .= '<table>
+              <tr>
+                <td>' . $dia . '</td>
+                <td>0</td>
+                <td>0</td>
+                <td>0</td>
+              </tr>
+            </table>';
+          }
+        }
+        $content .= '</td>';
+        $content .= '</tr>';
+      }
+    } //*/
+
+    $file = "reporte.html"; // \Yii::$app->getSecurity()->generateRandomString(10) . ".html";
+    $base = \Yii::getAlias("@app") . "/web/assets";
+    file_put_contents("{$base}/{$file}", $content);
+
+    $this->stdout("/assets/{$file}\n");
+  }
+
+}

+ 7 - 3
commands/SyncCompleteController.php

@@ -85,6 +85,10 @@ class SyncCompleteController extends Controller {
       ->andWhere(["id" => $idEstaciones])
       ->indexBy("clave")
       ->all();
+    
+    $fechaInicio = '2021-12-15 00:00:00';
+    $fechaFin = '2021-12-16 00:00:00';
+    $this->stdout("{$fechaInicio} - {$fechaFin}\n");
 
     while(true) {
       $inicio = time();
@@ -92,9 +96,9 @@ class SyncCompleteController extends Controller {
         ->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-24 00:00:00'])
+        ->andWhere([">=", "[[timestamp]] at time zone box.timezone", $fechaInicio])
         // ->andWhere([">=", "[[timestamp]] at time zone box.timezone", new Expression("now() - interval '6 day'")])
-        // ->andWhere(["<=", "[[timestamp]] at time zone box.timezone", '2021-10-01 00:00:00'])
+        ->andWhere(["<", "[[timestamp]] at time zone box.timezone", $fechaFin])
         ->orderBy(["timestamp" => $order]);
 
       // $this->stdout("{$archivos->createCommand()->getRawSql()}\n");
@@ -239,7 +243,7 @@ class SyncCompleteController extends Controller {
       ->andWhere(["<=", "[[timestamp]] at time zone box.timezone", "{$this->ff} 00:00:00"])
       ->orderBy(["timestamp" => $order]);
 
-    // $this->stdout("{$archivos->createCommand()->getRawSql()}\n");
+    $this->stdout("{$archivos->createCommand()->getRawSql()}\n");
 
     $count = 0;
     foreach($archivos->each() as $archivo) {

+ 4 - 2
commands/SyncController.php

@@ -272,6 +272,8 @@ class SyncController extends Controller {
     ];
     $order = SORT_ASC;
 
+    $_estaciones = explode(",", $this->estacion);
+
     $ciudades = (new Query())
       ->select(["id", "nombre", "idEstado", "timezone", "tipo"])
       ->from("box")
@@ -281,7 +283,7 @@ class SyncController extends Controller {
     $estaciones = (new Query())
       ->select(["id", "clave", "siglas", "frecuencia", "descripcion"])
       ->from("Estacion")
-      ->andWhere(["clave" => $this->estacion])
+      ->andWhere(["clave" => $_estaciones])
       ->indexBy("clave")
       ->all();
 
@@ -294,7 +296,7 @@ class SyncController extends Controller {
       ->andWhere(["<=", "[[timestamp]] at time zone box.timezone", $this->ff])
       ->andWhere([
         "box" => $this->ciudad,
-        "station" => $this->estacion
+        "station" => $_estaciones
       ])
       ->orderBy(["timestamp" => $order]);
 

+ 134 - 4
controllers/ReporteController.php

@@ -7,6 +7,8 @@ use yii\web\Controller;
 
 class ReporteController extends Controller {
 
+  public $layout = 'bulma';
+
   public function actionDescarga() {
     $req = \Yii::$app->getRequest();
     $pc = trim($req->get("pc", ""));
@@ -99,10 +101,13 @@ class ReporteController extends Controller {
         ];
       }
       $color = "darkgreen";
-      if($d["descargados"] >= 70 && $d["descargados"] < 90) {
-        $color = "darkgray";
-      } elseif($d["descargados"] < 70) {
-        $color = "red";
+      $porcentaje = $d["descargados"] * 100 / $d["total"];
+      if($d["total"] < 240) {
+        $color = "#ff5733";
+      } elseif($porcentaje  >= 70 && $porcentaje  < 90) {
+        $color = "darkblue";
+      } elseif($porcentaje  < 70) {
+        $color = "blue";
       }
       $descargas[$d['pc']][$d['clave']][$d['fecha']] = [
         "descargados" => $d['descargados'],
@@ -130,4 +135,129 @@ class ReporteController extends Controller {
     ]);
   }
 
+  public function actionDescargaSinPc() {
+    $req = \Yii::$app->getRequest();
+    $pc = trim($req->get("pc", ""));
+    $st = trim($req->get("st", ""));
+    $fi = trim($req->get("fi", "2021-12-01"));
+    $ff = trim($req->get("ff", ""));
+    $sql = intval($req->get("sql", "")) === 1;
+    $json = intval($req->get("json", "")) === 1;
+
+    $query = (new Query())
+      ->select([
+        "{{Estacion}}.id",
+        "{{Estacion}}.clave",
+        "{{Estacion}}.siglas",
+        "{{Estacion}}.frecuencia",
+        "ciudad",
+        "to_char(fecha, 'YYYY-MM-DD') as fecha",
+        "extract(epoch from to_char(fecha, 'YYYY-MM-DD')::DATE) as epoch",
+        "count(estacion) filter (where descargado = true) as descargados",
+        "count(estacion) filter (where descargado = false) as pendientes",
+        "count(estacion) as total"
+      ])
+      ->from("Descarga")
+      ->innerJoin("Estacion", "{{Estacion}}.clave = {{Descarga}}.estacion")
+      ->andWhere(['not', ['pc' => null]])
+      ->groupBy(["{{Estacion}}.id", "to_char(fecha, 'YYYY-MM-DD')", "ciudad"])
+      ->orderBy([
+        "ciudad" => SORT_ASC,
+        "clave" => SORT_ASC,
+        "fecha" => SORT_ASC,
+      ]);
+    
+    if($fi !== "") {
+      $query->andWhere([">=", "fecha", "{$fi} 00:00:00"]);
+    }
+  
+    if($ff !== "") {
+      $query->andWhere(["<=", "fecha", "{$ff} 23:59:59"]);
+    }
+
+    if($pc !== "") {
+      $aux = explode(",", $pc);
+      $pcs = [];
+      foreach($aux as $p) {
+        $v = trim($p);
+        if(!empty($v)) {
+          $pcs[] = $v;
+        }
+      }
+      $query->andWhere(["pc" => $pcs]);
+    }
+
+    if($st !== "") {
+      $aux = explode(",", $st);
+      $sts = [];
+      foreach($aux as $p) {
+        $v = trim($p);
+        if(!empty($v)) {
+          $sts[] = $v;
+        }
+      }
+      $query->andWhere(["estacion" => $sts]);
+    }
+
+    if($sql) {
+      \Yii::$app->getResponse()->format = \yii\web\Response::FORMAT_RAW;
+      return $query->createCommand()->getRawSql();
+    }
+
+    $aux = $query->all();
+    $primer = null;
+    $ultimo = null;
+    if(!empty($aux)) {
+      $tz = new \DateTimeZone("America/Mexico_City");
+      $primer = \DateTime::createFromFormat("Y-m-d", $aux[0]['fecha'], $tz);
+      $ultimo = \DateTime::createFromFormat("Y-m-d", $aux[count($aux) - 1]['fecha'], $tz);
+      $primer->setTime(0, 0, 0);
+      $ultimo->setTime(0, 0, 0);
+    }
+    $descargas = [];
+    $estaciones = [];
+    foreach($aux as $d) {
+      if(!isset($estaciones[$d['clave']])) {
+        $estaciones[$d['clave']] = [
+          "clave" => $d['clave'],
+          "siglas" => $d['siglas'],
+          "ciudad" => $d['ciudad'],
+          "frecuencia" => $d['frecuencia']
+        ];
+      }
+      $color = "darkgreen";
+      $porcentaje = $d["descargados"] * 100 / $d["total"];
+      if($d["total"] < 240) {
+        $color = "#ff5733";
+      } elseif($porcentaje  >= 70 && $porcentaje  < 90) {
+        $color = "darkblue";
+      } elseif($porcentaje  < 70) {
+        $color = "blue";
+      }
+      $descargas[$d['clave']][$d['fecha']] = [
+        "descargados" => $d['descargados'],
+        "pendientes" => $d['pendientes'],
+        "total" => $d['total'],
+        "color" => $color
+      ];
+    }
+
+    if($json) {
+      \Yii::$app->getResponse()->format = \yii\web\Response::FORMAT_JSON;
+      return [
+        "primer" => $primer,
+        "ultimo" => $ultimo,
+        "estaciones" => $estaciones,
+        "descargas" => $descargas
+      ];
+    }
+
+    return $this->render('sin-pc', [
+      "estaciones" => $estaciones,
+      "descargas" => $descargas,
+      "primer" => $primer,
+      "ultimo" => $ultimo
+    ]);
+  }
+
 }

+ 12 - 0
views/layouts/bulma.php

@@ -0,0 +1,12 @@
+<!doctype html>
+<html lang="en-us">
+  <head>
+    <meta charset="utf-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1">
+    <title>Descarga</title>
+    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.3/css/bulma.min.css">
+  </head>
+  <body>
+    <?= $content ?>
+  </body>
+</html>

+ 26 - 30
views/reporte/index.php

@@ -7,38 +7,33 @@
 
 ?>
 <style>
-  .site-card {
-    min-height: 100vh;
-    width: 100vw;
-    padding: 20px;
-    background: #ececec;
+  .scroll {
+    overflow-x: scroll;
   }
-
-  .content {
-    width: 100%;
-    height: 100%;
-  }
-
-  .full-width {
-    width: 100%;
+  .celda {
+    text-align: center; 
+    color: #d4d4d4; 
   }
 </style>
-<div>
-  <table border="1" width="100%" id="tabla">
-    <tr>
-      <th>pc</th>
-      <th>ciudad</th>
-      <th>clave</th>
-      <th>siglas</th>
-      <th>frecuencia</th>
-      <?php $f = (clone $primer); ?>
-      <?php while($f <= $ultimo): ?>
-        <th>
-        <?= $f->format("d/m/Y") ?>
-        </th>
-        <?php $f->add(new \DateInterval("P1D")); ?>
-      <?php endwhile; ?>
-    </tr>
+<div class="scroll">
+  <table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth">
+    <thead>
+      <tr>
+        <th>pc</th>
+        <th>ciudad</th>
+        <th>clave</th>
+        <th>siglas</th>
+        <th>frecuencia</th>
+        <?php $f = (clone $primer); ?>
+        <?php while($f <= $ultimo): ?>
+          <th>
+          <?= $f->format("d/m/Y") ?>
+          </th>
+          <?php $f->add(new \DateInterval("P1D")); ?>
+        <?php endwhile; ?>
+      </tr>
+    </thead>
+    <tbody>
     <?php $estacion = ""; ?>
     <?php foreach ($estaciones as $pc => $est): ?>
       <?php foreach($est as $clave => $info): ?>
@@ -55,7 +50,7 @@
               <?php $d = $descargas[$pc][$clave][$fecha->format("Y-m-d")]; ?>
             <?php endif; ?>
             <?php if($d !== null): ?>
-              <td style="background-color: <?= $d["color"] ?>; text-align: center ">
+              <td style="background-color: <?= $d["color"] ?>;" class="celda">
               <?= $d["descargados"] ?> / <?= $d["total"] ?>
               </td>
             <?php else: ?>
@@ -66,5 +61,6 @@
         </tr>
       <?php endforeach ?>
     <?php endforeach ?>
+    </tbody>
   </table>
 </div>

+ 62 - 0
views/reporte/sin-pc.php

@@ -0,0 +1,62 @@
+<?php
+
+/* @var $descargas app\models\Descarga[] */
+/* @var $estaciones array */
+/* @var $primer \DateTime */
+/* @var $ultimo \DateTime */
+
+?>
+<style>
+  .scroll {
+    overflow-x: scroll;
+  }
+  .celda {
+    text-align: center; 
+    color: #d4d4d4; 
+  }
+</style>
+<div class="scroll">
+  <table class="table is-bordered is-striped is-narrow is-hoverable is-fullwidth">
+    <thead>
+      <tr>
+        <th>ciudad</th>
+        <th>clave</th>
+        <th>siglas</th>
+        <th>frecuencia</th>
+        <?php $f = (clone $primer); ?>
+        <?php while($f <= $ultimo): ?>
+          <th>
+          <?= $f->format("d/m/Y") ?>
+          </th>
+          <?php $f->add(new \DateInterval("P1D")); ?>
+        <?php endwhile; ?>
+      </tr>
+    </thead>
+    <tbody>
+    <?php $estacion = ""; ?>
+    <?php foreach ($estaciones as $clave => $info): ?>
+      <tr>
+        <td><?= $info['ciudad'] ?></td>
+        <td><?= $clave ?></td>
+        <td><?= $info['siglas'] ?></td>
+        <td><?= $info['frecuencia'] ?></td>
+        <?php $fecha = (clone $primer); ?>
+        <?php while($fecha <= $ultimo): ?>
+          <?php $d = null; ?>
+          <?php if(isset($descargas[$clave][$fecha->format("Y-m-d")])): ?>
+            <?php $d = $descargas[$clave][$fecha->format("Y-m-d")]; ?>
+          <?php endif; ?>
+          <?php if($d !== null): ?>
+            <td style="background-color: <?= $d["color"] ?>;" class="celda">
+            <?= $d["descargados"] ?> / <?= $d["total"] ?>
+            </td>
+          <?php else: ?>
+            <td></td>
+          <?php endif; ?>
+          <?php $fecha->add(new \DateInterval("P1D")); ?>
+        <?php endwhile ?>
+      </tr>
+    <?php endforeach ?>
+    </tbody>
+  </table>
+</div>