create or update a panel in Grafana using Python - grafana

I have a dashboard named as "server-plots" and there is another dashboard named as "master-plots". panels under "master-plots" are most updated graphs and I want to add the new panels inside "master-plots" dashboard to "server-plots" as well, everything with Python code (not manually or using curl).
I am able to programmatically take the backup of these plots using Grafana GET APIs ,as JSON. I want to find the new panels inside the "master-plots" dashboard JSON and add those into "server-plots" dashboard , all using Python. I am unable to find any API to do that. Any idea how can I achieve this?

The way I achieved this was by taking the backup of the master-plots as JSON using:
/api/dashboards/uid/<uid of the dashboard>
Then comparing it with the one inside the server-plots (taken similarly), and then updating the server-plots JSON with the diff (basically replacing server-plots JSON with the master-plots JSON), and finally writing that to the following using the POST method:
/api/dashboards/db/
One thing to consider here: The new JSON which is being written into the server-plots should have different uid and overwrite=True:
--snip--
f = open(myjsonfile,) # the updated JSON of server-plots
data = json.load(f)
data["dashboard"]["id"] = None
data["overwrite"] = True
data["folderId"] = 123 # this is the folder ID of the server-plots
data["dashboard"]["uid"] = <logic to generate randum alpha-num>
url = "https://mygrafanaurl.com/api/dashboards/db"
headers = {'Authorization': 'auth', 'Content-Type': 'application/json' }
response = requests.request("POST", url, headers=headers, data=json.dumps(data))

Related

Get hyperlink to sheet based on SheetID

I am importing a Smartsheet Report through Python, using an API. One of the columns in this report contains a hyperlink that works in Smartsheet, however when importing the report with Python I only receive the words of this column, and not the link behind them. Is it possible to get the URLs of the sheets that these hyperlinks are referring to in any other way? I was thinking maybe based on SheetID (which I can find using the title of the indepentent sheets), but all other suggestions are very welcome!
I've been unable to reproduce the problem you've described.
The report I'm testing with contains the following data. The Google link in the first row is a normal URL that points to https://www.google.com and the Contacts List link in the second row is a sheet hyperlink that points to another sheet in Smartsheet.
First, I use the Python SDK to get the report and then print out the contents of the second cell of the first row (i.e., the one that contains the Google hyperlink):
reportID = 6667768033503108
report = smartsheet_client.Reports.get_report(reportID)
print(report.rows[0].cells[1])
The result of this code showed the following output (JSON formatted here for readability):
{
"columnId": 5228827298293636,
"displayValue": "Google",
"hyperlink": {
"url": "https://www.google.com"
},
"value": "Google",
"virtualColumnId": 2581703205119876
}
So, accessing the URL of the hyperlink can be accomplished with the following code:
url = report.rows[0].cells[1].hyperlink.url
print(url) #shows output: https://www.google.com
The same approach works for getting the URL of the sheet hyperlink in the second row. i.e., running the following code:
reportID = 6667768033503108
report = smartsheet_client.Reports.get_report(reportID)
url = report.rows[1].cells[1].hyperlink.url
print(url) #shows output: https://app.smartsheet.com/sheets/[ID]
This approach should work for you, but if for some reason you're seeing that the cell object in the JSON response (when using the Python SDK) for the cell that contains the link doesn't actually contain a hyperlink object with a url property -- that might indicate a bug either with the Python SDK or with the underlying API. In that case, you might try getting the URL string by using a dictionary, as shown in the following code. (Note: you'll need to import json for this code to work).
reportID = 6667768033503108
# get the report
report = smartsheet_client.Reports.get_report(reportID)
# load the contents of the second cell in the first row
resp_dict = json.loads(str(report.rows[0].cells[1]))
# read the url property from the dictionary
url = resp_dict['hyperlink']['url']
print(url) #shows output: https://www.google.com

Clockify - Best way to get ALL data into excel or access using the REST api?

I am trying to get all data into excel using the Get data from web option but I am not sure how to properly configure it. I did get the API key.
EDIT:
Below is the path I use, but that is only the time entries for one user, I need them for ALL users.
https://api.clockify.me/api/v1/workspaces/myWorkspace/user/myUser/time-entries
Any help is appreciated.
EDIT: I didn't read that you wanted to use Power Query in Excel. The Advanced Editor only works with this code in Power BI Desktop.
You'll need to program the request in the Advanced Editor in Power Query.
Here is an example of connecting to the Get Workspace Clients endpoint:
let
baseUrl = "https://api.clockify.me/api/v1/",
apiKey = "your-api-key",
workspaceID = "5dfb421c1b30342708229760",
GetWorkspace = (workspaceID as text) =>
let
options = [Headers = [#"X-Api-Key"= apiKey, #"Content-Type" = "application/Json"],
RelativePath = "workspaces/" & workspaceID & "/clients"],
call = Web.Contents(baseUrl, options),
jsonParsed = Json.Document(call)
in
jsonParsed
in
GetWorkspace
Using this function you just have to change the parameters you need according to the endpoint you want to hit.
The baseUrl will be the same, you'll need to change the RelativePath with the rest of the url and if you need to pass some query parameters, put them after RelativePath in a record, like this:
RelativePath = "workspaces/" & workspaceID & "/clients", Query = [page = "1"]],
I recommend using Postman to make the calls and Fiddler to track how the Url is being constructed. Then compare Postman requests with your power query requests, to check the differences.
Here are some other threads about the subject:
How to access Clockify API through Power Query
How to pull data from Toggl API with Power Query?

Adding Pull Requests & Issues to a Project

Does the Github API provide an easy way to add a Pull Request or an Issue to a Project Board?
This is the programmatic equivalent of a user going to a pull request and selecting one more "Projects" from the sidebar menu
NOTE: The API does seem to provide a way to add cards to the Project, but I have to specify a specific project column. I'd love to just add the project blindly and let automation rules determine the column, similar to clicking on it through the UI.
Thanks!
I think the best way to associate an existing pull request with a project, in some kind of default column, would be to daisy-chain three separate pieces of the Github API, the Get a Single Pull Request method, the Create a Project Card method, and the List Project Columns method. The idea is as follows:
Use 'Get a Single Pull Request' to retrieve the ID
Use 'List Project Columns' to get a list of the columns
Do any conditional logic if you want to see if a certain column exists, or just use the first one, or create a certain column if it doesn't exist
Use "Create a Project Card" to add the card using the Pull request ID and Column you've selected.
Here is a simplified example in Python:
import requests, json
#get pull request
r = requests.get('https://api.github.com/repos/[myusername]/[myrepo]/pulls/2')
pull = json.loads(r.text)
#requires authentication ... create your token through Github.com
api_token = "mytoken"
#prepare dictionary of header data
h = {"Accept":"application/vnd.github.inertia-preview+json", "Authorization": "token %s" % api_token}
projects_r = requests.get('https://api.github.com/repos/[myusername]/[myrepo]/projects', headers=h)
#get projects data
projects = json.loads(projects_r.text)
#get columns url for the first project in the list projects
columns_url = projects[0]['columns_url']
columns_r = requests.get(columns_url, headers=h)
columns = json.loads(columns_r.text)
#get column url for the first column in the list of columns
column_url = columns[0]['cards_url']
#use retrieved data to build post
data = {"content_id":pull_id, "content_type":"PullRequest"}
#use post method with headers and data to create card in column
result = requests.post(column_url, headers=h, data=json.dumps(data))
#returns with status 201, created

Downloading from google cloud storage with php

Is there a way to achieve downloading via. the google-php-api? I have tried the following:
using the medialink and trying to curl the object (Returns "Login Required")
reading the guzzle response stream (comes back empty even though all the headers have the correct data)
I am able to see everything but the body of the file via. the API.
Edit:
I am of course able to download the file via the medialink, taken it is set to public - however that will not work for this situation.
The solution is as follows...
You must make an authorized HTTP request, to do this you must:
$object = $service->objects->listObjects(BUCKET, OBJECT);
$http = $client->authorize();
$request = new GuzzleHttp\Psr7\Request('GET', $object->getMediaLink());
$response = $http->send($request);
$body = $response->getBody()->read($object->getSize());
The above is a small snippet but the jist of what you need to get the contents of a file.

Integrate AngularJS App with SoftwareAG webMethods Integration Server

I have been trying to set up a sample AngularJS app with webMethods Integration Server on the backend. Using $resource, I can easily pull normal JSON files and manipulate the data within the file. However, the goal is that I want to create services in webMethods Designer and call them from AngularJS using $resource to display the data in my app. The problem is that from AngularJS I cannot extract the data I need from the service that I'm creating in Designer. In Designer I can use (in WMPublic) documentToJSONString, and output something like:
jsonString {"id":"1", "name":"Dan", "quantity":"3"}
But I cannot extract the data because this is not a pure JSON string. Does anyone know how to (1) extract the JSON string output data using AnularJS or (2) output a JSON document from Designer? I am calling a REST service; something to the effect of
http://localhost:2222/rest/Get/getOrderData
from my services.js file in AngularJS.
Here is my services.js file:
/* Services */
var orderServices = angular.module('orderServices', ['ngResource']);
orderServices.factory('Order', ['$resource',
function($resource){
return $resource('http://localhost:2222/rest/REST/getOrderData', {}, {
query: {method:'GET', isArray:true}
});
}]);
Then, in my app, I want to use an ng-repeat to call things like {{order.id}}, {{order.name}} etc. Is anyone good with webMethods and Angular or done this before?
To force the response that you want, I would have used the service
pub.flow:setResponse mapping the jsonString to it's string parameter and probably hardcoded (eww!) the contentType parameter to 'application/json'
You may also need to use the service pub.flow:setResponseCode to set the response code.
They would be the last services in getOrderData
I would have invoked it using the below (where namespace is the folder structure in designer)
http://localhost:2222/invoke/namespace:getOrderData
The above applies to Integration Server V8 and it looks like you're using V9 since some of the services that you mention didn't exist in V8. This would also apply to a normal flow service, not a specific REST one (assuming they exist in V9).