First, some context about my application:
Databases were created on InfluxDB (in my VM)
I am already writing in a measurement
The script runs at a frequency of once per minute
I need to write a Python script to read the database. I want to fetch only the most recent measurement value, not all values.
Here is my code:
from influxdb import InfluxDBClient
client = InfluxDBClient(database='test')
client = InfluxDBClient()
rs = cli.query("SELECT * from CoreTemperature")
CoreTemperature = list(rs.get_points(measurement='CoreTemperature'))
print(CoreTemperature)
I am stuck because of:
The output of this code is: **NameError: name 'cli' is not defined**
Filtering by measurement and select * from CoreTemperature will read all points saved for the measurement, but I only want the most recent value.
The first error : NameError: name 'cli' is not defined is because you defined the influxdb client as client, and in the query you are calling it as cli
rs = client.query("SELECT * from CoreTemperature")
this will resolve this error.
The second error will be solved by:
client.query('SELECT last(<field_name>), time FROM CoreTemperature ')
It sounds like you want the most recent measurement value written to CoreTemperature. You can get that using the LAST selector:
LAST()
Returns the field value with the most recent timestamp.
Like this:
SELECT LAST(*) FROM CoreTemperature
Good morning everyone
I reach the target with the following code:
>from influxdb import InfluxDBClient
>dbClient = InfluxDBClient()
>loginRecords = dbClient.query('select last(*) from CoreTemperature',
database='test')
>print(loginRecords)
Thanks for all comments and help me!
Related
I am currently logged on to my BBG anywhere (web login) on my Mac. So first question is would I still be able to extract data using tia (as I am not actually on my terminal)
import pdblp
con = pdblp.BCon(debug=True, port=8194, timeout=5000)
con.start()
I got this error
pdblp.pdblp:WARNING:Message Received:
SessionStartupFailure = {
reason = {
source = "Session"
category = "IO_ERROR"
errorCode = 9
description = "Connection failed"
}
}
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/Users/prasadkamath/anaconda2/envs/Pk36/lib/python3.6/site-packages/pdblp/pdblp.py", line 147, in start
raise ConnectionError('Could not start blpapi.Session')
ConnectionError: Could not start blpapi.Session
I am assuming that I need to be on the terminal to be able to extract data, but wanted to confirm that.
This is a duplicate of this issue here on SO. It is not an issue with pdblp per se, but with blpapi not finding a connection. You mention that you are logged in via the web, which only allows you to use the terminal (or Excel add-in) within the browser, but not outside of it, since this way of accessing Bloomberg lacks a data feed and an API. More details and alternatives can be found here.
I am able successfully connect using LDAP3 and retrieve my LDAP group members as below.
from ldap3 import Server, Connection, ALL, SUBTREE
from ldap3.extend.microsoft.addMembersToGroups import ad_add_members_to_groups as addMembersToGroups
>>> conn = Connection(Server('ldaps://ldap.****.com:***', get_info=ALL),check_names=False, auto_bind=False,user="ANT\*****",password="******", authentication="NTLM")
>>>
>>> conn.open()
>>> conn.search('ou=Groups,o=****.com', '(&(cn=MY-LDAP-GROUP))', attributes=['cn', 'objectclass', 'memberuid'])
it returns True and I can see members by printing
conn.entries
>>>
The above line says MY-LDAP-GROUP exists and returns TRUE while searching but throws LDAP group not found when I try to an user to the group as below
>>> addMembersToGroups(conn, ['myuser'], 'MY-LDAP-GROUP')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/****/anaconda3/lib/python3.7/site-packages/ldap3/extend/microsoft/addMembersToGroups.py", line 69, in ad_add_members_to_groups
raise LDAPInvalidDnError(group + ' not found')
ldap3.core.exceptions.LDAPInvalidDnError: MY-LDAP-GROUP not found
>>>
The above line says MY-LDAP-GROUP exists and returns TRUE
Returning True just means that the search succeeded. It doesn't mean that anything was found. Is there anything in conn.entries?
But I suspect your real problem is something different. If this is the source code for ad_add_members_to_groups, then it is expecting the distinguishedName of the group (notice the parameter name group_dn), but you're passing the cn (common name). For example, your code should be something like:
addMembersToGroups(conn, ['myuser'], 'CN=MY-LDAP-GROUP,OU=Groups,DC=example,DC=com')
If you don't know the DN, then ask for the distinguishedName attribute from the search.
A word of warning: that code for ad_add_members_to_groups retrieves all the current members before adding the new member. You might run into performance problems if you're working with groups that have large membership because of that (e.g. if the group has 1000 members, it will load all 1000 before adding anyone). You don't actually need to do that (you can add a new member without looking at the current membership). I think what they're trying to avoid is the error you get when you try to add someone who is already in the group. But I think there are better ways to handle that. It might not matter to you if you're only working with small groups.
After so many trial and errors, I got frustrated and used the older python-ldap library to add existing users. Now my code is a mixture of ldap3 and ldap.
I know this is not what the OP has desired. But this may help someone.
Here the user Dinesh Kumar is already part of a group group1. I am trying to add him
to another group group2 which is successful and does not disturb the existing group
import ldap
import ldap.modlist as modlist
def add_existing_user_to_group(user_name, user_id, group_id):
"""
:return:
"""
# ldap expects a byte string.
converted_user_name = bytes(user_name, 'utf-8')
converted_user_id = bytes(user_id, 'utf-8')
converted_group_id = bytes(group_id, 'utf-8')
# Add all the attributes for the new dn
ldap_attr = {}
ldap_attr['uid'] = converted_user_name
ldap_attr['cn'] = converted_user_name
ldap_attr['uidNumber'] = converted_user_id
ldap_attr['gidNumber'] = converted_group_id
ldap_attr['objectClass'] = [b'top', b'posixAccount', b'inetOrgPerson']
ldap_attr['sn'] = b'Kumar'
ldap_attr['homeDirectory'] = b'/home/users/dkumar'
# Establish connection to server using ldap
conn = ldap.initialize(server_uri, bytes_mode=False)
bind_resp = conn.simple_bind_s("cn=admin,dc=testldap,dc=com", "password")
dn_new = "cn={},cn={},ou=MyOU,dc=testldap,dc=com".format('Dinesh Kumar','group2')
ldif = modlist.addModlist(ldap_attr)
try:
response = conn.add_s(dn_new, ldif)
except ldap.error as e:
response = e
print(" The response is ", response)
conn.unbind()
return response
I have a SQL query that will search a database and return a single row containing at least 3 different columns as follows:
SELECT
`epg_num` AS `epgNum`,
`service_key` AS `serviceKey`,
`service_name` AS `channelName`
FROM `ssr_services`
WHERE `epg_num` = %(channelValue)s
I only require the Service Key to added to a list in my code.
The above SQL query cannot be amended as it is used by other things and I would prefer not to write a new query just for this one function. The above query is also a function in another module that I am calling to try and extract the Service Key.
The user will specify the epg_num when running the code. Which will then bring back a row in SQL showing the epg_num, service key and service name
Imports:
import sys
import os
import MySQLdb
from subprocess import check_output, CalledProcessError
from collections import OrderedDict
from UPnP.core.proxy import ServerProxy
from Data_Types import PlannerExportEvent, PlannerExportResponse, UpnpError, Result, Channel, Event, Shelf
from Database_Connection import FetchAll, FetchOne
from Decorators import ResetRetry, Suppress
from Logs import Logging
from Utilities import GetRecycID, GetRackID, Wait
The code I have at the moment is as follows:
if epgNumsList:
serviceKeysList = self.GetMultiChannelInfo(epgNumsList=epgNumsList)
print(serviceKeysList)
whereString = "(epg_num IN ({epgNumsList}) AND epg_num LIKE '___')".format(epgNumsList=','.join(epgNumsList))
Which uses the following:
def GetMultiChannelInfo(self, epgNumsList=None):
serviceKeys = []
for epgNum in epgNumsList:
retObj = self.GetChannelInfo(epgNum=epgNum)
result = retObj.Result()
if result:
channelObj = retObj.Data()
result = channelObj.serviceKey
serviceKeys.append(result)
print(result)
else:
print('Channel not found')
print(serviceKeys)
return Result(serviceKeys)
I am seeing the following error:
Traceback (most recent call last):
File "C:\Users\MHE36\workspace\Scripts\SkyPlus__UPnP_Set_Recordings.py", line 42, in <module>
upnp.BackgroundRecordings(recordings, epgNumsList, serviceKeysList, duration)
File "C:\Users\MHE36\workspace\Libraries\SuperPlanner.py", line 1154, in BackgroundRecordings
print(serviceKeysList)
File "C:\Users\MHE36\workspace\Libraries\Data_Types.py", line 32, in __repr__
return 'Result (Result: {result}, Data: {data}, NumFailures: {numFailures})'.format(result=self.result, numFailures=self.numFailures)
KeyError: 'data'
I think the trace back related to this bit of code from another module:
def __repr__(self):
return 'Result (Result: {result}, Data: {data}, NumFailures: {numFailures})'.format(result=self.result, numFailures=self.numFailures)
Hopefully this gives enough detail but happy to provide more if needed.
Thanks in advance.
How are you executing that query? with pyodbc?
then you can actually turn that particular row in to a dictionary
columns = [column[0] for column in cursor.description]
list_of_dict =[]
for dict in cursor.fetchall():
list_of_dict.append(dict(zip(columns, dict)))
print that list_of_dict and you'll understand everything.
Note : you should not execute other queries before performing that code block because cursor will be refreshed.
What I suggest is you is that you access that list and return what you want at the time you are executing that query for some reason.
Beginner in python, pyRevit, and Revit API so my apologies if I'm phrasing my question poorly. Today I used pyRevit to develop a simple pushbutton tool that worked, and then after a few minutes stopped working without anything being changed (that I'm aware of)
My tool adds all groups with excluded elements to the selection. It worked perfectly for a time, then started throwing this error, which I can't make heads or tails of:
Exception: The input argument "document" of function `anonymous-namespace'::FilteredElementCollector_constructor or one item in the collection is null at line 326 of file d:\ship\2018_px64\source\revit\revitdbapi\APIFilteredElementCollectorProxy.cpp. Parameter name: document
The path in the error message isn't one I recognize on my computer. Here's the relevant code (the traceback goes to line 24, which is "groups = FilteredElementCollector...":
from pyrevit import script
from pyrevit.framework import List
from pyrevit.framework import clr
from pyrevit import revit, DB
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
groups = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_IOSModelGroups).WhereElementIsNotElementType().ToElements()
selection = revit.get_selection()
SelectionIds = []
for group in groups:
name = group.Name
if "(members excluded)" in name:
SelectionIds.append(group.Id)
selection.set_to(SelectionIds)
Thanks a lot for any solutions, or even help deciphering the error message.
I have this code to query a influx DB, but it is not working at all. Here is the python code.
import os
from influxdb import InfluxDBClient
username = u'{}'.format(os.environ['INFLUXDB_USERNAME'])
password = u'{}'.format(os.environ['INFLUXDB_PASSWORD'])
client = InfluxDBClient(host='127.0.0.1', port=8086, database='data',
username=username, password=password)
result = client.query("SELECT P_askbid_midprice1 FROM 'DCIX_OB' WHERE time > '2018-01-01';")
I got the following error, but it's still unclear how to fix the code above. If I query directly from influxdb with bash with SELECT P_askbid_midprice1 FROM 'DCIX_OB' WHERE time > '2018-01-01'; it worked perfectly fine.
Press ENTER or type command to continue
Traceback (most recent call last):
File "graph_influxdb.py", line 11, in <module>
result = client.query("SELECT P_askbid_midprice1 FROM 'DCIX_OB' WHERE time > '2018-01-01';")
File "/home/ubuntu/.local/lib/python3.5/site-packages/influxdb/client.py", line 394, in query
expected_response_code=expected_response_code
File "/home/ubuntu/.local/lib/python3.5/site-packages/influxdb/client.py", line 271, in request
raise InfluxDBClientError(response.content, response.status_code)
influxdb.exceptions.InfluxDBClientError: 400: {"error":"error parsing query: found DCIX_OB, expected identifier at line 1, char 31"}
How can I fix it?
result = client.query("SELECT P_askbid_midprice1 FROM DCIX_OB WHERE time > '2018-01-01'")
this should work
you might be able to use Pinform which is some kind of ORM/OSTM (Object time series mapping) for InfluxDB.
It can help with designing the schema and building normal or aggregation queries.
cli.get_fields_as_series(OHLC,
field_aggregations={'close': [AggregationMode.MEAN]},
tags={'symbol': 'AAPL'},
time_range=(start_datetime, end_datetime),
group_by_time_interval='10d')
Disclaimer: I am the author of this library