<commit_before>#!/usr/bin/env python

"""
Export kWh from aggregators
"""

import erppeek
import dbconfig

from plantmeter.isodates import isodate 
from dateutil import tz

import pymongo
import click

c = erppeek.Client(**dbconfig.erppeek)
m = pymongo.MongoClient(*dbconfig.mongo)
mdb = m['somenergia']
collection = mdb['generationkwh.production.measurement']

aggr_obj = c.GenerationkwhProductionAggregator
plant_obj = c.GenerationkwhProductionPlant
meter_obj = c.GenerationkwhProductionMeter
meas_obj = c.GenerationkwhProductionMeasurement
warn_obj = c.GenerationkwhProductionNotifier

from_zone = tz.tzutc()
to_zone = tz.tzlocal()

def get_kwh(meters, start, end):
    search_params = {}

    if meters:
        search_params.update({'name': {'$in': meters}})
    if start:
        start = isodate(start)
        search_params.update({'datetime': {'$gt': start}})
    if end:
        end = isodate(end)
        if 'datetime' in search_params:
            search_params['datetime'].update({'$lt': end})
        else:
            search_params.update({'datetime': {'$lt': end}})

    measures = [x for x in collection.find(
        search_params,
        {'datetime':1, 'ae':1},
        sort=[('datetime', pymongo.ASCENDING)])]

    return [(
        measure['datetime'].replace(tzinfo=from_zone).astimezone(to_zone),
        measure['ae'])
        for measure in measures]

def get(aggregator, start, end):
    search_params = []
    if aggregator:
        search_params = [('id', '=', aggregator)]
    aggrs = aggr_obj.search(search_params)

    meters= [str(meter) for aggr in aggrs
            for plant in aggr_obj.read(aggr, 'plants')
            for meter in plant_obj.read(plant, 'meters')]
    return get_kwh(meters, start, end)

@click.group()
def aggregator():
    ""

@aggregator.command(<commit_msg>explicit param names for clarity<commit_after>#!/usr/bin/env python

"""
Export kWh from aggregators
"""

import erppeek
import dbconfig

from plantmeter.isodates import isodate 
from dateutil import tz

import pymongo
import click

c = erppeek.Client(**dbconfig.erppeek)
m = pymongo.MongoClient(*dbconfig.mongo)
mdb = m['somenergia']
collection = mdb['generationkwh.production.measurement']

aggr_obj = c.GenerationkwhProductionAggregator
plant_obj = c.GenerationkwhProductionPlant
meter_obj = c.GenerationkwhProductionMeter
meas_obj = c.GenerationkwhProductionMeasurement
warn_obj = c.GenerationkwhProductionNotifier

from_zone = tz.tzutc()
to_zone = tz.tzlocal()

def get_kwh(meters, start, end):
    search_params = {}

    if meters:
        search_params.update({'name': {'$in': meters}})
    if start:
        start = isodate(start)
        search_params.update({'datetime': {'$gt': start}})
    if end:
        end = isodate(end)
        if 'datetime' in search_params:
            search_params['datetime'].update({'$lt': end})
        else:
            search_params.update({'datetime': {'$lt': end}})

    measures = [x for x in collection.find(
        filter=search_params,
        projection={'datetime':1, 'ae':1},
        sort=[('datetime', pymongo.ASCENDING)])]

    return [(
        measure['datetime'].replace(tzinfo=from_zone).astimezone(to_zone),
        measure['ae'])
        for measure in measures]

def get(aggregator, start, end):
    search_params = []
    if aggregator:
        search_params = [('id', '=', aggregator)]
    aggrs = aggr_obj.search(search_params)

    meters= [str(meter) for aggr in aggrs
            for plant in aggr_obj.read(aggr, 'plants')
            for meter in plant_obj.read(plant, 'meters')]
    return get_kwh(meters, start, end)

@click.group()
def aggregator():
    ""

@aggregator.command(
{'fraction_non_alphanumeric': 0.13042291950886767, 'fraction_numerical': 0.001091405184174625, 'mean_word_length': 3.5653798256537983, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1746672', 'n_tokens_mistral': 1235, 'n_tokens_neox': 1197, 'n_words': 261}