Skip to content

Commit c99b208

Browse files
committed
Added dayna's changes for db; updated object defs for reviewer and model
1 parent e931ae5 commit c99b208

File tree

3 files changed

+164
-58
lines changed

3 files changed

+164
-58
lines changed

database/war.sql

+87-13
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ create table PeopleReviews(
4848

4949
--Models--
5050
create table Models(
51-
modelId SERIAL PRIMARY KEY,
52-
modelVersion SERIAL,
51+
modelId SERIAL PRIMARY KEY,
52+
modelVersion text not null,
5353
balAccuracy decimal not null,
54-
loc text not null,
55-
dateAdded timestamp not null
56-
UNIQUE(modelId, modelVersion)
54+
isActive boolean not null,
55+
dateAdded timestamp not null,
56+
ruleId integer not null references Rules(ruleId)
5757
);
5858

5959
--Model Reviews--
@@ -146,20 +146,20 @@ returns int as $$
146146
end;
147147
$$ language 'plpgsql';
148148

149-
create function getCorrectReviewsCount(sentence int)
149+
create or replace function getCorrectReviewsCount(sentence int)
150150
returns int as $$
151151
begin
152-
return(select count(pr.rulereview)
152+
return(select count(pr.rulereview)/5
153153
from peoplereviews pr inner join sentencerules sr on pr.sentenceid = sr.sentenceid
154154
where pr.sentenceid = 1 and pr.rulereviewid = sr.taggedruleid and pr.rulereview = 1
155155
group by pr.rulereviewid);
156156
end;
157157
$$ language 'plpgsql';
158158

159-
create function getIncorrectReviewsCount(sentence int)
159+
create or replace function getIncorrectReviewsCount(sentence int)
160160
returns int as $$
161161
begin
162-
return(select count(pr.rulereview)
162+
return(select count(pr.rulereview)/5
163163
from peoplereviews pr inner join sentencerules sr on pr.sentenceid = sr.sentenceid
164164
where pr.sentenceid = 1 and pr.rulereviewid = sr.taggedruleid and pr.rulereview = 0
165165
group by pr.rulereviewid);
@@ -169,23 +169,28 @@ $$ language 'plpgsql';
169169
CREATE OR REPLACE PROCEDURE getSentenceReviewStatus(sentence int)
170170
AS $$
171171
begin
172-
if (getTotalCountOfReviews(sentence) == 5) then
173-
if (getCorrectReviewsCount(sentence) == 5) or (getIncorrectReviewsCount(sentence) == 5) then
172+
if (getTotalCountOfReviews(sentence) = 5) then
173+
if (getCorrectReviewsCount(sentence) = 5) or (getIncorrectReviewsCount(sentence) == 5) then
174174
perform sendToDataset(sentence);
175175
end if;
176176
elsif (getTotalCountOfReviews(sentence) > 5) then
177177
if ((getCorrectReviewsCount(sentence) / getIncorrectReviewsCount(sentence)) >= .75 and
178178
(getCorrectReviewsCount(sentence) / getIncorrectReviewsCount(sentence)) < 1) then
179179
perform sendToDataset(sentence);
180+
update sentenceRules
181+
set status = 'Corrected'
182+
where sentenceid = sentence;
180183
elsif ((getIncorrectReviewsCount(sentence) / getCorrectReviewsCount(sentence)) >= .75 and
181184
(getIncorrectReviewsCount(sentence) / getCorrectReviewsCount(sentence)) < 1) then
182185
perform sendToDataset(sentence);
186+
update sentenceRules
187+
set status = 'Corrected'
188+
where sentenceid = sentence;
183189
end if;
184190
end if;
185191
end;
186192
$$ language plpgsql;
187193

188-
189194
create function getSentenceFromID(sentID int)
190195
returns text as $$
191196
begin
@@ -205,4 +210,73 @@ BEGIN
205210
group by rulereviewid;
206211
END
207212
$$
208-
LANGUAGE plpgsql;
213+
LANGUAGE plpgsql;
214+
215+
CREATE OR REPLACE FUNCTION SentenceToBeReviewed (userID INT)
216+
RETURNS TABLE(sentenceid INT) AS $$
217+
BEGIN
218+
RETURN QUERY select sr.sentenceid
219+
from sentencerules sr inner join rules r on sr.taggedruleid = r.ruleid
220+
where sr.sentenceid not in (select sentenceid
221+
from peoplereviews
222+
where reviewerid = userID)
223+
and sr.status = 'In Review'
224+
order by priority desc
225+
limit 1;
226+
END;
227+
$$ LANGUAGE 'plpgsql';
228+
229+
create or replace procedure updatePriority()
230+
as $$
231+
declare temprow rules%rowtype;
232+
declare counter int := 1;
233+
begin
234+
for temprow in select ruleid, count(sr.sentenceid) filter (where sr.status = 'Corrected')
235+
from rules r left outer join sentencerules sr on r.ruleid = sr.taggedruleid
236+
group by r.ruleid
237+
order by count(sr.sentenceid) desc
238+
loop
239+
update rules
240+
set priority = counter
241+
where ruleid = temprow.ruleid;
242+
counter := counter + 1;
243+
end loop;
244+
end;
245+
$$ language plpgsql;
246+
247+
create or replace function getRuleGrade(sentid int)
248+
returns decimal as $$
249+
begin
250+
return (select td.rulecorrect
251+
from trainingdataset td inner join sentencerules sr on td.sentenceid = sr.sentenceid
252+
where td.rulecorrectid = sr.taggedruleid
253+
and td.sentenceid = sentid);
254+
end;
255+
$$ language plpgsql;
256+
257+
258+
CREATE OR REPLACE PROCEDURE updateReputation(sentID int)
259+
LANGUAGE sql
260+
AS $$
261+
UPDATE reviewers r
262+
SET reputation = reputation + (CASE WHEN
263+
(getRuleGrade(sentID) > .5)
264+
THEN
265+
CASE WHEN pr.rulereview = 1 THEN
266+
+10
267+
ELSE
268+
-10
269+
END
270+
ELSE
271+
CASE WHEN pr.rulereview = 0 THEN
272+
+10
273+
ELSE
274+
-10
275+
END
276+
END)
277+
FROM peoplereviews pr
278+
INNER JOIN sentencerules sr ON (pr.sentenceid = sr.sentenceid
279+
AND pr.rulereviewid = sr.taggedruleid)
280+
WHERE pr.reviewerid = r.reviewerid
281+
AND pr.sentenceid = sentID;
282+
$$;

swagger_server/models/model.py

+59-29
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,46 @@ class Model(Model):
1515
Do not edit the class manually.
1616
"""
1717

18-
def __init__(self, model_id: int=None, model_version: int=None, bal_accuracy: float=None, location: str=None, date_added: str=None): # noqa: E501
18+
def __init__(self, model_id: int=None, model_version: str=None, bal_accuracy: float=None, is_active: bool=None, date_added: str=None, rule_id: int=None): # noqa: E501
1919
"""Model - a model defined in Swagger
2020
2121
:param model_id: The model_id of this Model. # noqa: E501
2222
:type model_id: int
2323
:param model_version: The model_version of this Model. # noqa: E501
24-
:type model_version: int
24+
:type model_version: str
2525
:param bal_accuracy: The bal_accuracy of this Model. # noqa: E501
2626
:type bal_accuracy: float
27-
:param location: The location of this Model. # noqa: E501
28-
:type location: str
27+
:param is_active: The is_active of this Model. # noqa: E501
28+
:type is_active: bool
2929
:param date_added: The date_added of this Model. # noqa: E501
3030
:type date_added: str
31+
:param rule_id: The rule_id of this Model. # noqa: E501
32+
:type rule_id: int
3133
"""
3234
self.swagger_types = {
3335
'model_id': int,
34-
'model_version': int,
36+
'model_version': str,
3537
'bal_accuracy': float,
36-
'location': str,
37-
'date_added': str
38+
'is_active': bool,
39+
'date_added': str,
40+
'rule_id': int
3841
}
3942

4043
self.attribute_map = {
4144
'model_id': 'modelID',
4245
'model_version': 'modelVersion',
4346
'bal_accuracy': 'balAccuracy',
44-
'location': 'location',
45-
'date_added': 'dateAdded'
47+
'is_active': 'isActive',
48+
'date_added': 'dateAdded',
49+
'rule_id': 'ruleId'
4650
}
4751

4852
self._model_id = model_id
4953
self._model_version = model_version
5054
self._bal_accuracy = bal_accuracy
51-
self._location = location
55+
self._is_active = is_active
5256
self._date_added = date_added
57+
self._rule_id = rule_id
5358

5459
@classmethod
5560
def from_dict(cls, dikt) -> 'Model':
@@ -86,24 +91,24 @@ def model_id(self, model_id: int):
8691
self._model_id = model_id
8792

8893
@property
89-
def model_version(self) -> int:
94+
def model_version(self) -> str:
9095
"""Gets the model_version of this Model.
9196
9297
Version of the model # noqa: E501
9398
9499
:return: The model_version of this Model.
95-
:rtype: int
100+
:rtype: str
96101
"""
97102
return self._model_version
98103

99104
@model_version.setter
100-
def model_version(self, model_version: int):
105+
def model_version(self, model_version: str):
101106
"""Sets the model_version of this Model.
102107
103108
Version of the model # noqa: E501
104109
105110
:param model_version: The model_version of this Model.
106-
:type model_version: int
111+
:type model_version: str
107112
"""
108113
if model_version is None:
109114
raise ValueError("Invalid value for `model_version`, must not be `None`") # noqa: E501
@@ -136,29 +141,29 @@ def bal_accuracy(self, bal_accuracy: float):
136141
self._bal_accuracy = bal_accuracy
137142

138143
@property
139-
def location(self) -> str:
140-
"""Gets the location of this Model.
144+
def is_active(self) -> bool:
145+
"""Gets the is_active of this Model.
141146
142-
absolute path to model on disk # noqa: E501
147+
is the current model in use for a given rule # noqa: E501
143148
144-
:return: The location of this Model.
145-
:rtype: str
149+
:return: The is_active of this Model.
150+
:rtype: bool
146151
"""
147-
return self._location
152+
return self._is_active
148153

149-
@location.setter
150-
def location(self, location: str):
151-
"""Sets the location of this Model.
154+
@is_active.setter
155+
def is_active(self, is_active: bool):
156+
"""Sets the is_active of this Model.
152157
153-
absolute path to model on disk # noqa: E501
158+
is the current model in use for a given rule # noqa: E501
154159
155-
:param location: The location of this Model.
156-
:type location: str
160+
:param is_active: The is_active of this Model.
161+
:type is_active: bool
157162
"""
158-
if location is None:
159-
raise ValueError("Invalid value for `location`, must not be `None`") # noqa: E501
163+
if is_active is None:
164+
raise ValueError("Invalid value for `is_active`, must not be `None`") # noqa: E501
160165

161-
self._location = location
166+
self._is_active = is_active
162167

163168
@property
164169
def date_added(self) -> str:
@@ -184,3 +189,28 @@ def date_added(self, date_added: str):
184189
raise ValueError("Invalid value for `date_added`, must not be `None`") # noqa: E501
185190

186191
self._date_added = date_added
192+
193+
@property
194+
def rule_id(self) -> int:
195+
"""Gets the rule_id of this Model.
196+
197+
The Rule ID for the corresponding rule in the rules table # noqa: E501
198+
199+
:return: The rule_id of this Model.
200+
:rtype: int
201+
"""
202+
return self._rule_id
203+
204+
@rule_id.setter
205+
def rule_id(self, rule_id: int):
206+
"""Sets the rule_id of this Model.
207+
208+
The Rule ID for the corresponding rule in the rules table # noqa: E501
209+
210+
:param rule_id: The rule_id of this Model.
211+
:type rule_id: int
212+
"""
213+
if rule_id is None:
214+
raise ValueError("Invalid value for `rule_id`, must not be `None`") # noqa: E501
215+
216+
self._rule_id = rule_id

swagger_server/models/reviewer.py

+18-16
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Reviewer(Model):
1515
Do not edit the class manually.
1616
"""
1717

18-
def __init__(self, reviewer_id: int=None, email_address: str=None, first_name: str=None, last_name: str=None, admin: bool=None, reputation: int=None): # noqa: E501
18+
def __init__(self, reviewer_id: int=None, email_address: str=None, first_name: str=None, last_name: str=None, is_admin: bool=None, reputation: int=None): # noqa: E501
1919
"""Reviewer - a model defined in Swagger
2020
2121
:param reviewer_id: The reviewer_id of this Reviewer. # noqa: E501
@@ -26,8 +26,8 @@ def __init__(self, reviewer_id: int=None, email_address: str=None, first_name: s
2626
:type first_name: str
2727
:param last_name: The last_name of this Reviewer. # noqa: E501
2828
:type last_name: str
29-
:param admin: The admin of this Reviewer. # noqa: E501
30-
:type admin: bool
29+
:param is_admin: The is_admin of this Reviewer. # noqa: E501
30+
:type is_admin: bool
3131
:param reputation: The reputation of this Reviewer. # noqa: E501
3232
:type reputation: int
3333
"""
@@ -36,7 +36,7 @@ def __init__(self, reviewer_id: int=None, email_address: str=None, first_name: s
3636
'email_address': str,
3737
'first_name': str,
3838
'last_name': str,
39-
'admin': bool,
39+
'is_admin': bool,
4040
'reputation': int
4141
}
4242

@@ -45,15 +45,15 @@ def __init__(self, reviewer_id: int=None, email_address: str=None, first_name: s
4545
'email_address': 'emailAddress',
4646
'first_name': 'firstName',
4747
'last_name': 'lastName',
48-
'admin': 'admin',
48+
'is_admin': 'isAdmin',
4949
'reputation': 'reputation'
5050
}
5151

5252
self._reviewer_id = reviewer_id
5353
self._email_address = email_address
5454
self._first_name = first_name
5555
self._last_name = last_name
56-
self._admin = admin
56+
self._is_admin = is_admin
5757
self._reputation = reputation
5858

5959
@classmethod
@@ -166,27 +166,29 @@ def last_name(self, last_name: str):
166166
self._last_name = last_name
167167

168168
@property
169-
def admin(self) -> bool:
170-
"""Gets the admin of this Reviewer.
169+
def is_admin(self) -> bool:
170+
"""Gets the is_admin of this Reviewer.
171171
172172
Denotes wether this user has admin privileges # noqa: E501
173173
174-
:return: The admin of this Reviewer.
174+
:return: The is_admin of this Reviewer.
175175
:rtype: bool
176176
"""
177-
return self._admin
177+
return self._is_admin
178178

179-
@admin.setter
180-
def admin(self, admin: bool):
181-
"""Sets the admin of this Reviewer.
179+
@is_admin.setter
180+
def is_admin(self, is_admin: bool):
181+
"""Sets the is_admin of this Reviewer.
182182
183183
Denotes wether this user has admin privileges # noqa: E501
184184
185-
:param admin: The admin of this Reviewer.
186-
:type admin: bool
185+
:param is_admin: The is_admin of this Reviewer.
186+
:type is_admin: bool
187187
"""
188+
if is_admin is None:
189+
raise ValueError("Invalid value for `is_admin`, must not be `None`") # noqa: E501
188190

189-
self._admin = admin
191+
self._is_admin = is_admin
190192

191193
@property
192194
def reputation(self) -> int:

0 commit comments

Comments
 (0)