<commit_before>package models

import conversions._
import util.{Config, Feature, LldpRepresentation, LshwRepresentation, MessageHelper, Stats}
import util.power.PowerUnits
import util.views.Formatter.dateFormat

import play.api.Logger
import play.api.libs.json._
import play.api.libs.ws.WS
import play.api._
import play.api.mvc._

import org.squeryl.Schema
import org.squeryl.PrimitiveTypeMode._

import java.sql.Timestamp
import java.util.Date
import java.text.SimpleDateFormat

import akka.dispatch.Await
import akka.util.duration._

object AssetConfig {
  lazy val HiddenMeta: Set[String] = Feature("hideMeta").toSet
}

/**
 * An AssetView can be either a regular Asset or a RemoteAsset from another
 * collins instance.  This interface should only expose methods needed by the
 * list view, since from there the client is directed to whichever instnace
 * actually owns the asset.
 */
trait AssetView {

  def tag: String
  //def status: Int
  //def asset_type: Int
  def created: Timestamp
  def updated: Option[Timestamp]

  def toJsonObject(): JsObject
  def forJsonObject: Seq[(String, JsValue)]

  def getHostnameMetaValue(): Option[String]
  def getPrimaryRoleMetaValue(): Option[String]
  def getStatusName(): String

  def remoteHost: Option[String] //none if local
  

}

/**
 * An asset controlled by another collins instance, used during multi-collins
 * searching
 */
case class RemoteAsset(_host: String, json: JsObject) extends AssetView {

  
  private[this] def jsonDateToTimestamp(j: JsValue): Option[Timestamp] = {
    val formatter = new SimpleDateFormat(util.views.Formatter.ISO_8601_FORMAT)
    j.asOpt[String].filter{_ != ""}.map{s => new Timestamp(formatter.parse(s).getTime)}
  }

  def toJsonObject = json
  def forJsonObject = json.fields

  def tag = (json \ "TAG").as[String]
  def created = jsonDateToTimestamp(json \ "CREATED").getOrElse(new Timestamp(0))
  def updated = jsonDateToTimestamp(json \ "UPDATED")

  def getHostnameMetaValue() = (json \ "ATTRIBS" \ "0" \ "HOSTNAME").asOpt[String]
  def getPrimaryRoleMetaValue() = (json \ "ATTRIBS" \ "0" \ "PRINARY_ROLE").asOpt[String]
  def getStatusName() = (json \ "STATUS" ).asOpt[String].getOrElse("Unknown")

  def remoteHost = Some(_host)

}


case class Asset(tag: String, status: Int, asset_type: Int,
    created: Timestamp, updated: Option[Timestamp], deleted: Option[Timestamp],
    id: Long = 0) extends ValidatedEntity[Long] with AssetView
{
  override def validate() {
    require(Asset.isValidTag(tag), "Tag must be non-empty alpha numeric")
  }
  override def asJson: String = {
    Json.stringify(JsObject(forJsonObject))
  }

  def getHostnameMetaValue = getMetaAttribute("HOSTNAME").map{_.getValue}
  def getPrimaryRoleMetaValue = getMetaAttribute("PRIMARY_ROLE").map{_.getValue}<commit_msg>adding LOCATION top-level attribute for remote asset json generation<commit_after>package models

import conversions._
import util.{Config, Feature, LldpRepresentation, LshwRepresentation, MessageHelper, Stats}
import util.power.PowerUnits
import util.views.Formatter.dateFormat

import play.api.Logger
import play.api.libs.json._
import play.api.libs.ws.WS
import play.api._
import play.api.mvc._

import org.squeryl.Schema
import org.squeryl.PrimitiveTypeMode._

import java.sql.Timestamp
import java.util.Date
import java.text.SimpleDateFormat

import akka.dispatch.Await
import akka.util.duration._

object AssetConfig {
  lazy val HiddenMeta: Set[String] = Feature("hideMeta").toSet
}

/**
 * An AssetView can be either a regular Asset or a RemoteAsset from another
 * collins instance.  This interface should only expose methods needed by the
 * list view, since from there the client is directed to whichever instnace
 * actually owns the asset.
 */
trait AssetView {

  def tag: String
  //def status: Int
  //def asset_type: Int
  def created: Timestamp
  def updated: Option[Timestamp]

  def toJsonObject(): JsObject
  def forJsonObject: Seq[(String, JsValue)]

  def getHostnameMetaValue(): Option[String]
  def getPrimaryRoleMetaValue(): Option[String]
  def getStatusName(): String

  def remoteHost: Option[String] //none if local
  

}

/**
 * An asset controlled by another collins instance, used during multi-collins
 * searching
 */
case class RemoteAsset(_host: String, json: JsObject) extends AssetView {

  
  private[this] def jsonDateToTimestamp(j: JsValue): Option[Timestamp] = {
    val formatter = new SimpleDateFormat(util.views.Formatter.ISO_8601_FORMAT)
    j.asOpt[String].filter{_ != ""}.map{s => new Timestamp(formatter.parse(s).getTime)}
  }

  def toJsonObject = JsObject(forJsonObject)
  def forJsonObject = json.fields + ("LOCATION" -> JsString(_host))

  def tag = (json \ "TAG").as[String]
  def created = jsonDateToTimestamp(json \ "CREATED").getOrElse(new Timestamp(0))
  def updated = jsonDateToTimestamp(json \ "UPDATED")

  def getHostnameMetaValue() = (json \ "ATTRIBS" \ "0" \ "HOSTNAME").asOpt[String]
  def getPrimaryRoleMetaValue() = (json \ "ATTRIBS" \ "0" \ "PRINARY_ROLE").asOpt[String]
  def getStatusName() = (json \ "STATUS" ).asOpt[String].getOrElse("Unknown")

  def remoteHost = Some(_host)

}


case class Asset(tag: String, status: Int, asset_type: Int,
    created: Timestamp, updated: Option[Timestamp], deleted: Option[Timestamp],
    id: Long = 0) extends ValidatedEntity[Long] with AssetView
{
  override def validate() {
    require(Asset.isValidTag(tag), "Tag must be non-empty alpha numeric")
  }
  override def asJson: String = {
    Json.stringify(JsObject(forJsonObject))
  }

  def getHostnameMetaValue = getMetaAttribute("HOSTNAME").map{_.getValue}
  def getPrimaryRoleMetaValue = getMetaAttribute("PRIMARY_ROLE").map{_.getValue}
{'fraction_non_alphanumeric': 0.11095505617977527, 'fraction_numerical': 0.0028089887640449437, 'mean_word_length': 4.714142427281845, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '99468', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1745, 'n_words': 485}