GoodData 导出仪表板 API

GoodData Export Dashboard API

我正在研究如何从某个特定项目导出仪表板,就像这样 link 显示。

我编写的代码生成了 link 以供正确下载,正如预期的那样。但是导出的 PDF 文件并没有像我想要的那样过滤值。

我的问题出现在这条评论下面:

// Execute the dashboard with context

... 我应该在其中设置 "all filters that affects the dashboard",就像上面的 link 所说的那样。但它没有指定如何设置过滤器的值。

我制作了 PHP 中的所有代码(替换所有“{some-thing}”以使此代码有效):

<?php 

    $login = '{email}';
    $pass = '{password}';

    $headers = array('accept' => 'Accept: application/json','content-type' => 'Content-Type: application/json; charset=utf-8');
    $data = json_encode(array('postUserLogin'=>array('login'=> $login,'password'=> $pass,'remember'=>0)));

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_URL, "https://secure.gooddata.com/gdc/account/login");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $output = curl_exec($ch); 
    $AuthSST =  substr($output, strrpos($output, "GDCAuthSST=")+strlen("GDCAuthSST="), 17);

    $headers = array('accept' => 'Accept: application/json',
                    'content-type' => 'Content-Type: application/json; charset=utf-8', 
                    'cookie:' => 'Cookie: $Version=0; GDCAuthSST='.$AuthSST.' $Path=/gdc/account');

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HTTPGET, 1);
    curl_setopt($ch, CURLOPT_URL, "https://secure.gooddata.com/gdc/account/token");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $output = curl_exec($ch);         

    $start = strrpos($output, "GDCAuthTT=")+strlen("GDCAuthTT=");
    $finish = strrpos($output, "Path=/gdc");

    $GDCAuthTT =  substr($output, $start, ($finish-$start));

    $headers = array('accept' => 'Accept: application/json',
                    'content-type' => 'Content-Type: application/json; charset=utf-8', 
                    'cookie:' => 'Cookie: $Version=0; GDCAuthTT='.$GDCAuthTT.' $Path=/gdc/account');

    // Authentication finished, now start the dashboard export API

    // Execute the dashboard with context

    $project_id = '{project_id}';
    $filter_id = '{filter_id}';
    $filter_obj = '{0000}';
    $dash_obj = '{0000}';
    $url = 'https://secure.gooddata.com/gdc/internal/projects/'.$project_id.'/executionContexts';

    //  Inside filters: constraint, promptUri, uri, id ||||||||| Inside executionContext: dashboard, links, name, type, user

    $bode = json_encode(array('executionContext' => array('filters' => array(array(
                            'uri' => '/gdc/md/'.$project_id.'/obj/'.$filter_obj, // /elements?id=0000
                            'id' => $filter_id)), 
                            'dashboard' => '/gdc/md/'.$project_id.'/obj/'.$dash_obj,
                            'type' => 'export'
                        )));

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $bode);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $output = curl_exec($ch);

    // Export the Dashboard

    $dash_tab = 'ae2b1ab3a87c';

    $start = strrpos($output, "\"uri\":\"")+7;
    $finish = strrpos($output, "\"}");

    $url = "https://secure.gooddata.com/gdc/projects/".$project_id."/clientexport";

    $request_url = "https://secure.gooddata.com/dashboard.html#";
    $project = "project=/gdc/projects/".$project_id;
    $dashboard = "&dashboard=/gdc/md/".$project_id."/obj/".$dash_obj;
    $tab = "&tab=".$dash_tab."&export=1";
    $ctx = "&ctx=".substr($output, $start, ($finish-$start));

    $bode2 = json_encode(array('clientExport'=>array('url'=>$request_url.$project.$dashboard.$tab.$ctx, 'name'=>'{Dash_Name}')));

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $bode2);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    $output = curl_exec($ch);

    // Poll the URL from response Result:

    $start = strrpos($output, "\"poll\":\"")+8;
    $finish = strrpos($output, "\"}");
    $poll = "https://secure.gooddata.com".substr($output, $start, ($finish-$start));

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_HTTPGET, 1);
    curl_setopt($ch, CURLOPT_URL, $poll);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 1);

    $output = curl_exec($ch);

    echo "Link: ".$poll;
    echo "<br>Output: ".$output;

    // Wait 90 seconds and then make the download, otherwise it returns 202 http code

    sleep(90);

    $opts = array('http' =>
      array(
        'method'  => 'GET',
        'header'  => "Accept: application/json\r\n".
            "Content-Type: application/json; charset=utf-8\r\n".
          'Cookie: $Version=0; GDCAuthTT='.$GDCAuthTT.' $Path=/gdc/account'."\r\n",
        'timeout' => 6000
      )
    );

    $context  = stream_context_create($opts);
    $file = file_get_contents($poll."?download=true", false, $context);
    file_put_contents("Dashboard.pdf", $file);

    echo "<br><br>Link used: ".$poll."?download=true";

    $errors= error_get_last();
    echo "<br><br>".$errors['type'];
    echo "<br>".$errors['message'];

    curl_close($ch);
?>

您可以在过滤器定义中使用 constraint 对象指定过滤器值。确切的约束格式取决于过滤器的类型。如果过滤器类型为 list,则约束格式为:

"constraint": {
    "type": "list",
    "elements": [
      "/gdc/md/{project_id}/obj/{attribute_id}?id={element_id}",
      ...
    ]
}

如果它是日期过滤器,则约束如下所示:

"constraint": {
    "from": "2006-07-01",
    "to": "2008-03-31",
    "type": "interval"
}

"constraint": {
  "from": -5,
  "to": 0,
  "type": "floating"
}

POST 的整个 executionContext 对象示例,其中一个 list 过滤器选择了 2 个值:

{
  "executionContext": {
    "filters": [
      {
        "uri": "/gdc/md/{project_id}/obj/{attribute_id}",
        "constraint": {
          "type": "list",
          "elements": [
            "/gdc/md/{project_id}/obj/{displayForm_id}/elements?id={element_id_1}",
            "/gdc/md/{project_id}/obj/{displayForm_id}/elements?id={element_id_2}"
          ]
        },
        "id": "{filter_id}"
      }
    ],
    "dashboard": "/gdc/md/{project_id}/obj/{dashboard_id}",
    "type": "export"
  }
}

您可以看到过滤器约束的有效示例,例如在 handling drills or creating of saved view.

的文档中