<commit_before>import 'package:json_annotation/json_annotation.dart';

import 'experiment.dart';
import 'experiment_group.dart';
import '../util/zoned_date_time.dart';

part 'event.g.dart';

@JsonSerializable()
class Event {
  // The following fields are serializable, their exported names should be recognizable by the Paco server
  // DON'T change their names unless
  // (1) the Paco server protocol was changed;
  // or
  // (2) the correct exported name was specified in @JsonKey annotation

  @JsonKey(fromJson: _responsesFromListOfMap, toJson: _responsesToListOfMap)
  Map<String, dynamic> responses = {};

  @JsonKey(name: 'experimentId')
  int experimentServerId;

  String experimentName;

  @JsonKey(
      name: 'scheduledTime',
      fromJson: _zonedDateTimeFromString,
      toJson: _zonedDateTimeToString)
  ZonedDateTime scheduleTime;

  @JsonKey(fromJson: _zonedDateTimeFromString, toJson: _zonedDateTimeToString)
  ZonedDateTime responseTime;

  int experimentVersion;

  @JsonKey(name: 'experimentGroupName')
  String groupName;

  int actionTriggerId;

  int actionTriggerSpecId;

  int actionId;

  // End of serializable fields

  @JsonKey(ignore: true)
  int id;

  @JsonKey(ignore: true)
  int experimentId;

  @JsonKey(ignore: true)
  bool uploaded;

  Event();

  Event.of(Experiment experiment, ExperimentGroup experimentGroup) {
    experimentServerId = experiment.id;
    experimentName = experiment.title;
    experimentVersion = experiment.version;
    groupName = experimentGroup.name;
    uploaded = false;
  }

  factory Event.fromJson(Map<String, dynamic> json) => _$EventFromJson(json);

  Map<String, dynamic> toJson() => _$EventToJson(this);

  static List<Map<String, dynamic>> _responsesToListOfMap(
          Map<String, dynamic> responses) =>
      responses == null
          ? null
          : responses.entries
              .map((entry) => {'name': entry.key, 'answer': entry.value})
              .toList();

  static Map<String, dynamic> _responsesFromListOfMap(List listOfMap) =>
      listOfMap == null
          ? null
          : Map.fromIterable(listOfMap,
              key: (item) => item['name'], value: (item) => item['answer']);

  static ZonedDateTime _zonedDateTimeFromString(String string) =>
      string == null ? null : ZonedDateTime.fromString(string);
  static String _zonedDateTimeToString(ZonedDateTime zonedDateTime) =>
      zonedDateTime?.toString();

  @override
  String toString() {<commit_msg>When deserializing `Event.responses`, return `{}` instead of `null` when `null` is encountered (unlikely)<commit_after>import 'package:json_annotation/json_annotation.dart';

import 'experiment.dart';
import 'experiment_group.dart';
import '../util/zoned_date_time.dart';

part 'event.g.dart';

@JsonSerializable()
class Event {
  // The following fields are serializable, their exported names should be recognizable by the Paco server
  // DON'T change their names unless
  // (1) the Paco server protocol was changed;
  // or
  // (2) the correct exported name was specified in @JsonKey annotation

  @JsonKey(fromJson: _responsesFromListOfMap, toJson: _responsesToListOfMap)
  Map<String, dynamic> responses = {};

  @JsonKey(name: 'experimentId')
  int experimentServerId;

  String experimentName;

  @JsonKey(
      name: 'scheduledTime',
      fromJson: _zonedDateTimeFromString,
      toJson: _zonedDateTimeToString)
  ZonedDateTime scheduleTime;

  @JsonKey(fromJson: _zonedDateTimeFromString, toJson: _zonedDateTimeToString)
  ZonedDateTime responseTime;

  int experimentVersion;

  @JsonKey(name: 'experimentGroupName')
  String groupName;

  int actionTriggerId;

  int actionTriggerSpecId;

  int actionId;

  // End of serializable fields

  @JsonKey(ignore: true)
  int id;

  @JsonKey(ignore: true)
  int experimentId;

  @JsonKey(ignore: true)
  bool uploaded;

  Event();

  Event.of(Experiment experiment, ExperimentGroup experimentGroup) {
    experimentServerId = experiment.id;
    experimentName = experiment.title;
    experimentVersion = experiment.version;
    groupName = experimentGroup.name;
    uploaded = false;
  }

  factory Event.fromJson(Map<String, dynamic> json) => _$EventFromJson(json);

  Map<String, dynamic> toJson() => _$EventToJson(this);

  static List<Map<String, dynamic>> _responsesToListOfMap(
          Map<String, dynamic> responses) =>
       responses.entries
              .map((entry) => {'name': entry.key, 'answer': entry.value})
              .toList();

  static Map<String, dynamic> _responsesFromListOfMap(List listOfMap) =>
      listOfMap == null
          ? {}
          : Map.fromIterable(listOfMap,
              key: (item) => item['name'], value: (item) => item['answer']);

  static ZonedDateTime _zonedDateTimeFromString(String string) =>
      string == null ? null : ZonedDateTime.fromString(string);
  static String _zonedDateTimeToString(ZonedDateTime zonedDateTime) =>
      zonedDateTime?.toString();

  @override
  String toString() {
{'fraction_non_alphanumeric': 0.10843373493975904, 'fraction_numerical': 0.0008032128514056225, 'mean_word_length': 3.985985985985986, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 35, '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': '500878', 'n_tokens_mistral': 1508, 'n_tokens_neox': 1453, 'n_words': 365}