<commit_before>from rest_framework import serializers

from core.models import Instance, InstanceStatusHistory, Size

from api.v2.serializers.summaries import InstanceSuperSummarySerializer
from api.v2.serializers.summaries.size import SizeRelatedField

class InstanceRelatedField(serializers.PrimaryKeyRelatedField):

    def to_representation(self, value):
        instance = Instance.objects.get(pk=value.pk)
        # important! We have to use the SuperSummary because there are non-end_dated
        # instances that don't have a valid size (size='Unknown')
        serializer = InstanceSuperSummarySerializer(
            instance,
            context=self.context)
        return serializer.data

class InstanceStatusHistorySerializer(serializers.HyperlinkedModelSerializer):
    instance = InstanceRelatedField(queryset=Instance.objects.none())
    size = SizeRelatedField(queryset=Size.objects.none())
    status = serializers.SlugRelatedField(slug_field='name', read_only=True)

    class Meta:
        model = InstanceStatusHistory
        view_name = 'api:v2:instancestatushistory-detail'
        fields = (
            'id',
            'url',
            'instance',
            'status',
            'size',
            'start_date',
            'end_date',
        )<commit_msg>Added 'image' and 'provider' to instance history to avoid deep-nested queries clientside<commit_after>from rest_framework import serializers

from core.models import Instance, InstanceStatusHistory, Size, Provider, Application as Image

from api.v2.serializers.summaries import (
    InstanceSuperSummarySerializer, ProviderSummarySerializer, ImageSummarySerializer)
from api.v2.serializers.summaries.size import SizeRelatedField
from api.v2.serializers.fields import ModelRelatedField

class InstanceRelatedField(serializers.PrimaryKeyRelatedField):

    def to_representation(self, value):
        instance = Instance.objects.get(pk=value.pk)
        # important! We have to use the SuperSummary because there are non-end_dated
        # instances that don't have a valid size (size='Unknown')
        serializer = InstanceSuperSummarySerializer(
            instance,
            context=self.context)
        return serializer.data

class InstanceStatusHistorySerializer(serializers.HyperlinkedModelSerializer):
    instance = InstanceRelatedField(queryset=Instance.objects.none())
    size = SizeRelatedField(queryset=Size.objects.none())
    provider = ModelRelatedField(
        source='instance.provider_machine.provider',
        queryset=Provider.objects.all(),
        serializer_class=ProviderSummarySerializer,
        style={'base_template': 'input.html'})
    image = ModelRelatedField(
        source='instance.provider_machine.application_version.application',
        queryset=Image.objects.all(),
        serializer_class=ImageSummarySerializer,
        style={'base_template': 'input.html'})
    status = serializers.SlugRelatedField(slug_field='name', read_only=True)

    class Meta:
        model = InstanceStatusHistory
        view_name = 'api:v2:instancestatushistory-detail'
        fields = (
            'id',
            'url',
            'instance',
            'status',
            'size',
            'provider',
            'image',
            'start_date',
            'end_date',
        )
{'fraction_non_alphanumeric': 0.08579169175195665, 'fraction_numerical': 0.002107164358819988, 'mean_word_length': 3.26025641025641, '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': '6806985', 'n_tokens_mistral': 863, 'n_tokens_neox': 857, 'n_words': 183}