|
2
|
1 |
/*
|
|
|
2 |
* Copyright 2011 Tomas Zeman <tzeman@volny.cz>
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
package radview.model
|
|
|
17 |
|
|
|
18 |
import java.util.Date
|
|
|
19 |
import net.liftweb.common._
|
|
|
20 |
import net.liftweb.mapper._
|
|
|
21 |
import radview.snippet.SessionSnippet
|
|
|
22 |
|
|
|
23 |
/*
|
|
|
24 |
Table: v_radacct
|
|
|
25 |
*/
|
|
|
26 |
object Cdr extends Cdr with LongKeyedMetaMapper[Cdr] with Loggable {
|
|
|
27 |
override def dbTableName = "v_radacct"
|
|
|
28 |
override def dbDefaultConnectionIdentifier = RadAcctConnectionIdentifier
|
|
|
29 |
|
|
|
30 |
def bySession(s: CdrSession) = findAll(By(sid, s.sid),
|
|
|
31 |
OrderBy(radacctid, Ascending))
|
|
|
32 |
|
|
|
33 |
def fieldsForList = List(sid, ts, inBytes, outBytes, statusType, acctuniqueid,
|
|
|
34 |
username, groupname, realm, nasipaddress, nasportid, nasporttype,
|
|
|
35 |
acctsessiontime, acctauthentic, serviceType, framedProto, framedIp,
|
|
|
36 |
statusType, cell)
|
|
|
37 |
|
|
|
38 |
import java.sql.{Date => SqlDate}
|
|
|
39 |
implicit def d2sqlD(d: Date): SqlDate = new SqlDate(d.getTime)
|
|
|
40 |
|
|
|
41 |
private def diff(col: MappedField[_, _]) = String.format(
|
|
|
42 |
"MAX(%1$s) - MIN(%1$s) AS %1$s", col.dbColumnName)
|
|
|
43 |
private def min(col: MappedField[_, _]) = String.format(
|
|
|
44 |
"MIN(%1$s) AS %1$s", col.dbColumnName)
|
|
|
45 |
private def max(col: MappedField[_, _]) = String.format(
|
|
|
46 |
"MAX(%1$s) AS %1$s", col.dbColumnName)
|
|
|
47 |
|
|
|
48 |
def sessions(from: Date, to: Date, field: MappedField[_, Cdr],
|
|
|
49 |
value: String) = findAllByPreparedStatement { sc =>
|
|
|
50 |
val selectS = List(sid.dbColumnName, diff(inBytes), diff(outBytes))
|
|
|
51 |
val sql = String.format(
|
|
|
52 |
"SELECT %s FROM %s WHERE %s = ? AND %s BETWEEN ? AND ? GROUP BY %s",
|
|
|
53 |
selectS mkString ",", dbTableName, field.dbColumnName,
|
|
|
54 |
ts.dbColumnName, sid.dbColumnName)
|
|
|
55 |
logger.debug(sql + "; params: " + from + ", " + to + ", " +
|
|
|
56 |
field.dbColumnName + "=" + value)
|
|
|
57 |
val stm = sc.connection.prepareStatement(sql)
|
|
|
58 |
stm.setString(1, value)
|
|
|
59 |
stm.setDate(2, from)
|
|
|
60 |
stm.setDate(3, to)
|
|
|
61 |
stm
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
def session(sessionId: String): Box[CdrSession] =
|
|
|
65 |
Box(sessions(List(sessionId)))
|
|
|
66 |
|
|
|
67 |
def sessions(sessionIds: List[String]): List[CdrSession] = sessionIds match {
|
|
|
68 |
case Nil => Nil
|
|
|
69 |
case xs =>
|
|
|
70 |
val cdr1 = findAllByPreparedStatement { sc =>
|
|
|
71 |
val selectS = List(sid.dbColumnName, diff(inBytes), diff(outBytes),
|
|
|
72 |
min(radacctid), max(ts))
|
|
|
73 |
val sql = String.format(
|
|
|
74 |
"SELECT %s FROM %s WHERE %s IN (%s) GROUP BY %s",
|
|
|
75 |
selectS.mkString(","), dbTableName, sid.dbColumnName,
|
|
|
76 |
sessionIds.map(r=>"?").mkString(","), sid.dbColumnName)
|
|
|
77 |
logger.debug(sql)
|
|
|
78 |
val stm = sc.connection.prepareStatement(sql)
|
|
|
79 |
sessionIds.zipWithIndex.map { r => stm.setString(r._2 + 1, r._1) }
|
|
|
80 |
stm
|
|
|
81 |
}
|
|
|
82 |
val cdr2 = Cdr.findAll(ByList(radacctid, cdr1 map { _.radacctid.is }))
|
|
|
83 |
val tsMap = Map[String, Cdr]() ++ (cdr2 map { c => (c.sid.is, c) })
|
|
|
84 |
for {
|
|
|
85 |
c1 <- cdr1
|
|
|
86 |
c2 <- tsMap.get(c1.sid.is)
|
|
|
87 |
} yield {
|
|
|
88 |
CdrSession(c1.sid, c2.ts, c1.ts, c1.inBytes, c1.outBytes, c2.cell)
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
class Cdr extends LongKeyedMapper[Cdr] with CellAware {
|
|
|
94 |
def getSingleton = Cdr
|
|
|
95 |
def primaryKeyField = radacctid
|
|
|
96 |
|
|
|
97 |
object radacctid extends MappedLongIndex(this) {
|
|
|
98 |
override def dbColumnName = "radacctid"
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
object sid extends MappedPoliteStringColName(this, 64, "acctsessionid",
|
|
|
102 |
"Session Id") {
|
|
|
103 |
override def asHtml =
|
|
|
104 |
<a href={SessionSnippet.url(is)}>{super.asHtml}</a>
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
object acctuniqueid extends MappedPoliteStringColName(this, 32,
|
|
|
108 |
"acctuniqueid", "Unique Id")
|
|
|
109 |
|
|
|
110 |
object username extends MappedPoliteStringColName(this, 64, "username",
|
|
|
111 |
"User")
|
|
|
112 |
|
|
|
113 |
object groupname extends MappedPoliteStringColName(this, 64, "groupname",
|
|
|
114 |
"Group")
|
|
|
115 |
|
|
|
116 |
object realm extends MappedPoliteStringColName(this, 64, "realm", "Realm")
|
|
|
117 |
|
|
|
118 |
object nasipaddress extends MappedPoliteStringColName(this, 15,
|
|
|
119 |
"nasipaddress", "NAS IP")
|
|
|
120 |
|
|
|
121 |
object nasportid extends MappedPoliteStringColName(this, 15, "nasportid",
|
|
|
122 |
"NAS Port Id")
|
|
|
123 |
|
|
|
124 |
object nasporttype extends MappedPoliteStringColName(this, 32,
|
|
|
125 |
"nasporttype", "NAS Port Type")
|
|
|
126 |
|
|
|
127 |
object acctsessiontime extends MappedIntColName(this, "acctsessiontime",
|
|
|
128 |
"Session Time")
|
|
|
129 |
|
|
|
130 |
object acctauthentic extends MappedPoliteStringColName(this, 32,
|
|
|
131 |
"acctauthentic", "Authenticator")
|
|
|
132 |
|
|
|
133 |
object inBytes extends MappedLongColName(this, "acctinputoctets",
|
|
|
134 |
"Input bytes")
|
|
|
135 |
|
|
|
136 |
object outBytes extends MappedLongColName(this, "acctoutputoctets",
|
|
|
137 |
"Output bytes")
|
|
|
138 |
|
|
|
139 |
object calledNo extends MappedPoliteStringColName(this, 50,
|
|
|
140 |
"calledstationid", "Called No.")
|
|
|
141 |
|
|
|
142 |
object callingNo extends MappedLongColName(this, "callingstationid",
|
|
|
143 |
"Calling No.")
|
|
|
144 |
|
|
|
145 |
object termCause extends MappedPoliteStringColName(this, 32,
|
|
|
146 |
"acctterminatecause", "Termination Cause")
|
|
|
147 |
|
|
|
148 |
object serviceType extends MappedPoliteStringColName(this, 32,
|
|
|
149 |
"servicetype", "Service Type")
|
|
|
150 |
|
|
|
151 |
object framedProto extends MappedPoliteStringColName(this, 32,
|
|
|
152 |
"framedprotocol", "Framed Protocol")
|
|
|
153 |
|
|
|
154 |
object framedIp extends MappedPoliteStringColName(this, 15,
|
|
|
155 |
"framedipaddress", "Framed IP")
|
|
|
156 |
|
|
|
157 |
object startDelay extends MappedIntColName(this, "acctstartdelay",
|
|
|
158 |
"Start Delay")
|
|
|
159 |
|
|
|
160 |
object stopDelay extends MappedIntColName(this, "acctstopdelay",
|
|
|
161 |
"Stop Delay")
|
|
|
162 |
|
|
|
163 |
object corrId extends MappedPoliteStringColName(this, 64,
|
|
|
164 |
"3GPP2_Correlation_Id", "3GPP2 Correlation Id")
|
|
|
165 |
|
|
|
166 |
object statusType extends MappedPoliteStringColName(this, 15,
|
|
|
167 |
"Acct_Status_Type", "Status Type")
|
|
|
168 |
|
|
|
169 |
object ts extends MappedDateTime(this) {
|
|
|
170 |
override def dbColumnName = "Event_Timestamp"
|
|
|
171 |
override def displayName = "Timestamp"
|
|
|
172 |
}
|
|
|
173 |
|
|
|
174 |
object releaseInd extends MappedIntColName(this, "3GPP2_Release_Indicator",
|
|
|
175 |
"3GPP2 Release Indicator")
|
|
|
176 |
|
|
|
177 |
object activeTime extends MappedIntColName(this, "3GPP2_Active_Time",
|
|
|
178 |
"3GPP2 Active Time")
|
|
|
179 |
|
|
|
180 |
}
|
|
|
181 |
|
|
|
182 |
case class CdrSession(sid: String, start: Date, end: Date, inBytes: Long,
|
|
|
183 |
outBytes: Long, cellId: String) {
|
|
|
184 |
|
|
|
185 |
def cell: Box[Cell] = Cell.findByKey(cellId)
|
|
|
186 |
}
|
|
|
187 |
|
|
|
188 |
// vim: set ts=2 sw=2 et:
|