Esempio n. 1
0
def send_to_influx(model_data, site, timestamp, to_exclude=[],client=None, send_status=True):
    """
    Utility function to send data to influx
    """
    try:
        if client:
            i = client
        else:
            i = Influx()

        model_data_dict = model_to_dict(model_data)

        if to_exclude:
            # Remove any value we wish not to be submitted
            # Generally used with datetime measurement
            for val in to_exclude:
                #if to_exclude  in model_data_dict.keys():
                model_data_dict.pop(val)

        #Add our status will be used for RMC_Status
        if send_status:
            model_data_dict['status'] = 1.0

        status = i.send_object_measurements(model_data_dict, timestamp=timestamp, tags={"site_id":site.id, "site_name":site.site_name})
    except Exception,e:
        message = "Error sending to influx with exception %s in datapint %s"%(e,model_data_dict)
        handle_task_failure(message= message, name='send_to_influx' ,exception=e,data=model_data)
Esempio n. 2
0
def get_aggregate_data(site,
                       measurement,
                       bucket_size='1h',
                       clause=None,
                       toSum=True,
                       operator='mean',
                       start='now'):
    """
    Calculate aggregate values from Influx for provided measuruements
    """
    i = Influx()
    result = [0]

    clause_opts = {
        'negative': (lambda x: x[operator] < 0),
        'positive': (lambda x: x[operator] > 0),
        'zero': (lambda x: x[operator] == 0)
    }

    if clause and not clause in clause_opts.keys():
        logger.error("unkown clause provided %s allowed %s" %
                     (clause, clause_opts.keys()))
        return result

    #get measurement values from influx
    if not toSum:
        operator = 'min'

    aggr_results = i.get_measurement_bucket(measurement,
                                            bucket_size,
                                            'site_name',
                                            site.site_name,
                                            operator=operator,
                                            start=start)

    logger.debug("influx results %s " % (aggr_results))

    #we have mean values by the hour now aggregate them
    if aggr_results:
        agr_value = []
        if clause:
            #if we have a cluase filter to apply
            aggr_results = filter(clause_opts[clause], aggr_results)

        if toSum:
            to_sum_vals = map(lambda x: x[operator], aggr_results)
            agr_value.append(sum(to_sum_vals))
            result = agr_value
        else:
            result = aggr_results

        logger.debug("Aggregating %s %s agr:%s" %
                     (measurement, aggr_results, agr_value))
    else:
        message = "No Values returned for aggregate. Check Influx Connection."
        logger.warning(message)

    return result
    def setUp(self):

        # Need this to create a Site
        self.VRM = VRM_Account.objects.create(vrm_user_id='*****@*****.**',
                                              vrm_password="******")

        # Setup Influx
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
        except:
            self.i.delete_database(self._influx_db_name)
            sleep(1)
            self.i.create_database(self._influx_db_name)
            pass

        # Setup Kapacitor
        self.kap = Kapacitor()
        self.template_id = 'test_template'
        self.task_id = 'test_task'
        self.dj_template_name = 'alert_template'

        self.dbrps = [{'db': self._influx_db_name, 'rp': 'autogen'}]

        self.location = Geoposition(52.5, 24.3)
        dt = timezone.make_aware(timezone.datetime(2015, 12, 11, 22, 0))

        self.site = Sesh_Site.objects.create(site_name=u"Test_aggregate",
                                             comission_date=dt,
                                             location_city=u"kigali",
                                             location_country=u"rwanda",
                                             vrm_account=self.VRM,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=12,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=12321,
                                             has_genset=True,
                                             has_grid=True)

        #self.no_points = create_test_data(self.site,
        #                                 start = self.site.comission_date,
        #                                 end = dt + timedelta( hours = 48),
        #                                 interval = 30,
        #                                 random = False)

        #create test user
        self.test_user = Sesh_User.objects.create_user("john doe",
                                                       "*****@*****.**",
                                                       "asdasd12345")
        #assign a user to the sites
        assign_perm("view_Sesh_Site", self.test_user, self.site)
    def setUp(self):
        """
        Initializing
        """
        self.client = Client()

        self.location = Geoposition(52.5,24.3)

        # Setup Influx
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
        except:
           self.i.delete_database(self._influx_db_name)
           sleep(1)
           self.i.create_database(self._influx_db_name)
           pass

        self.site = Sesh_Site.objects.create(site_name=u"Test site",
                                             comission_date=timezone.datetime(2015, 12, 11, 22, 0),
                                             location_city=u"kigali",
                                             location_country=u"rwanda",
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=24,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=12321,
                                             has_genset=True,
                                             has_grid=True,
                                            )   
    def setUp(self):

        self.VRM = VRM_Account.objects.create(vrm_user_id='*****@*****.**',
                                              vrm_password="******")
        self.location = Geoposition(52.5, 24.3)
        self._influx_db_name = 'test_db'

        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
        except:
            self.i.delete_database(self._influx_db_name)
            sleep(1)
            self.i.create_database(self._influx_db_name)
            pass

        self.site = Sesh_Site.objects.create(site_name=u"Test_aggregate",
                                             comission_date=timezone.datetime(
                                                 2015, 12, 11, 22, 0),
                                             location_city=u"kigali",
                                             location_country=u"rwanda",
                                             vrm_account=self.VRM,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=12,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=12321,
                                             has_genset=True,
                                             has_grid=True)
        self.test_rmc_account = Sesh_RMC_Account.objects.create(
            site=self.site, api_key='lcda5c15ae5cdsac464zx8f49asc16a')

        timestamp = timezone.localtime(timezone.now()) - timedelta(minutes=60)
        #print ""
        #print timestamp
        #timestamp = timezone.now()
        self.i.insert_point(self.site, 'status', 1, time=timestamp)

        sleep(1)
        self.test_user = Sesh_User.objects.create_user(username="******",
                                                       email="*****@*****.**",
                                                       password="******")
        #assign a user to the sites
        assign_perm("view_Sesh_Site", self.test_user, self.site)
Esempio n. 6
0
def get_historical_BoM(site_pk, start_at):
    """
        Get Historical Data from VRM to backfill any days
        """
    i = Influx()
    count = 0
    site = Sesh_Site.objects.get(pk=site_pk)
    site_id = site.vrm_site_id
    if not site_id:
        logger.info("skipping site %s has not vrm_site_id" % site)
        return 0
    vh_client = VictronHistoricalAPI(site.vrm_account.vrm_user_id,
                                     site.vrm_account.vrm_password)
    #site_id is a tuple
    #print "getting data for siteid %s starting at %s"%(site.vrm_site_id,start_at)
    data = vh_client.get_data(site_id, start_at)
    logger.debug("Importing data for site:%s" % site)
    for row in data:
        try:
            parsed_date = datetime.strptime(row.get('Date Time'),
                                            '%Y-%m-%d %X')
            date = time_utils.localize(parsed_date, tz=site.time_zone)
            data_point = BoM_Data_Point(
                site=site,
                time=date,
                soc=row.get('Battery State of Charge (System)', 0),
                battery_voltage=row.get('Battery voltage', 0),
                AC_input=row.get('Input power 1', 0),
                AC_output=row.get('Output power 1', 0),
                AC_Load_in=row.get('Input current phase 1', 0),
                AC_Load_out=row.get('Output current phase 1', 0),
                inverter_state=row.get('VE.Bus Error', ''),
                pv_production=row.get('PV - AC-coupled on input L1',
                                      0),  # IF null need to put in 0
                #TODO these need to be activated
                genset_state=0,
                relay_state=0,
            )
            date = row.get('Date Time')

            with transaction.atomic():
                data_point.save()
            send_to_influx(data_point,
                           site,
                           date,
                           to_exclude=['time', 'inverter_state', 'id'],
                           client=i,
                           send_status=False)

            count = count + 1
            if count % 100:
                logger.debug("imported  %s points" % count)
            #print "saved %s BoM data points"%count
        except IntegrityError, e:
            logger.warning("data point already exist %s" % e)
            pass
        except ValueError, e:
            logger.warning("Invalid values in data point dropping  %s" % (e))
            pass
class StatusTestCase(TestCase):
    @override_settings(DEBUG=True)
    def setUp(self):

        self.db_name = 'test_db'
        self.Client = Influx()
        try:
            self.Client.create_database(self.db_name)
        except:
            self.Client.delete_database(self.db_name)
            sleep(1)
            self.Client.create_database(self.db_name)
            pass

        self.vrm = VRM_Account.objects.create(vrm_user_id='*****@*****.**',vrm_password='******')
        self.location = Geoposition(-1.5,29.5)
        #create Sesh_Site
        self.site = Sesh_Site.objects.create(site_name='Mukuyu',
                                             comission_date = timezone.datetime(2014,12,12,12,12),
                                             location_city='kigali',
                                             location_country='Rwanda',
                                             vrm_account=self.vrm,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=24,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=1200,
                                             has_genset = True,
                                             has_grid=True)

        #creating Site_Weather_Data
        self.site_weather = Site_Weather_Data.objects.create(site = self.site,
                                                             date = timezone.datetime(2016, 10, 10, 10, 10),
                                                             temp = 0,
                                                             condition = "none",
                                                             cloud_cover = 1,
                                                             sunrise = timezone.now(),
                                                             sunset = timezone.now())

        Sesh_User.objects.create_superuser(username='******',password='******',email='*****@*****.**')

    def test_status(self):
        #insert_point in influx
        self.Client.insert_point(self.site,'soc',20.0)
        sites = Sesh_Site.objects.all()
        response = get_quick_status(sites)
        print 'The reponse is: %s' % response
        self.assertEqual(response[0]['battery'],'red')
        self.assertEqual(response[0]['weather'],'yellow')

    #testing get_site_measurements function
    def test_get_measurements(self):
        site_measurements = get_site_measurements(self.site)
        measurements = Site_Measurements.objects.all()
        self.assertEqual(len(measurements),1)
    def setUp(self):
        self.VRM = VRM_Account.objects.create(vrm_user_id='*****@*****.**',vrm_password="******")

        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
        except:
           self.i.delete_database(self._influx_db_name)
           sleep(1)
           self.i.create_database(self._influx_db_name)
           pass

        self.location = Geoposition(52.5,24.3)

        self.site = Sesh_Site.objects.create(
                        site_name='test',
                        comission_date=timezone.now(),
                        location_city='Kigali',
                        location_country='Rwanda',
                        position=self.location,
                        installed_kw=25,
                        system_voltage=45,
                        number_of_panels=45,
                        battery_bank_capacity=450,
                   )



        #create sesh rmc account
        self.test_rmc_account = Sesh_RMC_Account.objects.create(site=self.site,
                                                                api_key='lcda5c15ae5cdsac464zx8f49asc16a')



        #assign a user to the sites



        generate_auto_rules(self.site.pk)

        self.user = Sesh_User.objects.create_superuser(username='******',password='******',email='*****@*****.**')

        self.test_user = Sesh_User.objects.create_user(username='******', password='******', email='*****@*****.**')
    def setUp(self):
        """
        Initializing
        """
        # Setup Influx
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
        except:
            self.i.delete_database(self._influx_db_name)
            sleep(1)
            self.i.create_database(self._influx_db_name)
            pass

        self.client = Client()
        self.organisation = Sesh_Organisation.objects.create(
            name='test_organisation')

        self.user = Sesh_User.objects.create_user(
            username='******',
            password='******',
            email='*****@*****.**',
            organisation=self.organisation)
        self.location = Geoposition(1, 4)
        self.site = Sesh_Site.objects.create(site_name='test_site',
                                             organisation=self.organisation,
                                             comission_date=timezone.now(),
                                             location_city='',
                                             location_country='',
                                             installed_kw='13',
                                             position=self.location,
                                             system_voltage=24,
                                             number_of_panels=12,
                                             battery_bank_capacity=1200)

        self.status_card = Status_Card.objects.create(row1='AC_Load_in',
                                                      row2='AC_Load_out',
                                                      row3='AC_Voltage_in',
                                                      row4='AC_Voltage_out')

        self.site.status_card = self.status_card
        self.site.save()

        assign_perm('view_Sesh_Site', self.user, self.site)
    def setUp(self):

        self.VRM = VRM_Account.objects.create(
            vrm_user_id='*****@*****.**', vrm_password="******")
        # Setup Influx
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)
        self.no_points = 288
        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
        except Exception, e:
            print e
            self.i.delete_database(self._influx_db_name)
            sleep(1)
            self.i.create_database(self._influx_db_name)
            pass
def create_test_data(site, start=None, end="now", interval=5, units='minutes' ,random=True, val=50, db='test_db'):
        #TODO test weekly and monthly reports

        _influx_db_name = db
        i = Influx(database=_influx_db_name)
        data_point_dates = generate_date_array(start=start, end=end, interval=interval, units=units)
        voltage_in = 220
        voltage_out = 220
        soc = val
        R1 = val
        R2 = val
        R3 = val
        R4 = val
        R5 = val
        print "creating %s test data points"%len(data_point_dates)
        print "between %s and %s "%(data_point_dates[0],data_point_dates[len(data_point_dates)-1:])
        # Simulate Grid outage
        for time_val in data_point_dates:
                if random:
                    soc = get_random_int()
                    R1 = voltage_in * get_random_binary()
                    R2 = get_random_interval(100,500)
                    R3 = get_random_interval(22,28)
                    R4 = get_random_interval(100,500)
                    R5 = get_random_interval(100,500)

                dp = Data_Point.objects.create(
                                            site=site,
                                            soc = soc ,
                                            battery_voltage = R3,
                                            time=time_val,
                                            AC_Voltage_in = R1,
                                            AC_Voltage_out = voltage_out,
                                            AC_input = R4,
                                            AC_output = R5,
                                            AC_output_absolute = R2,
                                            AC_Load_in = R2,
                                            AC_Load_out = R4,
                                            pv_production = R5)
                # Also send ton influx
                dp_dict = model_to_dict(dp)
                dp_dict.pop('time')
                dp_dict.pop('inverter_state')
                dp_dict.pop('id')
                i.send_object_measurements(dp_dict,timestamp=time_val.isoformat(),tags={"site_name":site.site_name})
        return len(data_point_dates)
Esempio n. 12
0
def get_weather_data(days=7, historical=False):
    #TODO figure out way to get weather daya periodicall for forecast of 7 days
    #get all sites
    i = Influx()
    sites = Sesh_Site.objects.all()
    forecast_result = []

    for site in sites:
        forecast_client = ForecastAPI(settings.FORECAST_KEY,
                                      site.position.latitude,
                                      site.position.longitude)
        if historical:
            #Get weather dates for an extended historical interval
            now = datetime.now()
            days_to_get_from = now - timedelta(days)
            days_arr = time_utils.get_days_interval_delta(
                days_to_get_from, now)
            forecast_result = forecast_client.get_historical_day_minimal(
                days_arr)
        else:
            forecast_result = forecast_client.get_n_day_minimal_solar(days)
        #we need to update values in the database
        for day in forecast_result.keys():
            site_weather = Site_Weather_Data.objects.filter(site=site,
                                                            date=day)
            #if forecast already exists update it
            if site_weather:
                for point in site_weather:
                    point.condition = forecast_result[day]["stat"]
                    point.cloud_cover = forecast_result[day]["cloudcover"]
                    point.save()

                    send_to_influx(point,
                                   site,
                                   day,
                                   to_exclude=['date'],
                                   send_status=False)

            else:
                #else create a new object
                w_data = Site_Weather_Data(
                    site=site,
                    date=day,
                    temp=0,
                    condition=forecast_result[day]["stat"],
                    cloud_cover=forecast_result[day]["cloudcover"],
                    sunrise=forecast_result[day]["sunrise"],
                    sunset=forecast_result[day]["sunset"])

                w_data.save()
                send_to_influx(w_data,
                               site,
                               day,
                               to_exclude=['date'],
                               send_status=False)

    return "updated weather for %s" % sites
Esempio n. 13
0
    def setUp(self):

        self.db_name = 'test_db'
        self.Client = Influx()
        try:
            self.Client.create_database(self.db_name)
        except:
            self.Client.delete_database(self.db_name)
            sleep(1)
            self.Client.create_database(self.db_name)
            pass

        self.vrm = VRM_Account.objects.create(vrm_user_id='*****@*****.**',
                                              vrm_password='******')
        self.location = Geoposition(-1.5, 29.5)
        #create Sesh_Site
        self.site = Sesh_Site.objects.create(site_name='Mukuyu',
                                             comission_date=timezone.datetime(
                                                 2014, 12, 12, 12, 12),
                                             location_city='kigali',
                                             location_country='Rwanda',
                                             vrm_account=self.vrm,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=24,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=1200,
                                             has_genset=True,
                                             has_grid=True)

        #creating Site_Weather_Data
        self.site_weather = Site_Weather_Data.objects.create(
            site=self.site,
            date=timezone.datetime(2016, 10, 10, 10, 10),
            temp=0,
            condition="none",
            cloud_cover=1,
            sunrise=timezone.now(),
            sunset=timezone.now())

        Sesh_User.objects.create_superuser(username='******',
                                           password='******',
                                           email='*****@*****.**')
Esempio n. 14
0
def get_aggregate_data(site, measurement, bucket_size='1h', clause=None, toSum=True, operator='mean', start='now'):
    """
    Calculate aggregate values from Influx for provided measuruements
    """
    i = Influx()
    result = [0]

    clause_opts = {
            'negative': (lambda x : x[operator] < 0),
            'positive' : (lambda x: x[operator] > 0 ),
            'zero' : (lambda x: x[operator] == 0)}

    if clause and not clause in clause_opts.keys():
        logger.error("unkown clause provided %s allowed %s"%(clause,clause_opts.keys()))
        return result

    #get measurement values from influx
    if not toSum:
        operator = 'min'

    aggr_results = i.get_measurement_bucket(measurement, bucket_size, 'site_name', site.site_name, operator=operator, start=start)

    logger.debug("influx results %s "%(aggr_results))

    #we have mean values by the hour now aggregate them
    if aggr_results:
        agr_value = []
        if clause:
            #if we have a cluase filter to apply
            aggr_results = filter(clause_opts[clause],aggr_results)

        if toSum:
            to_sum_vals = map (lambda x: x[operator], aggr_results)
            agr_value.append(sum(to_sum_vals))
            result = agr_value
        else:
            result = aggr_results

        logger.debug("Aggregating %s %s agr:%s"%(measurement,aggr_results,agr_value))
    else:
        message = "No Values returned for aggregate. Check Influx Connection."
        logger.warning(message)

    return result
    def setUp(self):

        self.VRM = VRM_Account.objects.create(vrm_user_id='*****@*****.**',
                                              vrm_password="******")
        # Setup Influx
        self.i = Influx()
        self.i.create_database('test_db')
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)
        self.no_points = 288
        self.location = Geoposition(52.5, 24.3)

        self.site = Sesh_Site.objects.create(site_name=u"Test_aggregate",
                                             comission_date=timezone.datetime(
                                                 2015, 12, 11, 22, 0),
                                             location_city=u"kigali",
                                             location_country=u"rwanda",
                                             vrm_account=self.VRM,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=12,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=12321,
                                             has_genset=True,
                                             has_grid=True,
                                             has_pv=True,
                                             has_batteries=True)

        self.test_user = Sesh_User.objects.create_user("john doe",
                                                       "*****@*****.**",
                                                       "asdasd12345")
        #assign a user to the sites
        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
            self.no_points = create_test_data(self.site, val=None)
        except Exception, e:
            self.i.delete_database(self._influx_db_name)
            sleep(1)
            self.i.create_database(self._influx_db_name)
            print e
            pass
    def setUp(self):

        # Need this to create a Site
        self.VRM = VRM_Account.objects.create(vrm_user_id='*****@*****.**',vrm_password="******")

        # Setup Influx
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
        except:
           self.i.delete_database(self._influx_db_name)
           sleep(1)
           self.i.create_database(self._influx_db_name)
           pass

        # Setup Kapacitor
        self.kap = Kapacitor()
        self.template_id = 'test_template'
        self.task_id = 'test_task'
        self.dj_template_name = 'alert_template'


        self.dbrps = [{'db': self._influx_db_name, 'rp':'autogen' }]


        self.location = Geoposition(52.5,24.3)
        dt = timezone.make_aware(timezone.datetime(2015, 12, 11, 22, 0))

        self.site = Sesh_Site.objects.create(site_name=u"Test_aggregate",
                                             comission_date = dt,
                                             location_city = u"kigali",
                                             location_country=u"rwanda",
                                             vrm_account = self.VRM,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=12,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=12321,
                                             has_genset=True,
                                             has_grid=True)

        #self.no_points = create_test_data(self.site,
       #                                 start = self.site.comission_date,
       #                                 end = dt + timedelta( hours = 48),
       #                                 interval = 30,
       #                                 random = False)

        #create test user
        self.test_user = Sesh_User.objects.create_user("john doe","*****@*****.**","asdasd12345")
        #assign a user to the sites
        assign_perm("view_Sesh_Site",self.test_user,self.site)
Esempio n. 17
0
def send_to_influx(model_data,
                   site,
                   timestamp,
                   to_exclude=[],
                   client=None,
                   send_status=True):
    """
    Utility function to send data to influx
    """
    try:
        if client:
            i = client
        else:
            i = Influx()

        model_data_dict = model_to_dict(model_data)

        if to_exclude:
            # Remove any value we wish not to be submitted
            # Generally used with datetime measurement
            for val in to_exclude:
                #if to_exclude  in model_data_dict.keys():
                model_data_dict.pop(val)

        #Add our status will be used for RMC_Status
        if send_status:
            model_data_dict['status'] = 1.0

        status = i.send_object_measurements(model_data_dict,
                                            timestamp=timestamp,
                                            tags={
                                                "site_id": site.id,
                                                "site_name": site.site_name
                                            })
    except Exception, e:
        message = "Error sending to influx with exception %s in datapint %s" % (
            e, model_data_dict)
        handle_task_failure(message=message,
                            name='send_to_influx',
                            exception=e,
                            data=model_data)
Esempio n. 18
0
def export_csv_measurement_data(request):
    """
    Returns a csv of a given measurement a request

    """
    import csv
    context_dict = {}
    measurement = request.POST.get('measurement', '')
    start_time = request.POST.get('start-time', None)
    end_time = request.POST.get('end-time', None)
    site_name = request.POST.get('site-name', '')

    site = Sesh_Site.objects.filter(site_name=site_name).first()

    if request.method == 'POST':
        # Converting strings to date
        start_time = datetime.strptime(start_time, '%Y-%m-%d')
        end_time = datetime.strptime(end_time, '%Y-%m-%d')

        i = Influx()

        results = i.get_measurement_range(measurement, start_time, end_time, site=site)
 
        response = HttpResponse(content_type='text/csv')
        response['Content-Disposition'] = 'attachment; filename="%s.csv"' % ( site.site_name + '_' + measurement + '_sesh')
        writer = csv.DictWriter(response, ['site_name', 'time', 'value'])

        writer.writeheader()
        for result in results:
            del result['source']
            writer.writerow(result)

        return response

    i = Influx()
    user_sites = _get_user_sites(request)
    context_dict['sites'] = user_sites
    context_dict['measurements'] = i.get_measurements()
    context_dict['permitted'] = get_org_edit_permissions(request.user)
    context_dict['sites_stats'] = get_quick_status(user_sites)
    return render(request, 'seshdash/data_analysis/export-csv.html', context_dict)
Esempio n. 19
0
class DataAnalysisTestCase(TestCase):
    """
    Testing the functions that help in analysing and presenting
    the Data sesh collects
    """
    def setUp(self):
        """
        Initializing
        """
        self.client = Client()

        self.location = Geoposition(52.5, 24.3)

        # Setup Influx
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
        except:
            self.i.delete_database(self._influx_db_name)
            sleep(1)
            self.i.create_database(self._influx_db_name)
            pass

        self.site = Sesh_Site.objects.create(
            site_name=u"Test site",
            comission_date=timezone.datetime(2015, 12, 11, 22, 0),
            location_city=u"kigali",
            location_country=u"rwanda",
            installed_kw=123.0,
            position=self.location,
            system_voltage=24,
            number_of_panels=12,
            vrm_site_id=213,
            battery_bank_capacity=12321,
            has_genset=True,
            has_grid=True,
        )
        self.test_user = Sesh_User.objects.create_user(username="******",
                                                       email="*****@*****.**",
                                                       password="******")

    def test_get_csv_data(self):
        """
        Testing the functions that return csv files
        for a given measuremnt
        """
        data = {
            'measurement': 'battery_voltage',
            'start-time': '2015-01-01',
            'end-time': '2017-01-01',
            'site-name': self.site.site_name
        }

        self.client.login(username='******', password='******')
        response = self.client.post(
            reverse('export-csv-data', args=[self.site.id]), data)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.get('Content-Type'), 'text/csv')
Esempio n. 20
0
    def setUp(self):
        """
        Initializing
        """
        self.client = Client()

        self.location = Geoposition(52.5, 24.3)

        # Setup Influx
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
        except:
            self.i.delete_database(self._influx_db_name)
            sleep(1)
            self.i.create_database(self._influx_db_name)
            pass

        self.site = Sesh_Site.objects.create(
            site_name=u"Test site",
            comission_date=timezone.datetime(2015, 12, 11, 22, 0),
            location_city=u"kigali",
            location_country=u"rwanda",
            installed_kw=123.0,
            position=self.location,
            system_voltage=24,
            number_of_panels=12,
            vrm_site_id=213,
            battery_bank_capacity=12321,
            has_genset=True,
            has_grid=True,
        )
        self.test_user = Sesh_User.objects.create_user(username="******",
                                                       email="*****@*****.**",
                                                       password="******")
    def setUp(self):

        self.VRM = VRM_Account.objects.create(vrm_user_id='*****@*****.**',vrm_password="******")
        # Setup Influx
        self.i = Influx()
        self.i.create_database('test_db')
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)
        self.no_points = 288
        self.location = Geoposition(52.5,24.3)

        self.site = Sesh_Site.objects.create(site_name=u"Test_aggregate",
                                             comission_date=timezone.datetime(2015, 12, 11, 22, 0),
                                             location_city=u"kigali",
                                             location_country=u"rwanda",
                                             vrm_account = self.VRM,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=12,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=12321,
                                             has_genset=True,
                                             has_grid=True)

        self.test_user = Sesh_User.objects.create_user("john doe","*****@*****.**","asdasd12345")
        #assign a user to the sites
        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
            self.no_points = create_test_data(self.site)
        except Exception,e:
           self.i.delete_database(self._influx_db_name)
           sleep(1)
           self.i.create_database(self._influx_db_name)
           print e
           pass
class DataAnalysisTestCase(TestCase):
    """
    Testing the functions that help in analysing and presenting
    the Data sesh collects
    """

    def setUp(self):
        """
        Initializing
        """
        self.client = Client()

        self.location = Geoposition(52.5,24.3)

        # Setup Influx
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
        except:
           self.i.delete_database(self._influx_db_name)
           sleep(1)
           self.i.create_database(self._influx_db_name)
           pass

        self.site = Sesh_Site.objects.create(site_name=u"Test site",
                                             comission_date=timezone.datetime(2015, 12, 11, 22, 0),
                                             location_city=u"kigali",
                                             location_country=u"rwanda",
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=24,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=12321,
                                             has_genset=True,
                                             has_grid=True,
                                            )   
    
   
    def test_get_csv_data(self):
        """
        Testing the functions that return csv files
        for a given measuremnt
        """
        data = {
            'measurement': 'battery_voltage',
            'start-time': '2015-01-01',
            'end-time': '2017-01-01',
            'site-name': self.site.site_name
        }

        response = self.client.post(reverse('export-csv-data'), data) 
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.get('Content-Type'), 'text/csv')
    def setUp(self):

        self.VRM = VRM_Account.objects.create(vrm_user_id='*****@*****.**',vrm_password="******")
        # Setup Influx
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)
        self.no_points = 288
        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
        except Exception, e:
           print e
           self.i.delete_database(self._influx_db_name)
           sleep(1)
           self.i.create_database(self._influx_db_name)
           pass
    def setUp(self):
        """
        Initializing
        """
        # Setup Influx
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
        except:
           self.i.delete_database(self._influx_db_name)
           sleep(1)
           self.i.create_database(self._influx_db_name)
           pass

 
        self.client = Client()
        self.organisation = Sesh_Organisation.objects.create(name='test_organisation')
                                                             
        self.user = Sesh_User.objects.create_user(username='******',
                                             password='******', 
                                             email='*****@*****.**',
                                             organisation=self.organisation
                                             )
        self.location = Geoposition(1, 4)
        self.site = Sesh_Site.objects.create(site_name='test_site', 
                                             organisation=self.organisation,
                                             comission_date=timezone.now(),
                                             location_city='',
                                             location_country='',
                                             installed_kw='13', 
                                             position=self.location,
                                             system_voltage=24,
                                             number_of_panels=12,
                                             battery_bank_capacity=1200)


        self.status_card = Status_Card.objects.create(row1='AC_Load_in',
                                                      row2='AC_Load_out',
                                                      row3='AC_Voltage_in',
                                                      row4='AC_Voltage_out') 

        self.site.status_card = self.status_card
        self.site.save()

        assign_perm('view_Sesh_Site', self.user, self.site)
    def setUp(self):
        """
        Setting up the db
        """

        # Adding an influx database used by the quick_status_icons functions used on the pages to render
        self.influx_db_name = 'test_db'
        self.i = Influx(database=self.influx_db_name)

        try:
            self.i.create_database(self.influx_db_name)
        except:
            # Database already exist
            self.i.delete_database(self.influx_db_name)
            sleep(1)
            self.i.create_database(self.influx_db_name)
            pass

        self.client = Client()

        self.site = Sesh_Site.objects.create(
                        site_name='test',
                        comission_date=timezone.now(),
                        location_city='Kigali',
                        location_country='Rwanda',
                        position=Geoposition(12,1),
                        installed_kw=25,
                        system_voltage=45,
                        number_of_panels=45,
                        battery_bank_capacity=450,
                    )

        self.alert_rule = Alert_Rule.objects.create(
                        site=self.site,
                        check_field='battery_voltage',
                        operator='lt',
                        value='0',
                    )


        self.sesh_user = Sesh_User.objects.create_superuser(username='******',
                                                             email='*****@*****.**',
                                                             password='******',
                                                             on_call=False,
                                                             send_sms=False,
                                                             send_mail=False)     
class VRM_Import_TestCase(TestCase):
    @override_settings(DEBUG=True)
    def setUp(self):

        self.VRM = VRM_Account.objects.create(
            vrm_user_id='*****@*****.**', vrm_password="******")
        # Setup Influx
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)
        self.no_points = 288
        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
        except Exception, e:
            print e
            self.i.delete_database(self._influx_db_name)
            sleep(1)
            self.i.create_database(self._influx_db_name)
            pass

        self.VRM_API = VictronAPI(self.VRM.vrm_user_id, self.VRM.vrm_password)

        if self.VRM_API.IS_INITIALIZED:
            sites = self.VRM_API.SYSTEMS_IDS
            print sites
            if len(sites) > 1:
                self.vrm_site_id = sites[0][0]

        self.location = Geoposition(52.5, 24.3)
        self.now = timezone.now()
        self.start_date = self.now - timedelta(days=2)

        self.site = Sesh_Site.objects.create(site_name=u"Test_aggregate",
                                             comission_date=self.start_date,
                                             location_city=u"kigali",
                                             location_country=u"rwanda",
                                             vrm_account=self.VRM,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=12,
                                             number_of_panels=12,
                                             vrm_site_id=self.vrm_site_id,
                                             battery_bank_capacity=12321,
                                             has_genset=True,
                                             has_grid=True,
                                             has_pv=True)

        #create test user
        self.test_user = Sesh_User.objects.create_user(username="******",
                                                       email="*****@*****.**",
                                                       password="******")
        #assign a user to the sites
        assign_perm("view_Sesh_Site", self.test_user, self.site)
    def setUp(self):

        self.VRM = VRM_Account.objects.create(vrm_user_id='*****@*****.**',vrm_password="******")
        self.location = Geoposition(52.5,24.3)
        self._influx_db_name = 'test_db'

        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
        except:
           self.i.delete_database(self._influx_db_name)
           sleep(1)
           self.i.create_database(self._influx_db_name)
           pass



        self.site = Sesh_Site.objects.create(site_name=u"Test_aggregate",
                                             comission_date=timezone.datetime(2015, 12, 11, 22, 0),
                                             location_city=u"kigali",
                                             location_country=u"rwanda",
                                             vrm_account = self.VRM,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=12,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=12321,
                                             has_genset=True,
                                             has_grid=True)
        self.test_rmc_account = Sesh_RMC_Account.objects.create(site = self.site, api_key='lcda5c15ae5cdsac464zx8f49asc16a')

        timestamp = timezone.localtime(timezone.now()) - timedelta(minutes=60)
        #print ""
        #print timestamp
        #timestamp = timezone.now()
        self.i.insert_point(self.site,'status', 1, time = timestamp)

        sleep(1)
        self.test_user = Sesh_User.objects.create_user(username="******",email="*****@*****.**",password="******")
        #assign a user to the sites
        assign_perm("view_Sesh_Site",self.test_user,self.site)
class VRM_Import_TestCase(TestCase):
    @override_settings(DEBUG=True)
    def setUp(self):

        self.VRM = VRM_Account.objects.create(vrm_user_id='*****@*****.**',vrm_password="******")
        # Setup Influx
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)
        self.no_points = 288
        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
        except Exception, e:
           print e
           self.i.delete_database(self._influx_db_name)
           sleep(1)
           self.i.create_database(self._influx_db_name)
           pass

        self.VRM_API = VictronAPI(self.VRM.vrm_user_id, self.VRM.vrm_password)

        if self.VRM_API.IS_INITIALIZED:
           sites = self.VRM_API.SYSTEMS_IDS
           vrm_site_id = sites[2][0]
           print sites

        self.location = Geoposition(52.5,24.3)
        self.now = timezone.now()
        self.start_date = self.now - timedelta(days=2)

        self.site = Sesh_Site.objects.create(site_name=u"Test_aggregate",
                                             comission_date=self.start_date,
                                             location_city=u"kigali",
                                             location_country=u"rwanda",
                                             vrm_account = self.VRM,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=12,
                                             number_of_panels=12,
                                             vrm_site_id=vrm_site_id,
                                             battery_bank_capacity=12321,
                                             has_genset=True,
                                             has_grid=True)


        #create test user
        self.test_user = Sesh_User.objects.create_user(username="******",email="*****@*****.**",password="******")
        #assign a user to the sites
        assign_perm("view_Sesh_Site",self.test_user,self.site)
    def setUp(self):

        self.db_name = 'test_db'
        self.Client = Influx()
        try:
            self.Client.create_database(self.db_name)
        except:
            self.Client.delete_database(self.db_name)
            sleep(1)
            self.Client.create_database(self.db_name)
            pass

        self.vrm = VRM_Account.objects.create(vrm_user_id='*****@*****.**',vrm_password='******')
        self.location = Geoposition(-1.5,29.5)
        #create Sesh_Site
        self.site = Sesh_Site.objects.create(site_name='Mukuyu',
                                             comission_date = timezone.datetime(2014,12,12,12,12),
                                             location_city='kigali',
                                             location_country='Rwanda',
                                             vrm_account=self.vrm,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=24,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=1200,
                                             has_genset = True,
                                             has_grid=True)

        #creating Site_Weather_Data
        self.site_weather = Site_Weather_Data.objects.create(site = self.site,
                                                             date = timezone.datetime(2016, 10, 10, 10, 10),
                                                             temp = 0,
                                                             condition = "none",
                                                             cloud_cover = 1,
                                                             sunrise = timezone.now(),
                                                             sunset = timezone.now())

        Sesh_User.objects.create_superuser(username='******',password='******',email='*****@*****.**')
class KapacitorTestCase(TestCase):
    def setUp(self):

        # Need this to create a Site
        self.VRM = VRM_Account.objects.create(vrm_user_id='*****@*****.**',
                                              vrm_password="******")

        # Setup Influx
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
        except:
            self.i.delete_database(self._influx_db_name)
            sleep(1)
            self.i.create_database(self._influx_db_name)
            pass

        # Setup Kapacitor
        self.kap = Kapacitor()
        self.template_id = 'test_template'
        self.task_id = 'test_task'
        self.dj_template_name = 'alert_template'

        self.dbrps = [{'db': self._influx_db_name, 'rp': 'autogen'}]

        self.location = Geoposition(52.5, 24.3)
        dt = timezone.make_aware(timezone.datetime(2015, 12, 11, 22, 0))

        self.site = Sesh_Site.objects.create(site_name=u"Test_aggregate",
                                             comission_date=dt,
                                             location_city=u"kigali",
                                             location_country=u"rwanda",
                                             vrm_account=self.VRM,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=12,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=12321,
                                             has_genset=True,
                                             has_grid=True)

        #self.no_points = create_test_data(self.site,
        #                                 start = self.site.comission_date,
        #                                 end = dt + timedelta( hours = 48),
        #                                 interval = 30,
        #                                 random = False)

        #create test user
        self.test_user = Sesh_User.objects.create_user("john doe",
                                                       "*****@*****.**",
                                                       "asdasd12345")
        #assign a user to the sites
        assign_perm("view_Sesh_Site", self.test_user, self.site)

    def tearDown(self):
        self.i.delete_database(self._influx_db_name)
        self.kap.delete_template(self.template_id)
        self.kap.delete_task(self.task_id)
        pass

    @override_settings(INFLUX_DB='test_db')
    def test_template_creation(self):
        """
        Test creating template in kapacitor
        """

        temp_script = """
        // Which measurement to consume
        var measurement string
        // Optional where filter
        var where_filter = lambda: TRUE
        // Optional list of group by dimensions
        var groups = [*]
        // Which field to process
        var field string
        // Warning criteria, has access to 'mean' field
        var warn lambda
        // Critical criteria, has access to 'mean' field
        var crit lambda
        // How much data to window
        var window = 5m
        // The slack channel for alerts
        var slack_channel = '#alerts'

        stream
            |from()
                .measurement(measurement)
                .where(where_filter)
                .groupBy(groups)
            |window()
                .period(window)
                .every(window)
            |mean(field)
            |alert()
                 .warn(warn)
                 .crit(crit)
                 .slack()
                 .channel(slack_channel)

        """
        temp_id = self.template_id
        temp_type = 'stream'

        # Create template
        temp = self.kap.create_template(temp_id, temp_type, temp_script)
        self.assertTrue(temp.has_key('vars'))

        # Verify template creation
        temp_res = self.kap.get_template(temp_id)
        self.assertTrue(temp_res.has_key('vars'))

        # List template
        temp_res = self.kap.list_templates()
        self.assertTrue(temp_res.has_key('templates'))

        # Update Template

        temp_script = """
        // Which measurement to consume
        var measurement = 'cpu'
        // Optional where filter
        var where_filter = lambda: TRUE
        // Optional list of group by dimensions
        var groups = [*]
        // Which field to process
        var field string
        // Warning criteria, has access to 'mean' field
        var warn lambda
        // Critical criteria, has access to 'mean' field
        var crit lambda
        // How much data to window
        var window = 5m
        // The slack channel for alerts
        var slack_channel = '#alerts'

        stream
            |from()
                .measurement(measurement)
                .where(where_filter)
                .groupBy(groups)
            |window()
                .period(window)
                .every(window)
            |mean(field)
            |alert()
                 .warn(warn)
                 .crit(crit)
                 .slack()
                 .channel(slack_channel)

        """
        temp_res = self.kap.update_template(temp_id, temp_script)

        # Delete template
        self.kap.delete_template(self.template_id)

    def test_task_creation(self):
        """
        Create a task and check if it actually causes an alert to trigger
        """

        temp_script = """
                    stream
                        |from()
                            .measurement('cpu')
                        |alert()
                            .crit(lambda: "value" <  70)
                            .log('/tmp/alerts.log')
                        """

        temp_id = self.template_id
        task_id = self.task_id

        # Create task
        temp = self.kap.create_task(task_id,
                                    dbrps=self.dbrps,
                                    script=temp_script,
                                    task_type='stream')
        self.assertEqual(temp['status'], 'enabled')
        sleep(20)

        for i in reversed(range(0, 5)):
            sleep(1)
            dp_dict = {'cpu': i}
            self.i.send_object_measurements(dp_dict,
                                            tags={"site_name": "test_site"},
                                            database=self._influx_db_name)
        temp = self.kap.get_task(task_id)

        self.assertGreater(
            temp['stats']['node-stats']['alert2']['alerts_triggered'], 0)

    def test_task_dj_template(self):
        """
        test task creation with django templates
        """

        template = get_template('seshdash/kapacitor_tasks/%s.tick' %
                                self.dj_template_name)

        alert_id = self.task_id
        alert_info = {
            'field': 'cpu',
            'where_filter_lambda': 'lambda: TRUE',
            'error_lambda': 'lambda: \"value\" < 30',
            'time_window': '5m',
            'slack_channel': '#alerts'
        }

        rendered_alert = template.render(alert_info)
        result = self.kap.create_task(alert_id,
                                      dbrps=self.dbrps,
                                      script=rendered_alert)
        self.assertEquals(result['status'], 'enabled')
class StatusCardTestCase(TestCase):
    """
    Testing the status card
    """
    def setUp(self):
        """
        Initializing
        """
        # Setup Influx
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
        except:
           self.i.delete_database(self._influx_db_name)
           sleep(1)
           self.i.create_database(self._influx_db_name)
           pass

 
        self.client = Client()
        self.organisation = Sesh_Organisation.objects.create(name='test_organisation')
                                                             
        self.user = Sesh_User.objects.create_user(username='******',
                                             password='******', 
                                             email='*****@*****.**',
                                             organisation=self.organisation
                                             )
        self.location = Geoposition(1, 4)
        self.site = Sesh_Site.objects.create(site_name='test_site', 
                                             organisation=self.organisation,
                                             comission_date=timezone.now(),
                                             location_city='',
                                             location_country='',
                                             installed_kw='13', 
                                             position=self.location,
                                             system_voltage=24,
                                             number_of_panels=12,
                                             battery_bank_capacity=1200)


        self.status_card = Status_Card.objects.create(row1='AC_Load_in',
                                                      row2='AC_Load_out',
                                                      row3='AC_Voltage_in',
                                                      row4='AC_Voltage_out') 

        self.site.status_card = self.status_card
        self.site.save()

        assign_perm('view_Sesh_Site', self.user, self.site)

    def test_edit_status_card(self):
        """
        Testing the editing of a status card
        """
        data = {
            'row1': 'battery_voltage',
            'row2': 'AC_Load_in',
            'row3': 'AC_Voltage_in',
            'row4': 'AC_Voltage_out'
        }

        # Testing for a user that is not an admin
        self.client.login(username='******', password='******')
        response = self.client.post(reverse('edit_status_card', args=[self.site.id]), data)
        self.assertEqual(response.status_code, 400)


        # Testing if the changes have been applied
        self.user.is_org_admin = True
        self.user.save()
        response = self.client.post(reverse('edit_status_card', args=[self.site.id]), data)
        self.assertRedirects(response, reverse('index', args=[self.site.id]))

        # Testing if the changes have been applied
        status_card = Status_Card.objects.filter(id=self.status_card.id).first()
        self.assertEqual(status_card.row1, 'battery_voltage')
Esempio n. 32
0
class StatusTestCase(TestCase):
    @override_settings(DEBUG=True)
    def setUp(self):

        self.db_name = 'test_db'
        self.Client = Influx()
        try:
            self.Client.create_database(self.db_name)
        except:
            self.Client.delete_database(self.db_name)
            sleep(1)
            self.Client.create_database(self.db_name)
            pass

        self.vrm = VRM_Account.objects.create(vrm_user_id='*****@*****.**',
                                              vrm_password='******')
        self.location = Geoposition(-1.5, 29.5)
        #create Sesh_Site
        self.site = Sesh_Site.objects.create(site_name='Mukuyu',
                                             comission_date=timezone.datetime(
                                                 2014, 12, 12, 12, 12),
                                             location_city='kigali',
                                             location_country='Rwanda',
                                             vrm_account=self.vrm,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=24,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=1200,
                                             has_genset=True,
                                             has_grid=True)

        #creating Site_Weather_Data
        self.site_weather = Site_Weather_Data.objects.create(
            site=self.site,
            date=timezone.datetime(2016, 10, 10, 10, 10),
            temp=0,
            condition="none",
            cloud_cover=1,
            sunrise=timezone.now(),
            sunset=timezone.now())

        Sesh_User.objects.create_superuser(username='******',
                                           password='******',
                                           email='*****@*****.**')

    def test_status(self):
        #insert_point in influx
        self.Client.insert_point(self.site, 'soc', 20.0)
        sites = Sesh_Site.objects.all()
        response = get_quick_status(sites)
        print 'The reponse is: %s' % response
        self.assertEqual(response[0]['battery'], 'red')
        self.assertEqual(response[0]['weather'], 'yellow')

    #testing get_site_measurements function
    def test_get_measurements(self):
        site_measurements = get_site_measurements(self.site)
        measurements = Site_Measurements.objects.all()
        self.assertEqual(len(measurements), 1)
class InfluxTestCase(TestCase):
    def setUp(self):

        self.VRM = VRM_Account.objects.create(vrm_user_id='*****@*****.**',
                                              vrm_password="******")
        # Setup Influx
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
        except:
            self.i.delete_database(self._influx_db_name)
            sleep(1)
            self.i.create_database(self._influx_db_name)
            pass

        self.location = Geoposition(52.5, 24.3)
        dt = timezone.make_aware(timezone.datetime(2015, 12, 11, 22, 0))

        self.site = Sesh_Site.objects.create(site_name=u"Test_aggregate",
                                             comission_date=dt,
                                             location_city=u"kigali",
                                             location_country=u"rwanda",
                                             vrm_account=self.VRM,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=12,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=12321,
                                             has_genset=True,
                                             has_grid=True)

        self.no_points = create_test_data(self.site,
                                          start=self.site.comission_date,
                                          end=dt + timedelta(hours=48),
                                          interval=30)
        #create test user
        self.test_user = Sesh_User.objects.create_user(username="******",
                                                       email="*****@*****.**",
                                                       password="******")
        #assign a user to the sites
        assign_perm("view_Sesh_Site", self.test_user, self.site)

    def tearDown(self):
        self.i.delete_database(self._influx_db_name)
        pass

    @override_settings(INFLUX_DB='test_db')
    def test_influx_aggr_query(self):
        """
        Test all the DP were created in MYSQL and INFLUX
        """

        mean = self.i.get_measurement_bucket('pv_production',
                                             '1h',
                                             'site_name',
                                             self.site.site_name,
                                             operator='mean',
                                             start=self.site.comission_date)

        self.assertEqual(25, len(mean))
        self.assertEqual(50, mean[0]['mean'])
class InfluxTestCase(TestCase):

    def setUp(self):

        self.VRM = VRM_Account.objects.create(vrm_user_id='*****@*****.**',vrm_password="******")
        # Setup Influx
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
        except:
           self.i.delete_database(self._influx_db_name)
           sleep(1)
           self.i.create_database(self._influx_db_name)
           pass

        self.location = Geoposition(52.5,24.3)
        dt = timezone.make_aware(timezone.datetime(2015, 12, 11, 22, 0))

        self.site = Sesh_Site.objects.create(site_name=u"Test_aggregate",
                                             comission_date = dt,
                                             location_city = u"kigali",
                                             location_country=u"rwanda",
                                             vrm_account = self.VRM,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=12,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=12321,
                                             has_genset=True,
                                             has_grid=True)

        self.no_points = create_test_data(self.site,
                                        start = self.site.comission_date,
                                        end = dt + timedelta( hours = 48),
                                        interval = 30,
                                        random = False)
        #create test user
        self.test_user = Sesh_User.objects.create_user(username="******",email="*****@*****.**",password="******")
        #assign a user to the sites
        assign_perm("view_Sesh_Site",self.test_user,self.site)

    def tearDown(self):
        self.i.delete_database(self._influx_db_name)
        pass

    @override_settings(INFLUX_DB='test_db')
    def test_influx_aggr_query(self):
        """
        Test all the DP were created in MYSQL and INFLUX
        """

        mean = self.i.get_measurement_bucket('pv_production',
                                            '1h',
                                            'site_name',
                                            self.site.site_name,
                                            operator='mean',
                                            start=self.site.comission_date)

        self.assertEqual(25,len(mean))
        self.assertEqual(50,mean[0]['mean'])
class AddTestCase(TestCase):
    @override_settings(DEBUG=True)
    def setUp(self):
        self.VRM = VRM_Account.objects.create(vrm_user_id='*****@*****.**',vrm_password="******")

        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
        except:
           self.i.delete_database(self._influx_db_name)
           sleep(1)
           self.i.create_database(self._influx_db_name)
           pass

        self.location = Geoposition(52.5,24.3)

        self.site = Sesh_Site.objects.create(
                        site_name='test',
                        comission_date=timezone.now(),
                        location_city='Kigali',
                        location_country='Rwanda',
                        position=self.location,
                        installed_kw=25,
                        system_voltage=45,
                        number_of_panels=45,
                        battery_bank_capacity=450,
                   )



        #create sesh rmc account
        self.test_rmc_account = Sesh_RMC_Account.objects.create(site=self.site,
                                                                api_key='lcda5c15ae5cdsac464zx8f49asc16a')



        #assign a user to the sites



        generate_auto_rules(self.site.pk)

        self.user = Sesh_User.objects.create_superuser(username='******',password='******',email='*****@*****.**')

        self.test_user = Sesh_User.objects.create_user(username='******', password='******', email='*****@*****.**')


    def tearDown(self):
        self.i.delete_database(self._influx_db_name)
        pass

    def test_edit_site(self):
        c = Client()
        val = c.login(username = "******",password = "******")

        print 'the login value is: %s' % val

        data= {'site_name':u'kibuye',
               'comission_date':datetime(2015,12,11,22,0),
               'location_city':u'kigali',
               'location_country':u'rwanda',
               'time_zone':'Africa/Kigali',
               'position_0':36,
               'position_1':42,
               'installed_kw':2,
               'system_voltage':4,
               'number_of_panels':100,
               'site_Id':2,
               'battery_bank_capacity':1200,
               'api_key': 'testing',
               'api_key_numeric': '123456'}

        response = c.post('/edit_site/' + str(self.site.id) , data)

        # Testing if the site has been edited succesfully
        self.assertEqual(response.status_code, 200)
        self.assertEqual(Sesh_Site.objects.first().site_name, 'kibuye')


        # Checking for another unauthorized user
        val = c.login(username="******",password="******")
        response = c.post('/edit_site/' + str(self.site.id), data)
        self.assertEqual(response.status_code, 403)
Esempio n. 36
0
    def setUp(self):
        """
        Setting up the db
        """

        # Adding an influx database used by the quick_status_icons functions used on the pages to render
        self.influx_db_name = 'test_db'
        self.i = Influx(database=self.influx_db_name)

        try:
            self.i.create_database(self.influx_db_name)
        except:
            # Database already exist
            self.i.delete_database(self.influx_db_name)
            sleep(1)
            self.i.create_database(self.influx_db_name)
            pass

        self.client = Client()

        self.organisation = Sesh_Organisation.objects.create(
            name="test_org", slack_token="secret")

        self.site = Sesh_Site.objects.create(
            site_name='test_site1',
            organisation=self.organisation,
            comission_date=timezone.now(),
            location_city='Kigali',
            location_country='Rwanda',
            position=Geoposition(12, 1),
            installed_kw=25,
            system_voltage=45,
            number_of_panels=45,
            battery_bank_capacity=450,
        )

        self.site2 = Sesh_Site.objects.create(
            site_name='test_site2',
            organisation=self.organisation,
            comission_date=timezone.now(),
            location_city='Kigali',
            location_country='Rwanda',
            position=Geoposition(12, 1),
            installed_kw=25,
            system_voltage=45,
            number_of_panels=45,
            battery_bank_capacity=450,
        )

        self.alert_rule = Alert_Rule.objects.create(
            site=self.site,
            check_field='battery_voltage',
            operator='lt',
            value='0',
        )

        self.sesh_user = Sesh_User.objects.create_superuser(
            username='******',
            email='*****@*****.**',
            password='******',
            on_call=False,
            send_sms=False,
            send_mail=False)
class AlertTestCase(TestCase):

    @override_settings(DEBUG=True)
    def setUp(self):

        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
        except:
           self.i.delete_database(self._influx_db_name)
           sleep(1)
           self.i.create_database(self._influx_db_name)
           pass



        self.VRM = VRM_Account.objects.create(vrm_user_id='*****@*****.**',vrm_password="******")

        self.location = Geoposition(52.5,24.3)

        self.site = Sesh_Site.objects.create(site_name=u"Test site",
                                             comission_date=timezone.datetime(2015, 12, 11, 22, 0),
                                             location_city=u"kigali",
                                             location_country=u"rwanda",
                                             vrm_account = self.VRM,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=24,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=12321,
                                             has_genset=True,
                                             has_grid=True)


        # Creating permissions for group
        content_type = ContentType.objects.get_for_model(Sesh_Site)
        self.permission = Permission.objects.create(codename='can_manage_sesh_site',
                                                    name='Can add Sesh Site',
                                                    content_type=content_type)

        self.data_point = Data_Point.objects.create(site=self.site,
                                                    soc=10,
                                                    battery_voltage=20,
                                                    time=timezone.now(),
                                                    AC_input=0.0,
                                                    AC_output=15.0,
                                                    AC_Load_in=0.0,
                                                    AC_Load_out=-0.7)
        #create sesh rmc account
        self.test_rmc_account = Sesh_RMC_Account(site=self.site, api_key='lcda5c15ae5cdsac464zx8f49asc16a')
        self.test_rmc_account.save()

        #create rmc status
        self.test_rmc_status = RMC_status.objects.create(site=self.site,
                                                        ip_address='127.0.0.1',
                                                        minutes_last_contact=100,
                                                        signal_strength=27,
                                                        data_sent_24h=12,
                                                        time=datetime.now())
        self.test_rmc_status.save()


        #create influx datapoint
        self.influx_data_point = insert_point(self.site, 'battery_voltage', 10)

        #create test user
        self.test_user = Sesh_User.objects.create_user(username="******",
                                                  email="*****@*****.**",
                                                  password="******",
                                                  phone_number='250786688713',
                                                  on_call=True,
                                                  send_mail=True,
                                                  send_sms=True)



        # Creating test group


        self.test_organisation = Sesh_Organisation.objects.create(name='test_organisation',
                                                                  send_slack=True,
                                                                  slack_token=settings.SLACK_TEST_KEY)

        # Creating test channels
        self.test_channels = Slack_Channel.objects.create(organisation=self.test_organisation,
                                                          name='test_alerts_channel',
                                                          is_alert_channel=True)

        #assign a user to the sites


        assign_perm("view_Sesh_Site",self.test_user,self.site)

        generate_auto_rules(self.site.pk)

        influx_rule = Alert_Rule.objects.create(check_field='battery_voltage',
                                                operator='lt',
                                                site=self.site,
                                                value=20)

        alert.alert_generator()


        self.new_influx_data_point = insert_point(self.site, 'battery_voltage',  24)
        sleep(2) # Added sleep to wait for sometime until the point is written to the db

        # Create data point that will silence alert
        self.new_data_point = Data_Point.objects.create(site=self.site,
                                                    soc=50,
                                                    battery_voltage=24,
                                                    time=timezone.now(),
                                                    AC_input=0.0,
                                                    AC_output=15.0,
                                                    AC_Load_in=0.0,
                                                    AC_Load_out=-0.7)

        self.new_rmc_status = RMC_status.objects.create(site=self.site,
                                                        ip_address='127.0.0.1',
                                                        minutes_last_contact=1,
                                                        signal_strength=27,
                                                        data_sent_24h=12,
                                                        time=datetime.now())


        self.client = Client()


    @override_settings(DEBUG=True)
    def test_alert_fires_and_reported(self):
        """
        Test if the alerts objects are fired and saved.
        and also if the alert is notified to mails, sms and slack.
        """
        alerts_created = Sesh_Alert.objects.filter(site=self.site)
        self.assertEqual(alerts_created.count(),4)

        alerts_mail_sent = alerts_created.filter(emailSent=True)
        self.assertEqual(alerts_mail_sent.count(),4)


        # test_sent_sms, sms are not sent where debug is false
        alert_sms_sent = Sesh_Alert.objects.filter(smsSent=True)
        if settings.DEBUG:
            self.assertEqual(alert_sms_sent.count(), 0)
        else:
            self.assertEqual(alert_sms_sent.count(), 1)

        #test_slack 
        alert_slack_sent = Sesh_Alert.objects.filter(slackSent=True)
        self.assertEqual(alert_slack_sent.count(), 4)


    def test_alert_display(self):
        """
        Test the display of alerts to the user
        """
        self.client.login(username='******', password='******')

        response = self.client.post('/get-alert-data/',{'alertId':'1'})
        self.assertEqual(response.status_code, 200)


        #test_get_alerts_notifications
        response = self.client.post('/notifications/',{})
        self.assertEqual(response.status_code, 200)

        # Test the display of the status card data
        response = self.client.post('/get-latest-bom-data/',{"siteId": 1})
        self.assertEqual(response.status_code, 200)



    def test_alert_silencing(self):
        """
        Testing the silencing of alerts
        """
        self.client.login(username="******", password="******")
        response = self.client.post('/silence-alert/',{'alert_id':'1'})

        self.assertEqual(response.status_code, 200)
        silenced_alert = Sesh_Alert.objects.filter(id=1).first()
        self.assertEqual(silenced_alert.isSilence, True)

        
            


    @override_settings(DEBUG=True)
    def test_alert_autosilencing(self):
        """
        Test if alerts are silencing correctly
        """
        alert.alert_status_check()
        unsilenced_alerts = Sesh_Alert.objects.filter(isSilence=False)
        self.assertEqual(unsilenced_alerts.count(), 0)
Esempio n. 38
0
    def setUp(self):

        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
        except:
           self.i.delete_database(self._influx_db_name)
           sleep(1)
           self.i.create_database(self._influx_db_name)
           pass



        self.VRM = VRM_Account.objects.create(vrm_user_id='*****@*****.**',vrm_password="******")

        self.location = Geoposition(52.5,24.3)

        self.site = Sesh_Site.objects.create(site_name=u"Test site",
                                             comission_date=timezone.datetime(2015, 12, 11, 22, 0),
                                             location_city=u"kigali",
                                             location_country=u"rwanda",
                                             vrm_account = self.VRM,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=24,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=12321,
                                             has_genset=True,
                                             has_grid=True)


        # Creating permissions for group
        content_type = ContentType.objects.get_for_model(Sesh_Site)
        self.permission = Permission.objects.create(codename='can_manage_sesh_site',
                                                    name='Can add Sesh Site',
                                                    content_type=content_type)

        self.data_point = Data_Point.objects.create(site=self.site,
                                                    soc=10,
                                                    battery_voltage=20,
                                                    time=timezone.now(),
                                                    AC_input=0.0,
                                                    AC_output=15.0,
                                                    AC_Load_in=0.0,
                                                    AC_Load_out=-0.7)
        #create sesh rmc account
        self.test_rmc_account = Sesh_RMC_Account(site=self.site, api_key='lcda5c15ae5cdsac464zx8f49asc16a')
        self.test_rmc_account.save()

        #create rmc status
        self.test_rmc_status = RMC_status.objects.create(site=self.site,
                                                        ip_address='127.0.0.1',
                                                        minutes_last_contact=100,
                                                        signal_strength=27,
                                                        data_sent_24h=12,
                                                        time=datetime.now())
        self.test_rmc_status.save()


        #create influx datapoint
        self.influx_data_point = insert_point(self.site, 'battery_voltage', 10)

        #create test user
        self.test_user = Sesh_User.objects.create_user(username="******",
                                                  email="*****@*****.**",
                                                  password="******",
                                                  phone_number='250786688713',
                                                  on_call=True,
                                                  send_mail=True,
                                                  send_sms=True)



        # Creating test group


        self.test_organisation = Sesh_Organisation.objects.create(name='test_organisation',
                                                                  send_slack=True,
                                                                  slack_token=settings.SLACK_TEST_KEY)

        # Creating test channels
        self.test_channels = Slack_Channel.objects.create(organisation=self.test_organisation,
                                                          name='test_alerts_channel',
                                                          is_alert_channel=True)

        #assign a user to the sites


        assign_perm("view_Sesh_Site",self.test_user,self.site)

        generate_auto_rules(self.site.pk)

        influx_rule = Alert_Rule.objects.create(check_field='battery_voltage',
                                                operator='lt',
                                                site=self.site,
                                                value=20)

        alert.alert_generator()


        self.new_influx_data_point = insert_point(self.site, 'battery_voltage',  24)
        sleep(2) # Added sleep to wait for sometime until the point is written to the db

        # Create data point that will silence alert
        self.new_data_point = Data_Point.objects.create(site=self.site,
                                                    soc=50,
                                                    battery_voltage=24,
                                                    time=timezone.now(),
                                                    AC_input=0.0,
                                                    AC_output=15.0,
                                                    AC_Load_in=0.0,
                                                    AC_Load_out=-0.7)

        self.new_rmc_status = RMC_status.objects.create(site=self.site,
                                                        ip_address='127.0.0.1',
                                                        minutes_last_contact=1,
                                                        signal_strength=27,
                                                        data_sent_24h=12,
                                                        time=datetime.now())


        self.client = Client()
Esempio n. 39
0
class AlertTestCase(TestCase):

    @override_settings(DEBUG=True)
    def setUp(self):

        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
        except:
           self.i.delete_database(self._influx_db_name)
           sleep(1)
           self.i.create_database(self._influx_db_name)
           pass



        self.VRM = VRM_Account.objects.create(vrm_user_id='*****@*****.**',vrm_password="******")

        self.location = Geoposition(52.5,24.3)

        self.site = Sesh_Site.objects.create(site_name=u"Test site",
                                             comission_date=timezone.datetime(2015, 12, 11, 22, 0),
                                             location_city=u"kigali",
                                             location_country=u"rwanda",
                                             vrm_account = self.VRM,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=24,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=12321,
                                             has_genset=True,
                                             has_grid=True)


        # Creating permissions for group
        content_type = ContentType.objects.get_for_model(Sesh_Site)
        self.permission = Permission.objects.create(codename='can_manage_sesh_site',
                                                    name='Can add Sesh Site',
                                                    content_type=content_type)

        self.data_point = Data_Point.objects.create(site=self.site,
                                                    soc=10,
                                                    battery_voltage=20,
                                                    time=timezone.now(),
                                                    AC_input=0.0,
                                                    AC_output=15.0,
                                                    AC_Load_in=0.0,
                                                    AC_Load_out=-0.7)
        #create sesh rmc account
        self.test_rmc_account = Sesh_RMC_Account(site=self.site, api_key='lcda5c15ae5cdsac464zx8f49asc16a')
        self.test_rmc_account.save()

        #create rmc status
        self.test_rmc_status = RMC_status.objects.create(site=self.site,
                                                        ip_address='127.0.0.1',
                                                        minutes_last_contact=100,
                                                        signal_strength=27,
                                                        data_sent_24h=12,
                                                        time=datetime.now())
        self.test_rmc_status.save()


        #create influx datapoint
        self.influx_data_point = insert_point(self.site, 'battery_voltage', 10)

        #create test user
        self.test_user = Sesh_User.objects.create_user(username="******",
                                                  email="*****@*****.**",
                                                  password="******",
                                                  phone_number='250786688713',
                                                  on_call=True,
                                                  send_mail=True,
                                                  send_sms=True)



        # Creating test group


        self.test_organisation = Sesh_Organisation.objects.create(name='test_organisation',
                                                                  send_slack=True,
                                                                  slack_token=settings.SLACK_TEST_KEY)

        # Creating test channels
        self.test_channels = Slack_Channel.objects.create(organisation=self.test_organisation,
                                                          name='test_alerts_channel',
                                                          is_alert_channel=True)

        #assign a user to the sites


        assign_perm("view_Sesh_Site",self.test_user,self.site)

        generate_auto_rules(self.site.pk)

        influx_rule = Alert_Rule.objects.create(check_field='battery_voltage',
                                                operator='lt',
                                                site=self.site,
                                                value=20)

        alert.alert_generator()


        self.new_influx_data_point = insert_point(self.site, 'battery_voltage',  24)
        sleep(2) # Added sleep to wait for sometime until the point is written to the db

        # Create data point that will silence alert
        self.new_data_point = Data_Point.objects.create(site=self.site,
                                                    soc=50,
                                                    battery_voltage=24,
                                                    time=timezone.now(),
                                                    AC_input=0.0,
                                                    AC_output=15.0,
                                                    AC_Load_in=0.0,
                                                    AC_Load_out=-0.7)

        self.new_rmc_status = RMC_status.objects.create(site=self.site,
                                                        ip_address='127.0.0.1',
                                                        minutes_last_contact=1,
                                                        signal_strength=27,
                                                        data_sent_24h=12,
                                                        time=datetime.now())


        self.client = Client()


    @override_settings(DEBUG=True)
    def test_alert_fires_and_reported(self):
        """
        Test if the alerts objects are fired and saved.
        and also if the alert is notified to mails, sms and slack.
        """
        alerts_created = Sesh_Alert.objects.filter(site=self.site)
        self.assertEqual(alerts_created.count(),4)

        alerts_mail_sent = alerts_created.filter(emailSent=True)
        self.assertEqual(alerts_mail_sent.count(),4)


        # test_sent_sms, sms are not sent where debug is false
        alert_sms_sent = Sesh_Alert.objects.filter(smsSent=True)
        if settings.DEBUG:
            self.assertEqual(alert_sms_sent.count(), 0)
        else:
            self.assertEqual(alert_sms_sent.count(), 1)

        #test_slack
        alert_slack_sent = Sesh_Alert.objects.filter(slackSent=True)
        self.assertEqual(alert_slack_sent.count(), 4)


    def test_alert_display(self):
        """
        Test the display of alerts to the user
        """
        self.client.login(username='******', password='******')
        alerts = Sesh_Alert.objects.all()
        print "#####################"
        print alerts
        for alert in alerts:
            print alert.id
        response = self.client.post('/get-alert-data/',{'alertId':'1'})
        self.assertEqual(response.status_code, 200)


        #test_get_alerts_notifications
        response = self.client.post('/notifications/',{})
        self.assertEqual(response.status_code, 200)

        # Test the display of the status card data
        response = self.client.post('/get-latest-bom-data/',{"siteId": 1})
        self.assertEqual(response.status_code, 200)



    def test_alert_silencing(self):
        """
        Testing the silencing of alerts
        """
        self.client.login(username="******", password="******")
        response = self.client.post('/silence-alert/',{'alert_id':'1'})

        self.assertEqual(response.status_code, 200)
        silenced_alert = Sesh_Alert.objects.filter(id=1).first()
        self.assertEqual(silenced_alert.isSilence, True)



    @override_settings(DEBUG=True)
    def test_alert_autosilencing(self):
        """
        Test if alerts are silencing correctly
        """
        alert.alert_status_check()
        unsilenced_alerts = Sesh_Alert.objects.filter(isSilence=False)
        self.assertEqual(unsilenced_alerts.count(), 0)
Esempio n. 40
0
def graphs(request): 
    """
    Returns json, containing data that is used in data analysis graphs
    """
    results = []

    # Getting values from Post request
    time = request.GET.get('time', '') # This is the time range it has to be: 24h, 7d or 30d
    choices = request.GET.getlist('choice[]')
    active_id = request.GET.get('active_site_id', None)
    start_time = request.GET.get('start_time', datetime.now() - timedelta(weeks=1))
    end_time = request.GET.get('end_time', datetime.now())
    resolution = request.GET.get('resolution', '1h')
    current_site = Sesh_Site.objects.filter(id=active_id).first()
    

    if (not current_site) or current_site.organisation != request.user.organisation:
        return HttpResponseBadRequest("Invalid site id, No site was found for the given site id")

    time_delta_dict = {'24h':{'hours':24},
                       '7d': {'days':7},
                       '30d':{'days':30},
                    }

    time_bucket_dict = {'24h':'1h',
                        '7d':'1d',
                        '30d':'5d',
                    }

    # processing post request values to be used in the influx queries
    for choice in choices:
        choice_dict = {}
        choice_dict['measurement'] = choice
        #time_delta = time_delta_dict[time]
        #time_bucket= time_bucket_dict[time]
        choice_dict['si_unit'] = get_measurement_unit(choice)
       
        # Gettting the values of the given element
        client = Influx()

        query_results = client.get_measurement_range_bucket(choice, start_time, end_time, group_by=resolution)

        
        #looping into values
        choice_data = []
        for result in query_results:
            choice_data_dict = {}
            result_time = parser.parse(result['time'])
            result_time = get_epoch_from_datetime(result_time)
            if result['mean'] is not None:
                result_value = round(result['mean'], 2)
            else:
                result_value = 0
            choice_data_dict['time'] = result_time
            choice_data_dict['value'] = result_value
            choice_data.append([result_time, result_value])


        choice_dict['data'] = choice_data
        results.append(choice_dict)

    return HttpResponse(json.dumps(results))
class StatusCardTestCase(TestCase):
    """
    Testing the status card
    """
    def setUp(self):
        """
        Initializing
        """
        # Setup Influx
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
        except:
            self.i.delete_database(self._influx_db_name)
            sleep(1)
            self.i.create_database(self._influx_db_name)
            pass

        self.client = Client()
        self.organisation = Sesh_Organisation.objects.create(
            name='test_organisation')

        self.user = Sesh_User.objects.create_user(
            username='******',
            password='******',
            email='*****@*****.**',
            organisation=self.organisation)
        self.location = Geoposition(1, 4)
        self.site = Sesh_Site.objects.create(site_name='test_site',
                                             organisation=self.organisation,
                                             comission_date=timezone.now(),
                                             location_city='',
                                             location_country='',
                                             installed_kw='13',
                                             position=self.location,
                                             system_voltage=24,
                                             number_of_panels=12,
                                             battery_bank_capacity=1200)

        self.status_card = Status_Card.objects.create(row1='AC_Load_in',
                                                      row2='AC_Load_out',
                                                      row3='AC_Voltage_in',
                                                      row4='AC_Voltage_out')

        self.site.status_card = self.status_card
        self.site.save()

        assign_perm('view_Sesh_Site', self.user, self.site)

    def test_edit_status_card(self):
        """
        Testing the editing of a status card
        """
        data = {
            'row1': 'battery_voltage',
            'row2': 'AC_Load_in',
            'row3': 'AC_Voltage_in',
            'row4': 'AC_Voltage_out'
        }

        # Testing for a user that is not an admin
        self.client.login(username='******', password='******')
        response = self.client.post(
            reverse('edit_status_card', args=[self.site.id]), data)
        self.assertEqual(response.status_code, 400)

        # Testing if the changes have been applied
        self.user.is_org_admin = True
        self.user.save()
        response = self.client.post(
            reverse('edit_status_card', args=[self.site.id]), data)
        self.assertRedirects(response, reverse('index', args=[self.site.id]))

        # Testing if the changes have been applied
        status_card = Status_Card.objects.filter(
            id=self.status_card.id).first()
        self.assertEqual(status_card.row1, 'battery_voltage')

        # Test negative test case
        data = {
            'row1': 'battery_voltage',
            'row2': 'AC_Load_in',
            'row3': 'AC_Voltage_in',
            'row4': 'Wrong_setting'
        }

        insert_point(self.site, 'battery_voltage', 2, db='test_db')
        insert_point(self.site, 'AC_Load_in', 12, db='test_db')
        insert_point(self.site, 'AC_Voltage_in', 12, db='test_db')
        insert_point(self.site, 'Wrong_setting', 12, db='test_db')

        # Testing with admin user
        self.client.login(username='******', password='******')
        self.user.is_org_admin = True
        self.user.save()
        response = self.client.post(
            reverse('edit_status_card', args=[self.site.id]), data)
        self.assertRedirects(response, reverse('index', args=[self.site.id]))

        # Testing if the changes have been applied
        status_card = Status_Card.objects.filter(
            id=self.status_card.id).first()
        self.assertEqual(status_card.row4, 'Wrong_setting')

        # call get-latest-bom-data
        response = self.client.post('/get-latest-bom-data',
                                    {'siteId': self.site.id})
        self.assertGreater(len(response.content), 1)
        self.assertEqual(response.status_code, 200)
Esempio n. 42
0
class TestSettings(TestCase):
    """
    Testing the alert rules options in the
    settings
    """
    def setUp(self):
        """
        Setting up the db
        """

        # Adding an influx database used by the quick_status_icons functions used on the pages to render
        self.influx_db_name = 'test_db'
        self.i = Influx(database=self.influx_db_name)

        try:
            self.i.create_database(self.influx_db_name)
        except:
            # Database already exist
            self.i.delete_database(self.influx_db_name)
            sleep(1)
            self.i.create_database(self.influx_db_name)
            pass

        self.client = Client()

        self.organisation = Sesh_Organisation.objects.create(
            name="test_org", slack_token="secret")

        self.site = Sesh_Site.objects.create(
            site_name='test_site1',
            organisation=self.organisation,
            comission_date=timezone.now(),
            location_city='Kigali',
            location_country='Rwanda',
            position=Geoposition(12, 1),
            installed_kw=25,
            system_voltage=45,
            number_of_panels=45,
            battery_bank_capacity=450,
        )

        self.site2 = Sesh_Site.objects.create(
            site_name='test_site2',
            organisation=self.organisation,
            comission_date=timezone.now(),
            location_city='Kigali',
            location_country='Rwanda',
            position=Geoposition(12, 1),
            installed_kw=25,
            system_voltage=45,
            number_of_panels=45,
            battery_bank_capacity=450,
        )

        self.alert_rule = Alert_Rule.objects.create(
            site=self.site,
            check_field='battery_voltage',
            operator='lt',
            value='0',
        )

        self.sesh_user = Sesh_User.objects.create_superuser(
            username='******',
            email='*****@*****.**',
            password='******',
            on_call=False,
            send_sms=False,
            send_mail=False)

    def test_settings_alert_rules_page(self):
        """
        Testing if the page is
        being rednered correctl
        """
        self.client.login(username='******', password='******')
        response = self.client.get(reverse('manage_alert_rules'))
        self.assertEqual(response.status_code, 200)

    def test_add_alert_rule(self):
        """
        Testing the addition of alert rules
        """
        data = {
            'check_field': 'battery_voltage',
            'operator': 'lt',
            'value': '20',
        }

        self.client.login(username='******', password='******')

        # Testing the creation of an alert rulses
        response = self.client.post(
            reverse('site_alert_rules', args=[self.site.id]), data)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(Alert_Rule.objects.filter(site=self.site).count(), 2)

    def test_edit_alert_rule(self):
        """
        Testing the editing of an alert rule
        """
        data = {
            'check_field': 'battery_voltage',
            'operator': 'gt',
            'value': '100',
        }

        self.client.login(username='******', password='******')

        response = self.client.post(
            reverse('edit_alert_rule', args=[self.alert_rule.id]), data)

        # Testing the success of editing an alert rule
        self.assertEqual(response.status_code, 302)
        alert_rule = Alert_Rule.objects.filter(id=self.alert_rule.id).first(
        )  # Need to query the database again to get updated version
        self.assertEqual(alert_rule.operator, 'gt')
        self.assertEqual(alert_rule.value, 100)

    def test_delete_alert_rule(self):
        """
        Testing the deletion of alert rules
        """
        self.client.login(username='******', password='******')

        response = self.client.get(
            reverse('delete_alert_rule', args=[self.alert_rule.id]))

        # Testing the success of deleting an alert rule
        self.assertEqual(response.status_code, 302)
        self.assertEqual(Alert_Rule.objects.all().count(), 0)

    def test_user_notifications(self):
        """
        Testing the user notifications
        """
        self.client.login(username='******', password='******')

        data = {
            u'form-MAX_NUM_FORMS': [u'1000'],
            u'form-INITIAL_FORMS': [u'3'],
            u'form-TOTAL_FORMS': [u'1'],
            u'form-0-send_sms': [u'on'],
            u'form-0-on_call': [u'on'],
            u'form-0-id': [u'%s' % self.sesh_user.id],
            u'form-MIN_NUM_FORMS': [u'0']
        }

        response = self.client.post(reverse('user_notifications'), data)

        # Asserting the correctness of the response and the result of the post
        self.assertEqual(response.status_code, 302)
        user = Sesh_User.objects.filter(id=self.sesh_user.id).first()
        self.assertEqual(user.on_call, True)
        self.assertEqual(user.send_sms, True)

    def test_delete_site(self):
        """
        Testing the view that deletes a site
        """
        self.client.login(username='******', password='******')

        # Users that are not admin of the organisation must not be allowed to delete a site
        response = self.client.get(reverse('delete_site', args=[self.site.id]))
        self.assertEqual(response.status_code, 403)

        # testing if the site is deleted when the user is admin of the organisation
        self.sesh_user.organisation = self.organisation
        self.sesh_user.is_org_admin = True
        self.sesh_user.save()
        response = self.client.get(reverse('delete_site',
                                           args=[self.site2.id]))
        self.assertRedirects(response, reverse('index'))
        sites = Sesh_Site.objects.all()
        self.assertEqual(sites.first().site_name, 'test_site1')
class TestSettings(TestCase):
    """
    Testing the alert rules options in the 
    settings
    """

    def setUp(self):
        """
        Setting up the db
        """

        # Adding an influx database used by the quick_status_icons functions used on the pages to render
        self.influx_db_name = 'test_db'
        self.i = Influx(database=self.influx_db_name)

        try:
            self.i.create_database(self.influx_db_name)
        except:
            # Database already exist
            self.i.delete_database(self.influx_db_name)
            sleep(1)
            self.i.create_database(self.influx_db_name)
            pass

        self.client = Client()

        self.site = Sesh_Site.objects.create(
                        site_name='test',
                        comission_date=timezone.now(),
                        location_city='Kigali',
                        location_country='Rwanda',
                        position=Geoposition(12,1),
                        installed_kw=25,
                        system_voltage=45,
                        number_of_panels=45,
                        battery_bank_capacity=450,
                    )

        self.alert_rule = Alert_Rule.objects.create(
                        site=self.site,
                        check_field='battery_voltage',
                        operator='lt',
                        value='0',
                    )


        self.sesh_user = Sesh_User.objects.create_superuser(username='******',
                                                             email='*****@*****.**',
                                                             password='******',
                                                             on_call=False,
                                                             send_sms=False,
                                                             send_mail=False)     
           
 
    def test_settings_alert_rules_page(self):
        """
        Testing if the page is 
        being rednered correctl
        """
        self.client.login(username='******', password='******')
        response = self.client.get(reverse('manage_alert_rules'))
        self.assertEqual(response.status_code, 200)

    def test_add_alert_rule(self):
        """
        Testing the addition of alert rules
        """
        data = {
                'check_field': 'battery_voltage',
                'operator': 'lt',
                'value': '20',
            }


        self.client.login(username='******', password='******')
        
        # Testing the creation of an alert rulses
        response = self.client.post(reverse('site_alert_rules', args=[self.site.id]), data)
        self.assertEqual(response.status_code, 302)
        self.assertEqual(Alert_Rule.objects.filter(site=self.site).count(), 2)

    def test_edit_alert_rule(self):
        """
        Testing the editing of an alert rule 
        """
        data = {
                'check_field': 'battery_voltage',
                'operator': 'gt',
                'value': '100',
            }

        self.client.login(username='******', password='******')

        response = self.client.post(reverse('edit_alert_rule', args=[self.alert_rule.id]), data)

        # Testing the success of editing an alert rule
        self.assertEqual(response.status_code, 302)
        alert_rule = Alert_Rule.objects.filter(id=self.alert_rule.id).first() # Need to query the database again to get updated version
        self.assertEqual(alert_rule.operator, 'gt')
        self.assertEqual(alert_rule.value, 100)


    def test_delete_alert_rule(self):
        """
        Testing the deletion of alert rules
        """
        self.client.login(username='******', password='******')

        response = self.client.get(reverse('delete_alert_rule', args=[self.alert_rule.id]))

        # Testing the success of deleting an alert rule
        self.assertEqual(response.status_code, 302)
        self.assertEqual(Alert_Rule.objects.all().count(), 0)

    def test_user_notifications(self):
        """
        Testing the user notifications
        """
        self.client.login(username='******', password='******')
                
        print 'The id for the sesh user is: %s ' % self.sesh_user.id

        data = {
            u'form-MAX_NUM_FORMS': [u'1000'],
            u'form-INITIAL_FORMS': [u'3'],
            u'form-TOTAL_FORMS': [u'1'],
            u'form-0-send_sms': [u'on'],
            u'form-0-on_call': [u'on'],
            u'form-0-id': [u'%s' % self.sesh_user.id],
            u'form-MIN_NUM_FORMS': [u'0']
        }
 
        response = self.client.post(reverse('user_notifications'), data)
                 
        # Asserting the correctness of the response and the result of the post
        self.assertEqual(response.status_code, 302)
        user = Sesh_User.objects.filter(id=self.sesh_user.id).first()
        self.assertEqual(user.on_call, True)
        self.assertEqual(user.send_sms, True)
    def setUp(self):

        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
        except:
           self.i.delete_database(self._influx_db_name)
           sleep(1)
           self.i.create_database(self._influx_db_name)
           pass



        self.VRM = VRM_Account.objects.create(vrm_user_id='*****@*****.**',vrm_password="******")

        self.location = Geoposition(52.5,24.3)

        self.site = Sesh_Site.objects.create(site_name=u"Test site",
                                             comission_date=timezone.datetime(2015, 12, 11, 22, 0),
                                             location_city=u"kigali",
                                             location_country=u"rwanda",
                                             vrm_account = self.VRM,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=24,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=12321,
                                             has_genset=True,
                                             has_grid=True)


        # Creating permissions for group
        content_type = ContentType.objects.get_for_model(Sesh_Site)
        self.permission = Permission.objects.create(codename='can_manage_sesh_site',
                                                    name='Can add Sesh Site',
                                                    content_type=content_type)

        self.data_point = Data_Point.objects.create(site=self.site,
                                                    soc=10,
                                                    battery_voltage=20,
                                                    time=timezone.now(),
                                                    AC_input=0.0,
                                                    AC_output=15.0,
                                                    AC_Load_in=0.0,
                                                    AC_Load_out=-0.7)
        #create sesh rmc account
        self.test_rmc_account = Sesh_RMC_Account(site=self.site, api_key='lcda5c15ae5cdsac464zx8f49asc16a')
        self.test_rmc_account.save()

        #create rmc status
        self.test_rmc_status = RMC_status.objects.create(site=self.site,
                                                        ip_address='127.0.0.1',
                                                        minutes_last_contact=100,
                                                        signal_strength=27,
                                                        data_sent_24h=12,
                                                        time=datetime.now())
        self.test_rmc_status.save()


        #create influx datapoint
        self.influx_data_point = insert_point(self.site, 'battery_voltage', 10)

        #create test user
        self.test_user = Sesh_User.objects.create_user(username="******",
                                                  email="*****@*****.**",
                                                  password="******",
                                                  phone_number='250786688713',
                                                  on_call=True,
                                                  send_mail=True,
                                                  send_sms=True)



        # Creating test group


        self.test_organisation = Sesh_Organisation.objects.create(name='test_organisation',
                                                                  send_slack=True,
                                                                  slack_token=settings.SLACK_TEST_KEY)

        # Creating test channels
        self.test_channels = Slack_Channel.objects.create(organisation=self.test_organisation,
                                                          name='test_alerts_channel',
                                                          is_alert_channel=True)

        #assign a user to the sites


        assign_perm("view_Sesh_Site",self.test_user,self.site)

        generate_auto_rules(self.site.pk)

        influx_rule = Alert_Rule.objects.create(check_field='battery_voltage',
                                                operator='lt',
                                                site=self.site,
                                                value=20)

        alert.alert_generator()


        self.new_influx_data_point = insert_point(self.site, 'battery_voltage',  24)
        sleep(2) # Added sleep to wait for sometime until the point is written to the db

        # Create data point that will silence alert
        self.new_data_point = Data_Point.objects.create(site=self.site,
                                                    soc=50,
                                                    battery_voltage=24,
                                                    time=timezone.now(),
                                                    AC_input=0.0,
                                                    AC_output=15.0,
                                                    AC_Load_in=0.0,
                                                    AC_Load_out=-0.7)

        self.new_rmc_status = RMC_status.objects.create(site=self.site,
                                                        ip_address='127.0.0.1',
                                                        minutes_last_contact=1,
                                                        signal_strength=27,
                                                        data_sent_24h=12,
                                                        time=datetime.now())


        self.client = Client()
Esempio n. 45
0
def create_test_data(site, start=None, end="now", interval=5, units='minutes' , val=50, db='test_db', data={}):
        """
        data = {'R1':[0,0,0,..],'R2':[0,0,123,12,...]...} will not generate date but use fixed data set
        if val is not set random data will be generated if data is not existing
        """


        _influx_db_name = db
        i = Influx(database=_influx_db_name)
        data_point_dates = generate_date_array(start=start, end=end, interval=interval, units=units)
        voltage_in = 220
        voltage_out = 220
        soc = val
        R1 = val
        R2 = val
        R3 = val
        R4 = val
        R5 = val
        count = 0
        print "creating %s test data points"%len(data_point_dates)
        print "between %s and %s "%(data_point_dates[0],data_point_dates[len(data_point_dates)-1:])
        # Simulate Grid outage
        for time_val in data_point_dates:
            if not val:
                try:
                    soc = data.get('soc',[])[count]
                except:
                    soc = get_random_int()
                try:
                    R1 = data.get('R1',[])[count]
                except:
                    R1 = voltage_in * get_random_binary()

                try:
                    R2 = data.get('R2',[])[count]
                except:
                    R2 = get_random_interval(100,500)

                try:
                    R3 = data.get('R3',[])[count]
                except:
                    R3 = get_random_interval(22,28)

                try:
                    R4 = data.get('R4',[])[count]
                except:
                    R4 = get_random_interval(100,500)
                try:
                    R5 = data.get('R5',[])[count]
                except:
                    R5 = get_random_interval(100,500)


            dp = Data_Point.objects.create(
                                            site=site,
                                            soc = soc ,
                                            battery_voltage = R3,
                                            time=time_val,
                                            AC_Voltage_in = R1,
                                            AC_Voltage_out = voltage_out,
                                            AC_input = R4,
                                            AC_output = R5,
                                            AC_output_absolute = R2,
                                            AC_Load_in = R2,
                                            AC_Load_out = R4,
                                            pv_production = R5)
            # Also send ton influx
            dp_dict = model_to_dict(dp)
            dp_dict.pop('time')
            dp_dict.pop('inverter_state')
            dp_dict.pop('id')
            i.send_object_measurements(dp_dict,timestamp=time_val.isoformat(),tags={"site_name":site.site_name})
            count = count + 1
            # Count number of outages


        return len(data_point_dates)
class KapacitorTestCase(TestCase):

    def setUp(self):

        # Need this to create a Site
        self.VRM = VRM_Account.objects.create(vrm_user_id='*****@*****.**',vrm_password="******")

        # Setup Influx
        self._influx_db_name = 'test_db'
        self.i = Influx(database=self._influx_db_name)

        try:
            self.i.create_database(self._influx_db_name)
            #Generate random data  points for 24h
        except:
           self.i.delete_database(self._influx_db_name)
           sleep(1)
           self.i.create_database(self._influx_db_name)
           pass

        # Setup Kapacitor
        self.kap = Kapacitor()
        self.template_id = 'test_template'
        self.task_id = 'test_task'
        self.dj_template_name = 'alert_template'


        self.dbrps = [{'db': self._influx_db_name, 'rp':'autogen' }]


        self.location = Geoposition(52.5,24.3)
        dt = timezone.make_aware(timezone.datetime(2015, 12, 11, 22, 0))

        self.site = Sesh_Site.objects.create(site_name=u"Test_aggregate",
                                             comission_date = dt,
                                             location_city = u"kigali",
                                             location_country=u"rwanda",
                                             vrm_account = self.VRM,
                                             installed_kw=123.0,
                                             position=self.location,
                                             system_voltage=12,
                                             number_of_panels=12,
                                             vrm_site_id=213,
                                             battery_bank_capacity=12321,
                                             has_genset=True,
                                             has_grid=True)

        #self.no_points = create_test_data(self.site,
       #                                 start = self.site.comission_date,
       #                                 end = dt + timedelta( hours = 48),
       #                                 interval = 30,
       #                                 random = False)

        #create test user
        self.test_user = Sesh_User.objects.create_user("john doe","*****@*****.**","asdasd12345")
        #assign a user to the sites
        assign_perm("view_Sesh_Site",self.test_user,self.site)

    def tearDown(self):
        self.i.delete_database(self._influx_db_name)
        self.kap.delete_template(self.template_id)
        self.kap.delete_task(self.task_id)
        pass

    @override_settings(INFLUX_DB='test_db')
    def test_template_creation(self):
        """
        Test creating template in kapacitor
        """


        temp_script = """
        // Which measurement to consume
        var measurement string
        // Optional where filter
        var where_filter = lambda: TRUE
        // Optional list of group by dimensions
        var groups = [*]
        // Which field to process
        var field string
        // Warning criteria, has access to 'mean' field
        var warn lambda
        // Critical criteria, has access to 'mean' field
        var crit lambda
        // How much data to window
        var window = 5m
        // The slack channel for alerts
        var slack_channel = '#alerts'

        stream
            |from()
                .measurement(measurement)
                .where(where_filter)
                .groupBy(groups)
            |window()
                .period(window)
                .every(window)
            |mean(field)
            |alert()
                 .warn(warn)
                 .crit(crit)
                 .slack()
                 .channel(slack_channel)

        """
        temp_id = self.template_id
        temp_type = 'stream'

        # Create template
        temp = self.kap.create_template(temp_id, temp_type, temp_script)
        self.assertTrue(temp.has_key('vars'))

        # Verify template creation
        temp_res = self.kap.get_template(temp_id)
        self.assertTrue(temp_res.has_key('vars'))

        # List template
        temp_res = self.kap.list_templates()
        self.assertTrue(temp_res.has_key('templates'))

        # Update Template

        temp_script = """
        // Which measurement to consume
        var measurement = 'cpu'
        // Optional where filter
        var where_filter = lambda: TRUE
        // Optional list of group by dimensions
        var groups = [*]
        // Which field to process
        var field string
        // Warning criteria, has access to 'mean' field
        var warn lambda
        // Critical criteria, has access to 'mean' field
        var crit lambda
        // How much data to window
        var window = 5m
        // The slack channel for alerts
        var slack_channel = '#alerts'

        stream
            |from()
                .measurement(measurement)
                .where(where_filter)
                .groupBy(groups)
            |window()
                .period(window)
                .every(window)
            |mean(field)
            |alert()
                 .warn(warn)
                 .crit(crit)
                 .slack()
                 .channel(slack_channel)

        """
        temp_res = self.kap.update_template(temp_id, temp_script)

        # Delete template
        self.kap.delete_template(self.template_id)

    def test_task_creation(self):
        """
        Create a task and check if it actually causes an alert to trigger
        """


        temp_script = """
                    stream
                        |from()
                            .measurement('cpu')
                        |alert()
                            .crit(lambda: "value" <  70)
                            .log('/tmp/alerts.log')
                        """

        temp_id = self.template_id
        task_id = self.task_id


        # Create task
        temp = self.kap.create_task(task_id, dbrps=self.dbrps, script=temp_script, task_type='stream')
        self.assertEqual(temp['status'],'enabled')
        sleep(20)

        for i in reversed(range(0,5)):
            sleep(1)
            dp_dict = {'cpu': i}
            self.i.send_object_measurements(dp_dict, tags={"site_name":"test_site"}, database=self._influx_db_name)
        temp = self.kap.get_task(task_id)

        self.assertGreater(temp['stats']['node-stats']['alert2']['alerts_triggered'], 0)

    def test_task_dj_template(self):
        """
        test task creation with django templates
        """

        template =  get_template('seshdash/kapacitor_tasks/%s.tick'%self.dj_template_name)

        alert_id = self.task_id
        alert_info ={
                'field': 'cpu',
                'where_filter_lambda' : 'lambda: TRUE',
                'error_lambda' : 'lambda: \"value\" < 30',
                'time_window' : '5m',
                'slack_channel' : '#alerts'
                }


        rendered_alert = template.render(alert_info)
        result = self.kap.create_task(alert_id, dbrps= self.dbrps, script=rendered_alert)
        self.assertEquals(result['status'], 'enabled')