<commit_before>from django.db import models

class Report(models.Model):
    reportId     = models.BigIntegerField()
    agentId     = models.BigIntegerField()
    testId       = models.PositiveIntegerField()
    time         = models.DateTimeField()
    responseTime = models.PositiveIntegerField(null=True)
    bandwidth    = models.FloatField(null=True)
    traceRoute   = models.ForeignKey('TraceRoute', null=True)
    
    class Meta:
        abstract = True


class WebsiteReport(Report):
    url          = models.URLField(max_length=255)
    statusCode   = models.PositiveSmallIntegerField()
    redirectLink = models.URLField(max_length=255, blank=True)
    htmlResponse = models.TextField(blank=True)
    # TODO: missing htmlMEdia. Where to store that info ? bd or harddisk

    def create(websiteReportMsg):
        report = WebsiteReport()

        # read WebsiteReportDetail
        report.url = websiteReportMsg.report.websiteURL
        report.statusCode = websiteReportMsg.report.statusCode
        if websiteReportMsg.report.HasField('responseTime'):
            report.responseTime = websiteReportMsg.report.responseTime
        if websiteReportMsg.report.HasField('bandwidth'):
            report.bandwidth = websiteReportMsg.report.bandwidth

        if websiteReportMsg.HasField('redirectLink'):
            report.redirectLink = websiteReportMsg.redirectLink
        if websiteReportMsg.HasField('htmlResponse'):
            report.htmlResponse = websiteReportMsg.htmlResponse

        # read ICMReport
        report.reportId = websiteReportMsg.header.reportID
        report.agentId = websiteReportMsg.header.agentID
        report.testId = websiteReportMsg.header.testID
        report.time = websiteReportMsg.header.timeUTC
        # TODO: why we need timeZone ?

        # read ICMReport passedNodes
        for node in websiteReportMsg.header.passedNode:
            reportNode = WebsiteReportNode()
            reportNode.node = node
            report.websitereportnode_set.add(reportNode)

        # read ICMReport TraceRoute
        if websiteReportMsg.header.HasField('traceroute'):
            traceRoute = TraceRoute()
            traceRoute.target = websiteReportMsg.header.traceroute.target
            traceRoute.hops = websiteReportMsg.header.traceroute.hops
            traceRoute.packetSize = websiteReportMsg.header.traceroute.packetSize

            # read ICMReport TraceRoute Traces
            for rcvTrace in websiteReportMsg.header.traceroute.traces:
                trace = Trace()
                trace.hop = rcvTrace.hop
                trace.ip = rcvTrace.ip

                # read ICMReport TraceRoute Traces PacketsTiming
                for rcvPacketTime in rcvTrace.packetsTiming:
                    packetTime = PacketTime()
                    packetTime.packetTiming = rcvPacketTime
                    # add packetTime to trace
                    trace.packettime_set.add(packetTime)
                 
                # add trace to traceroute
                traceRoute.trace_set.add(trace)

            # associate traceroute with report
            traceRoute.report_set.add(report)
        
        # TODO: missing htmlMEdia. Where to store that info ? bd or harddisk
        return report

    create = staticmethod(create)


class ServiceReport(Report):
    serviceName  = models.CharField(max_length=50)<commit_msg>Added field to store html media into database<commit_after>from django.db import models

class Report(models.Model):
    reportId     = models.BigIntegerField()
    agentId     = models.BigIntegerField()
    testId       = models.PositiveIntegerField()
    time         = models.DateTimeField()
    responseTime = models.PositiveIntegerField(null=True)
    bandwidth    = models.FloatField(null=True)
    traceRoute   = models.ForeignKey('TraceRoute', null=True)
    
    class Meta:
        abstract = True


class WebsiteReport(Report):
    url          = models.URLField(max_length=255)
    statusCode   = models.PositiveSmallIntegerField()
    redirectLink = models.URLField(max_length=255, blank=True)
    htmlResponse = models.TextField(blank=True)
    htmlMedia    = models.FileField(upload_to='reports/')
    # TODO: this is the correct path ?

    def create(websiteReportMsg):
        report = WebsiteReport()

        # read WebsiteReportDetail
        report.url = websiteReportMsg.report.websiteURL
        report.statusCode = websiteReportMsg.report.statusCode
        if websiteReportMsg.report.HasField('responseTime'):
            report.responseTime = websiteReportMsg.report.responseTime
        if websiteReportMsg.report.HasField('bandwidth'):
            report.bandwidth = websiteReportMsg.report.bandwidth

        if websiteReportMsg.HasField('redirectLink'):
            report.redirectLink = websiteReportMsg.redirectLink
        if websiteReportMsg.HasField('htmlResponse'):
            report.htmlResponse = websiteReportMsg.htmlResponse
        if websiteReportMsg.HasField('htmlMedia'):
            # TODO: which name it should have ?
            report.htmlMedia.save(websiteReportMsg.report.websiteURL, websiteReportMsg.htmlResponse)

        # read ICMReport
        report.reportId = websiteReportMsg.header.reportID
        report.agentId = websiteReportMsg.header.agentID
        report.testId = websiteReportMsg.header.testID
        report.time = websiteReportMsg.header.timeUTC
        # TODO: why we need timeZone ?

        # read ICMReport passedNodes
        for node in websiteReportMsg.header.passedNode:
            reportNode = WebsiteReportNode()
            reportNode.node = node
            report.websitereportnode_set.add(reportNode)

        # read ICMReport TraceRoute
        if websiteReportMsg.header.HasField('traceroute'):
            traceRoute = TraceRoute()
            traceRoute.target = websiteReportMsg.header.traceroute.target
            traceRoute.hops = websiteReportMsg.header.traceroute.hops
            traceRoute.packetSize = websiteReportMsg.header.traceroute.packetSize

            # read ICMReport TraceRoute Traces
            for rcvTrace in websiteReportMsg.header.traceroute.traces:
                trace = Trace()
                trace.hop = rcvTrace.hop
                trace.ip = rcvTrace.ip

                # read ICMReport TraceRoute Traces PacketsTiming
                for rcvPacketTime in rcvTrace.packetsTiming:
                    packetTime = PacketTime()
                    packetTime.packetTiming = rcvPacketTime
                    # add packetTime to trace
                    trace.packettime_set.add(packetTime)
                 
                # add trace to traceroute
                traceRoute.trace_set.add(trace)

            # associate traceroute with report
            traceRoute.report_set.add(report)
        

        return report

    create = staticmethod(create)


class ServiceReport(Report):
    serviceName  = models.CharField(max_length=50)
{'fraction_non_alphanumeric': 0.07136690647482015, 'fraction_numerical': 0.002302158273381295, 'mean_word_length': 2.4090240313879354, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '185335', 'n_tokens_mistral': 1824, 'n_tokens_neox': 1732, 'n_words': 350}