GithubHelp home page GithubHelp logo

Comments (1)

karimkawambwa avatar karimkawambwa commented on June 2, 2024

Here is the script to pull data from production and move to influxdb:

### PostgreSQL DB info ###
from influxdb import InfluxDBClient
import dj_database_url  # for python 3+ use: from urllib.parse import urlparse
import psycopg2
import psycopg2.extras
from geohash import encode
# postgresql_table_name = ""

result = dj_database_url.parse("<db url>")

conn = psycopg2.connect(
    database=result["NAME"],
    user=result["USER"],
    password=result["PASSWORD"],
    host=result["HOST"],
    port=result["PORT"]
)

### InfluxDB info ####
# influx_db_name = "sensorsafrica"
influxClient = InfluxDBClient(
    'localhost',
    8086,
    'sensorsafrica',
    'sensorsafrica',
    'sensorsafrica',
    timeout=10,
    ssl=False,
    verify_ssl=False,
)
# influxClient.delete_database(influx_db_name)
# influxClient.create_database(influx_db_name)

'''
Generates an collection of influxdb points from the given SQL records
'''

influxClient.drop_measurement("air")

# query relational DB for all records
curr = conn.cursor('cursor', cursor_factory=psycopg2.extras.RealDictCursor)
# curr = conn.cursor(dictionary=True)
curr.execute("select * from sensors_sensordatavalue")
row_count = 0
# process 1000 records at a time
while True:
    sensor_datavalues = curr.fetchmany(10)
    influx_points = []
    for sensor_datavalue in sensor_datavalues:
        c = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)

        c.execute("select * from sensors_sensordata sd where '%s' = sd.id" %
                  sensor_datavalue["sensordata_id"])
        sensor_data = c.fetchone()
        c.execute("select * from sensors_sensor s where '%s' = s.id" %
                  sensor_data["sensor_id"])
        sensor = c.fetchone()
        c.execute("select * from sensors_sensortype st where '%s' = st.id" %
                  sensor["sensor_type_id"])
        sensor_type = c.fetchone()
        c.execute("select * from sensors_node n where '%s' = n.id" %
                  sensor["node_id"])
        node = c.fetchone()
        c.execute("select * from sensors_sensorlocation l where '%s' = l.id limit 1" %
                  node["location_id"])
        location = c.fetchone()
        try:
            influx_points.append({
                "measurement": 'air',
                "tags": {
                    "node_uid": node["uid"],
                    "node_owner": node["owner_id"],
                    "node_is_indoors": node["indoor"],
                    "node_is_at_height": node["height"],
                    "sensor_type": sensor_type["uid"],
                    "sensor_value_type": sensor_datavalue["value_type"],
                    "sensor_manufacturer": sensor_type["manufacturer"],
                    "lat": location["latitude"],
                    "lng": location["longitude"],
                    "city": location["city"],
                    "country": location["country"],
                    "industry_in_area": location["industry_in_area"],
                    "geohash": encode(location["latitude"], location["longitude"])
                },
                "time": sensor_datavalue["created"],
                "fields": {
                    sensor_datavalue["value_type"]: float(
                        sensor_datavalue["value"])
                }
            })
        except:
            print("fail")
    influxClient.write_points(influx_points)
    print("written")
    row_count += 10
    if len(sensor_datavalues) < 10:
        break
conn.close()

from sensors.africa-api.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.