<commit_before>#!/usr/bin/env python
'''
uframe endpoints

'''
__author__ = '<NAME>'

from flask import jsonify, request, current_app, url_for
from ooiservices.app.uframe import uframe as api
from ooiservices.app import db
from ooiservices.app.main.authentication import auth
from ooiservices.app.models import Array, PlatformDeployment, InstrumentDeployment
from ooiservices.app.models import Stream, StreamParameter, Organization, Instrumentname
from ooiservices.app.models import Annotation
import requests
import json

from ooiservices.app.uframe.data import gen_data

import json
import datetime
import math

#ignore list for data fields
FIELDS_IGNORE = ["stream_name","quality_flag"]
#time minus ()
COSMO_CONSTANT = 2208988800


def _get_data_type(data_input):
    '''
    gets the data type in a format google charts understands
    '''
    if data_input is float or data_input is int:
        return "number"
    elif data_input is str or data_input is unicode:
        return "string"
    else:
        return "unknown"

def _get_annotation(instrument_name, stream_name):
    annotations = Annotation.query.filter_by(instrument_name=instrument_name, stream_name=stream_name).all()
    return [annotation.to_json() for annotation in annotations]

def _get_col_outline(data,pref_timestamp,inital_fields,hasAnnotation,annotations,fields_have_annotation):
    '''
    gets the column outline for the google chart response, figures out what annotations are required where...
    '''
    data_fields = []
    data_field_list= []
    #used to cound the fields, used for annotations
    field_count = 1
    #loop and generate inital col dict
    for field in inital_fields:
        if field == pref_timestamp:
            d_type = "datetime"                
        elif field in FIELDS_IGNORE or str(field).endswith('_timestamp'):         
            continue
        else:
            #map the data types to the correct data type for google charts       
            d_type = _get_data_type(type(data[0][field]))

        data_field_list.append(field)
        data_fields.append({"id": "",
                            "label": field,
                            "type":  d_type})

        if hasAnnotation:
            #only append annotation fields for fields that have annotations, makes resp smaller if possible
            if field in fields_have_annotation :
                data_field_list.append("annotation")    
                data_field_list.append("annotationText")
                data_fields.append({"label":"title"+str(field_count),"type":  "string" , "role":"annotation" , "origin_field":field}) 
                data_fields.append({"label":"text" +str(field_count),"type":  "string" , "role":"annotationText", "origin_field":field}) 

                field_count +=1
     
    return data_fields,data_field_list

def _get_annotation_content(annotation_field, pref_timestamp, annotations_list, d, data_field):    
    '''
    creates the annotation content for a given field
    '''
    #right now x and y are timeseries data
    for an in annotations_list:        
        if an['field_x'] == pref_timestamp or an['field_y'] == data_field:
            # and and y value
            if int(d['fixed_dt']) == int(an['pos_x']):
                if annotation_field == "annotation":
                    return {"v":an["title"]}
                elif annotation_field == "annotationText":
                    return {"v":an['comment']}

    #return nothing
    return {"v":None,"f":None}


@api.route('/get_data/<string:instrument>/<string:stream>',methods=['GET'])
def get_data(stream, instrument):
    #get data from uframe
    #-------------------
    # m@c: 02/01/2015
    #uframe url to get stream data from instrument:
    # /sensor/user/inv/<stream_name>/<instrument_name>
    #
    #-------------------
    #TODO: create better error handler if uframe is not online/responding
    try:
        data = requests.get(current_app.config['UFRAME_URL'] + '/sensor/user/inv/' + stream + '/' + instrument)
        data = data.json()
    except:
        raise

    hasAnnotation = False
    hasStartDate = False
    hasEndDate = False
    #this is needed as some plots dont have annotations
    if 'annotation' in request.args:
        #generate annotation plot
        if request.args['annotation'] == "true":
            hasAnnotation = True    

    if 'startdate' in request.args:
        request.args['startdate']            
        hasStartDate = True<commit_msg>forgot to init the annotations for things that dont require annotations<commit_after>#!/usr/bin/env python
'''
uframe endpoints

'''
__author__ = '<NAME>'

from flask import jsonify, request, current_app, url_for
from ooiservices.app.uframe import uframe as api
from ooiservices.app import db
from ooiservices.app.main.authentication import auth
from ooiservices.app.models import Array, PlatformDeployment, InstrumentDeployment
from ooiservices.app.models import Stream, StreamParameter, Organization, Instrumentname
from ooiservices.app.models import Annotation
import requests
import json

from ooiservices.app.uframe.data import gen_data

import json
import datetime
import math

#ignore list for data fields
FIELDS_IGNORE = ["stream_name","quality_flag"]
#time minus ()
COSMO_CONSTANT = 2208988800


def _get_data_type(data_input):
    '''
    gets the data type in a format google charts understands
    '''
    if data_input is float or data_input is int:
        return "number"
    elif data_input is str or data_input is unicode:
        return "string"
    else:
        return "unknown"

def _get_annotation(instrument_name, stream_name):
    annotations = Annotation.query.filter_by(instrument_name=instrument_name, stream_name=stream_name).all()
    return [annotation.to_json() for annotation in annotations]

def _get_col_outline(data,pref_timestamp,inital_fields,hasAnnotation,annotations,fields_have_annotation):
    '''
    gets the column outline for the google chart response, figures out what annotations are required where...
    '''
    data_fields = []
    data_field_list= []
    #used to cound the fields, used for annotations
    field_count = 1
    #loop and generate inital col dict
    for field in inital_fields:
        if field == pref_timestamp:
            d_type = "datetime"                
        elif field in FIELDS_IGNORE or str(field).endswith('_timestamp'):         
            continue
        else:
            #map the data types to the correct data type for google charts       
            d_type = _get_data_type(type(data[0][field]))

        data_field_list.append(field)
        data_fields.append({"id": "",
                            "label": field,
                            "type":  d_type})

        if hasAnnotation:
            #only append annotation fields for fields that have annotations, makes resp smaller if possible
            if field in fields_have_annotation :
                data_field_list.append("annotation")    
                data_field_list.append("annotationText")
                data_fields.append({"label":"title"+str(field_count),"type":  "string" , "role":"annotation" , "origin_field":field}) 
                data_fields.append({"label":"text" +str(field_count),"type":  "string" , "role":"annotationText", "origin_field":field}) 

                field_count +=1
     
    return data_fields,data_field_list

def _get_annotation_content(annotation_field, pref_timestamp, annotations_list, d, data_field):    
    '''
    creates the annotation content for a given field
    '''
    #right now x and y are timeseries data
    for an in annotations_list:        
        if an['field_x'] == pref_timestamp or an['field_y'] == data_field:
            # and and y value
            if int(d['fixed_dt']) == int(an['pos_x']):
                if annotation_field == "annotation":
                    return {"v":an["title"]}
                elif annotation_field == "annotationText":
                    return {"v":an['comment']}

    #return nothing
    return {"v":None,"f":None}


@api.route('/get_data/<string:instrument>/<string:stream>',methods=['GET'])
def get_data(stream, instrument):
    #get data from uframe
    #-------------------
    # m@c: 02/01/2015
    #uframe url to get stream data from instrument:
    # /sensor/user/inv/<stream_name>/<instrument_name>
    #
    #-------------------
    #TODO: create better error handler if uframe is not online/responding
    try:
        data = requests.get(current_app.config['UFRAME_URL'] + '/sensor/user/inv/' + stream + '/' + instrument)
        data = data.json()
    except:
        raise

    annotations = []
    hasAnnotation = False
    hasStartDate = False
    hasEndDate = False
    #this is needed as some plots dont have annotations
    if 'annotation' in request.args:
        #generate annotation plot
        if request.args['annotation'] == "true":
            hasAnnotation = True    

    if 'startdate' in request.args:
        request.args['startdate']            
        hasStartDate = True
{'fraction_non_alphanumeric': 0.10425649530127142, 'fraction_numerical': 0.004643449419568822, 'mean_word_length': 2.7380165289256198, 'pattern_counts': {'":': 36, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5698339', 'n_tokens_mistral': 2580, 'n_tokens_neox': 2482, 'n_words': 710}