Преглед изворни кода

CSV support for telemetry (#133)

Added CSV support for telemetry (#133)
V. D'AGOSTINO пре 8 година
родитељ
комит
12b90dd47a
3 измењених фајлова са 33 додато и 2 уклоњено
  1. 7 1
      doc.md
  2. 21 0
      telemetry.php
  3. 5 1
      telemetry_settings.php

+ 7 - 1
doc.md

@@ -319,7 +319,7 @@ w.postMessage('start {"url_dl": "newGarbageURL", "url_ul": "newEmptyURL", "url_p
 Telemetry currently requires PHP and either MySQL, PostgreSQL or SQLite.
 To set up the telemetry, we need to do 4 things:
 * copy `telemetry.php` and `telemetry_settings.php`
-* edit `telemetry_settings.php` to add your database settings
+* edit `telemetry_settings.php` to add your database settings or the csv filename and date timezone if CSV is used instead of database storage.
 * create the database
 * enable telemetry
 
@@ -352,6 +352,12 @@ $PostgreSql_hostname="DB_HOSTNAME"; //database address, usually localhost
 $PostgreSql_databasename="DB_NAME"; //the name of the database where you loaded telemetry_postgresql.sql
 ```
 
+If you choose to use CSV file, you must set the Csv_File and timezone variables.
+```php
+$Csv_File="myReportFile.csv";
+$timezone='Europe/Paris';
+```
+
 ### Enabling telemetry
 Edit your test page; where you start the worker, you need to specify the `telemetry_level`.  
 There are 3 levels:

+ 21 - 0
telemetry.php

@@ -49,4 +49,25 @@ if($db_type=="mysql"){
     $stmt->execute(array($ip,$ua,$lang,$dl,$ul,$ping,$jitter,$log)) or die("3");
     $conn = null;
 }
+elseif($db_type=="csv"){
+    // Prepare the csv formatted string
+    date_default_timezone_set($timezone);
+    $date = date('Y-m-d H:i:s');
+    $str = '"' . $date . '",';
+    $str .= '"' . $ip . '",';
+    $str .= '"' . $ua . '",';
+    $str .= '"' . $dl . '",';
+    $str .= '"' . $ul . '",';
+    $str .= '"' . $ping . '",';
+    $str .= '"' . $jitter . '"' . "\n";
+
+    // Set header if this is a new file
+    if (!file_exists($Csv_File)) {
+        $header = '"date","ip","ua","download","upload","ping","jitter"' . "\n";
+        file_put_contents($Csv_File, $header, FILE_APPEND);
+    }
+
+    // Writting line to file
+    file_put_contents($Csv_File, $str, FILE_APPEND);
+}
 ?>

+ 5 - 1
telemetry_settings.php

@@ -1,6 +1,6 @@
 <?php
 
-$db_type="mysql"; //Type of db: "mysql", "sqlite" or "postgresql"
+$db_type="mysql"; //Type of db: "mysql", "sqlite" or "postgresql" or "csv"
 
 // Sqlite3 settings
 $Sqlite_db_file = "../telemetry.sql";
@@ -17,4 +17,8 @@ $PostgreSql_password="PASSWORD";
 $PostgreSql_hostname="DB_HOSTNAME";
 $PostgreSql_databasename="DB_NAME";
 
+// CSV settings
+$Csv_File="reports.csv";
+$timezone='UTC';
+
 ?>