} $csdl = trim($csdl); if (strlen($csdl) == 0) { die("CSDL is empty\n\n"); } // Authenticate echo "Creating user...\n"; $user = new DataSift_User(USERNAME, API_KEY); // Create the definition echo "Creating definition...\n"; $definition = new DataSift_Definition($user, $csdl); // Get the DPU. This will compile the definition, so we catch potential // errors from that. echo "Getting DPU...\n"; try { $dpu = $definition->getDPUBreakdown(); } catch (DataSift_Exception_CompileFailed $e) { die("CSDL compilation failed: " . $e->getMessage() . "\n\n"); } // Format the DPU details for output in a table $dputable = array(); $maxlength = array('target' => strlen('Target'), 'times used' => strlen('Times used'), 'complexity' => strlen('Complexity')); foreach ($dpu['detail'] as $tgt => $c) { $maxlength['target'] = max($maxlength['target'], strlen($tgt)); $maxlength['times used'] = max($maxlength['times used'], strlen(number_format($c['count']))); $maxlength['complexity'] = max($maxlength['complexity'], strlen(number_format($c['dpu'], 2))); $dputable[] = array('target' => $tgt, 'times used' => number_format($c['count']), 'complexity' => number_format($c['dpu'], 2)); foreach ($c['targets'] as $tgt => $d) { $maxlength['target'] = max($maxlength['target'], 2 + strlen($tgt)); $maxlength['times used'] = max($maxlength['times used'], strlen(number_format($d['count']))); $maxlength['complexity'] = max($maxlength['complexity'], strlen(number_format($d['dpu'], 2)));
public function testGetDPUBreakdownOnInvalidDefinition() { $def = new DataSift_Definition($this->user, testdata('invalid_definition')); $this->assertEquals($def->get(), testdata('invalid_definition')); $response = array('response_code' => 400, 'data' => array('error' => 'The target interactin.content does not exist'), 'rate_limit' => 200, 'rate_limit_remaining' => 150); DataSift_MockApiClient::setResponse($response); try { $def->getDPUBreakdown(); $this->fail('CompileFailed exception expected, but not thrown'); } catch (DataSift_Exception_InvalidData $e) { // Check the error message $this->assertEquals($e->getMessage(), $response['data']['error']); } catch (DataSift_Exception_APIError $e) { $this->fail('APIError: ' . $e->getMessage() . ' (' . $e->getCode() . ')'); } catch (Exception $e) { $this->fail('Unhandled exception: ' . $e->getMessage() . ' (' . $e->getCode() . ')'); } }
public function testGetDPUBreakdown() { $def = new DataSift_Definition($this->user, testdata('definition')); $response = array('response_code' => 200, 'data' => array('hash' => testdata('definition_hash'), 'created_at' => date('Y-m-d H:i:s', time()), 'dpu' => 10, 'detail' => array('detail 1')), 'rate_limit' => 200, 'rate_limit_remaining' => 150); DataSift_MockApiClient::setResponse($response); $this->assertEquals($def->get(), testdata('definition'), 'Definition string not set correctly'); $dpu = $def->getDPUBreakdown(); $this->assertEquals(count($dpu['detail']), 1, 'The DPU breakdown is not what was expected'); $this->assertTrue($dpu['dpu'] > 0, 'The total DPU is not positive'); $this->assertEquals($dpu['dpu'], $def->getTotalDPU(), 'The total DPU returned by the definition is not correct'); }