import requests import polyline import json import psycopg2 import psycopg2.extensions from datetime import datetime, timezone from geojson import Point, Feature, FeatureCollection, dump conn = psycopg2.connect(host='localhost', database='nws', user='nws', password='nws') cursor = conn.cursor() allcountyoutages = [] S = requests.Session() #select distinct on (county,state,update) county,state,sum(outages) as outage, update as time, sum(served) as served from countyoutages where update > $2 and update < $3 and cwa = $1 group by county,state,update #select distinct on (county,state) max(outage),county,state from (select distinct on (county,state,update) county,state,sum(outages) as outage, update as time, sum(served) as served from countyoutages where update > '2023-04-01' and update < '2023-04-02' and cwa = 'RLX' group by county,state,update) as potato group by county,state; cursor.execute("select distinct on (county,state,update) county,state,sum(outages) as outage, update as time, sum(served) as served from countyoutages where update > $2 and update < $3 and cwa = $1 group by county,state,update") current_timestamp = str(datetime.utcnow()) for i in allcountyoutages: sql = 'insert into countyoutages (outages, served, county, state, update, company) values (%s, %s, %s, %s, %s, %s)' val = (i[0], i[1], i[2], i[3], current_timestamp, i[4]) cursor.execute(sql,val) conn.commit() cursor.execute('update countyoutages set cwa = county.cwa from county where county.countyname = countyoutages.county and county.state = countyoutages.state and countyoutages.cwa is null') conn.commit() cursor.execute("delete from countyoutages where cwa != 'RLX'") cursor.execute("delete from countyoutages where cwa is null") cursor.execute("delete from countyoutages where update < now () - interval '30 days'") conn.commit() cursor.close() conn.close()