Commit 9c13b80e authored by Volker Krause's avatar Volker Krause
Browse files

Load and store survey targeting expressions on the server

parent 8bb07aa5
......@@ -63,12 +63,14 @@
<column>col_name</column>
<column>col_url</column>
<column>col_active</column>
<column>col_target</column>
<row>
<value>101</value>
<value>2</value>
<value>myActiveSurvey</value>
<value>http://survey.example/active</value>
<value>1</value>
<value>usageTime.value &gt;= 3600</value>
</row>
<row>
<value>102</value>
......@@ -76,6 +78,7 @@
<value>myInactiveSurvey</value>
<value>http://survey.example/inactive</value>
<value>0</value>
<value>screen[0].dpi &lt; 200</value>
</row>
</table>
<table name="tbl_aggregation">
......
......@@ -104,6 +104,7 @@ private slots:
s.setName(QStringLiteral("unitTestSurvey"));
s.setUrl(QUrl(QStringLiteral("http://www.kde.org")));
s.setActive(false);
s.setTarget(QStringLiteral("screen[0].dpi >= 200"));
reply = RESTApi::createSurvey(&client, p, s);
QVERIFY(waitForFinished(reply));
QCOMPARE(reply->error(), QNetworkReply::NoError);
......
......@@ -28,12 +28,14 @@ class SurveyTest extends AbstractDatastoreTest
"name": "mySurvey",
"url": "http://survey.example/",
"active": true,
"target": "screen[0].dpi > 100",
"id": 42
}');
$this->assertEquals($s->name, 'mySurvey');
$this->assertEquals($s->url, 'http://survey.example/');
$this->assertEquals($s->active, true);
$this->assertEquals($s->id, 42);
$this->assertEquals($s->target, 'screen[0].dpi > 100');
}
public function testToJson()
......@@ -43,12 +45,14 @@ class SurveyTest extends AbstractDatastoreTest
$s->url = 'http://survey.example/';
$s->active = true;
$s->id = 42;
$s->target = 'screen[0].dpi > 100';
$this->assertJsonStringEqualsJsonString(json_encode($s), '{
"name": "mySurvey",
"url": "http://survey.example/",
"active": true,
"id": 42
"id": 42,
"target": "screen[0].dpi > 100"
}');
}
......@@ -73,10 +77,12 @@ class SurveyTest extends AbstractDatastoreTest
$this->assertEquals($survey1->name, 'myActiveSurvey');
$this->assertEquals($survey1->url, 'http://survey.example/active');
$this->assertEquals($survey1->active, true);
$this->assertEquals($survey1->target, 'usageTime.value >= 3600');
$this->assertEquals($survey2->name, 'myInactiveSurvey');
$this->assertEquals($survey2->url, 'http://survey.example/inactive');
$this->assertEquals($survey2->active, false);
$this->assertEquals($survey2->target, 'screen[0].dpi < 200');
$p = Product::productByName(self::$db, 'org.kde.UnitTest');
$this->assertNotNull($p);
......@@ -88,6 +94,7 @@ class SurveyTest extends AbstractDatastoreTest
$this->assertEquals($s->name, 'myActiveSurvey');
$this->assertEquals($s->url, 'http://survey.example/active');
$this->assertEquals($s->active, true);
$this->assertEquals($s->target, 'usageTime.value >= 3600');
}
public function testSurveyInsert()
......@@ -95,7 +102,8 @@ class SurveyTest extends AbstractDatastoreTest
$json = '{
"name": "newSurvey",
"url": "http://survey.example/new",
"active": true
"active": true,
"target": "startCount.value > 10"
}';
$p = Product::productByName(self::$db, 'org.kde.UnitTest');
......@@ -117,6 +125,7 @@ class SurveyTest extends AbstractDatastoreTest
$this->assertEquals($s->name, 'newSurvey');
$this->assertEquals($s->url, 'http://survey.example/new');
$this->assertEquals($s->active, true);
$this->assertEquals($s->target, 'startCount.value > 10');
}
public function testSurveyUpdate()
......@@ -125,7 +134,8 @@ class SurveyTest extends AbstractDatastoreTest
"name": "myChangedSurvey",
"url": "http://survey.example/changed",
"active": false,
"id": 101
"id": 101,
"target": "views[\"editor\"].ratio > 0.5"
}';
$s = Survey::fromJson($json);
......@@ -143,6 +153,7 @@ class SurveyTest extends AbstractDatastoreTest
$this->assertEquals($s->name, 'myChangedSurvey');
$this->assertEquals($s->url, 'http://survey.example/changed');
$this->assertEquals($s->active, false);
$this->assertEquals($s->target, 'views["editor"].ratio > 0.5');
}
public function testSurveyDelete()
......
......@@ -35,6 +35,7 @@ class SurveyData : public QSharedData
public:
QString name;
QUrl url;
QString target;
int id = -1;
bool active = false;
};
......@@ -51,6 +52,7 @@ bool Survey::operator==(const Survey& other) const
{
return d->name == other.d->name
&& d->url == other.d->url
&& d->target == other.d->target
&& d->id == other.d->id
&& d->active == other.d->active;
}
......@@ -100,6 +102,16 @@ void Survey::setActive(bool active)
d->active = active;
}
QString Survey::target() const
{
return d->target;
}
void Survey::setTarget(const QString& target)
{
d->target = target;
}
QByteArray Survey::toJson() const
{
QJsonObject obj;
......@@ -107,6 +119,7 @@ QByteArray Survey::toJson() const
obj.insert(QStringLiteral("name"), name());
obj.insert(QStringLiteral("url"), url().toString());
obj.insert(QStringLiteral("active"), isActive());
obj.insert(QStringLiteral("target"), target());
return QJsonDocument(obj).toJson();
}
......@@ -117,15 +130,16 @@ QVector<Survey> Survey::fromJson(const QByteArray &data)
const auto obj = v.toObject();
Survey survey;
const auto id = obj.value(QStringLiteral("id"));
const auto id = obj.value(QLatin1String("id"));
// TODO move this to a helper function
if (id.isDouble())
survey.setId(id.toInt(-1));
else if (id.isString())
survey.setId(id.toString().toInt());
survey.setName(obj.value(QStringLiteral("name")).toString());
survey.setUrl(QUrl(obj.value(QStringLiteral("url")).toString()));
survey.setActive(obj.value(QStringLiteral("active")).toBool());
survey.setName(obj.value(QLatin1String("name")).toString());
survey.setUrl(QUrl(obj.value(QLatin1String("url")).toString()));
survey.setActive(obj.value(QLatin1String("active")).toBool());
survey.setTarget(obj.value(QLatin1String("target")).toString());
surveys.push_back(survey);
}
return surveys;
......
......@@ -54,6 +54,9 @@ public:
bool isActive() const;
void setActive(bool enabled);
QString target() const;
void setTarget(const QString &target);
QByteArray toJson() const;
static QVector<Survey> fromJson(const QByteArray &data);
private:
......
......@@ -38,6 +38,12 @@
"sqlite": ["CREATE TABLE tbl_aggregation (col_id INTEGER PRIMARY KEY AUTOINCREMENT, col_product_id INTEGER REFERENCES tbl_product (col_id) ON DELETE CASCADE, col_type VARCHAR NOT NULL, col_elements VARCHAR)" ],
"mysql": ["CREATE TABLE tbl_aggregation (col_id INTEGER PRIMARY KEY AUTO_INCREMENT, col_product_id INTEGER REFERENCES tbl_product (col_id) ON DELETE CASCADE, col_type VARCHAR(255) NOT NULL, col_elements VARCHAR(255))" ],
"pgsql": ["CREATE TABLE tbl_aggregation (col_id SERIAL PRIMARY KEY, col_product_id INTEGER REFERENCES tbl_product (col_id) ON DELETE CASCADE, col_type VARCHAR NOT NULL, col_elements VARCHAR)" ]
},
{
"version": 7,
"sql": ["ALTER TABLE tbl_survey ADD COLUMN col_target VARCHAR"],
"mysql": ["ALTER TABLE tbl_survey ADD COLUMN col_target VARCHAR(255)"]
}
]
}
......@@ -27,12 +27,13 @@ class Survey
public $url = '';
public $id = -1;
public $active = false;
public $target = null;
/** Returns an array of all surveys for @p productName. */
public function surveysForProduct(Datastore $db, $productName)
{
$stmt = $db->prepare('
SELECT tbl_survey.col_id, tbl_survey.col_name, tbl_survey.col_url, tbl_survey.col_active
SELECT tbl_survey.col_id, tbl_survey.col_name, tbl_survey.col_url, tbl_survey.col_active, tbl_survey.col_target
FROM tbl_survey JOIN tbl_product ON (tbl_survey.col_product_id = tbl_product.col_id)
WHERE tbl_product.col_name = :productName
');
......@@ -46,6 +47,7 @@ class Survey
$s->name = strval($row['col_name']);
$s->url = strval($row['col_url']);
$s->active = boolval($row['col_active']);
$s->target = strval($row['col_target']);
array_push($surveys, $s);
}
......@@ -55,7 +57,7 @@ class Survey
/** Returns an array of all active surveys for @p product. */
public function activeSurveysForProduct(Datastore $db, Product $product)
{
$stmt = $db->prepare('SELECT col_id, col_name, col_url FROM tbl_survey WHERE col_product_id = :productId AND col_active = :active');
$stmt = $db->prepare('SELECT col_id, col_name, col_url, col_target FROM tbl_survey WHERE col_product_id = :productId AND col_active = :active');
$stmt->bindValue(':productId', $product->id(), PDO::PARAM_INT);
$stmt->bindValue(':active', true, PDO::PARAM_BOOL);
$db->execute($stmt);
......@@ -67,6 +69,7 @@ class Survey
$s->name = strval($row['col_name']);
$s->url = strval($row['col_url']);
$s->active = true;
$s->target = strval($row['col_target']);
array_push($surveys, $s);
}
......@@ -77,13 +80,14 @@ class Survey
public function insert(Datastore $db, Product $product)
{
$stmt = $db->prepare('
INSERT INTO tbl_survey (col_product_id, col_name, col_url, col_active)
VALUES (:productId, :name, :url, :active)
INSERT INTO tbl_survey (col_product_id, col_name, col_url, col_active, col_target)
VALUES (:productId, :name, :url, :active, :target)
');
$stmt->bindValue(':productId', $product->id(), PDO::PARAM_INT);
$stmt->bindValue(':name', $this->name, PDO::PARAM_STR);
$stmt->bindValue(':url', $this->url, PDO::PARAM_STR);
$stmt->bindValue(':active', $this->active, PDO::PARAM_BOOL);
$stmt->bindValue(':target', $this->target, PDO::PARAM_STR);
$db->execute($stmt);
$this->id = $db->pdoHandle()->lastInsertId();
}
......@@ -95,12 +99,14 @@ class Survey
UPDATE tbl_survey SET
col_name = :name,
col_url = :url,
col_active = :active
col_active = :active,
col_target = :target
WHERE col_id = :surveyId
');
$stmt->bindValue(':name', $this->name, PDO::PARAM_STR);
$stmt->bindValue(':url', $this->url, PDO::PARAM_STR);
$stmt->bindValue(':active', $this->active, PDO::PARAM_BOOL);
$stmt->bindValue(':target', $this->target, PDO::PARAM_STR);
$stmt->bindValue(':surveyId', $this->id, PDO::PARAM_INT);
$db->execute($stmt);
}
......@@ -124,6 +130,8 @@ class Survey
$s->name = $jsonObj->name;
$s->url = strval($jsonObj->url);
$s->active = boolval($jsonObj->active);
if (property_exists($jsonObj, 'target'))
$s->target = strval($jsonObj->target);
if (property_exists($jsonObj, 'id'))
$s->id = $jsonObj->id;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment