/** * Helper function that converts a {@code graph_type} to a GraphRenderer * object, which we can then use to get raw graph data and format it as necessary. */ function construct_graph_renderer($graph_type, $arg0, $arg0_resolved) { $bits = explode("_", $graph_type); $all_exchanges = get_all_exchanges(); if (count($bits) == 3) { $cur1 = false; $cur2 = false; if (strlen($bits[1]) == 6) { $cur1 = substr($bits[1], 0, 3); $cur2 = substr($bits[1], 3); $cur1 = in_array($cur1, get_all_currencies()) ? $cur1 : false; $cur2 = in_array($cur2, get_all_currencies()) ? $cur2 : false; } if (strlen($bits[2]) == 6 && !$cur1 && !$cur2) { $cur1 = substr($bits[2], 0, 3); $cur2 = substr($bits[2], 3); $cur1 = in_array($cur1, get_all_currencies()) ? $cur1 : false; $cur2 = in_array($cur2, get_all_currencies()) ? $cur2 : false; } if ($bits[2] == "daily" && $cur1 && $cur2 && isset($all_exchanges[$bits[0]])) { return new GraphRenderer_Ticker($bits[0], $cur1, $cur2); } if ($bits[2] == "markets" && $cur1 && $cur2 && $bits[0] == "average") { return new GraphRenderer_AverageMarketData($cur1, $cur2); } if ($bits[0] == "composition" && in_array($bits[1], get_all_currencies())) { switch ($bits[2]) { case "pie": return new GraphRenderer_CompositionPie($bits[1]); case "table": return new GraphRenderer_CompositionTable($bits[1]); case "daily": return new GraphRenderer_CompositionGraph($bits[1]); case "stacked": return new GraphRenderer_CompositionStacked($bits[1]); case "proportional": return new GraphRenderer_CompositionProportional($bits[1]); } } if ($bits[0] == "total" && in_array($bits[1], get_all_currencies()) && $bits[2] == "daily") { return new GraphRenderer_SummaryGraph('total' . $bits[1], $bits[1]); } if ($bits[0] == "hashrate" && in_array($bits[1], get_all_currencies()) && $bits[2] == "daily") { return new GraphRenderer_SummaryGraphHashrate('totalmh_' . $bits[1], $bits[1]); } if ($bits[0] == "securities" && in_array($bits[2], get_all_currencies())) { $renderer = new GraphRenderer_BalancesGraphSecurities('securities_' . $bits[1], $arg0, $bits[2], $arg0_resolved); return $renderer; } if ($bits[0] == "pair" && isset($all_exchanges[$bits[1]]) && $cur1 && $cur2) { return new GraphRenderer_ExchangePair($bits[1], $cur1, $cur2); } } // issue #273: fix bitmarket_pl exchange if (count($bits) == 4) { $cur1 = false; $cur2 = false; if (strlen($bits[2]) == 6) { $cur1 = substr($bits[2], 0, 3); $cur2 = substr($bits[2], 3); $cur1 = in_array($cur1, get_all_currencies()) ? $cur1 : false; $cur2 = in_array($cur2, get_all_currencies()) ? $cur2 : false; } if ($bits[3] == "daily" && $cur1 && $cur2 && isset($all_exchanges[$bits[0] . "_" . $bits[1]])) { return new GraphRenderer_Ticker($bits[0] . "_" . $bits[1], $cur1, $cur2); } } if (count($bits) >= 2) { if (substr($bits[0], 0, strlen("all2")) == "all2" || substr($bits[0], 0, strlen("crypto2")) == "crypto2") { $cur = substr($bits[0], -3); if (in_array($cur, get_all_currencies())) { if (count($bits) == 3 && $bits[2] == "daily" && isset($all_exchanges[$bits[1]])) { // e.g. all2nzd_bitnz_daily return new GraphRenderer_SummaryGraphConvertedExchange($bits[0] . "_" . $bits[1], $cur); } if (count($bits) == 4 && $bits[3] == "daily" && isset($all_exchanges[$bits[1] . "_" . $bits[2]])) { // e.g. all2pln_bitmarket_pl_daily (#273) return new GraphRenderer_SummaryGraphConvertedExchange($bits[0] . "_" . $bits[1] . "_" . $bits[2], $cur); } if (count($bits) == 2 && $bits[1] == "daily") { // e.g. crypto2ltc_daily return new GraphRenderer_SummaryGraphConvertedCrypto($bits[0], $cur); } } } } if (count($bits) >= 2 && $bits[0] == "metrics") { $possible = GraphRenderer_AdminMetrics::getMetrics(); $bits_two = explode("_", $graph_type, 2); if (isset($possible[$bits_two[1]])) { return new GraphRenderer_AdminMetrics($bits_two[1]); } } switch ($graph_type) { case "btc_equivalent": return new GraphRenderer_EquivalentPieBTC(); case "balances_table": return new GraphRenderer_BalancesTable(); case "total_converted_table": return new GraphRenderer_TotalConvertedTable(); case "crypto_converted_table": return new GraphRenderer_CryptoConvertedTable(); case "balances_offset_table": return new GraphRenderer_BalancesOffsetsTable(); case "ticker_matrix": return new GraphRenderer_TickerMatrix(); case "btc_equivalent_graph": return new GraphRenderer_BtcEquivalentGraph(); case "btc_equivalent_stacked": return new GraphRenderer_BtcEquivalentStacked(); case "btc_equivalent_proportional": return new GraphRenderer_BtcEquivalentProportional(); case "external_historical": return new GraphRenderer_ExternalHistorical($arg0_resolved); case "admin_statistics": return new GraphRenderer_AdminStatistics(); case "statistics_queue": return new GraphRenderer_StatisticsQueue(); case "statistics_system_load": return new GraphRenderer_StatisticsSystemLoad(""); case "statistics_db_system_load": return new GraphRenderer_StatisticsSystemLoad("db_"); default: throw new NoGraphRendererException("Unknown graph to render '{$graph_type}'"); } }
<?php $count = 0; foreach ($accounts as $a) { $count++; $balances = array(); $balances_wallet = array(); $balances_securities = array(); $last_updated = null; $job = false; // an account may have multiple currency balances $all_exchanges = get_all_exchanges(); foreach (array($a['exchange'], $a['exchange'] . '_wallet', $a['exchange'] . '_securities') as $exchange) { // only make requests for exchange keys that will actually ever exist if (isset($all_exchanges[$exchange])) { $q = db()->prepare("SELECT balances.* FROM balances WHERE user_id=? AND account_id=? AND exchange=? AND is_recent=1 ORDER BY currency ASC"); $q->execute(array(user_id(), $a['id'], $exchange)); while ($balance = $q->fetch()) { switch ($balance['exchange']) { case $a['exchange']: $balances[$balance['currency']] = $balance['balance']; break; case $a['exchange'] . "_wallet": $balances_wallet[$balance['currency']] = $balance['balance']; break; case $a['exchange'] . "_securities": $balances_securities[$balance['currency']] = $balance['balance']; break; default: throw new Exception("Unknown exchange '" . htmlspecialchars($balance['exchange']) . "' while retrieving account balances"); }
/** * Calculate all of the different types of managed graphs that * may be provided to a given user, in each category of managed * graphs (see get_managed_graph_categories()). */ function calculate_all_managed_graphs($user) { $result = array(); $summaries = get_all_summary_currencies(); $all_summaries = get_all_summaries(); $currencies = get_all_currencies(); $accounts = account_data_grouped(); $wallets = get_supported_wallets(); $order_currency = array(); foreach (get_all_currencies() as $c) { $order_currency[$c] = count($order_currency); } $order_exchange = array(); foreach (get_all_exchanges() as $key => $label) { $order_exchange[$key] = count($order_exchange) * 10; } $default_order = array('btc_equivalent' => -1000, 'composition_pie' => 0, 'balances_table' => 1000, 'exchange_daily' => 2000, 'total_daily' => 3000, 'all_daily' => 4000, 'composition_daily' => 5000, 'hashrate_daily' => 6000); $result['summary'] = array(); $result['all_summary'] = array(); $result['summary']['balances_table'] = array('order' => $default_order['balances_table'], 'width' => get_site_config('default_user_graph_height'), 'free' => true, 'priority' => 1); if (count($summaries) >= 2 && isset($summaries['btc'])) { $result['summary']['btc_equivalent'] = array('order' => $default_order['btc_equivalent'], 'width' => get_site_config('default_user_graph_height'), 'free' => true, 'priority' => 2); } foreach (get_all_cryptocurrencies() as $cur) { if (isset($summaries[$cur])) { $result['summary']["composition_" . $cur . "_pie"] = array('order' => $default_order['composition_pie'] + $order_currency[$cur], 'width' => get_site_config('default_user_graph_height'), 'free' => $cur == $user['preferred_crypto'], 'priority' => $cur == $user['preferred_crypto'] ? 100 : 300 + $order_currency[$cur]); $result['summary']["composition_" . $cur . "_daily"] = array('order' => $default_order['composition_daily'] + $order_currency[$cur], 'free' => $cur == $user['preferred_crypto'], 'priority' => $cur == $user['preferred_crypto'] ? 105 : 200 + $order_currency[$cur]); $result['summary']['total_' . $cur . '_daily'] = array('order' => $default_order['total_daily'] + $order_currency[$cur], 'source' => $cur, 'free' => $cur == $user['preferred_crypto'], 'priority' => 100 + $order_currency[$cur]); } } foreach (get_all_commodity_currencies() as $cur) { if (isset($summaries[$cur])) { $result['summary']['total_' . $cur . '_daily'] = array('order' => $default_order['total_daily'] + $order_currency[$cur], 'source' => $cur, 'free' => $cur == $user['preferred_crypto'], 'priority' => 100 + $order_currency[$cur]); } } $result['currency'] = array(); $result['all_currency'] = array(); foreach (get_exchange_pairs() as $exchange => $pairs) { foreach ($pairs as $pair) { // we are interested in both of these currencies if (isset($summaries[$pair[0]]) && isset($summaries[$pair[1]])) { // and one of these currencies are a preferred currency if ($pair[0] == $user['preferred_crypto'] || $pair[0] == $user['preferred_fiat'] || $pair[1] == $user['preferred_crypto'] || $pair[1] == $user['preferred_fiat']) { // and we have a summary instance for this pair somewhere $possible_summaries = array('summary_' . $pair[0] . '_' . $exchange, 'summary_' . $pair[1] . '_' . $exchange); if (in_array($pair[0], get_all_cryptocurrencies())) { $possible_summaries[] = "summary_" . $pair[0]; } if (in_array($pair[1], get_all_cryptocurrencies())) { $possible_summaries[] = "summary_" . $pair[1]; } foreach ($possible_summaries as $p) { if (isset($all_summaries[$p])) { $is_default = is_fiat_currency($pair[0]) && get_default_currency_exchange($pair[0]) == $exchange || is_fiat_currency($pair[1]) && get_default_currency_exchange($pair[1]) == $exchange; $result['all_currency'][$exchange . "_" . $pair[0] . $pair[1] . "_daily"] = array('order' => $default_order['exchange_daily'] + $order_exchange[$exchange] + $order_currency[$pair[0]], 'source' => $p, 'priority' => 150 + $order_currency[$pair[0]]); if ($is_default) { $result['currency'][$exchange . "_" . $pair[0] . $pair[1] . "_daily"] = array('order' => $default_order['exchange_daily'] + $order_exchange[$exchange] + $order_currency[$pair[0]], 'source' => $p, 'free' => true, 'priority' => 150 + $order_currency[$pair[0]]); } // don't display all2btc etc if (!in_array(substr($p, strlen("summary_")), get_all_cryptocurrencies())) { $result['all_summary']['all2' . substr($p, strlen("summary_")) . '_daily'] = array('order' => $default_order['all_daily'] + $order_exchange[$exchange] + $order_currency[$pair[0]], 'source' => $p, 'priority' => 50 + $order_currency[$pair[0]]); if ($is_default) { $result['summary']['all2' . substr($p, strlen("summary_")) . '_daily'] = array('order' => $default_order['all_daily'] + $order_exchange[$exchange] + $order_currency[$pair[0]], 'source' => $p, 'free' => $pair[0] == $user['preferred_crypto'] || $pair[0] == $user['preferred_fiat'], 'priority' => 5 + $order_currency[$pair[0]]); } } break; } } } } } } $result['securities'] = array(); // no graphs to put in here yet... // TODO in the future: securities composition graphs? e.g. composition_litecoinglobal_daily $result['mining'] = array(); foreach (get_all_hashrate_currencies() as $cur) { if (isset($summaries[$cur])) { // we need to have at least one pool that supports reporting hashrate $has_hashing_account = false; foreach ($accounts['Mining pools'] as $key => $account) { if (!isset($wallets[$key])) { continue; } $instances = get_all_user_account_instances($key); if ($instances) { if (in_array('hash', $wallets[$key])) { $has_hashing_account = $key; } } } if (!$has_hashing_account) { continue; } $result['mining']["hashrate_" . $cur . "_daily"] = array('order' => $default_order['hashrate_daily'] + $order_currency[$cur], 'source' => $has_hashing_account, 'free' => $cur == $user['preferred_crypto'], 'priority' => 150 + $order_currency[$cur]); } } // all 'summary' are also 'all_summary' etc foreach ($result['summary'] as $key => $value) { $result['all_summary'][$key] = $value; } foreach ($result['currency'] as $key => $value) { $result['all_currency'][$key] = $value; } // go through each category and sort by order foreach ($result as $key => $value) { uasort($result[$key], '_sort_by_order_key'); } return $result; }
function get_exchange_name($n) { if (in_array($n, Exchanges::getKeys())) { $exchange = Exchanges::getInstance($n); return $exchange->getName(); } if (in_array($n, Accounts::getKeys())) { $account = Accounts::getInstance($n); return $account->getName(); } $exchanges = get_all_exchanges(); if (isset($exchanges[$n])) { return $exchanges[$n]; } return "Unknown (" . htmlspecialchars($n) . "]"; }