<commit_before>/*

    Graph à gauche de l'appli
    @author : <NAME>

*/
import { Component, Input, OnInit } from '@angular/core';

import { OPTIONS } from './graph.options';
import { AppService } from '../app.service';

import { Transaction } from '../class/Transaction';
import * as _ from 'lodash';


let web3 = require('../utils/web3IPCExtension').web3;


declare var google: any;

@Component({
  selector: 'graph',
  styleUrls: ['./_graph.scss'],
  templateUrl: './graph.html'
})

export class GraphComponent extends OnInit {
  private googleLoaded: any;
  private options;
  private data;
  private chart;
  private lastValue = 0;
  @Input() delta = 0;
  @Input() user;
  @Input() balance = {amount:0,gas:0,count:0};

  constructor (private service: AppService) {
    super();
  }

  getGoogle() {
      return google;
  }

  // Initialisation des packages au lancement du composant et souscription à l'évennement de nouvelle transaction
  ngOnInit() {
    // if (!this.googleLoaded) {
    //   this.googleLoaded = true;
    //   google.charts.load('current', {packages: ['corechart', 'line']});
    // }
    // google.charts.setOnLoadCallback(() => {
    //   this.drawGraph();
    // });

    this.addUser();

    this.service.newTransac.subscribe(transaction => this.updateUser(transaction));
  }

  // Création du graphique en lignes
  createLineChart (element: any): any {
      return new google.visualization.LineChart(element);
  }
  // Création du tableau
  createDataTable (array: any[]): any {
      return google.visualization.arrayToDataTable(array);
  }

  addUser() {
    this.service.getIp().then(ip => {
      web3.setProvider(new web3.providers.HttpProvider(ip[0]['ip']));
      this.user['balance'] = Number(web3.eth.getBalance(this.user['address']).plus(2).toString()) / Math.pow(10, 18)
      if(this.user['balance'] < (1/Math.pow(10,17))) this.user['balance'] = 0;

      this.delta = 0;

      this.lastValue = this.user['balance'];
    });
  }

  updateUser (transaction: Transaction) {
    if(transaction.receiver.address == this.user.address && transaction.amount!=0)
      this.balance.amount = transaction.amount; 
    else if(transaction.sender.address == this.user.address && transaction.amount!=0)
      this.balance.amount = -transaction.amount;
    else
      this.balance.amount = 0;
    if(transaction.sender.address == this.user.address)
    {
      this.balance.gas = transaction.gasPrice * transaction.gasUsed;
      this.balance.count += 1;
    }
    else
      this.balance.gas = 0

    this.user['balance'] = Number(web3.eth.getBalance(this.user['address']).plus(2).toString()) / Math.pow(10, 18);

    this.delta = (this.user['balance'] > this.lastValue) ? 1 : ( (this.user['balance'] < this.lastValue) ? -1 : 0 );

    this.lastValue = this.user['balance'];
  }

  // // Initialisation du Graph
  // drawGraph() {
  //   let firstRow = [];
  //   let table = [];

  //   console.log(this.user);

  //   table.push('Date');
  //   table =  _.concat(table, this.user['name']);

  //   this.options = OPTIONS;
  //   this.options.title = this.user['name'];

  //   firstRow = [new Date().toLocaleTimeString(), this.user['balance']];
<commit_msg>modif graph delta<commit_after>/*

    Graph à gauche de l'appli
    @author : <NAME>

*/
import { Component, Input, OnInit } from '@angular/core';

import { OPTIONS } from './graph.options';
import { AppService } from '../app.service';

import { Transaction } from '../class/Transaction';
import * as _ from 'lodash';


let web3 = require('../utils/web3IPCExtension').web3;


declare var google: any;

@Component({
  selector: 'graph',
  styleUrls: ['./_graph.scss'],
  templateUrl: './graph.html'
})

export class GraphComponent extends OnInit {
  private googleLoaded: any;
  private options;
  private data;
  private chart;
  private lastValue = 0;
  @Input() delta = 0;
  @Input() user;
  @Input() balance = {amount:0,gas:0,count:0};

  constructor (private service: AppService) {
    super();
  }

  getGoogle() {
      return google;
  }

  // Initialisation des packages au lancement du composant et souscription à l'évennement de nouvelle transaction
  ngOnInit() {
    // if (!this.googleLoaded) {
    //   this.googleLoaded = true;
    //   google.charts.load('current', {packages: ['corechart', 'line']});
    // }
    // google.charts.setOnLoadCallback(() => {
    //   this.drawGraph();
    // });

    this.addUser();

    this.service.newTransac.subscribe(transaction => this.updateUser(transaction));
  }

  // Création du graphique en lignes
  createLineChart (element: any): any {
      return new google.visualization.LineChart(element);
  }
  // Création du tableau
  createDataTable (array: any[]): any {
      return google.visualization.arrayToDataTable(array);
  }

  addUser() {
    this.service.getIp().then(ip => {
      web3.setProvider(new web3.providers.HttpProvider(ip[0]['ip']));
      this.user['balance'] = Number(web3.eth.getBalance(this.user['address']).plus(2).toString()) / Math.pow(10, 18)
      if(this.user['balance'] < (1/Math.pow(10,17))) this.user['balance'] = 0;

      this.delta = 0;

      this.lastValue = this.user['balance'];
    });
  }

  updateUser (transaction: Transaction) {
    if(transaction.receiver.address == this.user.address && transaction.amount!=0)
      this.balance.amount = transaction.amount; 
    else if(transaction.sender.address == this.user.address && transaction.amount!=0)
      this.balance.amount = -transaction.amount;
    else
      this.balance.amount = 0;
    if(transaction.sender.address == this.user.address)
    {
      this.balance.gas = transaction.gasPrice * transaction.gasUsed;
      this.balance.count += 1;
    }
    else
      this.balance.gas = 0

    this.user['balance'] = Number(web3.eth.getBalance(this.user['address']).plus(2).toString()) / Math.pow(10, 18);

    let diff : any = 0;

    diff = this.user['balance'] - this.lastValue;

    this.delta = (diff > 100) ? 1 : ( (diff < -100) ? -1 : 0 );

    this.lastValue = this.user['balance'];
  }

  // // Initialisation du Graph
  // drawGraph() {
  //   let firstRow = [];
  //   let table = [];

  //   console.log(this.user);

  //   table.push('Date');
  //   table =  _.concat(table, this.user['name']);

  //   this.options = OPTIONS;
  //   this.options.title = this.user['name'];

  //   firstRow = [new Date().toLocaleTimeString(), this.user['balance']];

{'fraction_non_alphanumeric': 0.17017625955389176, 'fraction_numerical': 0.012946498206208079, 'mean_word_length': 3.0582278481012657, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5980040', 'n_tokens_mistral': 2146, 'n_tokens_neox': 2045, 'n_words': 417}