<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class Logging extends Controller
{

    public static $_items = [ 'netstat_sent' => 'i', 'netstat_recv' => 'i', 'memory_free' => 'B', 'memory_total' => 'B', 'memory_used' => 'B', 'disk_free' => 'B', 'disk_percent' => 'f', 'disk_used' => 'B', 'disk_total' => 'B', 'processes' => 'i', 'temp' => 'i', 'load' => 'f', 'username' =>'s', 'computer' => 's'];

		private $_values = [];
		private $_ucache = [];
		private $_ccache = [];

    public function __construct() {
			$this->set_cache();
    }

		private function set_cache() {
			
			$this->_ucache = \apcu_fetch('users',$ret);
			if ($ret == false) {
				$q = DB::table('users')->select('id', 'name')->get();
				foreach ($q as $v) {
					$this->_ucache[$v->name] = $v->id;	
				}
				\apcu_store('users',$this->_ucache);
			}
			$this->_ccache = \apcu_fetch('computers',$ret);
			if ($ret == false ) { 
				$q = DB::table('computers')->select('id', 'computer')->get();
				foreach ($q as $v) {
					$this->_ccache[$v->computer] = $v->id;
				}
				\apcu_store('computers',$this->_ccache);
			}
		}

		private function get_computer_id($comp){
			if (isset($this->_ccache[$comp])) {
				return $this->_ccache[$comp];
			}

			$id = DB::table('computers')->insertGetId(['computer' => $comp]);
			$this->_ccache[$comp] = $id;

			\apcu_store('computers',$this->_ccache);
			
			return $id;
		}

		private function get_user_id($user) {
			if (isset($this->_ucache[$user])) {
				return $this->_ucache[$user];
			}

			$id = DB::table('users')->insertGetId(['name' => $user, 'email' => $user]);
			$this->_ucache[$user] = $id;
			
			\apcu_store('users',$this->_ucache);
			return $id;
		}

    function conv_bytes($v) {
        $units = explode(' ', ' K M G T P E Z Y');

		
	preg_match('/([0-9\.]+)([GKMTgkmT])?B/',$v,$vu);
		
	$v1 = $vu[1];
        if (count($vu) == 3)
          $v2 = $vu[2];
        else
          $v2 = '';

        $pos = array_search($v2,$units);

        $num = floatval($v1) *  pow(1024,$pos);

        return $num;

    }



    public function parse($vals) {
		foreach ($vals as $k => $v)
		{
			if (is_array($v)) {
				foreach ($v as $k2 => $v2) {
					$this->_values["$k"."_"."$k2"] = $v2;   
				}	
			} else {
				$this->_values[$k] = $v;
			}       
		}

		foreach (self::$_items as $k => $t) {
			$val = $this->_values[$k];
			if ($t == 's') continue;

			if ($t == 'B') $val = intval($this->conv_bytes($val));

			if ($t == 'f') $val = floatval($val);
			if ($t == 'i') $val = intval($val);

			$this->_values[$k] = $val;
		}
  }

  public function log(Request $request) {
		$data = json_decode($request->getContent(), true);
		$v = $data["values"];
		$i = $data["items"];
		
		$this->parse($v);
			
		$ia = array_merge($this->_values);
		unset($ia["computer"]);
		unset($ia["username"]);

		$cid = $this->get_computer_id($this->_values["computer"]);
		$uid = $this->get_user_id($this->_values["username"]);
		
		$ia["computer_id"] = $cid;
		DB::table('values')->insert($ia);

		$uipa = [];
		$uipa["computer_id"] = $cid;
		$uipa["user_id"] = $uid;
		$uipa["ip"] = $_SERVER['REMOTE_ADDR'];

		DB::table('user_ip')->insert($uipa);

		return "OK";
  }
}
