Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#SB-26617 feat: Summary APIs #3

Open
wants to merge 12 commits into
base: release-4.4.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ server.pid
*.eml
/dist/
.cache
**/target
.idea
*.iml
66 changes: 66 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<artifactId>sunbird-viewer-service</artifactId>
<name>sunbird-viewer-service</name>
<modules>
<module>viewer-core</module>
<module>viewer-actors</module>
<module>viewer-service</module>
</modules>
<packaging>pom</packaging>
<groupId>org.sunbird</groupId>
<version>1.0</version>

<repositories>
<repository>
<id>central</id>
<name>Central Repository</name>
<url>https://repo.maven.apache.org/maven2</url>
<layout>default</layout>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<scoverage.plugin.version>1.4.0</scoverage.plugin.version>
<scala.maj.version>2.12</scala.maj.version>
<scala.version>2.12.8</scala.version>
<maven.compiler.target>11</maven.compiler.target>
<play2.version>2.7.2</play2.version>
<play2.plugin.version>1.0.0-rc5</play2.plugin.version>
<sbt-compiler.plugin.version>1.0.0-beta3</sbt-compiler.plugin.version>
<akka.version>2.5.22</akka.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
<plugin>
<groupId>org.scoverage</groupId>
<artifactId>scoverage-maven-plugin</artifactId>
<version>${scoverage.plugin.version}</version>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
<aggregate>true</aggregate>
<highlighting>true</highlighting>
<excludedFiles>.*RoutesPrefix.*;.*Routes.*;.*ReverseRoutes.*</excludedFiles>
</configuration>
</plugin>
</plugins>
</build>

</project>
134 changes: 134 additions & 0 deletions viewer-actors/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>sunbird-viewer-service</artifactId>
<groupId>org.sunbird</groupId>
<version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>viewer-actors</artifactId>

<repositories>
<repository>
<id>jcenter-repo</id>
<name>Jcenter Repo</name>
<url>https://jcenter.bintray.com/</url>
</repository>
</repositories>

<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<encoding>UTF-8</encoding>

</properties>
<dependencies>
<dependency>
<groupId>org.sunbird</groupId>
<artifactId>viewer-core</artifactId>
<version>1.0</version>
</dependency>

<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-storage</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-actor_${scala.maj.version}</artifactId>
<version>${akka.version}</version>
</dependency>
<dependency>
<groupId>org.scalameta</groupId>
<artifactId>scalameta_${scala.maj.version}</artifactId>
<version>4.3.0</version>
</dependency>



<!-->Test Dependencies<!-->
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-testkit_${scala.maj.version}</artifactId>
<version>${akka.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_${scala.maj.version}</artifactId>
<version>3.0.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.cassandraunit</groupId>
<artifactId>cassandra-unit</artifactId>
<version>3.11.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.3.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>4.4.0</version>
<configuration>
<source>11</source>
<target>11</target>
<checkMultipleScalaVersions>false</checkMultipleScalaVersions>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.scalatest</groupId>
<artifactId>scalatest-maven-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<id>test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.scoverage</groupId>
<artifactId>scoverage-maven-plugin</artifactId>
<version>${scoverage.plugin.version}</version>
<configuration>
<scalaVersion>${scala.version}</scalaVersion>
<aggregate>true</aggregate>
<highlighting>true</highlighting>
</configuration>
</plugin>
</plugins>
</build>

</project>
102 changes: 102 additions & 0 deletions viewer-actors/src/main/scala/org/sunbird/viewer/Models.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package org.sunbird.viewer

import org.sunbird.viewer.core.APIResponse

import java.util.UUID

object Models {
}

object ResponseCode extends Enumeration {
type Code = Value
val OK, CLIENT_ERROR, SERVER_ERROR = Value
}
//type StatusCode = Value
object StatusCode extends Enumeration {
type status = Int
val START,UPDATE = Value("1")
val END = Value("2")
}
object ProgressCode extends Enumeration {
type status = Int
val START= Value("1")
val END = Value("100")
}


object Constants{
val VIEW_START_REQUEST = "api.view.start"
val VIEW_END_REQUEST = "api.view.end"
val VIEW_UPDATE_REQUEST = "api.view.update"
val VIEW_READ_REQUEST = "api.view.read"
val VIEW_SUMMARY_LIST_API_ID = "api.summary.list"
val VIEW_SUMMARY_READ_API_ID = "api.summary.read"
val SUNBIRD_COURSES_KEYSPACE= "sunbird_courses"
val CONTENT_CONSUMPTION_TABLE="user_content_consumption_new"
val USER_ENROLMENTS_TABLE="user_enrolments"
val USER_ACTIVITY_TABLE="user_activity_agg"
val CONTENT_START_STATUS = 1
val CONTENT_END_ = 1
}
// Common Class

case class Params(resmsgid: String, msgid: String, err: String, status: String, errmsg: Map[String,String], client_key: Option[String] = None)
case class Response(id: String, ver: String, ts: String, params: Params, responseCode: String, result: Option[Map[String, AnyRef]]) extends APIResponse


case class BaseRequest(`type`:String,request: String)

trait BaseViewRequest {
def userId: String
def contentId : String
def collectionId: Option[String]
def contextId: Option[String]
}

case class ViewRequestBody(id: String, ver: String, ts: String, request: Map[String,AnyRef], params: Option[Params])


case class StartRequest(userId: String, contentId:String,collectionId :Option[String],contextId:Option[String])
extends BaseViewRequest {
def validateRequest: Either[Map[String,AnyRef],StartRequest] ={
if(null == userId || userId.isEmpty)
Left(Map("request.userId" -> "cannot be empty"))
else if(null == contentId || contentId.isEmpty)
Left(Map("request.contentId" -> "cannot be empty"))
else
Right(StartRequest(userId,contentId,Some(collectionId.getOrElse(contentId)),
Some(contextId.getOrElse(collectionId.getOrElse(contentId)
))))
}
}

case class ViewUpdateRequest(userId: String, contentId:String, batchId:String,collectionId :String,progress: Int)

case class ViewEndRequest(userId: String, contentId:String, batchId:String,collectionId :String,
assessments: List[Map[String,AnyRef]])


case class ContentEndEvent(eid: String = "BE_JOB_REQUEST", ets: Long = System.currentTimeMillis(),
mid: String = UUID.randomUUID.toString, actor: TypeId, context: Context, `object`: TypeId,
edata: EData, action: String, iteration: Int,
batchId: String,userId: String, courseId: String)
case class TypeId(`type`: String, id: String)
case class Context(pdata: PData)
case class PData(ver: String , id : String)
case class EData(contents: List[Content])
case class Content(contentId: String, status:Int)

case class ViewerSummaryRequest(userId: String, collectionId: Option[String], contextId: Option[String])

case class Summary(userId: String, collectionId: String, contextId: String, enrolledDate: Long, active: Boolean,
contentStatus: Map[String, Integer], assessmentStatus: Map[String, Map[String, AnyRef]],
collection: Map[String, AnyRef], issuedCertificates: java.util.List[java.util.Map[String, String]],
completedOn: Long, progress: Int, status: Int)

case class EnrolmentData(userId: String, collectionId: String, contextId: String, enrolledDate: Long, active: Boolean,
issuedCertificates: java.util.List[java.util.Map[String, String]], completedOn: Long, progress: Int, status: Int)

case class UserActivityData(userId: String, collectionId: String, contextId: String, contentStatus: Map[String, Integer],
assessmentStatus: Map[String, Map[String, AnyRef]]) {
def this() = this("", "", "", Map(), Map())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.sunbird.viewer.actors

import akka.actor.Actor
import org.sunbird.viewer.core.{CassandraUtil, JSONUtils, KafkaUtil, RedisUtil}

import javax.inject.Inject

case class ServiceHealth(name: String, healthy: Boolean, errMsg: Option[String] = None)
class HealthCheckActor @Inject() extends Actor {

def receive: Receive = {
case "checkhealth" => sender() ! getHealthStatus
case "checkserviceshealth" => sender() ! getServiceHealthStatus(List("redis","cassandra","kafka"))
}


def getHealthStatus(): String = {

val result = Map(
"name" -> "viewer.service.health.api",
"healthy" -> "true")
JSONUtils.serialize(result)
}


def getServiceHealthStatus(services: List[String]): String = {
val status = services.map(service => {
try {
service match {
case "redis" => ServiceHealth(service, new RedisUtil().checkConnection)
case "cassandra" => ServiceHealth(service, new CassandraUtil().checkConnection())
case "kafka" => ServiceHealth(service, new KafkaUtil().checKConnection())
}
}
catch {
case ex: Exception =>
ServiceHealth(service, false, Some(ex.getMessage))
}
})
val result= Map(
"name" -> "viewer.service.health.api",
"services" -> status)
JSONUtils.serialize(result)
}
}

Loading