summaryrefslogtreecommitdiffstats
path: root/Monitoring/src/main/python/DataProcessing/Sampler.py.old
blob: f1c0622b34ccfa284b01790f6910471eb4c40af7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
'''
Created on Nov 20, 2012

@author: steger
'''
from DataProcessing.DataReader import DataReader
from DataProcessing.DataHeader import DataHeader
from DataProcessing.DataSource import DataSource
from DataProcessing.Data import Data
from DataProcessing.DataError import SamplerError

class Sampler(DataSource):
    '''
    classdocs
    '''

    def __init__(self, datasource):
        '''
        Constructor
        @param datasource: table of records to manipulate with
        @type datasource: DataSource
        '''
        if not isinstance(datasource, DataSource):
            raise SamplerError("Wrong type of datasource %s" % datasource)
        DataSource.__init__(self, datasource)
        self._reader = DataReader(datasource = datasource._data)
        header = DataHeader("%sSample(%s)" % (self.name, self.source.name))
        for c in self._reader.headercells():
            header.addColumn(c)
        self._data = Data(self.um, header)
    
    @property
    def header(self):
        return self._data.header

    def __len__(self):
        self.process()
        return len(self._data)

    def __getitem__(self, k):
        return self._data._rawrecords.__getitem__(k)


class Head(Sampler):
    def __init__(self, datasource, head = 10):
        '''
        Constructor
        @param datasource: table of records to manipulate with
        @type datasource: DataSource
        @param head: the top n records of the table
        @type head: int 
        '''
        Sampler.__init__(self, datasource)
        self._head = head

    @property
    def name(self):
        return "Head"

    @property
    def head(self):
        return self._head
    @head.setter
    def head(self, head):
        self._head = int(head)
        self._data.clear()
        self._reader.rewind()
    
    def _process(self):
        if self._reader.sourceCleared.isSet():
            self._reader.sourceCleared.clear()
            self._reader.rewind()
            self._data.clear()
        if len(self._data) == self.head:
            return
        for x in self._reader:
            self._data._rawrecords.append(x)
            if len(self._data) == self.head:
                self._data._onexpand()
                return
        raise SamplerError("Not enough sample %d/%d" % (len(self._data), self.head))
        
class Tail(Sampler):
    def __init__(self, datasource, tail = 10):
        '''
        Constructor
        @param datasource: table of records to manipulate with
        @type datasource: DataSource
        @param tail: the last n records of the table
        @type tail: int 
        '''
        Sampler.__init__(self, datasource)
        self._tail = tail
    
    @property
    def name(self):
        return "Tail"

    @property
    def tail(self):
        return self._tail
    @tail.setter
    def tail(self, tail):
        self._tail = int(tail)
        self._data.clear()
        self._reader.rewind()

    def _process(self):
        clear = False
        if self._reader.sourceCleared.isSet():
            self._reader.sourceCleared.clear()
            self._reader.rewind()
            self._data.clear()
        for x in self._reader:
            if len(self._data) == self.tail:
                self._data._rawrecords.pop(0)
                clear = True
            self._data._rawrecords.append(x)
        if clear:
            self._data._onclear()
        if len(self._data) == self.tail:
            self._data._onexpand()
        else:
            raise SamplerError("Not enough sample %d/%d" % (len(self._data), self.tail))

class Sorter(Sampler):
    def __init__(self, datasource, keycell = None, ascending = True):
        '''
        Constructor
        @param datasource: table of records to manipulate with
        @type datasource: DataSource
        @param keycell: the key column to use for sorting
        @type keycell: CellRequest or None
        @param ascending: indicate the sortin order
        @type ascending: bool 
        '''
        Sampler.__init__(self, datasource)
        self._asc = ascending
        self._key = 0
        if keycell:
            self.keycell = keycell
    
    @property
    def name(self):
        return "Sort"
    
    @property
    def ascending(self):
        return self._asc
    @ascending.setter
    def ascending(self, ascending):
        if bool(ascending) != self._asc:
            self._asc = bool(ascending)
            self._data.clear()
            self._reader.rewind()
    
    @property
    def key(self):
        return self._key
    @key.setter
    def key(self, key):
        self._key = int(key)
    
    @property
    def keycell(self):
        raise SamplerError("don't read this property")
    @keycell.setter
    def keycell(self, cellrequest):
        for idx, _ in self.source.header.getCell(cellrequest):
            self._key = idx
            break
    
    def _process(self):
        clear = False
        if self._reader.sourceCleared.isSet():
            self._reader.sourceCleared.clear()
            clear = True
        if self._reader.sourceExpanded.isSet():
            self._reader.sourceExpanded.clear()
            clear = True
        if not clear:
            return
        self._reader.rewind()
        self._data.clear()
        self._data._rawrecords = sorted(self._reader.source._rawrecords, key=lambda r: r[self.key], reverse = not self.ascending)
        if len(self._data):
            self._data._onclear()
            self._data._onexpand()
        else:
            raise SamplerError("Not enough sample...")