Example #1
0
/**
 * Goes through an array (which may also contain other arrays) and find
 * the most latest 'created_at' value.
 */
function find_latest_created_at($a, $prefix = false)
{
    if (!is_array($a)) {
        return false;
    }
    $created_at = false;
    foreach ($a as $k => $v) {
        if (!is_numeric($k) && $k == "created_at") {
            $created_at = max($created_at, strtotime($v));
        } else {
            if (is_array($v)) {
                if (!$prefix || substr($k, 0, strlen($prefix)) == $prefix) {
                    $created_at = max($created_at, find_latest_created_at($v));
                }
            }
        }
    }
    return $created_at;
}
Example #2
0
 public function getData($days)
 {
     $key_column = array('type' => 'string', 'title' => ct("Currency"));
     $columns = array();
     // get all balances
     $balances = get_all_summary_instances($this->getUser());
     $last_updated = find_latest_created_at($balances, "total");
     // and convert them using the most recent rates
     $rates = get_all_recent_rates();
     // create data
     // TODO refactor this into generic any-currency balances
     $data = array();
     if (isset($balances['totalbtc']) && $balances['totalbtc']['balance'] != 0) {
         $columns[] = array('type' => 'number', 'title' => get_currency_abbr('btc'));
         $data[] = graph_number_format(demo_scale($balances['totalbtc']['balance']));
     }
     foreach (get_all_currencies() as $cur) {
         // if the key is a currency, use the same currency colour across all graphs (#293)
         $color = array_search($cur, get_all_currencies());
         if ($cur == 'btc') {
             continue;
         }
         if (!is_fiat_currency($cur) && isset($balances['total' . $cur]) && $balances['total' . $cur]['balance'] != 0 && isset($rates['btc' . $cur])) {
             $columns[] = array('type' => 'number', 'title' => get_currency_abbr($cur), 'color' => $color);
             $data[] = graph_number_format(demo_scale($balances['total' . $cur]['balance'] * $rates['btc' . $cur]['bid']));
         }
         if (is_fiat_currency($cur) && isset($balances['total' . $cur]) && $balances['total' . $cur]['balance'] != 0 && isset($rates[$cur . 'btc']) && $rates[$cur . 'btc']['ask']) {
             $columns[] = array('type' => 'number', 'title' => get_currency_abbr($cur), 'color' => $color);
             $data[] = graph_number_format(demo_scale($balances['total' . $cur]['balance'] / $rates[$cur . 'btc']['ask']));
         }
     }
     // display a helpful message if there's no data
     if (!$data) {
         throw new NoDataGraphException_AddAccountsAddresses();
     }
     // sort data by balance
     arsort($data);
     $data = array(get_currency_abbr('btc') => $data);
     return array('key' => $key_column, 'columns' => $columns, 'data' => $data, 'last_updated' => $last_updated);
 }
Example #3
0
 public function getData($days)
 {
     $key_column = array('type' => 'string', 'title' => ct("Currency"));
     $columns = array();
     // get data
     // TODO could probably cache this
     $q = db()->prepare("SELECT SUM(balance) AS balance, exchange, MAX(created_at) AS created_at FROM balances WHERE user_id=? AND is_recent=1 AND currency=? GROUP BY exchange");
     $q->execute(array($this->getUser(), $this->currency));
     $balances = $q->fetchAll();
     // need to also get address balances
     $summary_balances = get_all_summary_instances($this->getUser());
     // get additional balances
     $data = array();
     if (isset($summary_balances['blockchain' . $this->currency]) && $summary_balances['blockchain' . $this->currency]['balance'] != 0) {
         $balances[] = array("balance" => $summary_balances['blockchain' . $this->currency]['balance'], "exchange" => "blockchain", "created_at" => $summary_balances['blockchain' . $this->currency]['created_at']);
     }
     if (isset($summary_balances['offsets' . $this->currency]) && $summary_balances['offsets' . $this->currency]['balance'] != 0) {
         $balances[] = array("balance" => $summary_balances['offsets' . $this->currency]['balance'], "exchange" => "offsets", "created_at" => $summary_balances['offsets' . $this->currency]['created_at']);
     }
     // sort by balance
     usort($balances, array($this, 'sort_by_balance_desc'));
     $last_updated = find_latest_created_at($balances);
     // apply demo_scale and calculate total summary
     $data = array();
     $total = 0;
     foreach ($balances as $b) {
         if ($b['balance'] != 0) {
             $columns[] = array('type' => 'number', 'title' => get_exchange_name($b['exchange']));
             $data[] = demo_scale($b['balance']);
             $total += demo_scale($b['balance']);
         }
     }
     // return a more helpful message if there is no data
     if (!$data) {
         throw new NoDataGraphException_AddAccountsAddresses();
     }
     // sort data by balance
     $data = array(get_currency_abbr($this->currency) => $data);
     return array('key' => $key_column, 'columns' => $columns, 'data' => $data, 'last_updated' => $last_updated);
 }
Example #4
0
 public function getData($days)
 {
     $key_column = array('type' => 'string', 'title' => ct("Currency"));
     $columns = array();
     $columns[] = array('type' => 'string', 'title' => ct("Currency"), 'heading' => true);
     $columns[] = array('type' => 'string', 'title' => ct("Total"));
     // a table of each currency
     // get all balances
     $balances = get_all_summary_instances($this->getUser());
     $summaries = get_all_summary_currencies($this->getUser());
     $currencies = get_all_currencies();
     $last_updated = find_latest_created_at($balances, "total");
     // create data
     $data = array();
     foreach ($currencies as $c) {
         if (isset($summaries[$c])) {
             $balance = isset($balances['total' . $c]) ? $balances['total' . $c]['balance'] : 0;
             $data[] = array("<span title=\"" . htmlspecialchars(get_currency_name($c)) . "\">" . get_currency_abbr($c) . "</span>", currency_format($c, demo_scale($balance), 4));
         }
     }
     return array('key' => $key_column, 'columns' => $columns, 'data' => $data, 'last_updated' => $last_updated, 'add_more_currencies' => true, 'no_header' => true);
 }