Skip to content

Commit b3cb71e

Browse files
committed
Work on API pagination
1 parent fd02e54 commit b3cb71e

File tree

5 files changed

+47
-12
lines changed

5 files changed

+47
-12
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,5 +200,20 @@ curl -X POST -u USER:PASS -H "Content-Type: application/json" http://127.0.0.1/e
200200
'
201201
```
202202

203-
Note: `software_version` and `storage_name` must exist in the corresponding EMS database tables.
204-
The `file_path` will be automatically created in the `file_` table, if not there yet.
203+
The GET response also includes some extra data, like this (`limit` is the maximum number of events
204+
that were requested or could be returned, `offset` is offset starting from which events were retrieved,
205+
`count` is equal to actual number of events in `events`:
206+
207+
```
208+
{
209+
"events": [ ... ]
210+
"limit": 50000,
211+
"offset": 0,
212+
"count": 100
213+
}
214+
```
215+
216+
Note: when POSTing or PUTing events, `software_version` and `storage_name` must exist in the corresponding
217+
EMS database tables beforehand. However, the `file_path` entry will be automatically created in
218+
the `file_` table, if not there yet.
219+

src/commonMain/kotlin/ConfigFile.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package ru.mipt.npm.nica.ems
22

33
import kotlinx.serialization.Serializable
44

5-
val DEFAULT_LIMIT_FOR_WEB = 1000
5+
val DEFAULT_LIMIT_FOR_WEB = 10_000L
6+
val DEFAULT_LIMIT_FOR_API = 50_000L
7+
val MAX_LIMIT_FOR_API = 100_000L
68

79
/**
810
* Determines configuration file structure
@@ -46,7 +48,8 @@ class PageConfig(
4648
val parameters: List<ParameterConfig>,
4749
// By default, this number of records is returned in WebUI if limit is not specified
4850
// does not need to be specified in YAML (uses given default value if not specified)
49-
val default_limit_web: Int = DEFAULT_LIMIT_FOR_WEB
51+
val default_limit_web: Long = DEFAULT_LIMIT_FOR_WEB,
52+
val default_limit_api: Long = DEFAULT_LIMIT_FOR_API
5053
)
5154

5255
@Serializable

src/jvmMain/kotlin/Application.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,8 @@ fun Application.main() {
291291
try {
292292
connEMD = newEMDConnection(config, this.context)!!
293293
val res = queryEMD(parameterBundle, page, connCondition, connEMD!!, null)
294+
val limit = getLimit(page, parameterBundle.limit?.stringValue)
295+
val offset = parameterBundle.offset?.stringValue?.toLong() ?: 0
294296
val lstEvents = ArrayList<EventRepr>()
295297
while (res?.next() == true) {
296298
val paramMap = HashMap<String, Any>()
@@ -317,7 +319,12 @@ fun Application.main() {
317319
)
318320
)
319321
}
320-
call.respond(mapOf("events" to lstEvents))
322+
call.respond(mapOf(
323+
"events" to lstEvents,
324+
"limit" to limit,
325+
"offset" to offset,
326+
"count" to lstEvents.size
327+
))
321328
} catch (err: PSQLException) {
322329
if (err.toString().contains("The connection attempt failed.")) {
323330
call.respond(HttpStatusCode.ServiceUnavailable, "Database connection failed: $err")

src/jvmMain/kotlin/EventDBUtils.kt

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,25 @@ fun getStorageMap(conn: java.sql.Connection): StorageMap {
5858
return StorageMap(strToId)
5959
}
6060

61+
fun getLimit(page: PageConfig,providedLimit: String?): Long {
62+
if (providedLimit.isNullOrEmpty()) {
63+
return page.default_limit_api
64+
} else {
65+
if (providedLimit.toLong() > MAX_LIMIT_FOR_API) {
66+
return MAX_LIMIT_FOR_API
67+
} else {
68+
return providedLimit.toLong()
69+
}
70+
}
71+
}
72+
6173
fun queryEMD(
6274
parameterBundle: ParameterBundle,
6375
page: PageConfig,
6476
connCondition: Connection?,
6577
connEMD: Connection,
6678
body: BODY?,
67-
defaultLimit: Int? = null,
79+
//defaultLimit: Int? = null,
6880
countOnly: Boolean = false
6981
): ResultSet? {
7082
with(parameterBundle) {
@@ -109,12 +121,10 @@ fun queryEMD(
109121
query += " WHERE " + filterCriteria.joinToString(" AND ")
110122
}
111123

112-
if (limit != null) {
113-
query += " LIMIT ${limit.stringValue}"
114-
} else if (defaultLimit != null) {
115-
query += " LIMIT $defaultLimit"
116-
}
124+
query += " LIMIT ${getLimit(page, limit?.stringValue)}"
125+
117126
offset?.let {
127+
val offs = offset.stringValue.toLong() // TODO better check bad characters, esp. "|"
118128
query += " OFFSET ${offset.stringValue}"
119129
}
120130

src/jvmMain/kotlin/legacyUI/LegacyPages.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ fun Route.legacyPage(page: PageConfig, config: ConfigFile, connCondition: java.s
137137
h3 { +"Events found:" }
138138

139139
val res = queryEMD(
140-
parameterBundle, page, connCondition, connEMD, this, DEFAULT_LIMIT_FOR_WEB
140+
parameterBundle, page, connCondition, connEMD, this
141141
)
142142

143143
br {}

0 commit comments

Comments
 (0)