<commit_before>from time import sleep

from plenum.common.looper import Looper
from plenum.server.monitor import Monitor
from plenum.test.eventually import eventually
from plenum.test.helper import sendReqsToNodesAndVerifySuffReplies, \
    TestNodeSet
from plenum.common.util import getConfig

config = getConfig()

def testPostingThroughput(postingStatsEnabled, looper: Looper, nodeSet: TestNodeSet,
                          client1):
    """
    The throughput after `DashboardUpdateFreq` seconds and before sending any
    requests should be zero.
    Send `n` requests in less than `ThroughputWindowSize` seconds and the
    throughput till `ThroughputWindowSize` should consider those `n` requests.
    After `ThroughputWindowSize` seconds the throughput should be zero
    Test `totalRequests` too.
    """

    # We are sleeping for this window size, because we need to clear previous
    # values that were being stored for this much time in tests
    looper.runFor(config.ThroughputWindowSize)

    reqCount = 10
    for node in nodeSet:
        assert node.monitor.highResThroughput == 0
        assert node.monitor.totalRequests == 0

    sendReqsToNodesAndVerifySuffReplies(looper, client1, reqCount, nodeSet.f,
                                        timeout=20)

    for node in nodeSet:
        assert len(node.monitor.orderedRequestsInLast) == reqCount
        assert node.monitor.highResThroughput > 0
        assert node.monitor.totalRequests == reqCount
        count = node.monitor.spylog.count(Monitor.sendThroughput.__name__)
        if count > 0:
            assert node.monitor.hasMasterPrimary

    looper.runFor(config.DashboardUpdateFreq)

    for node in nodeSet:
        node.monitor.spylog.count(Monitor.sendThroughput.__name__) > 0

    # Run for latency window duration so that `orderedRequestsInLast`
    # becomes empty
    looper.runFor(config.ThroughputWindowSize)

    def chk():
        for node in nodeSet:
            assert len(node.monitor.orderedRequestsInLast) == 0
            assert node.monitor.highResThroughput == 0
            assert node.monitor.totalRequests == reqCount

    looper.run(eventually(chk, retryWait=1, timeout=10))

def testPostingLatency(postingStatsEnabled, looper: Looper,
                          nodeSet: TestNodeSet,
                          client1):
    """
    The latencies (master as well as average of backups) after
    `DashboardUpdateFreq` seconds and before sending any requests should be zero.
    Send `n` requests in less than `LatencyWindowSize` seconds and the
    latency till `LatencyWindowSize` should consider those `n` requests.
    After `LatencyWindowSize` seconds the latencies should be zero
    """
    # Run for latency window duration so that `latenciesByMasterInLast` and
    # `latenciesByBackupsInLast` become empty
    looper.runFor(config.LatencyWindowSize)
    reqCount = 10
    for node in nodeSet:
        assert node.monitor.masterLatency == 0
        assert node.monitor.avgBackupLatency == 0

    sendReqsToNodesAndVerifySuffReplies(looper, client1, reqCount,
                                        nodeSet.f,
                                        timeout=20)

    for node in nodeSet:
        assert node.monitor.masterLatency > 0
        assert node.monitor.avgBackupLatency > 0

    looper.runFor(config.DashboardUpdateFreq)

    for node in nodeSet:
        node.monitor.spylog.count(Monitor.sendLatencies.__name__) > 0

    # Run for latency window duration so that `latenciesByMasterInLast` and
    # `latenciesByBackupsInLast` become empty
    looper.runFor(config.LatencyWindowSize)

    def chk():
        for node in nodeSet:
            assert node.monitor.masterLatency == 0
            assert node.monitor.avgBackupLatency == 0

    looper.run(eventually(chk, retryWait=1, timeout=10))
<commit_msg>[#126218531] Fixed failing tests<commit_after>from time import sleep

from plenum.common.looper import Looper
from plenum.server.monitor import Monitor
from plenum.test.eventually import eventually
from plenum.test.helper import sendReqsToNodesAndVerifySuffReplies, \
    TestNodeSet
from plenum.common.util import getConfig

config = getConfig()

def testPostingThroughput(postingStatsEnabled, looper: Looper, nodeSet: TestNodeSet,
                          client1):
    """
    The throughput after `DashboardUpdateFreq` seconds and before sending any
    requests should be zero.
    Send `n` requests in less than `ThroughputWindowSize` seconds and the
    throughput till `ThroughputWindowSize` should consider those `n` requests.
    After `ThroughputWindowSize` seconds the throughput should be zero
    Test `totalRequests` too.
    """

    # We are sleeping for this window size, because we need to clear previous
    # values that were being stored for this much time in tests
    looper.runFor(config.ThroughputWindowSize)

    reqCount = 10
    for node in nodeSet:
        assert node.monitor.highResThroughput == 0
        assert node.monitor.totalRequests == 0

    sendReqsToNodesAndVerifySuffReplies(looper, client1, reqCount, nodeSet.f,
                                        timeout=20)

    for node in nodeSet:
        assert len(node.monitor.orderedRequestsInLast) == reqCount
        assert node.monitor.highResThroughput > 0
        assert node.monitor.totalRequests == reqCount
        # TODO: Add implementation to actually call firebase plugin
        # and test if firebase plugin is sending total request count
        # if node is primary

    looper.runFor(config.DashboardUpdateFreq)

    for node in nodeSet:
        node.monitor.spylog.count(Monitor.sendThroughput.__name__) > 0

    # Run for latency window duration so that `orderedRequestsInLast`
    # becomes empty
    looper.runFor(config.ThroughputWindowSize)

    def chk():
        for node in nodeSet:
            assert len(node.monitor.orderedRequestsInLast) == 0
            assert node.monitor.highResThroughput == 0
            assert node.monitor.totalRequests == reqCount

    looper.run(eventually(chk, retryWait=1, timeout=10))

def testPostingLatency(postingStatsEnabled, looper: Looper,
                          nodeSet: TestNodeSet,
                          client1):
    """
    The latencies (master as well as average of backups) after
    `DashboardUpdateFreq` seconds and before sending any requests should be zero.
    Send `n` requests in less than `LatencyWindowSize` seconds and the
    latency till `LatencyWindowSize` should consider those `n` requests.
    After `LatencyWindowSize` seconds the latencies should be zero
    """
    # Run for latency window duration so that `latenciesByMasterInLast` and
    # `latenciesByBackupsInLast` become empty
    looper.runFor(config.LatencyWindowSize)
    reqCount = 10
    for node in nodeSet:
        assert node.monitor.masterLatency == 0
        assert node.monitor.avgBackupLatency == 0

    sendReqsToNodesAndVerifySuffReplies(looper, client1, reqCount,
                                        nodeSet.f,
                                        timeout=20)

    for node in nodeSet:
        assert node.monitor.masterLatency > 0
        assert node.monitor.avgBackupLatency > 0

    looper.runFor(config.DashboardUpdateFreq)

    for node in nodeSet:
        node.monitor.spylog.count(Monitor.sendLatencies.__name__) > 0

    # Run for latency window duration so that `latenciesByMasterInLast` and
    # `latenciesByBackupsInLast` become empty
    looper.runFor(config.LatencyWindowSize)

    def chk():
        for node in nodeSet:
            assert node.monitor.masterLatency == 0
            assert node.monitor.avgBackupLatency == 0

    looper.run(eventually(chk, retryWait=1, timeout=10))
<|endoftext|>


<commit_before>import os
import sys
from django.views.generic import TemplateView, View
import datetime
from pprint import pprint
import json
from syndicate_storage.models import *
from core.models import *
from hpc.models import ContentProvider
from operator import attrgetter
from django import template
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse, HttpResponseServerError, HttpResponseForbidden
from django.core import urlresolvers
from django.contrib.gis.geoip import GeoIP
from django.db.models import Q
from ipware.ip import get_ip
from operator import itemgetter, attrgetter
import traceback
import math

if os.path.exists("/home/smbaker/projects/vicci/cdn/bigquery"):
    sys.path.append("/home/smbaker/projects/vicci/cdn/bigquery")
else:
    sys.path.append("/opt/planetstack/hpc_wizard")
from planetstack_analytics import DoPlanetStackAnalytics, PlanetStackAnalytics, RED_LOAD, BLUE_LOAD

def getDashboardContext(user, context={}, tableFormat = False):
        context = {}

        userSliceData = getSliceInfo(user)
        if (tableFormat):
            context['userSliceInfo'] = userSliceTableFormatter(userSliceData)
        else:
            context['userSliceInfo'] = userSliceData
        context['cdnData'] = getCDNOperatorData(wait=False)
        context['cdnContentProviders'] = getCDNContentProviderData()

        (dashboards, unusedDashboards)= getDashboards(user)
        unusedDashboards=[x for x in unusedDashboards if x!="Customize"]
        context['dashboards'] = dashboards
        context['unusedDashboards'] = unusedDashboards

        return context

def getDashboards(user):
    dashboards = user.get_dashboards()

    dashboard_names = [d.name for d in dashboards]

    unused_dashboard_names = []
    for dashboardView in DashboardView.objects.all():
        if not dashboardView.name in dashboard_names:
            unused_dashboard_names.append(dashboardView.name)

    return (dashboard_names, unused_dashboard_names)

def getSliceInfo(user):
    sliceList = Slice.objects.all()
    slicePrivs = SlicePrivilege.objects.filter(user=user)
    userSliceInfo = []
    for entry in slicePrivs:

        slicename = Slice.objects.get(id=entry.slice.id).name
        slice = Slice.objects.get(name=Slice.objects.get(id=entry.slice.id).name)
        sliverList=Sliver.objects.all()
        sites_used = {}
        for sliver in slice.slivers.all():
             #sites_used['deploymentSites'] = sliver.node.deployment.name
             # sites_used[sliver.image.name] = sliver.image.name
             sites_used[sliver.node.site.name] = sliver.numberCores
        sliceid = Slice.objects.get(id=entry.slice.id).id
        try:
            sliverList = Sliver.objects.filter(slice=entry.slice.id)
            siteList = {}
            for x in sliverList:
               if x.node.site not in siteList:
                  siteList[x.node.site] = 1
            slivercount = len(sliverList)
            sitecount = len(siteList)
        except:
            traceback.print_exc()
            slivercount = 0
            sitecount = 0

        userSliceInfo.append({'slicename': slicename, 'sliceid':sliceid,
                              'sitesUsed':sites_used,
                              'role': SliceRole.objects.get(id=entry.role.id).role,
                              'slivercount': slivercount,
                              'sitecount':sitecount})

    return userSliceInfo

def getCDNContentProviderData():
    cps = []
    for dm_cp in ContentProvider.objects.all():
        cp = {"name": dm_cp.name,
              "account": dm_cp.account}
        cps.append(cp)

    return cps

def getCDNOperatorData(randomizeData = False, wait=True):
    HPC_SLICE_NAME = "HyperCache"

    bq = PlanetStackAnalytics()

    rows = bq.get_cached_query_results(bq.compose_cached_query(), wait)

    # wait=False on the first time the Dashboard is opened. This means we might
    # not have any rows yet. The dashboard code polls every 30 seconds, so it
    # will eventually pick them up.

    if rows:
        rows = bq.postprocess_results(rows, filter={"event": "hpc_heartbeat"}, maxi=["cpu"], count=["hostname"], computed=["bytes_sent/elapsed"], groupBy=["Time","site"], maxDeltaTime=80)

        # dictionaryize the statistics rows by site name
        stats_rows = {}
        for row in rows:
            stats_rows[row["site"]] = row
    else:
        stats_rows = {}

    slice = Slice.objects.filter(name=HPC_SLICE_NAME)
    if slice:
        slice_slivers = list(slice[0].slivers.all())
    else:
        slice_slivers = []

    new_rows = {}
    for site in Site.objects.all():
        # compute number of slivers allocated in the data model
        allocated_slivers = 0
        for sliver in slice_slivers:
            if sliver.node.site == site:
                allocated_slivers = allocated_slivers + 1

        stats_row = stats_rows.get(site.name,{})

        max_cpu = stats_row.get("max_avg_cpu", stats_row.get("max_cpu",0))
        cpu=float(max_cpu)/100.0
        hotness = max(0.0, ((cpu*RED_LOAD) - BLUE_LOAD)/(RED_LOAD-BLUE_LOAD))

        try:
           lat=float(site.location.latitude)
           long=float(site.location.longitude)
        except:
           lat=0
           long=0

        # format it to what that CDN Operations View is expecting
        new_row = {"lat": lat,
               "long": long,
               "health": 0,
               "numNodes": int(site.nodes.count()),
               "activeHPCSlivers": int(stats_row.get("count_hostname", 0)),     # measured number of slivers, from bigquery statistics
               "numHPCSlivers": allocated_slivers,                              # allocated number of slivers, from data model
               "siteUrl": str(site.site_url),
               "bandwidth": stats_row.get("sum_computed_bytes_sent_div_elapsed",0),
               "load": max_cpu,
               "hot": float(hotness)}
        new_rows[str(site.name)] = new_row

    # get rid of sites with 0 slivers that overlap other sites with >0 slivers
    for (k,v) in new_rows.items():
        bad=False
        if v["numHPCSlivers"]==0:
            for v2 in new_rows.values():
                if (v!=v2) and (v2["numHPCSlivers"]>=0):
                    d = haversine(v["lat"],v["long"],v2["lat"],v2["long"])
                    if d<100:
                         bad=True
            if bad:
                del new_rows[k]

    return new_rows

def slice_increase_slivers(user, user_ip, siteList, slice, image, count, noAct=False):
    sitesChanged = {}

    # let's compute how many slivers are in use in each node of each site
    for site in siteList:
        site.nodeList = list(site.nodes.all())
        for node in site.nodeList:
            node.sliverCount = 0
            for sliver in node.slivers.all():
                 if sliver.slice.id == slice.id:
                     node.sliverCount = node.sliverCount + 1

    # Allocate slivers to nodes
    # for now, assume we want to allocate all slivers from the same site
    nodes = siteList[0].nodeList
    while (count>0):
        # Sort the node list by number of slivers per node, then pick the
        # node with the least number of slivers.
        nodes = sorted(nodes, key=attrgetter("sliverCount"))
        node = nodes[0]

        print "adding sliver at node", node.name, "of site", node.site.name

        if not noAct:
            sliver = Sliver(name=node.name,
                        slice=slice,
                        node=node,
                        image = image,
                        creator = User.objects.get(email=user),
                        deploymentNetwork=node.deployment,
                        numberCores =1 )
            sliver.save()

        node.sliverCount = node.sliverCount + 1

        count = count - 1

        sitesChanged[node.site.name] = sitesChanged.get(node.site.name,0) + 1

    return sitesChanged

def slice_decrease_slivers(user, siteList, slice, count, noAct=False):
    sitesChanged = {}
    if siteList:
        siteNames = [site.name for site in siteList]
    else:
        siteNames = None

    for sliver in list(slice.slivers.all()):
        if count>0:
            if(not siteNames) or (sliver.node.site.name in siteNames):
                sliver.delete()
                print "deleting sliver",sliver.name,"at node",sliver.node.name
                count=count-1
                sitesChanged[sliver.node.site.name] = sitesChanged.get(sliver.node.site.name,0) - 1

    return sitesChanged

def haversine(site_lat, site_lon, lat, lon):
    d=0
    if lat and lon and site_lat and site_lon:
        site_lat = float(site_lat)
        site_lon = float(site_lon)
        lat = float(lat)
        lon = float(lon)
        R = 6378.1
        a = math.sin( math.radians((lat - site_lat)/2.0) )**2 + math.cos( math.radians(lat) )*math.cos( math.radians(site_lat) )*(math.sin( math.radians((lon - site_lon)/2.0 ) )**2)
        c = 2 * math.atan2( math.sqrt(a), math.sqrt(1 - a) )
        d = R * c

    return d

def userSliceTableFormatter(data):
    formattedData = {
                     'rows' : data
                    }
    return formattedData
<commit_msg>remove sliver.numberCores<commit_after>import os
import sys
from django.views.generic import TemplateView, View
import datetime
from pprint import pprint
import json
from syndicate_storage.models import *
from core.models import *
from hpc.models import ContentProvider
from operator import attrgetter
from django import template
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse, HttpResponseServerError, HttpResponseForbidden
from django.core import urlresolvers
from django.contrib.gis.geoip import GeoIP
from django.db.models import Q
from ipware.ip import get_ip
from operator import itemgetter, attrgetter
import traceback
import math

if os.path.exists("/home/smbaker/projects/vicci/cdn/bigquery"):
    sys.path.append("/home/smbaker/projects/vicci/cdn/bigquery")
else:
    sys.path.append("/opt/planetstack/hpc_wizard")
from planetstack_analytics import DoPlanetStackAnalytics, PlanetStackAnalytics, RED_LOAD, BLUE_LOAD

def getDashboardContext(user, context={}, tableFormat = False):
        context = {}

        userSliceData = getSliceInfo(user)
        if (tableFormat):
            context['userSliceInfo'] = userSliceTableFormatter(userSliceData)
        else:
            context['userSliceInfo'] = userSliceData
        context['cdnData'] = getCDNOperatorData(wait=False)
        context['cdnContentProviders'] = getCDNContentProviderData()

        (dashboards, unusedDashboards)= getDashboards(user)
        unusedDashboards=[x for x in unusedDashboards if x!="Customize"]
        context['dashboards'] = dashboards
        context['unusedDashboards'] = unusedDashboards

        return context

def getDashboards(user):
    dashboards = user.get_dashboards()

    dashboard_names = [d.name for d in dashboards]

    unused_dashboard_names = []
    for dashboardView in DashboardView.objects.all():
        if not dashboardView.name in dashboard_names:
            unused_dashboard_names.append(dashboardView.name)

    return (dashboard_names, unused_dashboard_names)

def getSliceInfo(user):
    sliceList = Slice.objects.all()
    slicePrivs = SlicePrivilege.objects.filter(user=user)
    userSliceInfo = []
    for entry in slicePrivs:

        slicename = Slice.objects.get(id=entry.slice.id).name
        slice = Slice.objects.get(name=Slice.objects.get(id=entry.slice.id).name)
        sliverList=Sliver.objects.all()
        sites_used = {}
        for sliver in slice.slivers.all():
             #sites_used['deploymentSites'] = sliver.node.deployment.name
             # sites_used[sliver.image.name] = sliver.image.name
             sites_used[sliver.node.site.name] = 1 #sliver.numberCores
        sliceid = Slice.objects.get(id=entry.slice.id).id
        try:
            sliverList = Sliver.objects.filter(slice=entry.slice.id)
            siteList = {}
            for x in sliverList:
               if x.node.site not in siteList:
                  siteList[x.node.site] = 1
            slivercount = len(sliverList)
            sitecount = len(siteList)
        except:
            traceback.print_exc()
            slivercount = 0
            sitecount = 0

        userSliceInfo.append({'slicename': slicename, 'sliceid':sliceid,
                              'sitesUsed':sites_used,
                              'role': SliceRole.objects.get(id=entry.role.id).role,
                              'slivercount': slivercount,
                              'sitecount':sitecount})

    return userSliceInfo

def getCDNContentProviderData():
    cps = []
    for dm_cp in ContentProvider.objects.all():
        cp = {"name": dm_cp.name,
              "account": dm_cp.account}
        cps.append(cp)

    return cps

def getCDNOperatorData(randomizeData = False, wait=True):
    HPC_SLICE_NAME = "HyperCache"

    bq = PlanetStackAnalytics()

    rows = bq.get_cached_query_results(bq.compose_cached_query(), wait)

    # wait=False on the first time the Dashboard is opened. This means we might
    # not have any rows yet. The dashboard code polls every 30 seconds, so it
    # will eventually pick them up.

    if rows:
        rows = bq.postprocess_results(rows, filter={"event": "hpc_heartbeat"}, maxi=["cpu"], count=["hostname"], computed=["bytes_sent/elapsed"], groupBy=["Time","site"], maxDeltaTime=80)

        # dictionaryize the statistics rows by site name
        stats_rows = {}
        for row in rows:
            stats_rows[row["site"]] = row
    else:
        stats_rows = {}

    slice = Slice.objects.filter(name=HPC_SLICE_NAME)
    if slice:
        slice_slivers = list(slice[0].slivers.all())
    else:
        slice_slivers = []

    new_rows = {}
    for site in Site.objects.all():
        # compute number of slivers allocated in the data model
        allocated_slivers = 0
        for sliver in slice_slivers:
            if sliver.node.site == site:
                allocated_slivers = allocated_slivers + 1

        stats_row = stats_rows.get(site.name,{})

        max_cpu = stats_row.get("max_avg_cpu", stats_row.get("max_cpu",0))
        cpu=float(max_cpu)/100.0
        hotness = max(0.0, ((cpu*RED_LOAD) - BLUE_LOAD)/(RED_LOAD-BLUE_LOAD))

        try:
           lat=float(site.location.latitude)
           long=float(site.location.longitude)
        except:
           lat=0
           long=0

        # format it to what that CDN Operations View is expecting
        new_row = {"lat": lat,
               "long": long,
               "health": 0,
               "numNodes": int(site.nodes.count()),
               "activeHPCSlivers": int(stats_row.get("count_hostname", 0)),     # measured number of slivers, from bigquery statistics
               "numHPCSlivers": allocated_slivers,                              # allocated number of slivers, from data model
               "siteUrl": str(site.site_url),
               "bandwidth": stats_row.get("sum_computed_bytes_sent_div_elapsed",0),
               "load": max_cpu,
               "hot": float(hotness)}
        new_rows[str(site.name)] = new_row

    # get rid of sites with 0 slivers that overlap other sites with >0 slivers
    for (k,v) in new_rows.items():
        bad=False
        if v["numHPCSlivers"]==0:
            for v2 in new_rows.values():
                if (v!=v2) and (v2["numHPCSlivers"]>=0):
                    d = haversine(v["lat"],v["long"],v2["lat"],v2["long"])
                    if d<100:
                         bad=True
            if bad:
                del new_rows[k]

    return new_rows

def slice_increase_slivers(user, user_ip, siteList, slice, image, count, noAct=False):
    sitesChanged = {}

    # let's compute how many slivers are in use in each node of each site
    for site in siteList:
        site.nodeList = list(site.nodes.all())
        for node in site.nodeList:
            node.sliverCount = 0
            for sliver in node.slivers.all():
                 if sliver.slice.id == slice.id:
                     node.sliverCount = node.sliverCount + 1

    # Allocate slivers to nodes
    # for now, assume we want to allocate all slivers from the same site
    nodes = siteList[0].nodeList
    while (count>0):
        # Sort the node list by number of slivers per node, then pick the
        # node with the least number of slivers.
        nodes = sorted(nodes, key=attrgetter("sliverCount"))
        node = nodes[0]

        print "adding sliver at node", node.name, "of site", node.site.name

        if not noAct:
            sliver = Sliver(name=node.name,
                        slice=slice,
                        node=node,
                        image = image,
                        creator = User.objects.get(email=user),
                        deploymentNetwork=node.deployment)
            sliver.save()

        node.sliverCount = node.sliverCount + 1

        count = count - 1

        sitesChanged[node.site.name] = sitesChanged.get(node.site.name,0) + 1

    return sitesChanged

def slice_decrease_slivers(user, siteList, slice, count, noAct=False):
    sitesChanged = {}
    if siteList:
        siteNames = [site.name for site in siteList]
    else:
        siteNames = None

    for sliver in list(slice.slivers.all()):
        if count>0:
            if(not siteNames) or (sliver.node.site.name in siteNames):
                sliver.delete()
                print "deleting sliver",sliver.name,"at node",sliver.node.name
                count=count-1
                sitesChanged[sliver.node.site.name] = sitesChanged.get(sliver.node.site.name,0) - 1

    return sitesChanged

def haversine(site_lat, site_lon, lat, lon):
    d=0
    if lat and lon and site_lat and site_lon:
        site_lat = float(site_lat)
        site_lon = float(site_lon)
        lat = float(lat)
        lon = float(lon)
        R = 6378.1
        a = math.sin( math.radians((lat - site_lat)/2.0) )**2 + math.cos( math.radians(lat) )*math.cos( math.radians(site_lat) )*(math.sin( math.radians((lon - site_lon)/2.0 ) )**2)
        c = 2 * math.atan2( math.sqrt(a), math.sqrt(1 - a) )
        d = R * c

    return d

def userSliceTableFormatter(data):
    formattedData = {
                     'rows' : data
                    }
    return formattedData
<|endoftext|>


<commit_before>#https://open.fda.gov/
#self.path es mi url
#ul=unlimited list
#https://github.com/tylucaskelley/licenser/blob/master/LICENSE
#accion parecida a recieve: atribute

import http.server
import http.client
import json
# HTTPRequestHandler class
class testHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
	OPENFDA_API_URL='api.fda.gov'
	OPENFDA_API_EVENT='/drug/event.json'

	def get_main_page(self):
		html='''

	        <title> OpenFDA Cool App</title>

OpenFDA Client

			</form>
			<form method="get" action="listCompanies">
				<input type="submit" value="Company List: Send to OpenFDA">
				</input>
			</form>
	        <form method="get" action="listDrugs">
	            <input type="submit" value="Drug List: Send to OpenFDA">
	            </input>
	        </form>
			<form method="get" action="searchCompany">
				<input type="text" name="company">
				</input>
				<input type="submit" value="Company Search:Send to OpenFDA">
				</input>
			</form>
            <form method="get" action="searchDrug">
				<input type="text" name="drug">
				</input>
                <input type="submit" value="Drug Search: Send to OpendFDA">
				</input>

	    '''
		return html

	def get_list_html(self,drugs): #html con las 10 drugs
		drugs_html='''

				<title> OpenFDA Cool App</title>

		'''
		for drug in drugs:
			drugs_html+="
"+drug+"
"
		drugs_html+= '''

		'''
		return drugs_html

    # GET EVENT
	def get_events(self):
		conn = http.client.HTTPSConnection(self.OPENFDA_API_URL) #Te conectas
		conn.request('GET',self.OPENFDA_API_EVENT +'?limit=10') #Dices que te coja los eventos
		r1 = conn.getresponse() #Obtienes los eventos (respuesta)
		data1 = r1.read() #lee los datos r1
		events = data1.decode("utf8") #lo descodifica, formato json
		return events

	def get_any_drug(self,drug):
		conn = http.client.HTTPSConnection(self.OPENFDA_API_URL)
		conn.request('GET',self.OPENFDA_API_EVENT +'?search='+drug+'&limit=10')
		r1 = conn.getresponse()
		data1 = r1.read()
		events = data1.decode("utf8")
		return events

	def get_any_company(self,company):
		conn = http.client.HTTPSConnection(self.OPENFDA_API_URL)
		conn.request('GET',self.OPENFDA_API_EVENT +'?search='+company+'&limit=10')
		r1 = conn.getresponse()
		data1 = r1.read()
		events = data1.decode("utf8")
		return events

	def get_companies_from_events(self,events):
		companies=[]
		for event in events:
			companies=companies+[event['companynumb']]
		return companies

	def get_drugs_from_events(self, events):
		drugs=[]
		for event in events:
			drugs=drugs+[event['patient']['drug'][0]['medicinalproduct']]
			#lista_str= ",".join(lista) #Para transformarlo en string
		return drugs

	#def get_company_list(self,events):
		#company=[]
		#for event in events:
			#company+=[event['companynumb']]
		#return company

    # GET
	def do_GET(self):
		main_page=False
		drug_event=False
		search_drugs=False
		search_company=False
		company_event=False

		if self.path=='/':
			main_page=True
		elif self.path=="/listCompanies" in self.path :
			company_event=True
		elif self.path=="/listDrugs" in self.path:
			drug_event=True
		elif "/searchDrug" in self.path:
			search_drugs=True
		elif "/searchCompany" in self.path:
			search_company=True
		#print(self.path) con esto elegimos entre evento o html
        # Send response status code
		self.send_response(200)

        # Send headers
		self.send_header('Content-type','text/html')
		self.end_headers()

        # Send message back to client
		html=self.get_main_page()

        #event=event1
		if main_page:
			self.wfile.write(bytes(html, "utf8"))

		elif drug_event:
			events_str=self.get_events()
			events=json.loads(events_str)
			events=events["results"]
			drugs=self.get_drugs_from_events(events)
			html=self.get_list_html(drugs)
			self.wfile.write(bytes(html, "utf8"))
			#for i in range(len(events)):
			#	print (events[i])

		elif company_event:
			events_str=self.get_events()
			events=json.loads(events_str)
			events=events["results"]
			companies=self.get_companies_from_events(events)
			html=self.get_list_html(companies)
			self.wfile.write(bytes(html, "utf8"))

		elif search_company:
			company=self.path.split("=")[1]
			event_str=self.get_any_company(company)
			events=json.loads(event_str)
			events=events["results"]
			search=self.get_drugs_from_events(events)
			html=self.get_list_html(search)
			self.wfile.write(bytes(html, "utf8"))

		elif search_drugs:
			drug=self.path.split("=")[1]
			drugs=self.get_any_drug(drug)
			events=json.loads(drugs)
			events=events["results"]
			search=self.get_companies_from_events(events)
			html=self.get_list_html(search)
			self.wfile.write(bytes(html, "utf8"))
<commit_msg>Delete webserver.py<commit_after><|endoftext|>


<commit_before>
"""
This module implements an example server for the OpenID library.  Some
functionality has been omitted intentionally; this code is intended to
be instructive on the use of this library.  This server does not
perform actual user authentication and serves up only one OpenID URL,
with the exception of IDP-generated identifiers.

Some code conventions used here:

* 'request' is a Django request object.

* 'openid_request' is an OpenID library request object.

* 'openid_response' is an OpenID library response
"""

import cgi

from djopenid import util
from djopenid.util import getViewURL

from django import http
from django.views.generic.simple import direct_to_template

from openid.server.server import Server, ProtocolError, CheckIDRequest, \
     EncodingError
from openid.server.trustroot import verifyReturnTo
from openid.yadis.discover import DiscoveryFailure
from openid.consumer.discover import OPENID_IDP_2_0_TYPE
from openid.extensions import sreg
from openid.fetchers import HTTPFetchingError

def getOpenIDStore():
    """
    Return an OpenID store object fit for the currently-chosen
    database backend, if any.
    """
    return util.getOpenIDStore('/tmp/djopenid_s_store', 's_')

def getServer(request):
    """
    Get a Server object to perform OpenID authentication.
    """
    return Server(getOpenIDStore(), getViewURL(request, endpoint))

def setRequest(request, openid_request):
    """
    Store the openid request information in the session.
    """
    if openid_request:
        request.session['openid_request'] = openid_request
    else:
        request.session['openid_request'] = None

def getRequest(request):
    """
    Get an openid request from the session, if any.
    """
    return request.session.get('openid_request')

def server(request):
    """
    Respond to requests for the server's primary web page.
    """
    return direct_to_template(
        request,
        'server/index.html',
        {'user_url': getViewURL(request, idPage),
         'server_xrds_url': getViewURL(request, idpXrds),
         })

def idpXrds(request):
    """
    Respond to requests for the IDP's XRDS document, which is used in
    IDP-driven identifier selection.
    """
    return util.renderXRDS(
        request, [OPENID_IDP_2_0_TYPE], [getViewURL(request, endpoint)])

def idPage(request):
    """
    Serve the identity page for OpenID URLs.
    """
    return direct_to_template(
        request,
        'server/idPage.html',
        {'server_url': getViewURL(request, endpoint)})

def trustPage(request):
    """
    Display the trust page template, which allows the user to decide
    whether to approve the OpenID verification.
    """
    return direct_to_template(
        request,
        'server/trust.html',
        {'trust_handler_url':getViewURL(request, processTrustResult)})

def endpoint(request):
    """
    Respond to low-level OpenID protocol messages.
    """
    s = getServer(request)

    query = util.normalDict(request.GET or request.POST)

    # First, decode the incoming request into something the OpenID
    # library can use.
    try:
        openid_request = s.decodeRequest(query)
    except ProtocolError, why:
        # This means the incoming request was invalid.
        return direct_to_template(
            request,
            'server/endpoint.html',
            {'error': str(why)})

    # If we did not get a request, display text indicating that this
    # is an endpoint.
    if openid_request is None:
        return direct_to_template(
            request,
            'server/endpoint.html',
            {})

    # We got a request; if the mode is checkid_*, we will handle it by
    # getting feedback from the user or by checking the session.
    if openid_request.mode in ["checkid_immediate", "checkid_setup"]:
        return handleCheckIDRequest(request, openid_request)
    else:
        # We got some other kind of OpenID request, so we let the
        # server handle this.
        openid_response = s.handleRequest(openid_request)
        return displayResponse(request, openid_response)

def handleCheckIDRequest(request, openid_request):
    """
    Handle checkid_* requests.  Get input from the user to find out
    whether she trusts the RP involved.  Possibly, get intput about
    what Simple Registration information, if any, to send in the
    response.
    """
    # If the request was an IDP-driven identifier selection request
    # (i.e., the IDP URL was entered at the RP), then return the
    # default identity URL for this server. In a full-featured
    # provider, there could be interaction with the user to determine
    # what URL should be sent.
    if not openid_request.idSelect():

        id_url = getViewURL(request, idPage)

        # Confirm that this server can actually vouch for that
        # identifier
        if id_url != openid_request.identity:
            # Return an error response
            error_response = ProtocolError(
                openid_request.message,
                "This server cannot verify the URL %r" %
                (openid_request.identity,))

            return displayResponse(request, error_response)

    if openid_request.immediate:
        # Always respond with 'cancel' to immediate mode requests
        # because we don't track information about a logged-in user.
        # If we did, then the answer would depend on whether that user
        # had trusted the request's trust root and whether the user is
        # even logged in.
        openid_response = openid_request.answer(False)
        return displayResponse(request, openid_response)
    else:
        # Store the incoming request object in the session so we can
        # get to it later.
        setRequest(request, openid_request)
        return showDecidePage(request, openid_request)

def showDecidePage(request, openid_request):
    """
    Render a page to the user so a trust decision can be made.

    @type openid_request: openid.server.server.CheckIDRequest
    """
    trust_root = openid_request.trust_root
    return_to = openid_request.return_to

    try:
        # Stringify because template's ifequal can only compare to strings.
        trust_root_valid = verifyReturnTo(trust_root, return_to) \
                           and "Valid" or "Invalid"
    except DiscoveryFailure, err:
        trust_root_valid = "DISCOVERY_FAILED"
    except HTTPFetchingError, err:
        trust_root_valid = "Unreachable"

    return direct_to_template(
        request,
        'server/trust.html',
        {'trust_root': trust_root,
         'trust_handler_url':getViewURL(request, processTrustResult),
         'trust_root_valid': trust_root_valid,
         })

def processTrustResult(request):
    """
    Handle the result of a trust decision and respond to the RP
    accordingly.
    """
    # Get the request from the session so we can construct the
    # appropriate response.
    openid_request = getRequest(request)

    # The identifier that this server can vouch for
    response_identity = getViewURL(request, idPage)

    # If the decision was to allow the verification, respond
    # accordingly.
    allowed = 'allow' in request.POST

    # Generate a response with the appropriate answer.
    openid_response = openid_request.answer(allowed,
                                            identity=response_identity)

    # Send Simple Registration data in the response, if appropriate.
    if allowed:
        sreg_data = {
            'fullname': 'Example User',
            'nickname': 'example',
            'dob': '1970-01-01',
            'email': 'invalid@example.com',
            'gender': 'F',
            'postcode': '12345',
            'country': 'ES',
            'language': 'eu',
            'timezone': 'America/New_York',
            }

        sreg_req = sreg.SRegRequest.fromOpenIDRequest(openid_request)
        sreg_resp = sreg.SRegResponse.extractResponse(sreg_req, sreg_data)
        openid_response.addExtension(sreg_resp)

    return displayResponse(request, openid_response)

def displayResponse(request, openid_response):
    """
    Display an OpenID response.  Errors will be displayed directly to
    the user; successful responses and other protocol-level messages
    will be sent using the proper mechanism (i.e., direct response,
    redirection, etc.).
    """
    s = getServer(request)

    # Encode the response into something that is renderable.
    try:
        webresponse = s.encodeResponse(openid_response)
    except EncodingError, why:
        # If it couldn't be encoded, display an error.
        text = why.response.encodeToKVForm()
        return direct_to_template(
            request,
            'server/endpoint.html',
            {'error': cgi.escape(text)})

    # Construct the appropriate django framework response.
    r = http.HttpResponse(webresponse.body)
    r.status_code = webresponse.code

    for header, value in webresponse.headers.iteritems():
        r[header] = value

    return r
<commit_msg>[project @ Django demo server: send NIST auth level 0 PAPE response]<commit_after>
"""
This module implements an example server for the OpenID library.  Some
functionality has been omitted intentionally; this code is intended to
be instructive on the use of this library.  This server does not
perform actual user authentication and serves up only one OpenID URL,
with the exception of IDP-generated identifiers.

Some code conventions used here:

* 'request' is a Django request object.

* 'openid_request' is an OpenID library request object.

* 'openid_response' is an OpenID library response
"""

import cgi

from djopenid import util
from djopenid.util import getViewURL

from django import http
from django.views.generic.simple import direct_to_template

from openid.server.server import Server, ProtocolError, CheckIDRequest, \
     EncodingError
from openid.server.trustroot import verifyReturnTo
from openid.yadis.discover import DiscoveryFailure
from openid.consumer.discover import OPENID_IDP_2_0_TYPE
from openid.extensions import sreg
from openid.extensions import pape
from openid.fetchers import HTTPFetchingError

def getOpenIDStore():
    """
    Return an OpenID store object fit for the currently-chosen
    database backend, if any.
    """
    return util.getOpenIDStore('/tmp/djopenid_s_store', 's_')

def getServer(request):
    """
    Get a Server object to perform OpenID authentication.
    """
    return Server(getOpenIDStore(), getViewURL(request, endpoint))

def setRequest(request, openid_request):
    """
    Store the openid request information in the session.
    """
    if openid_request:
        request.session['openid_request'] = openid_request
    else:
        request.session['openid_request'] = None

def getRequest(request):
    """
    Get an openid request from the session, if any.
    """
    return request.session.get('openid_request')

def server(request):
    """
    Respond to requests for the server's primary web page.
    """
    return direct_to_template(
        request,
        'server/index.html',
        {'user_url': getViewURL(request, idPage),
         'server_xrds_url': getViewURL(request, idpXrds),
         })

def idpXrds(request):
    """
    Respond to requests for the IDP's XRDS document, which is used in
    IDP-driven identifier selection.
    """
    return util.renderXRDS(
        request, [OPENID_IDP_2_0_TYPE], [getViewURL(request, endpoint)])

def idPage(request):
    """
    Serve the identity page for OpenID URLs.
    """
    return direct_to_template(
        request,
        'server/idPage.html',
        {'server_url': getViewURL(request, endpoint)})

def trustPage(request):
    """
    Display the trust page template, which allows the user to decide
    whether to approve the OpenID verification.
    """
    return direct_to_template(
        request,
        'server/trust.html',
        {'trust_handler_url':getViewURL(request, processTrustResult)})

def endpoint(request):
    """
    Respond to low-level OpenID protocol messages.
    """
    s = getServer(request)

    query = util.normalDict(request.GET or request.POST)

    # First, decode the incoming request into something the OpenID
    # library can use.
    try:
        openid_request = s.decodeRequest(query)
    except ProtocolError, why:
        # This means the incoming request was invalid.
        return direct_to_template(
            request,
            'server/endpoint.html',
            {'error': str(why)})

    # If we did not get a request, display text indicating that this
    # is an endpoint.
    if openid_request is None:
        return direct_to_template(
            request,
            'server/endpoint.html',
            {})

    # We got a request; if the mode is checkid_*, we will handle it by
    # getting feedback from the user or by checking the session.
    if openid_request.mode in ["checkid_immediate", "checkid_setup"]:
        return handleCheckIDRequest(request, openid_request)
    else:
        # We got some other kind of OpenID request, so we let the
        # server handle this.
        openid_response = s.handleRequest(openid_request)
        return displayResponse(request, openid_response)

def handleCheckIDRequest(request, openid_request):
    """
    Handle checkid_* requests.  Get input from the user to find out
    whether she trusts the RP involved.  Possibly, get intput about
    what Simple Registration information, if any, to send in the
    response.
    """
    # If the request was an IDP-driven identifier selection request
    # (i.e., the IDP URL was entered at the RP), then return the
    # default identity URL for this server. In a full-featured
    # provider, there could be interaction with the user to determine
    # what URL should be sent.
    if not openid_request.idSelect():

        id_url = getViewURL(request, idPage)

        # Confirm that this server can actually vouch for that
        # identifier
        if id_url != openid_request.identity:
            # Return an error response
            error_response = ProtocolError(
                openid_request.message,
                "This server cannot verify the URL %r" %
                (openid_request.identity,))

            return displayResponse(request, error_response)

    if openid_request.immediate:
        # Always respond with 'cancel' to immediate mode requests
        # because we don't track information about a logged-in user.
        # If we did, then the answer would depend on whether that user
        # had trusted the request's trust root and whether the user is
        # even logged in.
        openid_response = openid_request.answer(False)
        return displayResponse(request, openid_response)
    else:
        # Store the incoming request object in the session so we can
        # get to it later.
        setRequest(request, openid_request)
        return showDecidePage(request, openid_request)

def showDecidePage(request, openid_request):
    """
    Render a page to the user so a trust decision can be made.

    @type openid_request: openid.server.server.CheckIDRequest
    """
    trust_root = openid_request.trust_root
    return_to = openid_request.return_to

    try:
        # Stringify because template's ifequal can only compare to strings.
        trust_root_valid = verifyReturnTo(trust_root, return_to) \
                           and "Valid" or "Invalid"
    except DiscoveryFailure, err:
        trust_root_valid = "DISCOVERY_FAILED"
    except HTTPFetchingError, err:
        trust_root_valid = "Unreachable"

    return direct_to_template(
        request,
        'server/trust.html',
        {'trust_root': trust_root,
         'trust_handler_url':getViewURL(request, processTrustResult),
         'trust_root_valid': trust_root_valid,
         })

def processTrustResult(request):
    """
    Handle the result of a trust decision and respond to the RP
    accordingly.
    """
    # Get the request from the session so we can construct the
    # appropriate response.
    openid_request = getRequest(request)

    # The identifier that this server can vouch for
    response_identity = getViewURL(request, idPage)

    # If the decision was to allow the verification, respond
    # accordingly.
    allowed = 'allow' in request.POST

    # Generate a response with the appropriate answer.
    openid_response = openid_request.answer(allowed,
                                            identity=response_identity)

    # Send Simple Registration data in the response, if appropriate.
    if allowed:
        sreg_data = {
            'fullname': 'Example User',
            'nickname': 'example',
            'dob': '1970-01-01',
            'email': 'invalid@example.com',
            'gender': 'F',
            'postcode': '12345',
            'country': 'ES',
            'language': 'eu',
            'timezone': 'America/New_York',
            }

        sreg_req = sreg.SRegRequest.fromOpenIDRequest(openid_request)
        sreg_resp = sreg.SRegResponse.extractResponse(sreg_req, sreg_data)
        openid_response.addExtension(sreg_resp)

        pape_response = pape.Response()
        pape_response.setAuthLevel(pape.LEVELS_NIST, 0)
        openid_response.addExtension(pape_response)

    return displayResponse(request, openid_response)

def displayResponse(request, openid_response):
    """
    Display an OpenID response.  Errors will be displayed directly to
    the user; successful responses and other protocol-level messages
    will be sent using the proper mechanism (i.e., direct response,
    redirection, etc.).
    """
    s = getServer(request)

    # Encode the response into something that is renderable.
    try:
        webresponse = s.encodeResponse(openid_response)
    except EncodingError, why:
        # If it couldn't be encoded, display an error.
        text = why.response.encodeToKVForm()
        return direct_to_template(
            request,
            'server/endpoint.html',
            {'error': cgi.escape(text)})

    # Construct the appropriate django framework response.
    r = http.HttpResponse(webresponse.body)
    r.status_code = webresponse.code

    for header, value in webresponse.headers.iteritems():
        r[header] = value

    return r
<|endoftext|>


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

# author: brendan@shellshockcomputer.com.au

import ConfigParser
from bottle import route, install, run, template, static_file, response, PasteServer, debug
from bottle_sqlite import SQLitePlugin
import json
import urllib
import urllib2
import datetime

config = ConfigParser.RawConfigParser()
config.read('config.ini')

install(SQLitePlugin(dbfile=(config.get("pool", "database"))))

def blocktime():
    payload = {
        'requestType': 'getForging',
        'secretPhrase': config.get("pool", "poolphrase")
    }
    opener = urllib2.build_opener(urllib2.HTTPHandler())
    data = urllib.urlencode(payload)
    forging = json.loads(opener.open(config.get("pool", "nhzhost")+'/nhz', data=data).read())
    getdl = forging["deadline"]
    return getdl

@route('/')
def default(db):
    response.headers['Cache-Control'] = 'public, max-age=3600'
    poolaccount = config.get("pool", "poolaccount")
    poolfee = config.get("pool", "feePercent")
    db.text_factory = str
    d = db.execute("SELECT height, timestamp, totalfee FROM blocks WHERE totalfee > 0 ORDER BY timestamp DESC limit 6")
    getlastheight = d.fetchone()
    lastheight = getlastheight[0]
    c = db.execute("SELECT account, heightto, amount FROM leased WHERE heightto > %s" % (lastheight))
    result = c.fetchall()
    block = d.fetchall()
    output = template('default', pa=poolaccount, fee=poolfee, rows=result, blocks=block)
    return output

@route('/static/:path#.+#', name='static')
def static(path):
    response.headers['Cache-Control'] = 'public, max-age=2592000'
    return static_file(path, root='static')

@route('/favicon.ico')
def get_favicon():
    response.headers['Cache-Control'] = 'public, max-age=2592000'
    return static('favicon.ico')

@route('/accounts')
def accounts(db):
    response.headers['Cache-Control'] = 'public, max-age=3600'
    db.text_factory = str
    d = db.execute("SELECT height FROM blocks ORDER BY timestamp DESC")
    getlastheight = d.fetchone()
    lastheight = getlastheight[0]
    print lastheight
    c = db.execute("SELECT account, heightfrom, heightto, amount FROM leased")
    result = c.fetchall()
    output = template('accounts', rows=result)
    return output

@route('/blocks')
def blocks(db):
    response.headers['Cache-Control'] = 'public, max-age=120'
    deadline = blocktime()
    dl = str(datetime.timedelta(seconds=deadline))
    c = db.execute("SELECT height, timestamp, block, totalfee FROM blocks WHERE totalfee > 0 ORDER BY timestamp DESC")
    result = c.fetchall()
    c.close()
    output = template('blocks', rows=result, fg=dl)
    return output

@route('/payouts')
def payouts(db):
    response.headers['Cache-Control'] = 'public, max-age=86400'
    c = db.execute("SELECT account, fee, payment FROM payouts")
    result = c.fetchall()
    output = template('payouts', rows=result)
    return output

@route('/unpaid')
def unpaid(db):
    response.headers['Cache-Control'] = 'public, max-age=1200'
    c = db.execute("SELECT blocktime, account, percentage, amount FROM accounts WHERE paid=0 ORDER BY blocktime DESC")
    result = c.fetchall()
    output = template('unpaid', rows=result)
    return output

@route('/paid')
def paid(db):
    response.headers['Cache-Control'] = 'public, max-age=1200'
    c = db.execute("SELECT blocktime, account, percentage, amount FROM accounts WHERE paid>0 ORDER BY blocktime DESC")
    result = c.fetchall()
    output = template('paid', rows=result)
    return output

debug(True)
run(server=PasteServer, port=8888, host='0.0.0.0')
<commit_msg>Turn off debuging<commit_after>#!/usr/bin/env python

# author: brendan@shellshockcomputer.com.au

import ConfigParser
from bottle import route, install, run, template, static_file, response, PasteServer
from bottle_sqlite import SQLitePlugin
import json
import urllib
import urllib2
import datetime

config = ConfigParser.RawConfigParser()
config.read('config.ini')

install(SQLitePlugin(dbfile=(config.get("pool", "database"))))

def blocktime():
    payload = {
        'requestType': 'getForging',
        'secretPhrase': config.get("pool", "poolphrase")
    }
    opener = urllib2.build_opener(urllib2.HTTPHandler())
    data = urllib.urlencode(payload)
    forging = json.loads(opener.open(config.get("pool", "nhzhost")+'/nhz', data=data).read())
    getdl = forging["deadline"]
    return getdl

@route('/')
def default(db):
    response.headers['Cache-Control'] = 'public, max-age=3600'
    poolaccount = config.get("pool", "poolaccount")
    poolfee = config.get("pool", "feePercent")
    db.text_factory = str
    d = db.execute("SELECT height, timestamp, totalfee FROM blocks WHERE totalfee > 0 ORDER BY timestamp DESC limit 6")
    getlastheight = d.fetchone()
    lastheight = getlastheight[0]
    c = db.execute("SELECT account, heightto, amount FROM leased WHERE heightto > %s" % (lastheight))
    result = c.fetchall()
    block = d.fetchall()
    output = template('default', pa=poolaccount, fee=poolfee, rows=result, blocks=block)
    return output

@route('/static/:path#.+#', name='static')
def static(path):
    response.headers['Cache-Control'] = 'public, max-age=2592000'
    return static_file(path, root='static')

@route('/favicon.ico')
def get_favicon():
    response.headers['Cache-Control'] = 'public, max-age=2592000'
    return static('favicon.ico')

@route('/accounts')
def accounts(db):
    response.headers['Cache-Control'] = 'public, max-age=3600'
    db.text_factory = str
    d = db.execute("SELECT height FROM blocks ORDER BY timestamp DESC")
    getlastheight = d.fetchone()
    lastheight = getlastheight[0]
    print lastheight
    c = db.execute("SELECT account, heightfrom, heightto, amount FROM leased")
    result = c.fetchall()
    output = template('accounts', rows=result)
    return output

@route('/blocks')
def blocks(db):
    response.headers['Cache-Control'] = 'public, max-age=120'
    deadline = blocktime()
    dl = str(datetime.timedelta(seconds=deadline))
    c = db.execute("SELECT height, timestamp, block, totalfee FROM blocks WHERE totalfee > 0 ORDER BY timestamp DESC")
    result = c.fetchall()
    c.close()
    output = template('blocks', rows=result, fg=dl)
    return output

@route('/payouts')
def payouts(db):
    response.headers['Cache-Control'] = 'public, max-age=86400'
    c = db.execute("SELECT account, fee, payment FROM payouts")
    result = c.fetchall()
    output = template('payouts', rows=result)
    return output

@route('/unpaid')
def unpaid(db):
    response.headers['Cache-Control'] = 'public, max-age=1200'
    c = db.execute("SELECT blocktime, account, percentage, amount FROM accounts WHERE paid=0 ORDER BY blocktime DESC")
    result = c.fetchall()
    output = template('unpaid', rows=result)
    return output

@route('/paid')
def paid(db):
    response.headers['Cache-Control'] = 'public, max-age=1200'
    c = db.execute("SELECT blocktime, account, percentage, amount FROM accounts WHERE paid>0 ORDER BY blocktime DESC")
    result = c.fetchall()
    output = template('paid', rows=result)
    return output

run(server=PasteServer, port=8888, host='0.0.0.0')
<|endoftext|>


<commit_before>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from pyHMI.Colors import *
from pyHMI.DS_Redis import RedisDevice
from pyHMI.Tag import Tag
import time
import tkinter as tk
from tkinter import ttk

class Devices(object):
    # init datasource
    # PLC TBox
    redis = RedisDevice()

class Tags(object):
    # tags list
    # from PLC
    CP_LOOP_COUNT = Tag(0, src=Devices.redis, ref={'type': 'int', 'key': 'cp4900:cp2redis:loop_count'})
    CP_COM_FAULT= Tag(0, src=Devices.redis, ref={'type': 'bool', 'key': 'cp4900:cp2redis:com_fault'})
    CP_THT = Tag(0, src=Devices.redis, ref={'type': 'float', 'key': 'cp4900:tht'})
    CP_AGE = Tag(0, src=Devices.redis, ref={'type': 'float', 'key': 'cp4900:age'})
    CP_STATE = Tag(0, src=Devices.redis, ref={'type': 'str', 'key': 'cp4900:state'})
    CP_RETENTION_TIME = Tag(0, src=Devices.redis, ref={'type': 'float', 'key': 'cp4900:retention_time'})
    CP_PEAK_AREA = Tag(0, src=Devices.redis, ref={'type': 'float', 'key': 'cp4900:peak_area'})
    CP_PRESSURE_GAS = Tag(0, src=Devices.redis, ref={'type': 'float', 'key': 'cp4900:pressure_gas'})
    CP_FLOW_GAS = Tag(0, src=Devices.redis, ref={'type': 'float', 'key': 'cp4900:flow_gas'})
    CP_ANALYSIS_FAULT = Tag(0, src=Devices.redis, ref={'type': 'int', 'key': 'cp4900:analysis_fault'})
    CP_SENSOR_FAULT = Tag(0, src=Devices.redis, ref={'type': 'int', 'key': 'cp4900:sensor_fault'})
    CP_STATE = Tag(0, src=Devices.redis, ref={'type': 'str', 'key': 'cp4900:state'})
    RPI_SIGFOX_SINCE_TX = Tag(0, src=Devices.redis, ref={'type': 'int', 'key': 'rpi_sigfox:since_tx'})

    @classmethod
    def update_tags(cls):
        # update tags
        pass

class HMITab(tk.Frame):
    def __init__(self, notebook, update_ms=500, *args, **kwargs):
        tk.Frame.__init__(self, notebook, *args, **kwargs)
        # global tk app shortcut
        self.notebook = notebook
        self.app = notebook.master
        # frame auto-refresh delay (in ms)
        self.update_ms = update_ms
        # setup auto-refresh of notebook tab (on-visibility and every update_ms)
        self.bind('<Visibility>', lambda evt: self.tab_update())
        self._tab_update()

    def _tab_update(self):
        if self.winfo_ismapped():
            self.tab_update()
        self.master.after(self.update_ms, self._tab_update)

    def tab_update(self):
        pass

class TabMisc(HMITab):
    def __init__(self, notebook, update_ms=500, *args, **kwargs):
        HMITab.__init__(self, notebook, update_ms, *args, **kwargs)
        # Com state
        self.frmStateCom = tk.LabelFrame(self, text='Etat Com CP-4900 vers Redis', padx=10, pady=10)
        self.frmStateCom.grid(row=0, column=0, padx=5, pady=5, sticky=tk.NSEW)
        self.com_l = HMIBoolList(self.frmStateCom, lbl_args={'width': 25})
        self.com_l.add('Liaison modbus ok', Tags.CP_COM_FAULT, label_1='Liaison modbus en défaut', alarm=True)
        self.com_l.build()
        self.frmInfoRedis = tk.LabelFrame(self, text='Base de données Redis', padx=10, pady=10)
        self.frmInfoRedis.grid(row=1, column=0, padx=5, pady=5, sticky=tk.NSEW)
        self.state_list = HMIAnalogList(self.frmInfoRedis, lbl_args={'width': 15})
        self.state_list.add('Gateway CP-4900 vers DB', Tags.CP_LOOP_COUNT, unit='+1/0.5s')
        self.state_list.add('Ancienneté envoi Sigfox', Tags.RPI_SIGFOX_SINCE_TX, unit='s')
        self.state_list.build()
        # CP values
        self.frmValue = tk.LabelFrame(self, text='Valeurs CP-4900', padx=10, pady=10)
        self.frmValue.grid(row=0, column=1, rowspan=2, padx=5, pady=5, sticky=tk.NSEW)
        self.val_list = HMIAnalogList(self.frmValue, lbl_args={'width': 15})
        self.val_list.add('Etat CP-4900', Tags.CP_STATE)
        self.val_list.add('THT', Tags.CP_THT, unit='mg/nm3', fmt='%.2f')
        self.val_list.add('Ancienneté', Tags.CP_AGE, unit='mn', fmt='%.2f')
        self.val_list.add('Surface du peak', Tags.CP_PEAK_AREA, fmt='%.2f')
        self.val_list.add('Temps de rétention', Tags.CP_RETENTION_TIME, unit='s', fmt='%.2f')
        self.val_list.add('Pression du gaz', Tags.CP_PRESSURE_GAS, unit='mbar', fmt='%.2f')
        self.val_list.add('Débit du gaz', Tags.CP_FLOW_GAS, unit='ml/h', fmt='%.2f')
        self.val_list.add('Défaut analyse', Tags.CP_ANALYSIS_FAULT, unit='ok si égal à 0')
        self.val_list.add('Défaut capteur', Tags.CP_SENSOR_FAULT, unit='ok si < 10000')
        self.val_list.build()

    def tab_update(self):
        self.com_l.update()
        self.state_list.update()
        self.val_list.update()

class HMIToolbar(tk.Frame):
    def __init__(self, tk_app, update_ms=500, *args, **kwargs):
        tk.Frame.__init__(self, tk_app, *args, **kwargs)
        self.tk_app = tk_app
        self.update_ms = update_ms
        # build toolbar
        self.butTbox = tk.Button(self, text='DB Redis', relief=tk.SOLID,
                                 state='disabled', disabledforeground='black')
        self.butTbox.pack(side=tk.LEFT)
        self.lblDate = tk.Label(self, text='', font=('TkDefaultFont', 12))
        self.lblDate.pack(side=tk.RIGHT)
        self.pack(side=tk.BOTTOM, fill=tk.X)
        # setup auto-refresh of notebook tab (on-visibility and every update_ms)
        self.bind('<Visibility>', lambda evt: self.tab_update())
        self._tab_update()

    def _tab_update(self):
        self.tab_update()
        self.master.after(self.update_ms, self._tab_update)

    def tab_update(self):
        self.butTbox.configure(background=GREEN if Devices.redis.connected else PINK)
        self.lblDate.configure(text=time.strftime('%H:%M:%S %d/%m/%Y'))

class HMIApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        # configure main window
        self.wm_title('CP-4900 nano HMI')
        # self.attributes('-fullscreen', True)
        self.geometry("900x300")
        # periodic tags update
        self.do_every(Tags.update_tags, every_ms=500)
        # build a notebook with tabs
        self.note = ttk.Notebook(self)
        self.tab_misc = TabMisc(self.note)
        self.note.add(self.tab_misc, text='CP-4900 (F1)')
        # defaut selected tab
        self.note.select(self.tab_misc)
        self.note.pack(fill=tk.BOTH, expand=True)
        # bind function keys to tabs
        self.bind('<F1>', lambda evt: self.note.select(self.tab_misc))
        # build toolbar
        self.toolbar = HMIToolbar(self, update_ms=500)

    def do_every(self, do_cmd, every_ms=1000):
        do_cmd()
        self.after(every_ms, lambda: self.do_every(do_cmd, every_ms=every_ms))

if __name__ == '__main__':
    # main Tk App
    app = HMIApp()
    app.mainloop()
<commit_msg>fix duplicate CP_STATE<commit_after>#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from pyHMI.Colors import *
from pyHMI.DS_Redis import RedisDevice
from pyHMI.Tag import Tag
import time
import tkinter as tk
from tkinter import ttk

class Devices(object):
    # init datasource
    # PLC TBox
    redis = RedisDevice()

class Tags(object):
    # tags list
    # from PLC
    CP_LOOP_COUNT = Tag(0, src=Devices.redis, ref={'type': 'int', 'key': 'cp4900:cp2redis:loop_count'})
    CP_COM_FAULT= Tag(0, src=Devices.redis, ref={'type': 'bool', 'key': 'cp4900:cp2redis:com_fault'})
    CP_THT = Tag(0, src=Devices.redis, ref={'type': 'float', 'key': 'cp4900:tht'})
    CP_AGE = Tag(0, src=Devices.redis, ref={'type': 'float', 'key': 'cp4900:age'})
    CP_STATE = Tag(0, src=Devices.redis, ref={'type': 'str', 'key': 'cp4900:state'})
    CP_RETENTION_TIME = Tag(0, src=Devices.redis, ref={'type': 'float', 'key': 'cp4900:retention_time'})
    CP_PEAK_AREA = Tag(0, src=Devices.redis, ref={'type': 'float', 'key': 'cp4900:peak_area'})
    CP_PRESSURE_GAS = Tag(0, src=Devices.redis, ref={'type': 'float', 'key': 'cp4900:pressure_gas'})
    CP_FLOW_GAS = Tag(0, src=Devices.redis, ref={'type': 'float', 'key': 'cp4900:flow_gas'})
    CP_ANALYSIS_FAULT = Tag(0, src=Devices.redis, ref={'type': 'int', 'key': 'cp4900:analysis_fault'})
    CP_SENSOR_FAULT = Tag(0, src=Devices.redis, ref={'type': 'int', 'key': 'cp4900:sensor_fault'})
    RPI_SIGFOX_SINCE_TX = Tag(0, src=Devices.redis, ref={'type': 'int', 'key': 'rpi_sigfox:since_tx'})

    @classmethod
    def update_tags(cls):
        # update tags
        pass

class HMITab(tk.Frame):
    def __init__(self, notebook, update_ms=500, *args, **kwargs):
        tk.Frame.__init__(self, notebook, *args, **kwargs)
        # global tk app shortcut
        self.notebook = notebook
        self.app = notebook.master
        # frame auto-refresh delay (in ms)
        self.update_ms = update_ms
        # setup auto-refresh of notebook tab (on-visibility and every update_ms)
        self.bind('<Visibility>', lambda evt: self.tab_update())
        self._tab_update()

    def _tab_update(self):
        if self.winfo_ismapped():
            self.tab_update()
        self.master.after(self.update_ms, self._tab_update)

    def tab_update(self):
        pass

class TabMisc(HMITab):
    def __init__(self, notebook, update_ms=500, *args, **kwargs):
        HMITab.__init__(self, notebook, update_ms, *args, **kwargs)
        # Com state
        self.frmStateCom = tk.LabelFrame(self, text='Etat Com CP-4900 vers Redis', padx=10, pady=10)
        self.frmStateCom.grid(row=0, column=0, padx=5, pady=5, sticky=tk.NSEW)
        self.com_l = HMIBoolList(self.frmStateCom, lbl_args={'width': 25})
        self.com_l.add('Liaison modbus ok', Tags.CP_COM_FAULT, label_1='Liaison modbus en défaut', alarm=True)
        self.com_l.build()
        self.frmInfoRedis = tk.LabelFrame(self, text='Base de données Redis', padx=10, pady=10)
        self.frmInfoRedis.grid(row=1, column=0, padx=5, pady=5, sticky=tk.NSEW)
        self.state_list = HMIAnalogList(self.frmInfoRedis, lbl_args={'width': 15})
        self.state_list.add('Gateway CP-4900 vers DB', Tags.CP_LOOP_COUNT, unit='+1/0.5s')
        self.state_list.add('Ancienneté envoi Sigfox', Tags.RPI_SIGFOX_SINCE_TX, unit='s')
        self.state_list.build()
        # CP values
        self.frmValue = tk.LabelFrame(self, text='Valeurs CP-4900', padx=10, pady=10)
        self.frmValue.grid(row=0, column=1, rowspan=2, padx=5, pady=5, sticky=tk.NSEW)
        self.val_list = HMIAnalogList(self.frmValue, lbl_args={'width': 15})
        self.val_list.add('Etat CP-4900', Tags.CP_STATE)
        self.val_list.add('THT', Tags.CP_THT, unit='mg/nm3', fmt='%.2f')
        self.val_list.add('Ancienneté', Tags.CP_AGE, unit='mn', fmt='%.2f')
        self.val_list.add('Surface du peak', Tags.CP_PEAK_AREA, fmt='%.2f')
        self.val_list.add('Temps de rétention', Tags.CP_RETENTION_TIME, unit='s', fmt='%.2f')
        self.val_list.add('Pression du gaz', Tags.CP_PRESSURE_GAS, unit='mbar', fmt='%.2f')
        self.val_list.add('Débit du gaz', Tags.CP_FLOW_GAS, unit='ml/h', fmt='%.2f')
        self.val_list.add('Défaut analyse', Tags.CP_ANALYSIS_FAULT, unit='ok si égal à 0')
        self.val_list.add('Défaut capteur', Tags.CP_SENSOR_FAULT, unit='ok si < 10000')
        self.val_list.build()

    def tab_update(self):
        self.com_l.update()
        self.state_list.update()
        self.val_list.update()

class HMIToolbar(tk.Frame):
    def __init__(self, tk_app, update_ms=500, *args, **kwargs):
        tk.Frame.__init__(self, tk_app, *args, **kwargs)
        self.tk_app = tk_app
        self.update_ms = update_ms
        # build toolbar
        self.butTbox = tk.Button(self, text='DB Redis', relief=tk.SOLID,
                                 state='disabled', disabledforeground='black')
        self.butTbox.pack(side=tk.LEFT)
        self.lblDate = tk.Label(self, text='', font=('TkDefaultFont', 12))
        self.lblDate.pack(side=tk.RIGHT)
        self.pack(side=tk.BOTTOM, fill=tk.X)
        # setup auto-refresh of notebook tab (on-visibility and every update_ms)
        self.bind('<Visibility>', lambda evt: self.tab_update())
        self._tab_update()

    def _tab_update(self):
        self.tab_update()
        self.master.after(self.update_ms, self._tab_update)

    def tab_update(self):
        self.butTbox.configure(background=GREEN if Devices.redis.connected else PINK)
        self.lblDate.configure(text=time.strftime('%H:%M:%S %d/%m/%Y'))

class HMIApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        # configure main window
        self.wm_title('CP-4900 nano HMI')
        # self.attributes('-fullscreen', True)
        self.geometry("900x300")
        # periodic tags update
        self.do_every(Tags.update_tags, every_ms=500)
        # build a notebook with tabs
        self.note = ttk.Notebook(self)
        self.tab_misc = TabMisc(self.note)
        self.note.add(self.tab_misc, text='CP-4900 (F1)')
        # defaut selected tab
        self.note.select(self.tab_misc)
        self.note.pack(fill=tk.BOTH, expand=True)
        # bind function keys to tabs
        self.bind('<F1>', lambda evt: self.note.select(self.tab_misc))
        # build toolbar
        self.toolbar = HMIToolbar(self, update_ms=500)

    def do_every(self, do_cmd, every_ms=1000):
        do_cmd()
        self.after(every_ms, lambda: self.do_every(do_cmd, every_ms=every_ms))

if __name__ == '__main__':
    # main Tk App
    app = HMIApp()
    app.mainloop()
<|endoftext|>


<commit_before>#!/usr/bin/env python
__author__ = "stakanashi"
import numpy as np
import pandas as pd
from .agps_reader import read_agps

def rot(v, theta, degrees=True):
    if degrees:
        theta = np.radians(theta)
    r = np.array([[np.cos(-theta), -np.sin(-theta)],
                  [np.sin(-theta), np.cos(-theta)]])
    return np.dot(r, v)

def calc_section_force(aoa, mac, centerofgravity, casenum, networknum):
    """calculate local aerodynamic coefficients from the pressure distribution in agps files
    aoa: AoA of the case (e.g. aoa = 2.)
    mac: mean aerodynamic chord of the wing

    reference: https://cfdws.chofu.jaxa.jp/apc/"""
    net_num = 1
    coordinates = ["x", "z"]
    span = "y"
    coor2id = {"x":1, "y":2, "z":3}
    # 迎角、翼弦長等を読み込む

    rot_center = np.asarray(centerofgravity)

    # read the pressure distribution from agps
    dat = read_agps()
    network = dat[net_num-1]

    result = []

    for data in network:
        data = data[:-1]
        # 断面圧力分布を読み込み、局所空力係数を返す
        y = float(data[0,coor2id[span]])
        data_clip = data[:, [coor2id[c] for c in coordinates]]
        diff_coord = (data_clip - np.roll(data_clip, 1, axis=0))
        l = np.linalg.norm(diff_coord, axis=1) # 格子点間の距離
        norm = np.fliplr(diff_coord / l[:,np.newaxis]) # 法線ベクトル
        min_id = np.argmin(data_clip, axis=0) # 座標が最小となる行
        flip = np.array([-1 if col[id] < 0 else 1 for (col, id) in zip(norm.T, min_id)])
        norm = norm * flip # 法線ベクトルを内側に向ける
        chord = np.max(data_clip[:,0]) - np.min(data_clip[:,0])

        coeff_all = [y]
        for (cp, aoa) in zip(data.T[4:], aoas):
            cp2 = cp
            cp1 = np.roll(cp2, 1, axis=0)
            cp3 = np.roll(cp2, -1, axis=0)
            n21 = norm
            n23 = np.roll(n21, -1, axis=0)
            l21 = l * 0.5
            l23 = np.roll(l, -1, axis=0) * 0.5
            cp21 = (0.75 * cp2 + 0.25 * cp1)[:,np.newaxis] * n21
            cp23 = (0.75 * cp2 + 0.25 * cp3)[:,np.newaxis] * n23
            cpdl = cp21 * l21[:,np.newaxis] + cp23 * l23[:,np.newaxis]
            cp_moment = cpdl * np.fliplr(data_clip - rot_center)
            cp_moment = cp_moment[:,0] - cp_moment[:,1]
            coeff = np.sum(cpdl, axis=0) / mac
            coeff = rot(coeff, aoa)
            cm = np.sum(cp_moment, axis=0) / mac / chord
            coeff = coeff.tolist()
            coeff.append(cm)
            coeff_all += coeff
        result.append(coeff_all)
    result = np.array(result)
    columns = ["pos"]
    columns += "cd cl cm".split()
    result = pd.DataFrame(result, columns=columns)
    result.to_csv("section_force.csv", index=False)<commit_msg>Fix calc_section_force<commit_after>#!/usr/bin/env python
__author__ = "stakanashi"
import numpy as np
import pandas as pd
from .agps_reader import read_agps

def rot(v, theta, degrees=True):
    if degrees:
        theta = np.radians(theta)
    r = np.array([[np.cos(-theta), -np.sin(-theta)],
                  [np.sin(-theta), np.cos(-theta)]])
    return np.dot(r, v)

def calc_section_force(aoa, mac, rot_center, casenum, networknum):
    """calculate local aerodynamic coefficients from the pressure distribution in agps files
    :param aoa: AoA of the case in degrees (e.g. 2.94)
    :param mac: mean aerodynamic chord of the wing (e.g. 275.8 [in])
    :param rot_center: center of rotation of the aircraft (e.g. (4.80, 0.00, 0.65) [in])
    :param casenum: case number to calculate the local aerodynamic coefficients (e.g. 1)
    :param networknum: the network number of the wing (e.g. 1)
    reference: JAXA, "APC2 Submission Format," https://cfdws.chofu.jaxa.jp/apc/apc2/submit.html"""
    rot_center = np.asarray(rot_center)

    coordinates = ["x", "z"]
    span = "y"
    coor2id = {"x":1, "y":2, "z":3}

    # read the pressure distribution from agps
    dat = read_agps()
    network = dat[networknum-1]
    result = []

    # calculate the local aerodynamic coefficients for each line in the network
    for line in network:
        line = line[:-1]
        y = float(line[0,coor2id[span]])
        line_clip = line[:, [coor2id[c] for c in coordinates]]
        diff_coord = (line_clip - np.roll(line_clip, 1, axis=0))
        l = np.linalg.norm(diff_coord, axis=1) # 格子点間の距離
        norm = np.fliplr(diff_coord / l[:,np.newaxis]) # 法線ベクトル
        min_id = np.argmin(line_clip, axis=0) # 座標が最小となる行
        flip = np.array([-1 if col[id] < 0 else 1 for (col, id) in zip(norm.T, min_id)])
        norm = norm * flip # 法線ベクトルを内側に向ける
        chord = np.max(line_clip[:,0]) - np.min(line_clip[:,0])

        coeff_all = [y]
        cp2 = data.T[3+casenum:]
        cp1 = np.roll(cp2, 1, axis=0)
        cp3 = np.roll(cp2, -1, axis=0)
        n21 = norm
        n23 = np.roll(n21, -1, axis=0)
        l21 = l * 0.5
        l23 = np.roll(l, -1, axis=0) * 0.5
        cp21 = (0.75 * cp2 + 0.25 * cp1)[:,np.newaxis] * n21
        cp23 = (0.75 * cp2 + 0.25 * cp3)[:,np.newaxis] * n23
        cpdl = cp21 * l21[:,np.newaxis] + cp23 * l23[:,np.newaxis]
        cp_moment = cpdl * np.fliplr(data_clip - rot_center)
        cp_moment = cp_moment[:,0] - cp_moment[:,1]
        coeff = np.sum(cpdl, axis=0) / mac
        coeff = rot(coeff, aoa)
        cm = np.sum(cp_moment, axis=0) / mac / chord
        coeff = coeff.tolist()
        coeff.append(cm)
        coeff_all += coeff
        result.append(coeff_all)
    result = np.array(result)
    columns = ["pos", "cd", "cl", "cm"]
    result = pd.DataFrame(result, columns=columns)
    result.to_csv("section_force.csv", index=False)<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
###############################################################################
#
# ODOO (ex OpenERP)
# Open Source Management Solution
# Copyright (C) 2001-2015 Micronaet S.r.l. (<http://www.micronaet.it>)
# Developer: Nicola Riolini @thebrush (<https://it.linkedin.com/in/thebrush>)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################

import os
import sys
import logging
import openerp
import openerp.addons.decimal_precision as dp
from openerp.osv import fields, osv, expression, orm
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from openerp import SUPERUSER_ID
from openerp import tools
from openerp.tools.translate import _
from openerp.tools import (DEFAULT_SERVER_DATE_FORMAT,
    DEFAULT_SERVER_DATETIME_FORMAT,
    DATETIME_FORMATS_MAP,
    float_compare)

_logger = logging.getLogger(__name__)

class EdiLoadDdtLineWizard(orm.TransientModel):
    ''' Wizard for load DDT report
    '''
    _name = 'edi.load.ddt.line.wizard'

    # --------------------
    # Wizard button event:
    # --------------------
    def action_reload(self, cr, uid, ids, context=None):
        ''' Reload DDT file
            Update Order touched
        '''
        if context is None:
            context = {}

        # 1. Import DDT file from Mexal (previously extracted)

        # 2. Refresh Order in ODOO (from files loaded nightly)

        return True

    def action_export_report(self, cr, uid, ids, context=None):
        ''' Extract DDT report with parameters
        '''
        _logger.info('Start export order VS DDT')
        tollerance = 0.0001

        if context is None:
            context = {}

        # Pool used:
        excel_pool = self.pool.get('excel.writer')
        order_pool = self.pool.get('edi.order')

        # ---------------------------------------------------------------------
        # Parameters and domain filter:
        # ---------------------------------------------------------------------
        wiz_proxy = self.browse(cr, uid, ids, context=context)[0]

        mode = wiz_proxy.mode

        domain = [('has_ddt', '=', True)]
        domain_text = _('Order VS DDT (%s)') % mode

        if wiz_proxy.from_date:
            domain.append(
                ('date', '>=', wiz_proxy.from_date))
            domain_text.append(_(' - From date %s') % wiz_proxy.from_date)
        if wiz_proxy.to_date:
            domain.append(
                ('date', '<=', wiz_proxy.from_date))
            domain_text.append(_(' - To date %s') % wiz_proxy.to_date)

        # ---------------------------------------------------------------------
        # Create XSXL file:
        # ---------------------------------------------------------------------
        ws_name = _('Order')
        row = 0

        excel_pool.create_worksheet(ws_name)

        # ---------------------------------------------------------------------
        # Format elements:
        # ---------------------------------------------------------------------
        excel_pool.set_format()

        f_title = excel_pool.get_format('title')
        f_header = excel_pool.get_format('header')

        f_text = excel_pool.get_format('text')
        f_bg_white = excel_pool.get_format('bg_white')
        f_bg_red = excel_pool.get_format('bg_red')
        f_bg_green = excel_pool.get_format('bg_green')

        f_number = excel_pool.get_format('number')

        # ---------------------------------------------------------------------
        # Format columns:
        # ---------------------------------------------------------------------
        excel_pool.column_width(ws_name, [
            20, 20,
            15, 15,
            15, 15,
            15, 15,
            20,
            ])

        # ---------------------------------------------------------------------
        # Write title and header:
        # ---------------------------------------------------------------------
        # Title:
        excel_pool.write_xls_line(ws_name, row, [
            _('Filter:'),
            domain_text,
            ], default_format=f_title)

        # Header:
        row += 2
        if mode == 'ddt':
            header = [
                _('DDT'),
                _('Date'),
                _('Differenza'),
                ]
        else:
            header = [
                _('DDT'),
                _('Code'),
                _('OC: Pdv'),
                _('DDT: Pdv'),
                _('OC: Q.'),
                _('DDT: Q.'),
                _('OC: Total'),
                _('DDT: Total'),
                _('Differenza'),
                ]

        excel_pool.write_xls_line(
            ws_name, row, header, default_format=f_header)

        # ---------------------------------------------------------------------
        # Extract data:
        # ---------------------------------------------------------------------
        res = {}
        order_ids = order_pool.search(cr, uid, domain, context=context)
        for order in order_pool.browse(cr, uid, order_ids, context=context):
            for check in order.check_ddt_ids:
                ddt = check.ddt_order_id

                if ddt not in res:
                    res[ddt] = [0.0, []]
                    # DDT ref: (gap, check list)

                if abs(check.difference) >= tollerance:
                    res[ddt][0] += check.difference

                if mode != 'ddt': # add line only if not ddt mode
                    res[ddt][1].append(check)

        for ddt in sorted(res, key=lambda x: x.name):
            row += 1
            excel_pool.write_xls_line(ws_name, row, [
                ddt.name,
                ddt.date,
                res[ddt][0],
                ], default_format=f_text)

            # Detailed extra data:
            if mode != 'ddt':
                excel_pool.write_xls_line(ws_name, row, [
                    '%s [%s]' % (ddt.name, ddt.date),
                    '', '', '', '', '', '', '',
                    res[ddt][0],
                    ], default_format=f_text)

                # Detailed check line:
                for check in res[ddt][1]:
                    if mode == 'difference' and abs(res[ddt][0]) <= tollerance:
                        continue # only case to jump

                    row += 1
                    excel_pool.write_xls_line(ws_name, row, [
                        '',
                        check.article,

                        check.order_price,
                        check.invoice_price,

                        check.order_qty,
                        check.invoice_qty,

                        check.order_total,
                        check.invoice_total,

                        res[ddt][0], # total difference:
                        ], default_format=f_text)

        return excel_pool.return_attachment(cr, uid, ws_name,
            version='7.0', php=True, context=context)

    _columns = {
        'mode': fields.selection([
            ('ddt', 'Only DDT'),
            ('difference', 'With difference'),
            ('all', 'All details'),
            ], 'Mode', required=True),

        'from_date': fields.date('From date'),
        'to_date': fields.date('To date'),

        'note': fields.text('Procedure', readonly=True),
        }

    _defaults = {
        'mode': lambda *x: 'ddt',

        'note': lambda *x: _('''
            DDT compare procedure:

            Launch sprix from Mexal Service menu:
            # 780 (Export DDT)

            When the exprot is finished press DONE

            '''),
        }

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
<commit_msg>add color format<commit_after># -*- coding: utf-8 -*-
###############################################################################
#
# ODOO (ex OpenERP)
# Open Source Management Solution
# Copyright (C) 2001-2015 Micronaet S.r.l. (<http://www.micronaet.it>)
# Developer: Nicola Riolini @thebrush (<https://it.linkedin.com/in/thebrush>)
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################

import os
import sys
import logging
import openerp
import openerp.addons.decimal_precision as dp
from openerp.osv import fields, osv, expression, orm
from datetime import datetime, timedelta
from dateutil.relativedelta import relativedelta
from openerp import SUPERUSER_ID
from openerp import tools
from openerp.tools.translate import _
from openerp.tools import (DEFAULT_SERVER_DATE_FORMAT,
    DEFAULT_SERVER_DATETIME_FORMAT,
    DATETIME_FORMATS_MAP,
    float_compare)

_logger = logging.getLogger(__name__)

class EdiLoadDdtLineWizard(orm.TransientModel):
    ''' Wizard for load DDT report
    '''
    _name = 'edi.load.ddt.line.wizard'

    # --------------------
    # Wizard button event:
    # --------------------
    def action_reload(self, cr, uid, ids, context=None):
        ''' Reload DDT file
            Update Order touched
        '''
        if context is None:
            context = {}

        # 1. Import DDT file from Mexal (previously extracted)

        # 2. Refresh Order in ODOO (from files loaded nightly)

        return True

    def action_export_report(self, cr, uid, ids, context=None):
        ''' Extract DDT report with parameters
        '''
        _logger.info('Start export order VS DDT')
        tollerance = 0.0001

        if context is None:
            context = {}

        # Pool used:
        excel_pool = self.pool.get('excel.writer')
        order_pool = self.pool.get('edi.order')

        # ---------------------------------------------------------------------
        # Parameters and domain filter:
        # ---------------------------------------------------------------------
        wiz_proxy = self.browse(cr, uid, ids, context=context)[0]

        mode = wiz_proxy.mode

        domain = [('has_ddt', '=', True)]
        domain_text = _('Order VS DDT (%s)') % mode

        if wiz_proxy.from_date:
            domain.append(
                ('date', '>=', wiz_proxy.from_date))
            domain_text.append(_(' - From date %s') % wiz_proxy.from_date)
        if wiz_proxy.to_date:
            domain.append(
                ('date', '<=', wiz_proxy.from_date))
            domain_text.append(_(' - To date %s') % wiz_proxy.to_date)

        # ---------------------------------------------------------------------
        # Create XSXL file:
        # ---------------------------------------------------------------------
        ws_name = _('Order')
        row = 0

        excel_pool.create_worksheet(ws_name)

        # ---------------------------------------------------------------------
        # Format elements:
        # ---------------------------------------------------------------------
        excel_pool.set_format()

        f_title = excel_pool.get_format('title')
        f_header = excel_pool.get_format('header')

        f_text = excel_pool.get_format('text')

        # Color setup:
        f_bg_white = excel_pool.get_format('bg_white')
        f_bg_red = excel_pool.get_format('bg_red')
        f_bg_green = excel_pool.get_format('bg_green')
        f_bg_yellow = excel_pool.get_format('bg_yellow')
        f_bg_blue = excel_pool.get_format('bg_blue')

        f_bg_white_number = excel_pool.get_format('bg_white_number')
        f_bg_red_number = excel_pool.get_format('bg_red_number')
        f_bg_green_number = excel_pool.get_format('bg_green_number')
        f_bg_yellow_number = excel_pool.get_format('bg_yellow_number')
        f_bg_blue_number = excel_pool.get_format('bg_blue_number')

        f_number = excel_pool.get_format('number')

        # ---------------------------------------------------------------------
        # Format columns:
        # ---------------------------------------------------------------------
        excel_pool.column_width(ws_name, [
            20, 20,
            15, 15,
            15, 15,
            15, 15,
            20,
            ])

        # ---------------------------------------------------------------------
        # Write title and header:
        # ---------------------------------------------------------------------
        # Title:
        excel_pool.write_xls_line(ws_name, row, [
            _('Filter:'),
            domain_text,
            ], default_format=f_title)

        # Header:
        row += 2
        if mode == 'ddt':
            header = [
                _('DDT'),
                _('Date'),
                _('Differenza'),
                ]
        else:
            header = [
                _('DDT'),
                _('Code'),
                _('OC: Pdv'),
                _('DDT: Pdv'),
                _('OC: Q.'),
                _('DDT: Q.'),
                _('OC: Total'),
                _('DDT: Total'),
                _('Differenza'),
                ]

        excel_pool.write_xls_line(
            ws_name, row, header, default_format=f_header)

        # ---------------------------------------------------------------------
        # Extract data:
        # ---------------------------------------------------------------------
        res = {}
        order_ids = order_pool.search(cr, uid, domain, context=context)
        for order in order_pool.browse(cr, uid, order_ids, context=context):
            for check in order.check_ddt_ids:
                ddt = check.ddt_order_id

                if ddt not in res:
                    res[ddt] = [0.0, []]
                    # DDT ref: (gap, check list)

                if abs(check.difference) >= tollerance:
                    res[ddt][0] += check.difference

                if mode != 'ddt': # add line only if not ddt mode
                    res[ddt][1].append(check)

        for ddt in sorted(res, key=lambda x: x.name):
            row += 1

            # DDT Line color depend on difference: (red - green - white)
            difference = res[ddt][0]
            if abs(difference) <= tollerance:
                f_text_default = f_bg_white
                f_number_default = f_bg_white_number
            elif difference < 0:
                f_text_default = f_bg_red
                f_number_default = f_bg_red_number
            else: # >0
                f_text_default = f_bg_green
                f_number_default = f_bg_green_number

            excel_pool.write_xls_line(ws_name, row, [
                ddt.name,
                ddt.date,
                (difference, f_number_default),
                ], default_format=f_text_default)

            # Detailed extra data:
            if mode != 'ddt':
                excel_pool.write_xls_line(ws_name, row, [
                    '%s [%s]' % (ddt.name, ddt.date),
                    '', '', '', '', '', '', '',
                    (res[ddt][0], f_number),
                    ], default_format=f_text)

                # Detailed check line:
                for check in res[ddt][1]:
                    difference = res[ddt][0]
                    if mode == 'difference' and abs(difference) <= tollerance:
                        continue # only case to jump

                    row += 1
                    # DDT Line color depend on status:
                    if check.state == 'correct':
                        f_text_default = f_bg_white
                        f_number_default = f_bg_white_number
                    elif check.state == 'difference':
                        f_text_default = f_bg_red
                        f_number_default = f_bg_red_number
                    elif check.state == 'order':
                        f_text_default = f_bg_blue
                        f_number_default = f_bg_blue_number
                    elif check.state == 'invoice':
                        f_text_default = f_bg_yellow
                        f_number_default = f_bg_yellow_number

                    excel_pool.write_xls_line(ws_name, row, [
                        '',
                        check.article,

                        check.order_price,
                        check.invoice_price,

                        check.order_qty,
                        check.invoice_qty,

                        check.order_total,
                        check.invoice_total,

                        (difference, f_number_default),
                        ], default_format=f_text_default)

        return excel_pool.return_attachment(cr, uid, ws_name,
            version='7.0', php=True, context=context)

    _columns = {
        'mode': fields.selection([
            ('ddt', 'Only DDT'),
            ('difference', 'With difference'),
            ('all', 'All details'),
            ], 'Mode', required=True),

        'from_date': fields.date('From date'),
        'to_date': fields.date('To date'),

        'note': fields.text('Procedure', readonly=True),
        }

    _defaults = {
        'mode': lambda *x: 'ddt',

        'note': lambda *x: _('''
            DDT compare procedure:

            Launch sprix from Mexal Service menu:
            # 780 (Export DDT)

            When the exprot is finished press DONE

            '''),
        }

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
<|endoftext|>